You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

694 rivejä
16 KiB

  1. /*!
  2. * jQuery UI Selectmenu 1.13.1
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://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: http://api.jqueryui.com/selectmenu/
  15. //>>demos: http://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.13.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. // Support: IE8
  148. // If the item was selected via a click, the text selection
  149. // will be destroyed in IE
  150. that._setSelection();
  151. that._select( ui.item.data( "ui-selectmenu-item" ), event );
  152. },
  153. focus: function( event, ui ) {
  154. var item = ui.item.data( "ui-selectmenu-item" );
  155. // Prevent inital focus from firing and check if its a newly focused item
  156. if ( that.focusIndex != null && item.index !== that.focusIndex ) {
  157. that._trigger( "focus", event, { item: item } );
  158. if ( !that.isOpen ) {
  159. that._select( item, event );
  160. }
  161. }
  162. that.focusIndex = item.index;
  163. that.button.attr( "aria-activedescendant",
  164. that.menuItems.eq( item.index ).attr( "id" ) );
  165. }
  166. } )
  167. .menu( "instance" );
  168. // Don't close the menu on mouseleave
  169. this.menuInstance._off( this.menu, "mouseleave" );
  170. // Cancel the menu's collapseAll on document click
  171. this.menuInstance._closeOnDocumentClick = function() {
  172. return false;
  173. };
  174. // Selects often contain empty items, but never contain dividers
  175. this.menuInstance._isDivider = function() {
  176. return false;
  177. };
  178. },
  179. refresh: function() {
  180. this._refreshMenu();
  181. this.buttonItem.replaceWith(
  182. this.buttonItem = this._renderButtonItem(
  183. // Fall back to an empty object in case there are no options
  184. this._getSelectedItem().data( "ui-selectmenu-item" ) || {}
  185. )
  186. );
  187. if ( this.options.width === null ) {
  188. this._resizeButton();
  189. }
  190. },
  191. _refreshMenu: function() {
  192. var item,
  193. options = this.element.find( "option" );
  194. this.menu.empty();
  195. this._parseOptions( options );
  196. this._renderMenu( this.menu, this.items );
  197. this.menuInstance.refresh();
  198. this.menuItems = this.menu.find( "li" )
  199. .not( ".ui-selectmenu-optgroup" )
  200. .find( ".ui-menu-item-wrapper" );
  201. this._rendered = true;
  202. if ( !options.length ) {
  203. return;
  204. }
  205. item = this._getSelectedItem();
  206. // Update the menu to have the correct item focused
  207. this.menuInstance.focus( null, item );
  208. this._setAria( item.data( "ui-selectmenu-item" ) );
  209. // Set disabled state
  210. this._setOption( "disabled", this.element.prop( "disabled" ) );
  211. },
  212. open: function( event ) {
  213. if ( this.options.disabled ) {
  214. return;
  215. }
  216. // If this is the first time the menu is being opened, render the items
  217. if ( !this._rendered ) {
  218. this._refreshMenu();
  219. } else {
  220. // Menu clears focus on close, reset focus to selected item
  221. this._removeClass( this.menu.find( ".ui-state-active" ), null, "ui-state-active" );
  222. this.menuInstance.focus( null, this._getSelectedItem() );
  223. }
  224. // If there are no options, don't open the menu
  225. if ( !this.menuItems.length ) {
  226. return;
  227. }
  228. this.isOpen = true;
  229. this._toggleAttr();
  230. this._resizeMenu();
  231. this._position();
  232. this._on( this.document, this._documentClick );
  233. this._trigger( "open", event );
  234. },
  235. _position: function() {
  236. this.menuWrap.position( $.extend( { of: this.button }, this.options.position ) );
  237. },
  238. close: function( event ) {
  239. if ( !this.isOpen ) {
  240. return;
  241. }
  242. this.isOpen = false;
  243. this._toggleAttr();
  244. this.range = null;
  245. this._off( this.document );
  246. this._trigger( "close", event );
  247. },
  248. widget: function() {
  249. return this.button;
  250. },
  251. menuWidget: function() {
  252. return this.menu;
  253. },
  254. _renderButtonItem: function( item ) {
  255. var buttonItem = $( "<span>" );
  256. this._setText( buttonItem, item.label );
  257. this._addClass( buttonItem, "ui-selectmenu-text" );
  258. return buttonItem;
  259. },
  260. _renderMenu: function( ul, items ) {
  261. var that = this,
  262. currentOptgroup = "";
  263. $.each( items, function( index, item ) {
  264. var li;
  265. if ( item.optgroup !== currentOptgroup ) {
  266. li = $( "<li>", {
  267. text: item.optgroup
  268. } );
  269. that._addClass( li, "ui-selectmenu-optgroup", "ui-menu-divider" +
  270. ( item.element.parent( "optgroup" ).prop( "disabled" ) ?
  271. " ui-state-disabled" :
  272. "" ) );
  273. li.appendTo( ul );
  274. currentOptgroup = item.optgroup;
  275. }
  276. that._renderItemData( ul, item );
  277. } );
  278. },
  279. _renderItemData: function( ul, item ) {
  280. return this._renderItem( ul, item ).data( "ui-selectmenu-item", item );
  281. },
  282. _renderItem: function( ul, item ) {
  283. var li = $( "<li>" ),
  284. wrapper = $( "<div>", {
  285. title: item.element.attr( "title" )
  286. } );
  287. if ( item.disabled ) {
  288. this._addClass( li, null, "ui-state-disabled" );
  289. }
  290. this._setText( wrapper, item.label );
  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. if ( window.getSelection ) {
  330. selection = window.getSelection();
  331. selection.removeAllRanges();
  332. selection.addRange( this.range );
  333. // Support: IE8
  334. } else {
  335. this.range.select();
  336. }
  337. // Support: IE
  338. // Setting the text selection kills the button focus in IE, but
  339. // restoring the focus doesn't kill the selection.
  340. this.button.focus();
  341. },
  342. _documentClick: {
  343. mousedown: function( event ) {
  344. if ( !this.isOpen ) {
  345. return;
  346. }
  347. if ( !$( event.target ).closest( ".ui-selectmenu-menu, #" +
  348. $.escapeSelector( this.ids.button ) ).length ) {
  349. this.close( event );
  350. }
  351. }
  352. },
  353. _buttonEvents: {
  354. // Prevent text selection from being reset when interacting with the selectmenu (#10144)
  355. mousedown: function() {
  356. var selection;
  357. if ( window.getSelection ) {
  358. selection = window.getSelection();
  359. if ( selection.rangeCount ) {
  360. this.range = selection.getRangeAt( 0 );
  361. }
  362. // Support: IE8
  363. } else {
  364. this.range = document.selection.createRange();
  365. }
  366. },
  367. click: function( event ) {
  368. this._setSelection();
  369. this._toggle( event );
  370. },
  371. keydown: function( event ) {
  372. var preventDefault = true;
  373. switch ( event.keyCode ) {
  374. case $.ui.keyCode.TAB:
  375. case $.ui.keyCode.ESCAPE:
  376. this.close( event );
  377. preventDefault = false;
  378. break;
  379. case $.ui.keyCode.ENTER:
  380. if ( this.isOpen ) {
  381. this._selectFocusedItem( event );
  382. }
  383. break;
  384. case $.ui.keyCode.UP:
  385. if ( event.altKey ) {
  386. this._toggle( event );
  387. } else {
  388. this._move( "prev", event );
  389. }
  390. break;
  391. case $.ui.keyCode.DOWN:
  392. if ( event.altKey ) {
  393. this._toggle( event );
  394. } else {
  395. this._move( "next", event );
  396. }
  397. break;
  398. case $.ui.keyCode.SPACE:
  399. if ( this.isOpen ) {
  400. this._selectFocusedItem( event );
  401. } else {
  402. this._toggle( event );
  403. }
  404. break;
  405. case $.ui.keyCode.LEFT:
  406. this._move( "prev", event );
  407. break;
  408. case $.ui.keyCode.RIGHT:
  409. this._move( "next", event );
  410. break;
  411. case $.ui.keyCode.HOME:
  412. case $.ui.keyCode.PAGE_UP:
  413. this._move( "first", event );
  414. break;
  415. case $.ui.keyCode.END:
  416. case $.ui.keyCode.PAGE_DOWN:
  417. this._move( "last", event );
  418. break;
  419. default:
  420. this.menu.trigger( event );
  421. preventDefault = false;
  422. }
  423. if ( preventDefault ) {
  424. event.preventDefault();
  425. }
  426. }
  427. },
  428. _selectFocusedItem: function( event ) {
  429. var item = this.menuItems.eq( this.focusIndex ).parent( "li" );
  430. if ( !item.hasClass( "ui-state-disabled" ) ) {
  431. this._select( item.data( "ui-selectmenu-item" ), event );
  432. }
  433. },
  434. _select: function( item, event ) {
  435. var oldIndex = this.element[ 0 ].selectedIndex;
  436. // Change native select element
  437. this.element[ 0 ].selectedIndex = item.index;
  438. this.buttonItem.replaceWith( this.buttonItem = this._renderButtonItem( item ) );
  439. this._setAria( item );
  440. this._trigger( "select", event, { item: item } );
  441. if ( item.index !== oldIndex ) {
  442. this._trigger( "change", event, { item: item } );
  443. }
  444. this.close( event );
  445. },
  446. _setAria: function( item ) {
  447. var id = this.menuItems.eq( item.index ).attr( "id" );
  448. this.button.attr( {
  449. "aria-labelledby": id,
  450. "aria-activedescendant": id
  451. } );
  452. this.menu.attr( "aria-activedescendant", id );
  453. },
  454. _setOption: function( key, value ) {
  455. if ( key === "icons" ) {
  456. var icon = this.button.find( "span.ui-icon" );
  457. this._removeClass( icon, null, this.options.icons.button )
  458. ._addClass( icon, null, value.button );
  459. }
  460. this._super( key, value );
  461. if ( key === "appendTo" ) {
  462. this.menuWrap.appendTo( this._appendTo() );
  463. }
  464. if ( key === "width" ) {
  465. this._resizeButton();
  466. }
  467. },
  468. _setOptionDisabled: function( value ) {
  469. this._super( value );
  470. this.menuInstance.option( "disabled", value );
  471. this.button.attr( "aria-disabled", value );
  472. this._toggleClass( this.button, null, "ui-state-disabled", value );
  473. this.element.prop( "disabled", value );
  474. if ( value ) {
  475. this.button.attr( "tabindex", -1 );
  476. this.close();
  477. } else {
  478. this.button.attr( "tabindex", 0 );
  479. }
  480. },
  481. _appendTo: function() {
  482. var element = this.options.appendTo;
  483. if ( element ) {
  484. element = element.jquery || element.nodeType ?
  485. $( element ) :
  486. this.document.find( element ).eq( 0 );
  487. }
  488. if ( !element || !element[ 0 ] ) {
  489. element = this.element.closest( ".ui-front, dialog" );
  490. }
  491. if ( !element.length ) {
  492. element = this.document[ 0 ].body;
  493. }
  494. return element;
  495. },
  496. _toggleAttr: function() {
  497. this.button.attr( "aria-expanded", this.isOpen );
  498. // We can't use two _toggleClass() calls here, because we need to make sure
  499. // we always remove classes first and add them second, otherwise if both classes have the
  500. // same theme class, it will be removed after we add it.
  501. this._removeClass( this.button, "ui-selectmenu-button-" +
  502. ( this.isOpen ? "closed" : "open" ) )
  503. ._addClass( this.button, "ui-selectmenu-button-" +
  504. ( this.isOpen ? "open" : "closed" ) )
  505. ._toggleClass( this.menuWrap, "ui-selectmenu-open", null, this.isOpen );
  506. this.menu.attr( "aria-hidden", !this.isOpen );
  507. },
  508. _resizeButton: function() {
  509. var width = this.options.width;
  510. // For `width: false`, just remove inline style and stop
  511. if ( width === false ) {
  512. this.button.css( "width", "" );
  513. return;
  514. }
  515. // For `width: null`, match the width of the original element
  516. if ( width === null ) {
  517. width = this.element.show().outerWidth();
  518. this.element.hide();
  519. }
  520. this.button.outerWidth( width );
  521. },
  522. _resizeMenu: function() {
  523. this.menu.outerWidth( Math.max(
  524. this.button.outerWidth(),
  525. // Support: IE10
  526. // IE10 wraps long text (possibly a rounding bug)
  527. // so we add 1px to avoid the wrapping
  528. this.menu.width( "" ).outerWidth() + 1
  529. ) );
  530. },
  531. _getCreateOptions: function() {
  532. var options = this._super();
  533. options.disabled = this.element.prop( "disabled" );
  534. return options;
  535. },
  536. _parseOptions: function( options ) {
  537. var that = this,
  538. data = [];
  539. options.each( function( index, item ) {
  540. if ( item.hidden ) {
  541. return;
  542. }
  543. data.push( that._parseOption( $( item ), index ) );
  544. } );
  545. this.items = data;
  546. },
  547. _parseOption: function( option, index ) {
  548. var optgroup = option.parent( "optgroup" );
  549. return {
  550. element: option,
  551. index: index,
  552. value: option.val(),
  553. label: option.text(),
  554. optgroup: optgroup.attr( "label" ) || "",
  555. disabled: optgroup.prop( "disabled" ) || option.prop( "disabled" )
  556. };
  557. },
  558. _destroy: function() {
  559. this._unbindFormResetHandler();
  560. this.menuWrap.remove();
  561. this.button.remove();
  562. this.element.show();
  563. this.element.removeUniqueId();
  564. this.labels.attr( "for", this.ids.element );
  565. }
  566. } ] );
  567. } );