Нема описа

selectmenu.js 16KB

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