-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ui.dropdownbrowser.js
254 lines (211 loc) · 6.21 KB
/
jquery.ui.dropdownbrowser.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// the widget definition, where 'custom' is the namespace,
// 'dropdownbrowser' the widget name
$.widget( 'custom.dropdownbrowser', {
// default options
options: {
height: 200,
width: 250,
next_ui: true,
previous_ui: true,
selected: 0,
data: null,
// callbacks
onSelect: null,
beforeNext: null,
afterNext: null,
beforePrevious: null,
afterPrevious: null,
},
// the constructor
_create: function() {
var this_element = this;
if (typeof(this.options.selected) === 'number') {
if (this.options.data && this.options.data[this.options.selected]) {
this.options.selected = this.options.data[this.options.selected];
} else {
this.options.selected = '';
}
} else {
if (this.options.data && this.options.data.indexOf(this.options.selected) === -1) {
this.options.selected = '';
}
}
this.wrapper = $( '<div>', {
class: 'custom-dropdownbrowser-wrapper',
});
this.element
// add a class for theming
.addClass( 'custom-dropdownbrowser' )
.wrap(this.wrapper);
this.wrapper = this.element.parent();
if (this.options.previous_ui) {
this.prev_changer = $( '<button>', {
text: 'Previous',
'class': 'custom-dropdownbrowser-changer-prev'
})
.appendTo( this.element )
.button({
icons: {
primary: 'ui-icon-carat-1-w'
},
text: false
});
// bind click events on the changer button to the randomize method
this._on( this.prev_changer, {
// _on won't call randomize when widget is disabled
click: 'previous'
});
}
this.this_button = $( '<button>', {
'class': 'custom-dropdownbrowser-button'
});
this.keep_focus = false;
this.this_button.button({
icons: {
secondary: 'ui-icon-triangle-1-s'
},
});
this._on( this.this_button, {
click: function( event ) {
var this_pos = this.this_button.position();
var this_height = this.this_button.outerHeight();
this.this_menu
.css({
top: this_pos.top + this_height,
left: this_pos.left,
})
.show();
}
});
this.this_button
.css('width', this.options.width)
.appendTo( this.element );
this.this_menu = $( '<ul />' )
.addClass('dropdownbrowser-menu')
.css({
width: this.options.width,
height: this.options.height,
overflow: 'auto',
position: 'absolute',
'z-index': 1,
});
for (ii in this.options.data) {
$( '<a />', {
text: this.options.data[ii],
href: '#',
})
.appendTo( $( '<li />' )
.appendTo(this.this_menu)
);
}
this.this_menu
.menu()
.hide()
.appendTo( this.wrapper );
this._on(this.this_menu, {
menublur: function(e) {
var hide_menu = function(widget) {
if (!widget.keep_focus) {
widget.this_menu.hide();
}
};
this.keep_focus = false;
var this_element = this;
setTimeout(function() { hide_menu(this_element) }, 200);
},
menuselect: function(e, u) {
this._trigger( 'onSelect', e, u );
this._setOption('selected', u.item.find('a').text());
this._refresh();
this.this_menu.hide();
},
menufocus: function(e) {
this.keep_focus = true;
},
});
if (this.options.next_ui) {
this.next_changer = $( '<button>', {
text: 'Next',
'class': 'custom-dropdownbrowser-changer-next'
})
.appendTo( this.element )
.button({
icons: {
primary: 'ui-icon-carat-1-e'
},
text: false
});
this._on( this.next_changer, {
// _on won't call random when widget is disabled
click: 'next'
});
}
this.element.buttonset();
// Clicks outside of a menu collapse any open menus
this._on( this.document, {
click: function( event ) {
if ( !$( event.target ).closest('.ui-menu').length
&& this.this_menu
&& $(event.target).get(0) != this.this_button.get(0) ) {
this.this_menu.hide();
}
}
});
this.option( 'selected', this.options.selected );
},
// called when created, and later when changing options
_refresh: function() { },
previous: function( event ) {
this._trigger( 'beforePrevious', event );
var this_selected = this.options.selected;
var this_ndx = this.options.data.indexOf(this_selected) - 1;
if (this_ndx < 0) {
this_ndx = this.options.data.length - 1;
}
var this_selection = this.options.data[this_ndx];
this.option( 'selected', this_selection );
this._trigger( 'afterPrevious', event );
},
next: function( event ) {
this._trigger( 'beforeNext', event );
var this_selected = this.options.selected;
var this_ndx = this.options.data.indexOf(this_selected);
if (this_ndx === this.options.data.length - 1) {
this_ndx = -1;
}
var this_selection = this.options.data[this_ndx + 1];
this.option( 'selected', this_selection );
this._trigger( 'afterNext', event );
},
// events bound via _on are removed automatically
// revert other modifications here
_destroy: function() {
// remove generated elements
this.this_button.remove();
this.prev_changer.remove();
this.next_changer.remove();
this
.removeClass( 'custom-dropdownbrowser' )
.enableSelection();
$(this.options.apply_to)
.css( 'background-image', 'none' );
},
// _setOptions is called with a hash of all options that are changing
// always refresh when changing options
_setOptions: function() {
// _super and _superApply handle keeping the right this-context
this._superApply( arguments );
this._refresh();
},
// _setOption is called for each individual option that is changing
_setOption: function( key, value ) {
// prevent invalid color values
//if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) {
// return;
//}
if (key === 'selected') {
this.this_button.button('option', 'label', value);
}
this._super( key, value );
}
});