ace.widget-box.js
5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
<b>Widget boxes</b>
*/
ace.widget_boxes = function($) {
//bootstrap collapse component icon toggle
$(document).on('hide.bs.collapse show.bs.collapse', function (ev) {
var panel_id = ev.target.getAttribute('id')
var panel = $('a[href*="#'+ panel_id+'"]');
if(panel.length == 0) panel = $('a[data-target*="#'+ panel_id+'"]');
if(panel.length == 0) return;
panel.find(ace.vars['.icon']).each(function(){
var $icon = $(this)
var $match
var $icon_down = null
var $icon_up = null
if( ($icon_down = $icon.attr('data-icon-show')) ) {
$icon_up = $icon.attr('data-icon-hide')
}
else if( $match = $icon.attr('class').match(/fa\-(.*)\-(up|down)/) ) {
$icon_down = 'fa-'+$match[1]+'-down'
$icon_up = 'fa-'+$match[1]+'-up'
}
if($icon_down) {
if(ev.type == 'show') $icon.removeClass($icon_down).addClass($icon_up)
else $icon.removeClass($icon_up).addClass($icon_down)
return false;//ignore other icons that match, one is enough
}
});
})
var Widget_Box = function(box, options) {
this.$box = $(box);
var that = this;
//this.options = $.extend({}, $.fn.widget_box.defaults, options);
this.reload = function() {
var $box = this.$box;
var $remove_position = false;
if($box.css('position') == 'static') {
$remove_position = true;
$box.addClass('position-relative');
}
$box.append('<div class="widget-box-overlay"><i class="'+ ace.vars['icon'] + 'loading-icon fa fa-spinner fa-spin fa-2x white"></i></div>');
$box.one('reloaded.ace.widget', function() {
$box.find('.widget-box-overlay').remove();
if($remove_position) $box.removeClass('position-relative');
});
}
this.close = function() {
var $box = this.$box;
var closeSpeed = 300;
$box.fadeOut(closeSpeed , function(){
$box.trigger('closed.ace.widget');
$box.remove();
}
)
}
this.toggle = function(type, button) {
var $box = this.$box;
var $body = $box.find('.widget-body');
var $icon = null;
var event_name = typeof type !== 'undefined' ? type : ($box.hasClass('collapsed') ? 'show' : 'hide');
var event_complete_name = event_name == 'show' ? 'shown' : 'hidden';
if(typeof button === 'undefined') {
button = $box.find('> .widget-header a[data-action=collapse]').eq(0);
if(button.length == 0) button = null;
}
if(button) {
$icon = button.find(ace.vars['.icon']).eq(0);
var $match
var $icon_down = null
var $icon_up = null
if( ($icon_down = $icon.attr('data-icon-show')) ) {
$icon_up = $icon.attr('data-icon-hide')
}
else if( $match = $icon.attr('class').match(/fa\-(.*)\-(up|down)/) ) {
$icon_down = 'fa-'+$match[1]+'-down'
$icon_up = 'fa-'+$match[1]+'-up'
}
}
var expandSpeed = 250;
var collapseSpeed = 200;
if( event_name == 'show' ) {
if($icon) $icon.removeClass($icon_down).addClass($icon_up);
$body.hide();
$box.removeClass('collapsed');
$body.slideDown(expandSpeed, function(){
$box.trigger(event_complete_name+'.ace.widget')
})
}
else {
if($icon) $icon.removeClass($icon_up).addClass($icon_down);
$body.slideUp(collapseSpeed, function(){
$box.addClass('collapsed')
$box.trigger(event_complete_name+'.ace.widget')
}
);
}
}
this.hide = function() {
this.toggle('hide');
}
this.show = function() {
this.toggle('show');
}
this.fullscreen = function() {
var $icon = this.$box.find('> .widget-header a[data-action=fullscreen]').find(ace.vars['.icon']).eq(0);
var $icon_expand = null
var $icon_compress = null
if( ($icon_expand = $icon.attr('data-icon1')) ) {
$icon_compress = $icon.attr('data-icon2')
}
else {
$icon_expand = 'fa-expand';
$icon_compress = 'fa-compress';
}
if(!this.$box.hasClass('fullscreen')) {
$icon.removeClass($icon_expand).addClass($icon_compress);
this.$box.addClass('fullscreen');
}
else {
$icon.addClass($icon_expand).removeClass($icon_compress);
this.$box.removeClass('fullscreen');
}
this.$box.trigger('fullscreened.ace.widget')
}
}
$.fn.widget_box = function (option, value) {
var method_call;
var $set = this.each(function () {
var $this = $(this);
var data = $this.data('widget_box');
var options = typeof option === 'object' && option;
if (!data) $this.data('widget_box', (data = new Widget_Box(this, options)));
if (typeof option === 'string') method_call = data[option](value);
});
return (method_call === undefined) ? $set : method_call;
};
$(document).on('click.ace.widget', '.widget-header a[data-action]', function (ev) {
ev.preventDefault();
var $this = $(this);
var $box = $this.closest('.widget-box');
if( $box.length == 0 || $box.hasClass('ui-sortable-helper') ) return;
var $widget_box = $box.data('widget_box');
if (!$widget_box) {
$box.data('widget_box', ($widget_box = new Widget_Box($box.get(0))));
}
var $action = $this.data('action');
if($action == 'collapse') {
var event_name = $box.hasClass('collapsed') ? 'show' : 'hide';
var event
$box.trigger(event = $.Event(event_name+'.ace.widget'))
if (event.isDefaultPrevented()) return
$widget_box.toggle(event_name, $this);
}
else if($action == 'close') {
var event
$box.trigger(event = $.Event('close.ace.widget'))
if (event.isDefaultPrevented()) return
$widget_box.close();
}
else if($action == 'reload') {
$this.blur();
var event
$box.trigger(event = $.Event('reload.ace.widget'))
if (event.isDefaultPrevented()) return
$widget_box.reload();
}
else if($action == 'fullscreen') {
var event
$box.trigger(event = $.Event('fullscreen.ace.widget'))
if (event.isDefaultPrevented()) return
$widget_box.fullscreen();
}
else if($action == 'settings') {
$box.trigger('setting.ace.widget')
}
});
}