jquery.ui.menu.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*!
  2. * jQuery UI Menu 1.10.4
  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. * 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: "1.10.4",
  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.select( event );
  74. // Only set the mouseHandled flag if the event will bubble, see #9469.
  75. if ( !event.isPropagationStopped() ) {
  76. this.mouseHandled = true;
  77. }
  78. // Open submenu on click
  79. if ( target.has( ".ui-menu" ).length ) {
  80. this.expand( event );
  81. } else if ( !this.element.is( ":focus" ) && $( this.document[ 0 ].activeElement ).closest( ".ui-menu" ).length ) {
  82. // Redirect focus to the menu
  83. this.element.trigger( "focus", [ true ] );
  84. // If the active item is on the top level, let it stay active.
  85. // Otherwise, blur the active item since it is no longer visible.
  86. if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) {
  87. clearTimeout( this.timer );
  88. }
  89. }
  90. }
  91. },
  92. "mouseenter .ui-menu-item": function( event ) {
  93. var target = $( event.currentTarget );
  94. // Remove ui-state-active class from siblings of the newly focused menu item
  95. // to avoid a jump caused by adjacent elements both having a class with a border
  96. target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" );
  97. this.focus( event, target );
  98. },
  99. mouseleave: "collapseAll",
  100. "mouseleave .ui-menu": "collapseAll",
  101. focus: function( event, keepActiveItem ) {
  102. // If there's already an active item, keep it active
  103. // If not, activate the first item
  104. var item = this.active || this.element.children( ".ui-menu-item" ).eq( 0 );
  105. if ( !keepActiveItem ) {
  106. this.focus( event, item );
  107. }
  108. },
  109. blur: function( event ) {
  110. this._delay(function() {
  111. if ( !$.contains( this.element[0], this.document[0].activeElement ) ) {
  112. this.collapseAll( event );
  113. }
  114. });
  115. },
  116. keydown: "_keydown"
  117. });
  118. this.refresh();
  119. // Clicks outside of a menu collapse any open menus
  120. this._on( this.document, {
  121. click: function( event ) {
  122. if ( !$( event.target ).closest( ".ui-menu" ).length ) {
  123. this.collapseAll( event );
  124. }
  125. // Reset the mouseHandled flag
  126. this.mouseHandled = false;
  127. }
  128. });
  129. },
  130. _destroy: function() {
  131. // Destroy (sub)menus
  132. this.element
  133. .removeAttr( "aria-activedescendant" )
  134. .find( ".ui-menu" ).addBack()
  135. .removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" )
  136. .removeAttr( "role" )
  137. .removeAttr( "tabIndex" )
  138. .removeAttr( "aria-labelledby" )
  139. .removeAttr( "aria-expanded" )
  140. .removeAttr( "aria-hidden" )
  141. .removeAttr( "aria-disabled" )
  142. .removeUniqueId()
  143. .show();
  144. // Destroy menu items
  145. this.element.find( ".ui-menu-item" )
  146. .removeClass( "ui-menu-item" )
  147. .removeAttr( "role" )
  148. .removeAttr( "aria-disabled" )
  149. .children( "a" )
  150. .removeUniqueId()
  151. .removeClass( "ui-corner-all ui-state-hover" )
  152. .removeAttr( "tabIndex" )
  153. .removeAttr( "role" )
  154. .removeAttr( "aria-haspopup" )
  155. .children().each( function() {
  156. var elem = $( this );
  157. if ( elem.data( "ui-menu-submenu-carat" ) ) {
  158. elem.remove();
  159. }
  160. });
  161. // Destroy menu dividers
  162. this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" );
  163. },
  164. _keydown: function( event ) {
  165. var match, prev, character, skip, regex,
  166. preventDefault = true;
  167. function escape( value ) {
  168. return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" );
  169. }
  170. switch ( event.keyCode ) {
  171. case $.ui.keyCode.PAGE_UP:
  172. this.previousPage( event );
  173. break;
  174. case $.ui.keyCode.PAGE_DOWN:
  175. this.nextPage( event );
  176. break;
  177. case $.ui.keyCode.HOME:
  178. this._move( "first", "first", event );
  179. break;
  180. case $.ui.keyCode.END:
  181. this._move( "last", "last", event );
  182. break;
  183. case $.ui.keyCode.UP:
  184. this.previous( event );
  185. break;
  186. case $.ui.keyCode.DOWN:
  187. this.next( event );
  188. break;
  189. case $.ui.keyCode.LEFT:
  190. this.collapse( event );
  191. break;
  192. case $.ui.keyCode.RIGHT:
  193. if ( this.active && !this.active.is( ".ui-state-disabled" ) ) {
  194. this.expand( event );
  195. }
  196. break;
  197. case $.ui.keyCode.ENTER:
  198. case $.ui.keyCode.SPACE:
  199. this._activate( event );
  200. break;
  201. case $.ui.keyCode.ESCAPE:
  202. this.collapse( event );
  203. break;
  204. default:
  205. preventDefault = false;
  206. prev = this.previousFilter || "";
  207. character = String.fromCharCode( event.keyCode );
  208. skip = false;
  209. clearTimeout( this.filterTimer );
  210. if ( character === prev ) {
  211. skip = true;
  212. } else {
  213. character = prev + character;
  214. }
  215. regex = new RegExp( "^" + escape( character ), "i" );
  216. match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
  217. return regex.test( $( this ).children( "a" ).text() );
  218. });
  219. match = skip && match.index( this.active.next() ) !== -1 ?
  220. this.active.nextAll( ".ui-menu-item" ) :
  221. match;
  222. // If no matches on the current filter, reset to the last character pressed
  223. // to move down the menu to the first item that starts with that character
  224. if ( !match.length ) {
  225. character = String.fromCharCode( event.keyCode );
  226. regex = new RegExp( "^" + escape( character ), "i" );
  227. match = this.activeMenu.children( ".ui-menu-item" ).filter(function() {
  228. return regex.test( $( this ).children( "a" ).text() );
  229. });
  230. }
  231. if ( match.length ) {
  232. this.focus( event, match );
  233. if ( match.length > 1 ) {
  234. this.previousFilter = character;
  235. this.filterTimer = this._delay(function() {
  236. delete this.previousFilter;
  237. }, 1000 );
  238. } else {
  239. delete this.previousFilter;
  240. }
  241. } else {
  242. delete this.previousFilter;
  243. }
  244. }
  245. if ( preventDefault ) {
  246. event.preventDefault();
  247. }
  248. },
  249. _activate: function( event ) {
  250. if ( !this.active.is( ".ui-state-disabled" ) ) {
  251. if ( this.active.children( "a[aria-haspopup='true']" ).length ) {
  252. this.expand( event );
  253. } else {
  254. this.select( event );
  255. }
  256. }
  257. },
  258. refresh: function() {
  259. var menus,
  260. icon = this.options.icons.submenu,
  261. submenus = this.element.find( this.options.menus );
  262. this.element.toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length );
  263. // Initialize nested menus
  264. submenus.filter( ":not(.ui-menu)" )
  265. .addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" )
  266. .hide()
  267. .attr({
  268. role: this.options.role,
  269. "aria-hidden": "true",
  270. "aria-expanded": "false"
  271. })
  272. .each(function() {
  273. var menu = $( this ),
  274. item = menu.prev( "a" ),
  275. submenuCarat = $( "<span>" )
  276. .addClass( "ui-menu-icon ui-icon " + icon )
  277. .data( "ui-menu-submenu-carat", true );
  278. item
  279. .attr( "aria-haspopup", "true" )
  280. .prepend( submenuCarat );
  281. menu.attr( "aria-labelledby", item.attr( "id" ) );
  282. });
  283. menus = submenus.add( this.element );
  284. // Don't refresh list items that are already adapted
  285. menus.children( ":not(.ui-menu-item):has(a)" )
  286. .addClass( "ui-menu-item" )
  287. .attr( "role", "presentation" )
  288. .children( "a" )
  289. .uniqueId()
  290. .addClass( "ui-corner-all" )
  291. .attr({
  292. tabIndex: -1,
  293. role: this._itemRole()
  294. });
  295. // Initialize unlinked menu-items containing spaces and/or dashes only as dividers
  296. menus.children( ":not(.ui-menu-item)" ).each(function() {
  297. var item = $( this );
  298. // hyphen, em dash, en dash
  299. if ( !/[^\-\u2014\u2013\s]/.test( item.text() ) ) {
  300. item.addClass( "ui-widget-content ui-menu-divider" );
  301. }
  302. });
  303. // Add aria-disabled attribute to any disabled menu item
  304. menus.children( ".ui-state-disabled" ).attr( "aria-disabled", "true" );
  305. // If the active item has been removed, blur the menu
  306. if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) {
  307. this.blur();
  308. }
  309. },
  310. _itemRole: function() {
  311. return {
  312. menu: "menuitem",
  313. listbox: "option"
  314. }[ this.options.role ];
  315. },
  316. _setOption: function( key, value ) {
  317. if ( key === "icons" ) {
  318. this.element.find( ".ui-menu-icon" )
  319. .removeClass( this.options.icons.submenu )
  320. .addClass( value.submenu );
  321. }
  322. this._super( key, value );
  323. },
  324. focus: function( event, item ) {
  325. var nested, focused;
  326. this.blur( event, event && event.type === "focus" );
  327. this._scrollIntoView( item );
  328. this.active = item.first();
  329. focused = this.active.children( "a" ).addClass( "ui-state-focus" );
  330. // Only update aria-activedescendant if there's a role
  331. // otherwise we assume focus is managed elsewhere
  332. if ( this.options.role ) {
  333. this.element.attr( "aria-activedescendant", focused.attr( "id" ) );
  334. }
  335. // Highlight active parent menu item, if any
  336. this.active
  337. .parent()
  338. .closest( ".ui-menu-item" )
  339. .children( "a:first" )
  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.height();
  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.children( "a" ).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( "a.ui-state-active" )
  436. .removeClass( "ui-state-active" );
  437. },
  438. collapse: function( event ) {
  439. var newItem = this.active &&
  440. this.active.parent().closest( ".ui-menu-item", this.element );
  441. if ( newItem && newItem.length ) {
  442. this._close();
  443. this.focus( event, newItem );
  444. }
  445. },
  446. expand: function( event ) {
  447. var newItem = this.active &&
  448. this.active
  449. .children( ".ui-menu " )
  450. .children( ".ui-menu-item" )
  451. .first();
  452. if ( newItem && newItem.length ) {
  453. this._open( newItem.parent() );
  454. // Delay so Firefox will not hide activedescendant change in expanding submenu from AT
  455. this._delay(function() {
  456. this.focus( event, newItem );
  457. });
  458. }
  459. },
  460. next: function( event ) {
  461. this._move( "next", "first", event );
  462. },
  463. previous: function( event ) {
  464. this._move( "prev", "last", event );
  465. },
  466. isFirstItem: function() {
  467. return this.active && !this.active.prevAll( ".ui-menu-item" ).length;
  468. },
  469. isLastItem: function() {
  470. return this.active && !this.active.nextAll( ".ui-menu-item" ).length;
  471. },
  472. _move: function( direction, filter, event ) {
  473. var next;
  474. if ( this.active ) {
  475. if ( direction === "first" || direction === "last" ) {
  476. next = this.active
  477. [ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" )
  478. .eq( -1 );
  479. } else {
  480. next = this.active
  481. [ direction + "All" ]( ".ui-menu-item" )
  482. .eq( 0 );
  483. }
  484. }
  485. if ( !next || !next.length || !this.active ) {
  486. next = this.activeMenu.children( ".ui-menu-item" )[ filter ]();
  487. }
  488. this.focus( event, next );
  489. },
  490. nextPage: function( event ) {
  491. var item, base, height;
  492. if ( !this.active ) {
  493. this.next( event );
  494. return;
  495. }
  496. if ( this.isLastItem() ) {
  497. return;
  498. }
  499. if ( this._hasScroll() ) {
  500. base = this.active.offset().top;
  501. height = this.element.height();
  502. this.active.nextAll( ".ui-menu-item" ).each(function() {
  503. item = $( this );
  504. return item.offset().top - base - height < 0;
  505. });
  506. this.focus( event, item );
  507. } else {
  508. this.focus( event, this.activeMenu.children( ".ui-menu-item" )
  509. [ !this.active ? "first" : "last" ]() );
  510. }
  511. },
  512. previousPage: function( event ) {
  513. var item, base, height;
  514. if ( !this.active ) {
  515. this.next( event );
  516. return;
  517. }
  518. if ( this.isFirstItem() ) {
  519. return;
  520. }
  521. if ( this._hasScroll() ) {
  522. base = this.active.offset().top;
  523. height = this.element.height();
  524. this.active.prevAll( ".ui-menu-item" ).each(function() {
  525. item = $( this );
  526. return item.offset().top - base + height > 0;
  527. });
  528. this.focus( event, item );
  529. } else {
  530. this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() );
  531. }
  532. },
  533. _hasScroll: function() {
  534. return this.element.outerHeight() < this.element.prop( "scrollHeight" );
  535. },
  536. select: function( event ) {
  537. // TODO: It should never be possible to not have an active item at this
  538. // point, but the tests don't trigger mouseenter before click.
  539. this.active = this.active || $( event.target ).closest( ".ui-menu-item" );
  540. var ui = { item: this.active };
  541. if ( !this.active.has( ".ui-menu" ).length ) {
  542. this.collapseAll( event, true );
  543. }
  544. this._trigger( "select", event, ui );
  545. }
  546. });
  547. }( jQuery ));