Debian Codegraph @

resources/lib/jquery.ui/jquery.ui.menu.js

raw symbols SCIP
1 /*!
2 * jQuery UI Menu 1.9.2
3 * http://jqueryui.com
4 *
5 * Copyright 2012 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
8 *
9 * http://api.jqueryui.com/menu/
10 *
11 * Depends:
12 * jquery.ui.core.js
13 * jquery.ui.widget.js
14 * jquery.ui.position.js
15 */
16 (function( $, undefined ) {
17
18 var mouseHandled = false;
19
20 $.widget( "ui.menu", {
21 version: "1.9.2",
22 defaultElement: "<ul>",
23 delay: 300,
24 options: {
25 icons: {
26 submenu: "ui-icon-carat-1-e"
27 },
28 menus: "ul",
29 position: {
30 my: "left top",
31 at: "right top"
32 },
33 role: "menu",
34
35 // callbacks
36 blur: null,
37 focus: null,
38 select: null
39 },
40
41 _create: function() {
42 this.activeMenu = this.element;
43 this.element
44 .uniqueId()
45 .addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
46 .toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length )
47 .attr({
48 role: this.options.role,
49 tabIndex: 0
50 })
51 .on( "click" + this.eventNamespace, $.proxy(function( event ) {
52 if ( this.options.disabled ) {
53 event.preventDefault();
54 }
55 }, this ));
56
57 if ( this.options.disabled ) {
58 this.element
59 .addClass( "ui-state-disabled" )
60 .attr( "aria-disabled", "true" );
61 }
62
63 this._on({
64 // Prevent focus from sticking to links inside menu after clicking
65 // them (focus should always stay on UL during navigation).
66 "mousedown .ui-menu-item > a": function( event ) {
67 event.preventDefault();
68 },
69 "click .ui-state-disabled > a": function( event ) {
70 event.preventDefault();
71 },
72 "click .ui-menu-item:has(a)": function( event ) {
73 var target = $( event.target ).closest( ".ui-menu-item" );
74 if ( !mouseHandled && target.not( ".ui-state-disabled" ).length ) {
75 mouseHandled = true;
76
77 this.select( event );
78 // Open submenu on click
79 if ( target.has( ".ui-menu" ).length ) {
80 this.expand( event );
81 } else if ( !this.element.is( ":focus" ) ) {
82 // Redirect focus to the menu
83 this.element.trigger( "focus", [ true ] );
84
85 // If the active item is on the top level, let it stay active.
86 // Otherwise, blur the active item since it is no longer visible.
87 if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) {
88 clearTimeout( this.timer );
89 }
90 }
91 }
92 },
93 "mouseenter .ui-menu-item": function( event ) {
94 var target = $( event.currentTarget );
95 // Remove ui-state-active class from siblings of the newly focused menu item
96 // to avoid a jump caused by adjacent elements both having a class with a border
97 target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
98 this.focus( event, target );
99 },
100 mouseleave: "collapseAll",
101 "mouseleave .ui-menu": "collapseAll",
102 focus: function( event, keepActiveItem ) {
103 // If there's already an active item, keep it active
104 // If not, activate the first item
105 var item = this.active || this.element.children( ".ui-menu-item" ).eq( 0 );
106
107 if ( !keepActiveItem ) {
108 this.focus( event, item );
109 }
110 },
111 blur: function( event ) {
112 this._delay(function() {
113 if ( !$.contains( this.element[0], this.document[0].activeElement ) ) {
114 this.collapseAll( event );
115 }
116 });
117 },
118 keydown: "_keydown"
119 });
120
121 this.refresh();
122
123 // Clicks outside of a menu collapse any open menus
124 this._on( this.document, {
125 click: function( event ) {
126 if ( !$( event.target ).closest( ".ui-menu" ).length ) {
127 this.collapseAll( event );
128 }
129
130 // Reset the mouseHandled flag
131 mouseHandled = false;
132 }
133 });
134 },
135
136 _destroy: function() {
137 // Destroy (sub)menus
138 this.element
139 .removeAttr( "aria-activedescendant" )
140 .find( ".ui-menu" ).addBack()
141 .removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" )
142 .removeAttr( "role" )
143 .removeAttr( "tabIndex" )
144 .removeAttr( "aria-labelledby" )
145 .removeAttr( "aria-expanded" )
146 .removeAttr( "aria-hidden" )
147 .removeAttr( "aria-disabled" )
148 .removeUniqueId()
149 .show();
150
151 // Destroy menu items
152 this.element.find( ".ui-menu-item" )
153 .removeClass( "ui-menu-item" )
154 .removeAttr( "role" )
155 .removeAttr( "aria-disabled" )
156 .children( "a" )
157 .removeUniqueId()
158 .removeClass( "ui-corner-all ui-state-hover" )
159 .removeAttr( "tabIndex" )
160 .removeAttr( "role" )
161 .removeAttr( "aria-haspopup" )
162 .children().each( function() {
163 var elem = $( this );
164 if ( elem.data( "ui-menu-submenu-carat" ) ) {
165 elem.remove();
166 }
167 });
168
169 // Destroy menu dividers
170 this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
171 },
172
173 _keydown: function( event ) {
174 var match, prev, character, skip, regex,
175 preventDefault = true;
176
177 function escape( value ) {
178 return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
179 }
180
181 switch ( event.keyCode ) {
182 case $.ui.keyCode.PAGE_UP:
183 this.previousPage( event );
184 break;
185 case $.ui.keyCode.PAGE_DOWN:
186 this.nextPage( event );
187 break;
188 case $.ui.keyCode.HOME:
189 this._move( "first", "first", event );
190 break;
191 case $.ui.keyCode.END:
192 this._move( "last", "last", event );
193 break;
194 case $.ui.keyCode.UP:
195 this.previous( event );
196 break;
197 case $.ui.keyCode.DOWN:
198 this.next( event );
199 break;
200 case $.ui.keyCode.LEFT:
201 this.collapse( event );
202 break;
203 case $.ui.keyCode.RIGHT:
204 if ( this.active && !this.active.is( ".ui-state-disabled" ) ) {
205 this.expand( event );
206 }
207 break;
208 case $.ui.keyCode.ENTER:
209 case $.ui.keyCode.SPACE:
210 this._activate( event );
211 break;
212 case $.ui.keyCode.ESCAPE:
213 this.collapse( event );
214 break;
215 default:
216 preventDefault = false;
217 prev = this.previousFilter || "";
218 character = String.fromCharCode( event.keyCode );
219 skip = false;
220
221 clearTimeout( this.filterTimer );
222
223 if ( character === prev ) {
224 skip = true;
225 } else {
226 character = prev + character;
227 }
228
229 regex = new RegExp( "^" + escape( character ), "i" );
230 match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
231 return regex.test( $( this ).children( "a" ).text() );
232 });
233 match = skip && match.index( this.active.next() ) !== -1 ?
234 this.active.nextAll( ".ui-menu-item" ) :
235 match;
236
237 // If no matches on the current filter, reset to the last character pressed
238 // to move down the menu to the first item that starts with that character
239 if ( !match.length ) {
240 character = String.fromCharCode( event.keyCode );
241 regex = new RegExp( "^" + escape( character ), "i" );
242 match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
243 return regex.test( $( this ).children( "a" ).text() );
244 });
245 }
246
247 if ( match.length ) {
248 this.focus( event, match );
249 if ( match.length > 1 ) {
250 this.previousFilter = character;
251 this.filterTimer = this._delay(function() {
252 delete this.previousFilter;
253 }, 1000 );
254 } else {
255 delete this.previousFilter;
256 }
257 } else {
258 delete this.previousFilter;
259 }
260 }
261
262 if ( preventDefault ) {
263 event.preventDefault();
264 }
265 },
266
267 _activate: function( event ) {
268 if ( !this.active.is( ".ui-state-disabled" ) ) {
269 if ( this.active.children( "a[aria-haspopup='true']" ).length ) {
270 this.expand( event );
271 } else {
272 this.select( event );
273 }
274 }
275 },
276
277 refresh: function() {
278 var menus,
279 icon = this.options.icons.submenu,
280 submenus = this.element.find( this.options.menus );
281
282 // Initialize nested menus
283 submenus.filter( ":not(.ui-menu)" )
284 .addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
285 .hide()
286 .attr({
287 role: this.options.role,
288 "aria-hidden": "true",
289 "aria-expanded": "false"
290 })
291 .each(function() {
292 var menu = $( this ),
293 item = menu.prev( "a" ),
294 submenuCarat = $( "<span>" )
295 .addClass( "ui-menu-icon ui-icon " + icon )
296 .data( "ui-menu-submenu-carat", true );
297
298 item
299 .attr( "aria-haspopup", "true" )
300 .prepend( submenuCarat );
301 menu.attr( "aria-labelledby", item.attr( "id" ) );
302 });
303
304 menus = submenus.add( this.element );
305
306 // Don't refresh list items that are already adapted
307 menus.children( ":not(.ui-menu-item):has(a)" )
308 .addClass( "ui-menu-item" )
309 .attr( "role", "presentation" )
310 .children( "a" )
311 .uniqueId()
312 .addClass( "ui-corner-all" )
313 .attr({
314 tabIndex: -1,
315 role: this._itemRole()
316 });
317
318 // Initialize unlinked menu-items containing spaces and/or dashes only as dividers
319 menus.children( ":not(.ui-menu-item)" ).each(function() {
320 var item = $( this );
321 // hyphen, em dash, en dash
322 if ( !/[^\-—–\s]/.test( item.text() ) ) {
323 item.addClass( "ui-widget-content ui-menu-divider" );
324 }
325 });
326
327 // Add aria-disabled attribute to any disabled menu item
328 menus.children( ".ui-state-disabled" ).attr( "aria-disabled", "true" );
329
330 // If the active item has been removed, blur the menu
331 if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
332 this.blur();
333 }
334 },
335
336 _itemRole: function() {
337 return {
338 menu: "menuitem",
339 listbox: "option"
340 }[ this.options.role ];
341 },
342
343 focus: function( event, item ) {
344 var nested, focused;
345 this.blur( event, event && event.type === "focus" );
346
347 this._scrollIntoView( item );
348
349 this.active = item.first();
350 focused = this.active.children( "a" ).addClass( "ui-state-focus" );
351 // Only update aria-activedescendant if there's a role
352 // otherwise we assume focus is managed elsewhere
353 if ( this.options.role ) {
354 this.element.attr( "aria-activedescendant", focused.attr( "id" ) );
355 }
356
357 // Highlight active parent menu item, if any
358 this.active
359 .parent()
360 .closest( ".ui-menu-item" )
361 .children( "a:first" )
362 .addClass( "ui-state-active" );
363
364 if ( event && event.type === "keydown" ) {
365 this._close();
366 } else {
367 this.timer = this._delay(function() {
368 this._close();
369 }, this.delay );
370 }
371
372 nested = item.children( ".ui-menu" );
373 if ( nested.length && ( /^mouse/.test( event.type ) ) ) {
374 this._startOpening(nested);
375 }
376 this.activeMenu = item.parent();
377
378 this._trigger( "focus", event, { item: item } );
379 },
380
381 _scrollIntoView: function( item ) {
382 var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
383 if ( this._hasScroll() ) {
384 borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
385 paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
386 offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
387 scroll = this.activeMenu.scrollTop();
388 elementHeight = this.activeMenu.height();
389 itemHeight = item.height();
390
391 if ( offset < 0 ) {
392 this.activeMenu.scrollTop( scroll + offset );
393 } else if ( offset + itemHeight > elementHeight ) {
394 this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
395 }
396 }
397 },
398
399 blur: function( event, fromFocus ) {
400 if ( !fromFocus ) {
401 clearTimeout( this.timer );
402 }
403
404 if ( !this.active ) {
405 return;
406 }
407
408 this.active.children( "a" ).removeClass( "ui-state-focus" );
409 this.active = null;
410
411 this._trigger( "blur", event, { item: this.active } );
412 },
413
414 _startOpening: function( submenu ) {
415 clearTimeout( this.timer );
416
417 // Don't open if already open fixes a Firefox bug that caused a .5 pixel
418 // shift in the submenu position when mousing over the carat icon
419 if ( submenu.attr( "aria-hidden" ) !== "true" ) {
420 return;
421 }
422
423 this.timer = this._delay(function() {
424 this._close();
425 this._open( submenu );
426 }, this.delay );
427 },
428
429 _open: function( submenu ) {
430 var position = $.extend({
431 of: this.active
432 }, this.options.position );
433
434 clearTimeout( this.timer );
435 this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) )
436 .hide()
437 .attr( "aria-hidden", "true" );
438
439 submenu
440 .show()
441 .removeAttr( "aria-hidden" )
442 .attr( "aria-expanded", "true" )
443 .position( position );
444 },
445
446 collapseAll: function( event, all ) {
447 clearTimeout( this.timer );
448 this.timer = this._delay(function() {
449 // If we were passed an event, look for the submenu that contains the event
450 var currentMenu = all ? this.element :
451 $( event && event.target ).closest( this.element.find( ".ui-menu" ) );
452
453 // If we found no valid submenu ancestor, use the main menu to close all sub menus anyway
454 if ( !currentMenu.length ) {
455 currentMenu = this.element;
456 }
457
458 this._close( currentMenu );
459
460 this.blur( event );
461 this.activeMenu = currentMenu;
462 }, this.delay );
463 },
464
465 // With no arguments, closes the currently active menu - if nothing is active
466 // it closes all menus. If passed an argument, it will search for menus BELOW
467 _close: function( startMenu ) {
468 if ( !startMenu ) {
469 startMenu = this.active ? this.active.parent() : this.element;
470 }
471
472 startMenu
473 .find( ".ui-menu" )
474 .hide()
475 .attr( "aria-hidden", "true" )
476 .attr( "aria-expanded", "false" )
477 .end()
478 .find( "a.ui-state-active" )
479 .removeClass( "ui-state-active" );
480 },
481
482 collapse: function( event ) {
483 var newItem = this.active &&
484 this.active.parent().closest( ".ui-menu-item", this.element );
485 if ( newItem && newItem.length ) {
486 this._close();
487 this.focus( event, newItem );
488 }
489 },
490
491 expand: function( event ) {
492 var newItem = this.active &&
493 this.active
494 .children( ".ui-menu " )
495 .children( ".ui-menu-item" )
496 .first();
497
498 if ( newItem && newItem.length ) {
499 this._open( newItem.parent() );
500
501 // Delay so Firefox will not hide activedescendant change in expanding submenu from AT
502 this._delay(function() {
503 this.focus( event, newItem );
504 });
505 }
506 },
507
508 next: function( event ) {
509 this._move( "next", "first", event );
510 },
511
512 previous: function( event ) {
513 this._move( "prev", "last", event );
514 },
515
516 isFirstItem: function() {
517 return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
518 },
519
520 isLastItem: function() {
521 return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
522 },
523
524 _move: function( direction, filter, event ) {
525 var next;
526 if ( this.active ) {
527 if ( direction === "first" || direction === "last" ) {
528 next = this.active
529 [ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" )
530 .eq( -1 );
531 } else {
532 next = this.active
533 [ direction + "All" ]( ".ui-menu-item" )
534 .eq( 0 );
535 }
536 }
537 if ( !next || !next.length || !this.active ) {
538 next = this.activeMenu.children( ".ui-menu-item" )[ filter ]();
539 }
540
541 this.focus( event, next );
542 },
543
544 nextPage: function( event ) {
545 var item, base, height;
546
547 if ( !this.active ) {
548 this.next( event );
549 return;
550 }
551 if ( this.isLastItem() ) {
552 return;
553 }
554 if ( this._hasScroll() ) {
555 base = this.active.offset().top;
556 height = this.element.height();
557 this.active.nextAll( ".ui-menu-item" ).each(function() {
558 item = $( this );
559 return item.offset().top - base - height < 0;
560 });
561
562 this.focus( event, item );
563 } else {
564 this.focus( event, this.activeMenu.children( ".ui-menu-item" )
565 [ !this.active ? "first" : "last" ]() );
566 }
567 },
568
569 previousPage: function( event ) {
570 var item, base, height;
571 if ( !this.active ) {
572 this.next( event );
573 return;
574 }
575 if ( this.isFirstItem() ) {
576 return;
577 }
578 if ( this._hasScroll() ) {
579 base = this.active.offset().top;
580 height = this.element.height();
581 this.active.prevAll( ".ui-menu-item" ).each(function() {
582 item = $( this );
583 return item.offset().top - base + height > 0;
584 });
585
586 this.focus( event, item );
587 } else {
588 this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() );
589 }
590 },
591
592 _hasScroll: function() {
593 return this.element.outerHeight() < this.element.prop( "scrollHeight" );
594 },
595
596 select: function( event ) {
597 // TODO: It should never be possible to not have an active item at this
598 // point, but the tests don't trigger mouseenter before click.
599 this.active = this.active || $( event.target ).closest( ".ui-menu-item" );
600 var ui = { item: this.active };
601 if ( !this.active.has( ".ui-menu" ).length ) {
602 this.collapseAll( event, true );
603 }
604 this._trigger( "select", event, ui );
605 }
606 });
607
608 }( jQuery ));