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
|
/*
* jQuery UI Menu @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/Menu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function($) {
var idIncrement = 0;
$.widget("ui.menu", {
defaultElement: "<ul>",
delay: 150,
options: {
position: {
my: "left top",
at: "right top"
}
},
_create: function() {
var self = this;
this.activeMenu = this.element;
this.menuId = this.element.attr( "id" ) || "ui-menu-" + idIncrement++;
this.element
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.attr({
id: this.menuId,
role: "listbox"
})
.bind( "click.menu", function( event ) {
var item = $( event.target ).closest( ".ui-menu-item:has(a)" );
if ( self.options.disabled ) {
return false;
}
if ( !item.length ) {
return;
}
// temporary
event.preventDefault();
// it's possible to click an item without hovering it (#7085)
if ( !self.active || ( self.active[ 0 ] !== item[ 0 ] ) ) {
self.focus( event, item );
}
self.select( event );
})
.bind( "mouseover.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
var target = $( event.target ).closest( ".ui-menu-item" );
if ( target.length ) {
self.focus( event, target );
}
})
.bind("mouseout.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
var target = $( event.target ).closest( ".ui-menu-item" );
if ( target.length ) {
self.blur( event );
}
});
this.refresh();
this.element.attr( "tabIndex", 0 ).bind( "keydown.menu", function( event ) {
if ( self.options.disabled ) {
return;
}
switch ( event.keyCode ) {
case $.ui.keyCode.PAGE_UP:
self.previousPage( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.PAGE_DOWN:
self.nextPage( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.UP:
self.previous( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.DOWN:
self.next( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.LEFT:
if (self.left( event )) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
case $.ui.keyCode.RIGHT:
if (self.right( event )) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
case $.ui.keyCode.ENTER:
self.select( event );
event.preventDefault();
event.stopImmediatePropagation();
break;
case $.ui.keyCode.ESCAPE:
if ( self.left( event ) ) {
event.stopImmediatePropagation();
}
event.preventDefault();
break;
default:
event.stopPropagation();
clearTimeout(self.filterTimer);
var prev = self.previousFilter || "";
var character = String.fromCharCode(event.keyCode);
var skip = false;
if (character == prev) {
skip = true;
} else {
character = prev + character;
}
function escape(value) {
return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
var match = self.widget().children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
});
var match = skip && match.index(self.active.next()) != -1 ? self.active.nextAll(".ui-menu-item") : match;
if (!match.length) {
character = String.fromCharCode(event.keyCode);
match = self.widget().children(".ui-menu-item").filter(function() {
return new RegExp("^" + escape(character), "i").test($(this).children("a").text());
});
}
if (match.length) {
self.focus(event, match);
if (match.length > 1) {
self.previousFilter = character;
self.filterTimer = setTimeout(function() {
delete self.previousFilter;
}, 1000);
} else {
delete self.previousFilter;
}
} else {
delete self.previousFilter;
}
}
});
},
_destroy: function() {
this.element
.removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.removeAttr( "tabIndex" )
.removeAttr( "role" )
.removeAttr( "aria-activedescendant" );
this.element.children( ".ui-menu-item" )
.removeClass( "ui-menu-item" )
.removeAttr( "role" )
.children( "a" )
.removeClass( "ui-corner-all ui-state-hover" )
.removeAttr( "tabIndex" )
.unbind( ".menu" );
},
refresh: function() {
// initialize nested menus
// TODO add role=listbox to these, too? or just the top level menu?
var submenus = this.element.find("ul:not(.ui-menu)")
.addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
.hide()
submenus
.prev("a")
.prepend('<span class="ui-icon ui-icon-carat-1-e"></span>');
// don't refresh list items that are already adapted
var items = submenus.add(this.element).children( "li:not(.ui-menu-item):has(a)" )
.addClass( "ui-menu-item" )
.attr( "role", "menuitem" );
items.children( "a" )
.addClass( "ui-corner-all" )
.attr( "tabIndex", -1 );
},
focus: function( event, item ) {
var self = this;
this.blur();
if ( this._hasScroll() ) {
var borderTop = parseFloat( $.curCSS( this.element[0], "borderTopWidth", true) ) || 0,
paddingtop = parseFloat( $.curCSS( this.element[0], "paddingTop", true) ) || 0,
offset = item.offset().top - this.element.offset().top - borderTop - paddingtop,
scroll = this.element.attr( "scrollTop" ),
elementHeight = this.element.height(),
itemHeight = item.height();
if ( offset < 0 ) {
this.element.attr( "scrollTop", scroll + offset );
} else if ( offset + itemHeight > elementHeight ) {
this.element.attr( "scrollTop", scroll + offset - elementHeight + itemHeight );
}
}
this.active = item.first()
.children( "a" )
.addClass( "ui-state-focus" )
.attr( "id", function(index, id) {
return (self.itemId = id || self.menuId + "-activedescendant");
})
.end();
// need to remove the attribute before adding it for the screenreader to pick up the change
// see http://groups.google.com/group/jquery-a11y/msg/929e0c1e8c5efc8f
this.element.removeAttr("aria-activedescendant").attr("aria-activedescendant", self.itemId)
self.timer = setTimeout(function() {
self._close();
}, self.delay)
var nested = $(">ul", item);
if (nested.length && /^mouse/.test(event.type)) {
self._startOpening(nested);
}
this.activeMenu = item.parent();
this._trigger( "focus", event, { item: item } );
},
blur: function(event) {
if (!this.active) {
return;
}
clearTimeout(this.timer);
this.active.children( "a" ).removeClass( "ui-state-focus" );
// remove only generated id
$( "#" + this.menuId + "-activedescendant" ).removeAttr( "id" );
this.element.removeAttr( "aria-activedescenant" );
this._trigger( "blur", event );
this.active = null;
},
_startOpening: function(submenu) {
clearTimeout(this.timer);
var self = this;
self.timer = setTimeout(function() {
self._close();
self._open(submenu);
}, self.delay);
},
_open: function(submenu) {
this.element.find(".ui-menu").not(submenu.parents()).hide();
var position = $.extend({}, {
of: this.active
}, $.type(this.options.position) == "function"
? this.options.position(this.active)
: this.options.position
);
submenu.show().position(position);
this.active.find(">a:first").addClass("ui-state-active");
},
closeAll: function() {
this.element
.find("ul").hide().end()
.find("a.ui-state-active").removeClass("ui-state-active");
this.blur();
this.activeMenu = this.element;
},
_close: function() {
this.active.parent()
.find("ul").hide().end()
.find("a.ui-state-active").removeClass("ui-state-active");
},
left: function(event) {
var newItem = this.active && this.active.parents("li").first();
if (newItem && newItem.length) {
this.active.parent().hide();
this.focus(event, newItem);
return true;
}
},
right: function(event) {
var newItem = this.active && this.active.children("ul").children("li").first();
if (newItem && newItem.length) {
this._open(newItem.parent());
var current = this.active;
this.focus(event, newItem);
return true;
}
},
next: function(event) {
this._move( "next", ".ui-menu-item", "first", event );
},
previous: function(event) {
this._move( "prev", ".ui-menu-item", "last", event );
},
first: function() {
return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
},
last: function() {
return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
},
_move: function(direction, edge, filter, event) {
if ( !this.active ) {
this.focus( event, this.activeMenu.children(edge)[filter]() );
return;
}
var next = this.active[ direction + "All" ]( ".ui-menu-item" ).eq( 0 );
if ( next.length ) {
this.focus( event, next );
} else {
this.focus( event, this.activeMenu.children(edge)[filter]() );
}
},
nextPage: function( event ) {
if ( this._hasScroll() ) {
if ( !this.active || this.last() ) {
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() );
return;
}
var base = this.active.offset().top,
height = this.element.height(),
result;
this.active.nextAll( ".ui-menu-item" ).each( function() {
result = $( this );
return $( this ).offset().top - base - height < 0;
});
this.focus( event, result );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" )
[ !this.active || this.last() ? "first" : "last" ]() );
}
},
previousPage: function( event ) {
if ( this._hasScroll() ) {
if ( !this.active || this.first() ) {
this.focus( event, this.activeMenu.children( ".ui-menu-item" ).last() );
return;
}
var base = this.active.offset().top,
height = this.element.height(),
result;
this.active.prevAll( ".ui-menu-item" ).each( function() {
result = $( this );
return $(this).offset().top - base + height > 0;
});
this.focus( event, result );
} else {
this.focus( event, this.activeMenu.children( ".ui-menu-item" )
[ !this.active || this.first() ? ":last" : ":first" ]() );
}
},
_hasScroll: function() {
return this.element.height() < this.element.attr( "scrollHeight" );
},
select: function( event ) {
// save active reference before closeAll triggers blur
var ui = {
item: this.active
};
this.closeAll();
this._trigger( "select", event, ui );
}
});
$.ui.menu.version = "@VERSION";
}( jQuery ));
|