selectmenu.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*!
  2. * jQuery UI Selectmenu 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: Selectmenu
  10. //>>group: Widgets
  11. /* eslint-disable max-len */
  12. //>>description: Duplicates and extends the functionality of a native HTML select element, allowing it to be customizable in behavior and appearance far beyond the limitations of a native select.
  13. /* eslint-enable max-len */
  14. //>>docs: https://api.jqueryui.com/selectmenu/
  15. //>>demos: https://jqueryui.com/selectmenu/
  16. //>>css.structure: ../../themes/base/core.css
  17. //>>css.structure: ../../themes/base/selectmenu.css, ../../themes/base/button.css
  18. //>>css.theme: ../../themes/base/theme.css
  19. ( function( factory ) {
  20. "use strict";
  21. if ( typeof define === "function" && define.amd ) {
  22. // AMD. Register as an anonymous module.
  23. define( [
  24. "jquery",
  25. "./menu",
  26. "../form-reset-mixin",
  27. "../keycode",
  28. "../labels",
  29. "../position",
  30. "../unique-id",
  31. "../version",
  32. "../widget"
  33. ], factory );
  34. } else {
  35. // Browser globals
  36. factory( jQuery );
  37. }
  38. } )( function( $ ) {
  39. "use strict";
  40. return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, {
  41. version: "1.14.1",
  42. defaultElement: "<select>",
  43. options: {
  44. appendTo: null,
  45. classes: {
  46. "ui-selectmenu-button-open": "ui-corner-top",
  47. "ui-selectmenu-button-closed": "ui-corner-all"
  48. },
  49. disabled: null,
  50. icons: {
  51. button: "ui-icon-triangle-1-s"
  52. },
  53. position: {
  54. my: "left top",
  55. at: "left bottom",
  56. collision: "none"
  57. },
  58. width: false,
  59. // Callbacks
  60. change: null,
  61. close: null,
  62. focus: null,
  63. open: null,
  64. select: null
  65. },
  66. _create: function() {
  67. var selectmenuId = this.element.uniqueId().attr( "id" );
  68. this.ids = {
  69. element: selectmenuId,
  70. button: selectmenuId + "-button",
  71. menu: selectmenuId + "-menu"
  72. };
  73. this._drawButton();
  74. this._drawMenu();
  75. this._bindFormResetHandler();
  76. this._rendered = false;
  77. this.menuItems = $();
  78. },
  79. _drawButton: function() {
  80. var icon,
  81. that = this,
  82. item = this._parseOption(
  83. this.element.find( "option:selected" ),
  84. this.element[ 0 ].selectedIndex
  85. );
  86. // Associate existing label with the new button
  87. this.labels = this.element.labels().attr( "for", this.ids.button );
  88. this._on( this.labels, {
  89. click: function( event ) {
  90. this.button.trigger( "focus" );
  91. event.preventDefault();
  92. }
  93. } );
  94. // Hide original select element
  95. this.element.hide();
  96. // Create button
  97. this.button = $( "<span>", {
  98. tabindex: this.options.disabled ? -1 : 0,
  99. id: this.ids.button,
  100. role: "combobox",
  101. "aria-expanded": "false",
  102. "aria-autocomplete": "list",
  103. "aria-owns": this.ids.menu,
  104. "aria-haspopup": "true",
  105. title: this.element.attr( "title" )
  106. } )
  107. .insertAfter( this.element );
  108. this._addClass( this.button, "ui-selectmenu-button ui-selectmenu-button-closed",
  109. "ui-button ui-widget" );
  110. icon = $( "<span>" ).appendTo( this.button );
  111. this._addClass( icon, "ui-selectmenu-icon", "ui-icon " + this.options.icons.button );
  112. this.buttonItem = this._renderButtonItem( item )
  113. .appendTo( this.button );
  114. if ( this.options.width !== false ) {
  115. this._resizeButton();
  116. }
  117. this._on( this.button, this._buttonEvents );
  118. this.button.one( "focusin", function() {
  119. // Delay rendering the menu items until the button receives focus.
  120. // The menu may have already been rendered via a programmatic open.
  121. if ( !that._rendered ) {
  122. that._refreshMenu();
  123. }
  124. } );
  125. },
  126. _drawMenu: function() {
  127. var that = this;
  128. // Create menu
  129. this.menu = $( "<ul>", {
  130. "aria-hidden": "true",
  131. "aria-labelledby": this.ids.button,
  132. id: this.ids.menu
  133. } );
  134. // Wrap menu
  135. this.menuWrap = $( "<div>" ).append( this.menu );
  136. this._addClass( this.menuWrap, "ui-selectmenu-menu", "ui-front" );
  137. this.menuWrap.appendTo( this._appendTo() );
  138. // Initialize menu widget
  139. this.menuInstance = this.menu
  140. .menu( {
  141. classes: {
  142. "ui-menu": "ui-corner-bottom"
  143. },
  144. role: "listbox",
  145. select: function( event, ui ) {
  146. event.preventDefault();
  147. that._select( ui.item.data( "ui-selectmenu-item" ), event );
  148. },
  149. focus: function( event, ui ) {
  150. var item = ui.item.data( "ui-selectmenu-item" );
  151. // Prevent inital focus from firing and check if its a newly focused item
  152. if ( that.focusIndex != null && item.index !== that.focusIndex ) {
  153. that._trigger( "focus", event, { item: item } );
  154. if ( !that.isOpen ) {
  155. that._select( item, event );
  156. }
  157. }
  158. that.focusIndex = item.index;
  159. that.button.attr( "aria-activedescendant",
  160. that.menuItems.eq( item.index ).attr( "id" ) );
  161. }
  162. } )
  163. .menu( "instance" );
  164. // Don't close the menu on mouseleave
  165. this.menuInstance._off( this.menu, "mouseleave" );
  166. // Cancel the menu's collapseAll on document click
  167. this.menuInstance._closeOnDocumentClick = function() {
  168. return false;
  169. };
  170. // Selects often contain empty items, but never contain dividers
  171. this.menuInstance._isDivider = function() {
  172. return false;
  173. };
  174. },
  175. refresh: function() {
  176. this._refreshMenu();
  177. this.buttonItem.replaceWith(
  178. this.buttonItem = this._renderButtonItem(
  179. // Fall back to an empty object in case there are no options
  180. this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
  181. )
  182. );
  183. if ( this.options.width === null ) {
  184. this._resizeButton();
  185. }
  186. },
  187. _refreshMenu: function() {
  188. var item,
  189. options = this.element.find( "option" );
  190. this.menu.empty();
  191. this._parseOptions( options );
  192. this._renderMenu( this.menu, this.items );
  193. this.menuInstance.refresh();
  194. this.menuItems = this.menu.find( "li" )
  195. .not( ".ui-selectmenu-optgroup" )
  196. .find( ".ui-menu-item-wrapper" );
  197. this._rendered = true;
  198. if ( !options.length ) {
  199. return;
  200. }
  201. item = this._getSelectedItem();
  202. // Update the menu to have the correct item focused
  203. this.menuInstance.focus( null, item );
  204. this._setAria( item.data( "ui-selectmenu-item" ) );
  205. // Set disabled state
  206. this._setOption( "disabled", this.element.prop( "disabled" ) );
  207. },
  208. open: function( event ) {
  209. if ( this.options.disabled ) {
  210. return;
  211. }
  212. // If this is the first time the menu is being opened, render the items
  213. if ( !this._rendered ) {
  214. this._refreshMenu();
  215. } else {
  216. // Menu clears focus on close, reset focus to selected item
  217. this._removeClass( this.menu.find( ".ui-state-active" ), null, "ui-state-active" );
  218. this.menuInstance.focus( null, this._getSelectedItem() );
  219. }
  220. // If there are no options, don't open the menu
  221. if ( !this.menuItems.length ) {
  222. return;
  223. }
  224. this.isOpen = true;
  225. this._toggleAttr();
  226. this._resizeMenu();
  227. this._position();
  228. this._on( this.document, this._documentClick );
  229. this._trigger( "open", event );
  230. },
  231. _position: function() {
  232. this.menuWrap.position( $.extend( { of: this.button }, this.options.position ) );
  233. },
  234. close: function( event ) {
  235. if ( !this.isOpen ) {
  236. return;
  237. }
  238. this.isOpen = false;
  239. this._toggleAttr();
  240. this.range = null;
  241. this._off( this.document );
  242. this._trigger( "close", event );
  243. },
  244. widget: function() {
  245. return this.button;
  246. },
  247. menuWidget: function() {
  248. return this.menu;
  249. },
  250. _renderButtonItem: function( item ) {
  251. var buttonItem = $( "<span>" );
  252. this._setText( buttonItem, item.label );
  253. this._addClass( buttonItem, "ui-selectmenu-text" );
  254. return buttonItem;
  255. },
  256. _renderMenu: function( ul, items ) {
  257. var that = this,
  258. currentOptgroup = "";
  259. $.each( items, function( index, item ) {
  260. var li;
  261. if ( item.optgroup !== currentOptgroup ) {
  262. li = $( "<li>", {
  263. text: item.optgroup
  264. } );
  265. that._addClass( li, "ui-selectmenu-optgroup", "ui-menu-divider" +
  266. ( item.element.parent( "optgroup" ).prop( "disabled" ) ?
  267. " ui-state-disabled" :
  268. "" ) );
  269. li.appendTo( ul );
  270. currentOptgroup = item.optgroup;
  271. }
  272. that._renderItemData( ul, item );
  273. } );
  274. },
  275. _renderItemData: function( ul, item ) {
  276. return this._renderItem( ul, item ).data( "ui-selectmenu-item", item );
  277. },
  278. _renderItem: function( ul, item ) {
  279. var li = $( "<li>" ),
  280. wrapper = $( "<div>", {
  281. title: item.element.attr( "title" )
  282. } );
  283. if ( item.disabled ) {
  284. this._addClass( li, null, "ui-state-disabled" );
  285. }
  286. if ( item.hidden ) {
  287. li.prop( "hidden", true );
  288. } else {
  289. this._setText( wrapper, item.label );
  290. }
  291. return li.append( wrapper ).appendTo( ul );
  292. },
  293. _setText: function( element, value ) {
  294. if ( value ) {
  295. element.text( value );
  296. } else {
  297. element.html( "&#160;" );
  298. }
  299. },
  300. _move: function( direction, event ) {
  301. var item, next,
  302. filter = ".ui-menu-item";
  303. if ( this.isOpen ) {
  304. item = this.menuItems.eq( this.focusIndex ).parent( "li" );
  305. } else {
  306. item = this.menuItems.eq( this.element[ 0 ].selectedIndex ).parent( "li" );
  307. filter += ":not(.ui-state-disabled)";
  308. }
  309. if ( direction === "first" || direction === "last" ) {
  310. next = item[ direction === "first" ? "prevAll" : "nextAll" ]( filter ).eq( -1 );
  311. } else {
  312. next = item[ direction + "All" ]( filter ).eq( 0 );
  313. }
  314. if ( next.length ) {
  315. this.menuInstance.focus( event, next );
  316. }
  317. },
  318. _getSelectedItem: function() {
  319. return this.menuItems.eq( this.element[ 0 ].selectedIndex ).parent( "li" );
  320. },
  321. _toggle: function( event ) {
  322. this[ this.isOpen ? "close" : "open" ]( event );
  323. },
  324. _setSelection: function() {
  325. var selection;
  326. if ( !this.range ) {
  327. return;
  328. }
  329. selection = window.getSelection();
  330. selection.removeAllRanges();
  331. selection.addRange( this.range );
  332. },
  333. _documentClick: {
  334. mousedown: function( event ) {
  335. if ( !this.isOpen ) {
  336. return;
  337. }
  338. if ( !$( event.target ).closest( ".ui-selectmenu-menu, #" +
  339. CSS.escape( this.ids.button ) ).length ) {
  340. this.close( event );
  341. }
  342. }
  343. },
  344. _buttonEvents: {
  345. // Prevent text selection from being reset when interacting with the selectmenu (#10144)
  346. mousedown: function() {
  347. var selection = window.getSelection();
  348. if ( selection.rangeCount ) {
  349. this.range = selection.getRangeAt( 0 );
  350. }
  351. },
  352. click: function( event ) {
  353. this._setSelection();
  354. this._toggle( event );
  355. },
  356. keydown: function( event ) {
  357. var preventDefault = true;
  358. switch ( event.keyCode ) {
  359. case $.ui.keyCode.TAB:
  360. case $.ui.keyCode.ESCAPE:
  361. this.close( event );
  362. preventDefault = false;
  363. break;
  364. case $.ui.keyCode.ENTER:
  365. if ( this.isOpen ) {
  366. this._selectFocusedItem( event );
  367. }
  368. break;
  369. case $.ui.keyCode.UP:
  370. if ( event.altKey ) {
  371. this._toggle( event );
  372. } else {
  373. this._move( "prev", event );
  374. }
  375. break;
  376. case $.ui.keyCode.DOWN:
  377. if ( event.altKey ) {
  378. this._toggle( event );
  379. } else {
  380. this._move( "next", event );
  381. }
  382. break;
  383. case $.ui.keyCode.SPACE:
  384. if ( this.isOpen ) {
  385. this._selectFocusedItem( event );
  386. } else {
  387. this._toggle( event );
  388. }
  389. break;
  390. case $.ui.keyCode.LEFT:
  391. this._move( "prev", event );
  392. break;
  393. case $.ui.keyCode.RIGHT:
  394. this._move( "next", event );
  395. break;
  396. case $.ui.keyCode.HOME:
  397. case $.ui.keyCode.PAGE_UP:
  398. this._move( "first", event );
  399. break;
  400. case $.ui.keyCode.END:
  401. case $.ui.keyCode.PAGE_DOWN:
  402. this._move( "last", event );
  403. break;
  404. default:
  405. this.menu.trigger( event );
  406. preventDefault = false;
  407. }
  408. if ( preventDefault ) {
  409. event.preventDefault();
  410. }
  411. }
  412. },
  413. _selectFocusedItem: function( event ) {
  414. var item = this.menuItems.eq( this.focusIndex ).parent( "li" );
  415. if ( !item.hasClass( "ui-state-disabled" ) ) {
  416. this._select( item.data( "ui-selectmenu-item" ), event );
  417. }
  418. },
  419. _select: function( item, event ) {
  420. var oldIndex = this.element[ 0 ].selectedIndex;
  421. // Change native select element
  422. this.element[ 0 ].selectedIndex = item.index;
  423. this.buttonItem.replaceWith( this.buttonItem = this._renderButtonItem( item ) );
  424. this._setAria( item );
  425. this._trigger( "select", event, { item: item } );
  426. if ( item.index !== oldIndex ) {
  427. this._trigger( "change", event, { item: item } );
  428. }
  429. this.close( event );
  430. },
  431. _setAria: function( item ) {
  432. var id = this.menuItems.eq( item.index ).attr( "id" );
  433. this.button.attr( {
  434. "aria-labelledby": id,
  435. "aria-activedescendant": id
  436. } );
  437. this.menu.attr( "aria-activedescendant", id );
  438. },
  439. _setOption: function( key, value ) {
  440. if ( key === "icons" ) {
  441. var icon = this.button.find( "span.ui-icon" );
  442. this._removeClass( icon, null, this.options.icons.button )
  443. ._addClass( icon, null, value.button );
  444. }
  445. this._super( key, value );
  446. if ( key === "appendTo" ) {
  447. this.menuWrap.appendTo( this._appendTo() );
  448. }
  449. if ( key === "width" ) {
  450. this._resizeButton();
  451. }
  452. },
  453. _setOptionDisabled: function( value ) {
  454. this._super( value );
  455. this.menuInstance.option( "disabled", value );
  456. this.button.attr( "aria-disabled", value );
  457. this._toggleClass( this.button, null, "ui-state-disabled", value );
  458. this.element.prop( "disabled", value );
  459. if ( value ) {
  460. this.button.attr( "tabindex", -1 );
  461. this.close();
  462. } else {
  463. this.button.attr( "tabindex", 0 );
  464. }
  465. },
  466. _appendTo: function() {
  467. var element = this.options.appendTo;
  468. if ( element ) {
  469. element = element.jquery || element.nodeType ?
  470. $( element ) :
  471. this.document.find( element ).eq( 0 );
  472. }
  473. if ( !element || !element[ 0 ] ) {
  474. element = this.element.closest( ".ui-front, dialog" );
  475. }
  476. if ( !element.length ) {
  477. element = this.document[ 0 ].body;
  478. }
  479. return element;
  480. },
  481. _toggleAttr: function() {
  482. this.button.attr( "aria-expanded", this.isOpen );
  483. // We can't use two _toggleClass() calls here, because we need to make sure
  484. // we always remove classes first and add them second, otherwise if both classes have the
  485. // same theme class, it will be removed after we add it.
  486. this._removeClass( this.button, "ui-selectmenu-button-" +
  487. ( this.isOpen ? "closed" : "open" ) )
  488. ._addClass( this.button, "ui-selectmenu-button-" +
  489. ( this.isOpen ? "open" : "closed" ) )
  490. ._toggleClass( this.menuWrap, "ui-selectmenu-open", null, this.isOpen );
  491. this.menu.attr( "aria-hidden", !this.isOpen );
  492. },
  493. _resizeButton: function() {
  494. var width = this.options.width;
  495. // For `width: false`, just remove inline style and stop
  496. if ( width === false ) {
  497. this.button.css( "width", "" );
  498. return;
  499. }
  500. // For `width: null`, match the width of the original element
  501. if ( width === null ) {
  502. width = this.element.show().outerWidth();
  503. this.element.hide();
  504. }
  505. this.button.outerWidth( width );
  506. },
  507. _resizeMenu: function() {
  508. this.menu.outerWidth( Math.max(
  509. this.button.outerWidth(),
  510. this.menu.width( "" ).outerWidth()
  511. ) );
  512. },
  513. _getCreateOptions: function() {
  514. var options = this._super();
  515. options.disabled = this.element.prop( "disabled" );
  516. return options;
  517. },
  518. _parseOptions: function( options ) {
  519. var that = this,
  520. data = [];
  521. options.each( function( index, item ) {
  522. data.push( that._parseOption( $( item ), index ) );
  523. } );
  524. this.items = data;
  525. },
  526. _parseOption: function( option, index ) {
  527. var optgroup = option.parent( "optgroup" );
  528. return {
  529. element: option,
  530. index: index,
  531. value: option.val(),
  532. label: option.text(),
  533. hidden: optgroup.prop( "hidden" ) || option.prop( "hidden" ),
  534. optgroup: optgroup.attr( "label" ) || "",
  535. disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
  536. };
  537. },
  538. _destroy: function() {
  539. this._unbindFormResetHandler();
  540. this.menuWrap.remove();
  541. this.button.remove();
  542. this.element.show();
  543. this.element.removeUniqueId();
  544. this.labels.attr( "for", this.ids.element );
  545. }
  546. } ] );
  547. } );