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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* jQuery UI Selectmenu @VERSION
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Selectmenu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.position.js
* jquery.ui.menu.js
* jquery.ui.button.js
*/
(function( $, undefined ) {
$.widget( "ui.selectmenu", {
version: "@VERSION",
defaultElement: "<select>",
options: {
dropdown: true,
appendTo: "body",
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
value: null,
// callbacks
open: null,
focus: null,
select: null,
close: null,
change: null
},
_create: function() {
// set a default id value, generate a new random one if not set by developer
var selectmenuId = this.element.attr( 'id' ) || 'ui-selectmenu-' + Math.random().toString( 16 ).slice( 2, 10 );
// array of button and menu id's
this.ids = { id: selectmenuId, button: selectmenuId + '-button', menu: selectmenuId + '-menu' };
// set current value
if ( this.options.value ) {
this.element[0].value = this.options.value;
} else {
this.options.value = this.element[0].value;
}
// catch click event of the label
this._bind({
'click': function( event ) {
this.button.focus();
event.preventDefault();
}
});
this._drawButton();
this._bind( this.button, this._buttonEvents );
this._drawMenu();
this.refresh();
},
_drawButton: function() {
var tabindex = this.element.attr( 'tabindex' );
// hide original select tag
this.element.hide();
// create button
this.button = $( '<a />', {
href: '#' + this.ids.id,
tabindex: ( tabindex ? tabindex : this.element.attr( 'disabled' ) ? -1 : 0 ),
id: this.ids.button,
css: {
width: this.element.outerWidth()
},
'aria-disabled': this.options.disabled,
'aria-owns': this.ids.menu,
'aria-haspopup': true
})
.button({
label: this.element.find( "option:selected" ).text(),
icons: {
primary: ( this.options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' )
}
});
// wrap and insert new button
this.buttonWrap = $( '<span />' )
.addClass( 'ui-selectmenu-button' )
.append( this.button )
.insertAfter( this.element );
},
_drawMenu: function() {
var that = this;
// create menu portion, append to body
this.menu = $( '<ul />', {
'class': 'ui-widget ui-widget-content',
'aria-hidden': true,
'aria-labelledby': this.ids.button,
role: 'menubox',
id: this.ids.menu
});
// set width
if ( this.options.dropdown ) {
var setWidth = this.button.outerWidth();
} else {
var text = this.button.find( "span.ui-button-text");
var setWidth = text.width() + parseFloat( text.css( "padding-left" ) ) || 0 + parseFloat( text.css( "margin-left" ) || 0 );
}
// wrap menu
this.menuWrap = $( '<div />' )
.addClass( 'ui-selectmenu-menu' )
.width( setWidth )
.append( this.menu )
.appendTo( this.options.appendTo );
// init menu widget
this.menu.menu({
select: function( event, ui ) {
var flag = false,
item = ui.item.data( "item.selectmenu" ),
oldIndex = that.element[0].selectedIndex;
that._setOption( "value", item.value );
that._trigger( "select", event, { item: item } );
if ( item.index != oldIndex ) {
that._trigger( "change", event, { item: item } );
}
if ( that.isOpen ) {
event.preventDefault();
that.close( event, true);
}
},
focus: function( event, ui ) {
var item = ui.item.data( "item.selectmenu" );
if ( that.focus !== undefined && item.index != that.focus ) {
that._trigger( "focus", event, { item: item } );
}
that.focus = item.index;
}
});
// document click closes menu
this._bind( document, {
'click': function( event ) {
if ( this.isOpen && !$( event.target ).closest( "#" + this.ids.button).length ) {
this.close( event );
}
}
});
},
refresh: function() {
this.menu.empty();
this._readOptions();
this._renderMenu( this.menu, this.items );
this.menu.menu( "refresh" );
// adjust ARIA
this.menu.find( "li" ).not( '.ui-selectmenu-optgroup' ).find( 'a' ).attr( 'role', 'option' );
if ( this.options.dropdown ) {
this.menu
.addClass( 'ui-corner-bottom' )
.removeClass( 'ui-corner-all' );
}
// transfer disabled state
if ( this.element.attr( 'disabled' ) ) {
this.disable();
} else {
this.enable()
}
},
open: function( event ) {
var currentItem = this._getSelectedItem();
if ( !this.options.disabled ) {
if ( this.options.dropdown ) {
this.button
.addClass( 'ui-corner-top' )
.removeClass( 'ui-corner-all' );
}
this.menuWrap.addClass( 'ui-selectmenu-open' );
this.menu.attr("aria-hidden", false);
// needs to be fired after the document click event has closed all other Selectmenus
// otherwise the current item is not indicated
// TODO check if this should be handled by Menu
this._delay( function(){
this.menu.menu( "focus", event, currentItem );
}, 1);
if ( !this.options.dropdown ) {
// center current item
if ( this.menu.css("overflow") == "auto" ) {
this.menu.scrollTop( this.menu.scrollTop() + currentItem.position().top - this.menu.outerHeight()/2 + currentItem.outerHeight()/2 );
}
// calculate offset
var _offset = ( this.menu.offset().top - currentItem.offset().top + ( this.button.outerHeight() - currentItem.outerHeight() ) / 2);
$.extend( this.options.position, {
my: "left top",
at: "left top",
offset: "0 " + _offset
});
}
this.menuWrap
.zIndex( this.element.zIndex() + 1 )
.position( $.extend({
of: this.button
}, this.options.position ));
this.isOpen = true;
this._trigger( "open", event );
}
},
close: function( event, focus ) {
if ( this.isOpen ) {
if ( this.options.dropdown ) {
this.button
.addClass( 'ui-corner-all' )
.removeClass( 'ui-corner-top' );
}
this.menuWrap.removeClass( 'ui-selectmenu-open' );
this.menu.attr("aria-hidden", true);
this.isOpen = false;
if ( focus ) {
this.button.focus();
}
this._trigger( "close", event );
}
},
widget: function() {
return this.buttonWrap.add( this.menuWrap );
},
_renderMenu: function( ul, items ) {
var that = this,
currentOptgroup = "";
$.each( items, function( index, item ) {
if ( item.optgroup != currentOptgroup ) {
var optgroup = $( '<li class="ui-selectmenu-optgroup">' + item.optgroup + '</li>' );
if ( item.element.parent( "optgroup" ).attr( "disabled" ) ) optgroup.addClass( 'ui-state-disabled' );
ul.append( optgroup );
currentOptgroup = item.optgroup;
}
that._renderItem( ul, item );
});
},
_renderItem: function( ul, item) {
var li = $( "<li />" ).data( "item.selectmenu", item );
if ( item.disabled ) {
li.addClass( 'ui-state-disabled' ).text( item.label );
} else {
li.append( $( "<a />", {
text: item.label,
href: '#'
})
);
}
return li.appendTo( ul );
},
_move: function( key, event ) {
if ( !this.isOpen ) {
this.menu.menu( "focus", event, this._getSelectedItem() );
}
this.menu.menu( key, event );
if ( !this.isOpen ) {
this.menu.menu( "select", event );
}
},
_getSelectedItem: function() {
return this.menu.find( "li" ).not( '.ui-selectmenu-optgroup' ).eq( this.element[0].selectedIndex );
},
_toggle: function( event ) {
if ( this.isOpen ) {
this.close( event );
} else {
this.open( event );
}
},
_buttonEvents: {
click: function( event ) {
this._toggle( event );
event.preventDefault();
},
keydown: function( event ) {
switch (event.keyCode) {
case $.ui.keyCode.TAB:
if ( this.isOpen ) {
this.close( event );
}
break;
case $.ui.keyCode.ENTER:
if ( this.isOpen ) {
this.menu.menu( "select", this._getSelectedItem() );
}
event.preventDefault();
break;
case $.ui.keyCode.SPACE:
event.preventDefault();
break;
case $.ui.keyCode.UP:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "previous", event );
}
event.preventDefault();
break;
case $.ui.keyCode.DOWN:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "next", event );
}
event.preventDefault();
break;
case $.ui.keyCode.LEFT:
this._move( "previous", event );
event.preventDefault();
break;
case $.ui.keyCode.RIGHT:
this._move( "next", event );
event.preventDefault();
break;
default:
this.menu.trigger( event );
}
}
},
_setOption: function( key, value ) {
this._super( key, value );
if ( key === "appendTo" ) {
this.menuWrap.appendTo( $( value || "body", this.element[0].ownerDocument )[0] );
}
if ( key === "value" && value !== undefined ) {
this.element[0].value = value;
this.button.children( '.ui-button-text' ).text( this.items[ this.element[0].selectedIndex ].label );
}
if ( key === "disabled" ) {
this.button.button( "option", "disabled", value );
if ( value ) {
this.element.attr( "disabled", "disabled" );
this.button.attr( "tabindex", -1 );
this.close();
} else {
this.element.removeAttr( "disabled" );
this.button.attr( "tabindex", 0 );
}
this.menu.attr( "aria-disabled", value );
}
},
_readOptions: function() {
var data = [];
$.each( this.element.find( 'option' ), function( index, item ) {
var option = $( item ),
optgroup = option.parent( "optgroup" );
data.push({
element: option,
index: index,
value: option.attr( 'value' ),
label: option.text(),
optgroup: optgroup.attr( "label" ) || false,
disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" )
});
});
this.items = data;
},
_destroy: function() {
this.menuWrap.remove();
this.buttonWrap.remove();
this.element.show();
}
});
}( jQuery ));
|