jquery.ui.menu.js 16 KB

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