jquery.multiselect.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* jshint forin:true, noarg:true, noempty:true, eqeqeq:true, boss:true, undef:true, curly:true, browser:true, jquery:true */
  2. /*
  3. * jQuery MultiSelect UI Widget 1.14pre
  4. * Copyright (c) 2012 Eric Hynds
  5. *
  6. * http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/
  7. *
  8. * Depends:
  9. * - jQuery 1.4.2+
  10. * - jQuery UI 1.8 widget factory
  11. *
  12. * Optional:
  13. * - jQuery UI effects
  14. * - jQuery UI position utility
  15. *
  16. * Dual licensed under the MIT and GPL licenses:
  17. * http://www.opensource.org/licenses/mit-license.php
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. * Patched by Denis Flaven to add the minWidthButton option
  21. *
  22. */
  23. (function($, undefined) {
  24. var multiselectID = 0;
  25. var $doc = $(document);
  26. $.widget("ech.multiselect", {
  27. // default options
  28. options: {
  29. header: true,
  30. height: 175,
  31. minWidthButton: 150,
  32. minWidth: 250,
  33. classes: '',
  34. checkAllText: 'Check all',
  35. uncheckAllText: 'Uncheck all',
  36. noneSelectedText: 'Select options',
  37. selectedText: '# selected',
  38. selectedList: 0,
  39. show: null,
  40. hide: null,
  41. autoOpen: false,
  42. multiple: true,
  43. position: {},
  44. appendTo: "body"
  45. },
  46. _create: function() {
  47. var el = this.element.hide();
  48. var o = this.options;
  49. this.speed = $.fx.speeds._default; // default speed for effects
  50. this._isOpen = false; // assume no
  51. // create a unique namespace for events that the widget
  52. // factory cannot unbind automatically. Use eventNamespace if on
  53. // jQuery UI 1.9+, and otherwise fallback to a custom string.
  54. this._namespaceID = this.eventNamespace || ('multiselect' + multiselectID);
  55. var button = (this.button = $('<button type="button"><span class="ui-icon ui-icon-triangle-1-s"></span></button>'))
  56. .addClass('ui-multiselect ui-widget ui-state-default ui-corner-all')
  57. .addClass(o.classes)
  58. .attr({ 'title':el.attr('title'), 'aria-haspopup':true, 'tabIndex':el.attr('tabIndex') })
  59. .insertAfter(el),
  60. buttonlabel = (this.buttonlabel = $('<span />'))
  61. .html(o.noneSelectedText)
  62. .appendTo(button),
  63. menu = (this.menu = $('<div />'))
  64. .addClass('ui-multiselect-menu ui-widget ui-widget-content ui-corner-all')
  65. .addClass(o.classes)
  66. .appendTo($(o.appendTo)),
  67. header = (this.header = $('<div />'))
  68. .addClass('ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix')
  69. .appendTo(menu),
  70. headerLinkContainer = (this.headerLinkContainer = $('<ul />'))
  71. .addClass('ui-helper-reset')
  72. .html(function() {
  73. if(o.header === true) {
  74. return '<li><a class="ui-multiselect-all" href="#"><span class="ui-icon ui-icon-check"></span><span>' + o.checkAllText + '</span></a></li><li><a class="ui-multiselect-none" href="#"><span class="ui-icon ui-icon-closethick"></span><span>' + o.uncheckAllText + '</span></a></li>';
  75. } else if(typeof o.header === "string") {
  76. return '<li>' + o.header + '</li>';
  77. } else {
  78. return '';
  79. }
  80. })
  81. .append('<li class="ui-multiselect-close"><a href="#" class="ui-multiselect-close"><span class="ui-icon ui-icon-circle-close"></span></a></li>')
  82. .appendTo(header),
  83. checkboxContainer = (this.checkboxContainer = $('<ul />'))
  84. .addClass('ui-multiselect-checkboxes ui-helper-reset')
  85. .appendTo(menu);
  86. // perform event bindings
  87. this._bindEvents();
  88. // build menu
  89. this.refresh(true);
  90. // some addl. logic for single selects
  91. if(!o.multiple) {
  92. menu.addClass('ui-multiselect-single');
  93. }
  94. // bump unique ID
  95. multiselectID++;
  96. },
  97. _init: function() {
  98. if(this.options.header === false) {
  99. this.header.hide();
  100. }
  101. if(!this.options.multiple) {
  102. this.headerLinkContainer.find('.ui-multiselect-all, .ui-multiselect-none').hide();
  103. }
  104. if(this.options.autoOpen) {
  105. this.open();
  106. }
  107. if(this.element.is(':disabled')) {
  108. this.disable();
  109. }
  110. },
  111. refresh: function(init) {
  112. var el = this.element;
  113. var o = this.options;
  114. var menu = this.menu;
  115. var checkboxContainer = this.checkboxContainer;
  116. var optgroups = [];
  117. var html = "";
  118. var id = el.attr('id') || multiselectID++; // unique ID for the label & option tags
  119. // build items
  120. el.find('option').each(function(i) {
  121. var $this = $(this);
  122. var parent = this.parentNode;
  123. var description = this.innerHTML;
  124. var title = this.title;
  125. var value = this.value;
  126. var inputID = 'ui-multiselect-' + (this.id || id + '-option-' + i);
  127. var isDisabled = this.disabled;
  128. var isSelected = this.selected;
  129. var labelClasses = [ 'ui-corner-all' ];
  130. var liClasses = (isDisabled ? 'ui-multiselect-disabled ' : ' ') + this.className;
  131. var optLabel;
  132. // is this an optgroup?
  133. if(parent.tagName === 'OPTGROUP') {
  134. optLabel = parent.getAttribute('label');
  135. // has this optgroup been added already?
  136. if($.inArray(optLabel, optgroups) === -1) {
  137. html += '<li class="ui-multiselect-optgroup-label ' + parent.className + '"><a href="#">' + optLabel + '</a></li>';
  138. optgroups.push(optLabel);
  139. }
  140. }
  141. if(isDisabled) {
  142. labelClasses.push('ui-state-disabled');
  143. }
  144. // browsers automatically select the first option
  145. // by default with single selects
  146. if(isSelected && !o.multiple) {
  147. labelClasses.push('ui-state-active');
  148. }
  149. html += '<li class="' + liClasses + '">';
  150. // create the label
  151. html += '<label for="' + inputID + '" title="' + title + '" class="' + labelClasses.join(' ') + '">';
  152. html += '<input id="' + inputID + '" name="multiselect_' + id + '" type="' + (o.multiple ? "checkbox" : "radio") + '" value="' + value + '" title="' + title + '"';
  153. // pre-selected?
  154. if(isSelected) {
  155. html += ' checked="checked"';
  156. html += ' aria-selected="true"';
  157. }
  158. // disabled?
  159. if(isDisabled) {
  160. html += ' disabled="disabled"';
  161. html += ' aria-disabled="true"';
  162. }
  163. // add the title and close everything off
  164. html += ' /><span>' + description + '</span></label></li>';
  165. });
  166. // insert into the DOM
  167. checkboxContainer.html(html);
  168. // cache some moar useful elements
  169. this.labels = menu.find('label');
  170. this.inputs = this.labels.children('input');
  171. // set widths
  172. this._setButtonWidth();
  173. this._setMenuWidth();
  174. // remember default value
  175. this.button[0].defaultValue = this.update();
  176. // broadcast refresh event; useful for widgets
  177. if(!init) {
  178. this._trigger('refresh');
  179. }
  180. },
  181. // updates the button text. call refresh() to rebuild
  182. update: function() {
  183. var o = this.options;
  184. var $inputs = this.inputs;
  185. var $checked = $inputs.filter(':checked');
  186. var numChecked = $checked.length;
  187. var value;
  188. if(numChecked === 0) {
  189. value = o.noneSelectedText;
  190. } else {
  191. if($.isFunction(o.selectedText)) {
  192. value = o.selectedText.call(this, numChecked, $inputs.length, $checked.get());
  193. } else if(/\d/.test(o.selectedList) && o.selectedList > 0 && numChecked <= o.selectedList) {
  194. value = $checked.map(function() { return $(this).next().html(); }).get().join(', ');
  195. } else {
  196. value = o.selectedText.replace('#', numChecked).replace('#', $inputs.length);
  197. }
  198. }
  199. this._setButtonValue(value);
  200. return value;
  201. },
  202. // this exists as a separate method so that the developer
  203. // can easily override it.
  204. _setButtonValue: function(value) {
  205. this.buttonlabel.text(value);
  206. },
  207. // binds events
  208. _bindEvents: function() {
  209. var self = this;
  210. var button = this.button;
  211. function clickHandler() {
  212. self[ self._isOpen ? 'close' : 'open' ]();
  213. return false;
  214. }
  215. // webkit doesn't like it when you click on the span :(
  216. button
  217. .find('span')
  218. .bind('click.multiselect', clickHandler);
  219. // button events
  220. button.bind({
  221. click: clickHandler,
  222. keypress: function(e) {
  223. switch(e.which) {
  224. case 27: // esc
  225. case 38: // up
  226. case 37: // left
  227. self.close();
  228. break;
  229. case 39: // right
  230. case 40: // down
  231. self.open();
  232. break;
  233. }
  234. },
  235. mouseenter: function() {
  236. if(!button.hasClass('ui-state-disabled')) {
  237. $(this).addClass('ui-state-hover');
  238. }
  239. },
  240. mouseleave: function() {
  241. $(this).removeClass('ui-state-hover');
  242. },
  243. focus: function() {
  244. if(!button.hasClass('ui-state-disabled')) {
  245. $(this).addClass('ui-state-focus');
  246. }
  247. },
  248. blur: function() {
  249. $(this).removeClass('ui-state-focus');
  250. }
  251. });
  252. // header links
  253. this.header.delegate('a', 'click.multiselect', function(e) {
  254. // close link
  255. if($(this).hasClass('ui-multiselect-close')) {
  256. self.close();
  257. // check all / uncheck all
  258. } else {
  259. self[$(this).hasClass('ui-multiselect-all') ? 'checkAll' : 'uncheckAll']();
  260. }
  261. e.preventDefault();
  262. });
  263. // optgroup label toggle support
  264. this.menu.delegate('li.ui-multiselect-optgroup-label a', 'click.multiselect', function(e) {
  265. e.preventDefault();
  266. var $this = $(this);
  267. var $inputs = $this.parent().nextUntil('li.ui-multiselect-optgroup-label').find('input:visible:not(:disabled)');
  268. var nodes = $inputs.get();
  269. var label = $this.parent().text();
  270. // trigger event and bail if the return is false
  271. if(self._trigger('beforeoptgrouptoggle', e, { inputs:nodes, label:label }) === false) {
  272. return;
  273. }
  274. // toggle inputs
  275. self._toggleChecked(
  276. $inputs.filter(':checked').length !== $inputs.length,
  277. $inputs
  278. );
  279. self._trigger('optgrouptoggle', e, {
  280. inputs: nodes,
  281. label: label,
  282. checked: nodes[0].checked
  283. });
  284. })
  285. .delegate('label', 'mouseenter.multiselect', function() {
  286. if(!$(this).hasClass('ui-state-disabled')) {
  287. self.labels.removeClass('ui-state-hover');
  288. $(this).addClass('ui-state-hover').find('input').focus();
  289. }
  290. })
  291. .delegate('label', 'keydown.multiselect', function(e) {
  292. e.preventDefault();
  293. switch(e.which) {
  294. case 9: // tab
  295. case 27: // esc
  296. self.close();
  297. break;
  298. case 38: // up
  299. case 40: // down
  300. case 37: // left
  301. case 39: // right
  302. self._traverse(e.which, this);
  303. break;
  304. case 13: // enter
  305. $(this).find('input')[0].click();
  306. break;
  307. }
  308. })
  309. .delegate('input[type="checkbox"], input[type="radio"]', 'click.multiselect', function(e) {
  310. var $this = $(this);
  311. var val = this.value;
  312. var checked = this.checked;
  313. var tags = self.element.find('option');
  314. // bail if this input is disabled or the event is cancelled
  315. if(this.disabled || self._trigger('click', e, { value: val, text: this.title, checked: checked }) === false) {
  316. e.preventDefault();
  317. return;
  318. }
  319. // make sure the input has focus. otherwise, the esc key
  320. // won't close the menu after clicking an item.
  321. $this.focus();
  322. // toggle aria state
  323. $this.attr('aria-selected', checked);
  324. // change state on the original option tags
  325. tags.each(function() {
  326. if(this.value === val) {
  327. this.selected = checked;
  328. } else if(!self.options.multiple) {
  329. this.selected = false;
  330. }
  331. });
  332. // some additional single select-specific logic
  333. if(!self.options.multiple) {
  334. self.labels.removeClass('ui-state-active');
  335. $this.closest('label').toggleClass('ui-state-active', checked);
  336. // close menu
  337. self.close();
  338. }
  339. // fire change on the select box
  340. self.element.trigger("change");
  341. // setTimeout is to fix multiselect issue #14 and #47. caused by jQuery issue #3827
  342. // http://bugs.jquery.com/ticket/3827
  343. setTimeout($.proxy(self.update, self), 10);
  344. });
  345. // close each widget when clicking on any other element/anywhere else on the page
  346. $doc.bind('mousedown.' + this._namespaceID, function(event) {
  347. var target = event.target;
  348. if(self._isOpen
  349. && target !== self.button[0]
  350. && target !== self.menu[0]
  351. && !$.contains(self.menu[0], target)
  352. && !$.contains(self.button[0], target)
  353. ) {
  354. self.close();
  355. }
  356. });
  357. // deal with form resets. the problem here is that buttons aren't
  358. // restored to their defaultValue prop on form reset, and the reset
  359. // handler fires before the form is actually reset. delaying it a bit
  360. // gives the form inputs time to clear.
  361. $(this.element[0].form).bind('reset.multiselect', function() {
  362. setTimeout($.proxy(self.refresh, self), 10);
  363. });
  364. },
  365. // set button width
  366. _setButtonWidth: function() {
  367. var width = this.element.outerWidth();
  368. var o = this.options;
  369. if(/\d/.test(o.minWidthButton) && width < o.minWidthButton) {
  370. width = o.minWidthButton;
  371. }
  372. // set widths
  373. this.button.outerWidth(width);
  374. },
  375. // set menu width
  376. _setMenuWidth: function() {
  377. var o = this.options;
  378. var m = this.menu;
  379. if(/\d/.test(o.minWidth) && this.button.outerWidth() < o.minWidth) {
  380. m.outerWidth(o.minWidth);
  381. }
  382. else
  383. {
  384. m.outerWidth(this.button.outerWidth());
  385. }
  386. },
  387. // move up or down within the menu
  388. _traverse: function(which, start) {
  389. var $start = $(start);
  390. var moveToLast = which === 38 || which === 37;
  391. // select the first li that isn't an optgroup label / disabled
  392. var $next = $start.parent()[moveToLast ? 'prevAll' : 'nextAll']('li:not(.ui-multiselect-disabled, .ui-multiselect-optgroup-label)').first();
  393. // if at the first/last element
  394. if(!$next.length) {
  395. var $container = this.menu.find('ul').last();
  396. // move to the first/last
  397. this.menu.find('label')[ moveToLast ? 'last' : 'first' ]().trigger('mouseover');
  398. // set scroll position
  399. $container.scrollTop(moveToLast ? $container.height() : 0);
  400. } else {
  401. $next.find('label').trigger('mouseover');
  402. }
  403. },
  404. // This is an internal function to toggle the checked property and
  405. // other related attributes of a checkbox.
  406. //
  407. // The context of this function should be a checkbox; do not proxy it.
  408. _toggleState: function(prop, flag) {
  409. return function() {
  410. if(!this.disabled) {
  411. this[ prop ] = flag;
  412. }
  413. if(flag) {
  414. this.setAttribute('aria-selected', true);
  415. } else {
  416. this.removeAttribute('aria-selected');
  417. }
  418. };
  419. },
  420. _toggleChecked: function(flag, group) {
  421. var $inputs = (group && group.length) ? group : this.inputs;
  422. var self = this;
  423. // toggle state on inputs
  424. $inputs.each(this._toggleState('checked', flag));
  425. // give the first input focus
  426. $inputs.eq(0).focus();
  427. // update button text
  428. this.update();
  429. // gather an array of the values that actually changed
  430. var values = $inputs.map(function() {
  431. return this.value;
  432. }).get();
  433. // toggle state on original option tags
  434. this.element
  435. .find('option')
  436. .each(function() {
  437. if(!this.disabled && $.inArray(this.value, values) > -1) {
  438. self._toggleState('selected', flag).call(this);
  439. }
  440. });
  441. // trigger the change event on the select
  442. if($inputs.length) {
  443. this.element.trigger("change");
  444. }
  445. },
  446. _toggleDisabled: function(flag) {
  447. this.button.attr({ 'disabled':flag, 'aria-disabled':flag })[ flag ? 'addClass' : 'removeClass' ]('ui-state-disabled');
  448. var inputs = this.menu.find('input');
  449. var key = "ech-multiselect-disabled";
  450. if(flag) {
  451. // remember which elements this widget disabled (not pre-disabled)
  452. // elements, so that they can be restored if the widget is re-enabled.
  453. inputs = inputs.filter(':enabled').data(key, true)
  454. } else {
  455. inputs = inputs.filter(function() {
  456. return $.data(this, key) === true;
  457. }).removeData(key);
  458. }
  459. inputs
  460. .attr({ 'disabled':flag, 'arial-disabled':flag })
  461. .parent()[ flag ? 'addClass' : 'removeClass' ]('ui-state-disabled');
  462. this.element.attr({
  463. 'disabled':flag,
  464. 'aria-disabled':flag
  465. });
  466. },
  467. // open the menu
  468. open: function(e) {
  469. var self = this;
  470. var button = this.button;
  471. var menu = this.menu;
  472. var speed = this.speed;
  473. var o = this.options;
  474. var args = [];
  475. // bail if the multiselectopen event returns false, this widget is disabled, or is already open
  476. if(this._trigger('beforeopen') === false || button.hasClass('ui-state-disabled') || this._isOpen) {
  477. return;
  478. }
  479. var $container = menu.find('ul').last();
  480. var effect = o.show;
  481. // figure out opening effects/speeds
  482. if($.isArray(o.show)) {
  483. effect = o.show[0];
  484. speed = o.show[1] || self.speed;
  485. }
  486. // if there's an effect, assume jQuery UI is in use
  487. // build the arguments to pass to show()
  488. if(effect) {
  489. args = [ effect, speed ];
  490. }
  491. // set the scroll of the checkbox container
  492. $container.scrollTop(0).height(o.height);
  493. // positon
  494. this.position();
  495. // show the menu, maybe with a speed/effect combo
  496. $.fn.show.apply(menu, args);
  497. // select the first not disabled option
  498. // triggering both mouseover and mouseover because 1.4.2+ has a bug where triggering mouseover
  499. // will actually trigger mouseenter. the mouseenter trigger is there for when it's eventually fixed
  500. this.labels.filter(':not(.ui-state-disabled)').eq(0).trigger('mouseover').trigger('mouseenter').find('input').trigger('focus');
  501. button.addClass('ui-state-active');
  502. this._isOpen = true;
  503. this._trigger('open');
  504. },
  505. // close the menu
  506. close: function() {
  507. if(this._trigger('beforeclose') === false) {
  508. return;
  509. }
  510. var o = this.options;
  511. var effect = o.hide;
  512. var speed = this.speed;
  513. var args = [];
  514. // figure out opening effects/speeds
  515. if($.isArray(o.hide)) {
  516. effect = o.hide[0];
  517. speed = o.hide[1] || this.speed;
  518. }
  519. if(effect) {
  520. args = [ effect, speed ];
  521. }
  522. $.fn.hide.apply(this.menu, args);
  523. this.button.removeClass('ui-state-active').trigger('blur').trigger('mouseleave');
  524. this._isOpen = false;
  525. this._trigger('close');
  526. },
  527. enable: function() {
  528. this._toggleDisabled(false);
  529. },
  530. disable: function() {
  531. this._toggleDisabled(true);
  532. },
  533. checkAll: function(e) {
  534. this._toggleChecked(true);
  535. this._trigger('checkAll');
  536. },
  537. uncheckAll: function() {
  538. this._toggleChecked(false);
  539. this._trigger('uncheckAll');
  540. },
  541. getChecked: function() {
  542. return this.menu.find('input').filter(':checked');
  543. },
  544. destroy: function() {
  545. // remove classes + data
  546. $.Widget.prototype.destroy.call(this);
  547. // unbind events
  548. $doc.unbind(this._namespaceID);
  549. this.button.remove();
  550. this.menu.remove();
  551. this.element.show();
  552. return this;
  553. },
  554. isOpen: function() {
  555. return this._isOpen;
  556. },
  557. widget: function() {
  558. return this.menu;
  559. },
  560. getButton: function() {
  561. return this.button;
  562. },
  563. position: function() {
  564. var o = this.options;
  565. // use the position utility if it exists and options are specifified
  566. if($.ui.position && !$.isEmptyObject(o.position)) {
  567. o.position.of = o.position.of || this.button;
  568. this.menu
  569. .show()
  570. .position(o.position)
  571. .hide();
  572. // otherwise fallback to custom positioning
  573. } else {
  574. var pos = this.button.offset();
  575. this.menu.css({
  576. top: pos.top + this.button.outerHeight(),
  577. left: pos.left
  578. });
  579. }
  580. },
  581. // react to option changes after initialization
  582. _setOption: function(key, value) {
  583. var menu = this.menu;
  584. switch(key) {
  585. case 'header':
  586. menu.find('div.ui-multiselect-header')[value ? 'show' : 'hide']();
  587. break;
  588. case 'checkAllText':
  589. menu.find('a.ui-multiselect-all span').eq(-1).text(value);
  590. break;
  591. case 'uncheckAllText':
  592. menu.find('a.ui-multiselect-none span').eq(-1).text(value);
  593. break;
  594. case 'height':
  595. menu.find('ul').last().height(parseInt(value, 10));
  596. break;
  597. case 'minWidth':
  598. this.options[key] = parseInt(value, 10);
  599. this._setButtonWidth();
  600. this._setMenuWidth();
  601. break;
  602. case 'selectedText':
  603. case 'selectedList':
  604. case 'noneSelectedText':
  605. this.options[key] = value; // these all needs to update immediately for the update() call
  606. this.update();
  607. break;
  608. case 'classes':
  609. menu.add(this.button).removeClass(this.options.classes).addClass(value);
  610. break;
  611. case 'multiple':
  612. menu.toggleClass('ui-multiselect-single', !value);
  613. this.options.multiple = value;
  614. this.element[0].multiple = value;
  615. this.refresh();
  616. break;
  617. case 'position':
  618. this.position();
  619. }
  620. $.Widget.prototype._setOption.apply(this, arguments);
  621. }
  622. });
  623. })(jQuery);