1
0

selectmenu.js 13 KB

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