menu.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*!
  2. * jQuery UI Menu @VERSION
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 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. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./core",
  17. "./widget",
  18. "./position"
  19. ], factory );
  20. } else {
  21. // Browser globals
  22. factory( jQuery );
  23. }
  24. }(function( $ ) {
  25. return $.widget( "ui.menu", {
  26. version: "@VERSION",
  27. defaultElement: "<ul>",
  28. delay: 300,
  29. options: {
  30. icons: {
  31. submenu: "ui-icon-carat-1-e"
  32. },
  33. items: "> *",
  34. menus: "ul",
  35. position: {
  36. my: "left-1 top",
  37. at: "right top"
  38. },
  39. role: "menu",
  40. // callbacks
  41. blur: null,
  42. focus: null,
  43. select: null
  44. },
  45. _create: function() {
  46. this.activeMenu = this.element;
  47. // Flag used to prevent firing of the click handler
  48. // as the event bubbles up through nested menus
  49. this.mouseHandled = false;
  50. this.element
  51. .uniqueId()
  52. .addClass( "ui-menu ui-widget ui-widget-content" )
  53. .toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length )
  54. .attr({
  55. role: this.options.role,
  56. tabIndex: 0
  57. });
  58. if ( this.options.disabled ) {
  59. this.element
  60. .addClass( "ui-state-disabled" )
  61. .attr( "aria-disabled", "true" );
  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": function( event ) {
  67. event.preventDefault();
  68. },
  69. "click .ui-menu-item": function( event ) {
  70. var target = $( event.target );
  71. if ( !this.mouseHandled && target.not( ".ui-state-disabled" ).length ) {
  72. this.select( event );
  73. // Only set the mouseHandled flag if the event will bubble, see #9469.
  74. if ( !event.isPropagationStopped() ) {
  75. this.mouseHandled = true;
  76. }
  77. // Open submenu on click
  78. if ( target.has( ".ui-menu" ).length ) {
  79. this.expand( event );
  80. } else if ( !this.element.is( ":focus" ) && $( this.document[ 0 ].activeElement ).closest( ".ui-menu" ).length ) {
  81. // Redirect focus to the menu
  82. this.element.trigger( "focus", [ true ] );
  83. // If the active item is on the top level, let it stay active.
  84. // Otherwise, blur the active item since it is no longer visible.
  85. if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) {
  86. clearTimeout( this.timer );
  87. }
  88. }
  89. }
  90. },
  91. "mouseenter .ui-menu-item": function( event ) {
  92. var target = $( event.currentTarget );
  93. // Remove ui-state-active class from siblings of the newly focused menu item
  94. // to avoid a jump caused by adjacent elements both having a class with a border
  95. target.siblings( ".ui-state-active" ).removeClass( "ui-state-active" );
  96. this.focus( event, target );
  97. },
  98. mouseleave: "collapseAll",
  99. "mouseleave .ui-menu": "collapseAll",
  100. focus: function( event, keepActiveItem ) {
  101. // If there's already an active item, keep it active
  102. // If not, activate the first item
  103. var item = this.active || this.element.find( this.options.items ).eq( 0 );
  104. if ( !keepActiveItem ) {
  105. this.focus( event, item );
  106. }
  107. },
  108. blur: function( event ) {
  109. this._delay(function() {
  110. if ( !$.contains( this.element[0], this.document[0].activeElement ) ) {
  111. this.collapseAll( event );
  112. }
  113. });
  114. },
  115. keydown: "_keydown"
  116. });
  117. this.refresh();
  118. // Clicks outside of a menu collapse any open menus
  119. this._on( this.document, {
  120. click: function( event ) {
  121. if ( this._closeOnDocumentClick( event ) ) {
  122. this.collapseAll( event );
  123. }
  124. // Reset the mouseHandled flag
  125. this.mouseHandled = false;
  126. }
  127. });
  128. },
  129. _destroy: function() {
  130. // Destroy (sub)menus
  131. this.element
  132. .removeAttr( "aria-activedescendant" )
  133. .find( ".ui-menu" ).addBack()
  134. .removeClass( "ui-menu ui-widget ui-widget-content ui-menu-icons ui-front" )
  135. .removeAttr( "role" )
  136. .removeAttr( "tabIndex" )
  137. .removeAttr( "aria-labelledby" )
  138. .removeAttr( "aria-expanded" )
  139. .removeAttr( "aria-hidden" )
  140. .removeAttr( "aria-disabled" )
  141. .removeUniqueId()
  142. .show();
  143. // Destroy menu items
  144. this.element.find( ".ui-menu-item" )
  145. .removeClass( "ui-menu-item" )
  146. .removeAttr( "role" )
  147. .removeAttr( "aria-disabled" )
  148. .removeUniqueId()
  149. .removeClass( "ui-state-hover" )
  150. .removeAttr( "tabIndex" )
  151. .removeAttr( "role" )
  152. .removeAttr( "aria-haspopup" )
  153. .children().each( function() {
  154. var elem = $( this );
  155. if ( elem.data( "ui-menu-submenu-carat" ) ) {
  156. elem.remove();
  157. }
  158. });
  159. // Destroy menu dividers
  160. this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
  161. },
  162. _keydown: function( event ) {
  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.find( this.options.items ).filter(function() {
  215. return regex.test( $( this ).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.find( this.options.items ).filter(function() {
  226. return regex.test( $( this ).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.is( "[aria-haspopup='true']" ) ) {
  250. this.expand( event );
  251. } else {
  252. this.select( event );
  253. }
  254. }
  255. },
  256. refresh: function() {
  257. var menus, items,
  258. that = this,
  259. icon = this.options.icons.submenu,
  260. submenus = this.element.find( this.options.menus );
  261. this.element.toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length );
  262. // Initialize nested menus
  263. submenus.filter( ":not(.ui-menu)" )
  264. .addClass( "ui-menu ui-widget ui-widget-content ui-front" )
  265. .hide()
  266. .attr({
  267. role: this.options.role,
  268. "aria-hidden": "true",
  269. "aria-expanded": "false"
  270. })
  271. .each(function() {
  272. var menu = $( this ),
  273. item = menu.parent(),
  274. submenuCarat = $( "<span>" )
  275. .addClass( "ui-menu-icon ui-icon " + icon )
  276. .data( "ui-menu-submenu-carat", true );
  277. item
  278. .attr( "aria-haspopup", "true" )
  279. .prepend( submenuCarat );
  280. menu.attr( "aria-labelledby", item.attr( "id" ) );
  281. });
  282. menus = submenus.add( this.element );
  283. items = menus.find( this.options.items );
  284. // Initialize menu-items containing spaces and/or dashes only as dividers
  285. items.not( ".ui-menu-item" ).each(function() {
  286. var item = $( this );
  287. if ( that._isDivider( item ) ) {
  288. item.addClass( "ui-widget-content ui-menu-divider" );
  289. }
  290. });
  291. // Don't refresh list items that are already adapted
  292. items.not( ".ui-menu-item, .ui-menu-divider" )
  293. .addClass( "ui-menu-item" )
  294. .uniqueId()
  295. .attr({
  296. tabIndex: -1,
  297. role: this._itemRole()
  298. });
  299. // Add aria-disabled attribute to any disabled menu item
  300. items.filter( ".ui-state-disabled" ).attr( "aria-disabled", "true" );
  301. // If the active item has been removed, blur the menu
  302. if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
  303. this.blur();
  304. }
  305. },
  306. _itemRole: function() {
  307. return {
  308. menu: "menuitem",
  309. listbox: "option"
  310. }[ this.options.role ];
  311. },
  312. _setOption: function( key, value ) {
  313. if ( key === "icons" ) {
  314. this.element.find( ".ui-menu-icon" )
  315. .removeClass( this.options.icons.submenu )
  316. .addClass( value.submenu );
  317. }
  318. if ( key === "disabled" ) {
  319. this.element
  320. .toggleClass( "ui-state-disabled", !!value )
  321. .attr( "aria-disabled", value );
  322. }
  323. this._super( key, value );
  324. },
  325. focus: function( event, item ) {
  326. var nested, focused;
  327. this.blur( event, event && event.type === "focus" );
  328. this._scrollIntoView( item );
  329. this.active = item.first();
  330. focused = this.active.addClass( "ui-state-focus" ).removeClass( "ui-state-active" );
  331. // Only update aria-activedescendant if there's a role
  332. // otherwise we assume focus is managed elsewhere
  333. if ( this.options.role ) {
  334. this.element.attr( "aria-activedescendant", focused.attr( "id" ) );
  335. }
  336. // Highlight active parent menu item, if any
  337. this.active
  338. .parent()
  339. .closest( ".ui-menu-item" )
  340. .addClass( "ui-state-active" );
  341. if ( event && event.type === "keydown" ) {
  342. this._close();
  343. } else {
  344. this.timer = this._delay(function() {
  345. this._close();
  346. }, this.delay );
  347. }
  348. nested = item.children( ".ui-menu" );
  349. if ( nested.length && event && ( /^mouse/.test( event.type ) ) ) {
  350. this._startOpening(nested);
  351. }
  352. this.activeMenu = item.parent();
  353. this._trigger( "focus", event, { item: item } );
  354. },
  355. _scrollIntoView: function( item ) {
  356. var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight;
  357. if ( this._hasScroll() ) {
  358. borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0;
  359. paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0;
  360. offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop;
  361. scroll = this.activeMenu.scrollTop();
  362. elementHeight = this.activeMenu.height();
  363. itemHeight = item.outerHeight();
  364. if ( offset < 0 ) {
  365. this.activeMenu.scrollTop( scroll + offset );
  366. } else if ( offset + itemHeight > elementHeight ) {
  367. this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight );
  368. }
  369. }
  370. },
  371. blur: function( event, fromFocus ) {
  372. if ( !fromFocus ) {
  373. clearTimeout( this.timer );
  374. }
  375. if ( !this.active ) {
  376. return;
  377. }
  378. this.active.removeClass( "ui-state-focus" );
  379. this.active = null;
  380. this._trigger( "blur", event, { item: this.active } );
  381. },
  382. _startOpening: function( submenu ) {
  383. clearTimeout( this.timer );
  384. // Don't open if already open fixes a Firefox bug that caused a .5 pixel
  385. // shift in the submenu position when mousing over the carat icon
  386. if ( submenu.attr( "aria-hidden" ) !== "true" ) {
  387. return;
  388. }
  389. this.timer = this._delay(function() {
  390. this._close();
  391. this._open( submenu );
  392. }, this.delay );
  393. },
  394. _open: function( submenu ) {
  395. var position = $.extend({
  396. of: this.active
  397. }, this.options.position );
  398. clearTimeout( this.timer );
  399. this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) )
  400. .hide()
  401. .attr( "aria-hidden", "true" );
  402. submenu
  403. .show()
  404. .removeAttr( "aria-hidden" )
  405. .attr( "aria-expanded", "true" )
  406. .position( position );
  407. },
  408. collapseAll: function( event, all ) {
  409. clearTimeout( this.timer );
  410. this.timer = this._delay(function() {
  411. // If we were passed an event, look for the submenu that contains the event
  412. var currentMenu = all ? this.element :
  413. $( event && event.target ).closest( this.element.find( ".ui-menu" ) );
  414. // If we found no valid submenu ancestor, use the main menu to close all sub menus anyway
  415. if ( !currentMenu.length ) {
  416. currentMenu = this.element;
  417. }
  418. this._close( currentMenu );
  419. this.blur( event );
  420. this.activeMenu = currentMenu;
  421. }, this.delay );
  422. },
  423. // With no arguments, closes the currently active menu - if nothing is active
  424. // it closes all menus. If passed an argument, it will search for menus BELOW
  425. _close: function( startMenu ) {
  426. if ( !startMenu ) {
  427. startMenu = this.active ? this.active.parent() : this.element;
  428. }
  429. startMenu
  430. .find( ".ui-menu" )
  431. .hide()
  432. .attr( "aria-hidden", "true" )
  433. .attr( "aria-expanded", "false" )
  434. .end()
  435. .find( ".ui-state-active" ).not( ".ui-state-focus" )
  436. .removeClass( "ui-state-active" );
  437. },
  438. _closeOnDocumentClick: function( event ) {
  439. return !$( event.target ).closest( ".ui-menu" ).length;
  440. },
  441. _isDivider: function( item ) {
  442. // Match hyphen, em dash, en dash
  443. return !/[^\-\u2014\u2013\s]/.test( item.text() );
  444. },
  445. collapse: function( event ) {
  446. var newItem = this.active &&
  447. this.active.parent().closest( ".ui-menu-item", this.element );
  448. if ( newItem && newItem.length ) {
  449. this._close();
  450. this.focus( event, newItem );
  451. }
  452. },
  453. expand: function( event ) {
  454. var newItem = this.active &&
  455. this.active
  456. .children( ".ui-menu " )
  457. .find( this.options.items )
  458. .first();
  459. if ( newItem && newItem.length ) {
  460. this._open( newItem.parent() );
  461. // Delay so Firefox will not hide activedescendant change in expanding submenu from AT
  462. this._delay(function() {
  463. this.focus( event, newItem );
  464. });
  465. }
  466. },
  467. next: function( event ) {
  468. this._move( "next", "first", event );
  469. },
  470. previous: function( event ) {
  471. this._move( "prev", "last", event );
  472. },
  473. isFirstItem: function() {
  474. return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
  475. },
  476. isLastItem: function() {
  477. return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
  478. },
  479. _move: function( direction, filter, event ) {
  480. var next;
  481. if ( this.active ) {
  482. if ( direction === "first" || direction === "last" ) {
  483. next = this.active
  484. [ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" )
  485. .eq( -1 );
  486. } else {
  487. next = this.active
  488. [ direction + "All" ]( ".ui-menu-item" )
  489. .eq( 0 );
  490. }
  491. }
  492. if ( !next || !next.length || !this.active ) {
  493. next = this.activeMenu.find( this.options.items )[ filter ]();
  494. }
  495. this.focus( event, next );
  496. },
  497. nextPage: function( event ) {
  498. var item, base, height;
  499. if ( !this.active ) {
  500. this.next( event );
  501. return;
  502. }
  503. if ( this.isLastItem() ) {
  504. return;
  505. }
  506. if ( this._hasScroll() ) {
  507. base = this.active.offset().top;
  508. height = this.element.height();
  509. this.active.nextAll( ".ui-menu-item" ).each(function() {
  510. item = $( this );
  511. return item.offset().top - base - height < 0;
  512. });
  513. this.focus( event, item );
  514. } else {
  515. this.focus( event, this.activeMenu.find( this.options.items )
  516. [ !this.active ? "first" : "last" ]() );
  517. }
  518. },
  519. previousPage: function( event ) {
  520. var item, base, height;
  521. if ( !this.active ) {
  522. this.next( event );
  523. return;
  524. }
  525. if ( this.isFirstItem() ) {
  526. return;
  527. }
  528. if ( this._hasScroll() ) {
  529. base = this.active.offset().top;
  530. height = this.element.height();
  531. this.active.prevAll( ".ui-menu-item" ).each(function() {
  532. item = $( this );
  533. return item.offset().top - base + height > 0;
  534. });
  535. this.focus( event, item );
  536. } else {
  537. this.focus( event, this.activeMenu.find( this.options.items ).first() );
  538. }
  539. },
  540. _hasScroll: function() {
  541. return this.element.outerHeight() < this.element.prop( "scrollHeight" );
  542. },
  543. select: function( event ) {
  544. // TODO: It should never be possible to not have an active item at this
  545. // point, but the tests don't trigger mouseenter before click.
  546. this.active = this.active || $( event.target ).closest( ".ui-menu-item" );
  547. var ui = { item: this.active };
  548. if ( !this.active.has( ".ui-menu" ).length ) {
  549. this.collapseAll( event, true );
  550. }
  551. this._trigger( "select", event, ui );
  552. }
  553. });
  554. }));