jquery.bootstrap-duallistbox.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Bootstrap Duallistbox - v4.0.2
  3. * A responsive dual listbox widget optimized for Twitter Bootstrap. It works on all modern browsers and on touch devices.
  4. * http://www.virtuosoft.eu/code/bootstrap-duallistbox/
  5. *
  6. * Made by István Ujj-Mészáros
  7. * Under Apache License v2.0 License
  8. */
  9. (function(factory) {
  10. if (typeof define === 'function' && define.amd) {
  11. define(['jquery'], factory);
  12. } else if (typeof module === 'object' && module.exports) {
  13. module.exports = function(root, jQuery) {
  14. if (jQuery === undefined) {
  15. if (typeof window !== 'undefined') {
  16. jQuery = require('jquery');
  17. }
  18. else {
  19. jQuery = require('jquery')(root);
  20. }
  21. }
  22. factory(jQuery);
  23. return jQuery;
  24. };
  25. } else {
  26. factory(jQuery);
  27. }
  28. }(function($) {
  29. // Create the defaults once
  30. var pluginName = 'bootstrapDualListbox',
  31. defaults = {
  32. filterTextClear: 'show all',
  33. filterPlaceHolder: 'Filter',
  34. moveSelectedLabel: 'Move selected',
  35. moveAllLabel: 'Move all',
  36. removeSelectedLabel: 'Remove selected',
  37. removeAllLabel: 'Remove all',
  38. moveOnSelect: true, // true/false (forced true on androids, see the comment later)
  39. moveOnDoubleClick: true, // true/false (forced false on androids, cause moveOnSelect is forced to true)
  40. preserveSelectionOnMove: false, // 'all' / 'moved' / false
  41. selectedListLabel: false, // 'string', false
  42. nonSelectedListLabel: false, // 'string', false
  43. helperSelectNamePostfix: '_helper', // 'string_of_postfix' / false
  44. selectorMinimalHeight: 100,
  45. showFilterInputs: true, // whether to show filter inputs
  46. nonSelectedFilter: '', // string, filter the non selected options
  47. selectedFilter: '', // string, filter the selected options
  48. infoText: 'Showing all {0}', // text when all options are visible / false for no info text
  49. infoTextFiltered: '<span class="badge badge-warning">Filtered</span> {0} from {1}', // when not all of the options are visible due to the filter
  50. infoTextEmpty: 'Empty list', // when there are no options present in the list
  51. filterOnValues: false, // filter by selector's values, boolean
  52. sortByInputOrder: false,
  53. eventMoveOverride: false, // boolean, allows user to unbind default event behaviour and run their own instead
  54. eventMoveAllOverride: false, // boolean, allows user to unbind default event behaviour and run their own instead
  55. eventRemoveOverride: false, // boolean, allows user to unbind default event behaviour and run their own instead
  56. eventRemoveAllOverride: false, // boolean, allows user to unbind default event behaviour and run their own instead
  57. btnClass: 'btn-outline-secondary', // sets the button style class for all the buttons
  58. btnMoveText: '&gt;', // string, sets the text for the "Move" button
  59. btnRemoveText: '&lt;', // string, sets the text for the "Remove" button
  60. btnMoveAllText: '&gt;&gt;', // string, sets the text for the "Move All" button
  61. btnRemoveAllText: '&lt;&lt;' // string, sets the text for the "Remove All" button
  62. },
  63. // Selections are invisible on android if the containing select is styled with CSS
  64. // http://code.google.com/p/android/issues/detail?id=16922
  65. isBuggyAndroid = /android/i.test(navigator.userAgent.toLowerCase());
  66. // The actual plugin constructor
  67. function BootstrapDualListbox(element, options) {
  68. this.element = $(element);
  69. // jQuery has an extend method which merges the contents of two or
  70. // more objects, storing the result in the first object. The first object
  71. // is generally empty as we don't want to alter the default options for
  72. // future instances of the plugin
  73. this.settings = $.extend({}, defaults, options);
  74. this._defaults = defaults;
  75. this._name = pluginName;
  76. this.init();
  77. }
  78. function triggerChangeEvent(dualListbox) {
  79. dualListbox.element.trigger('change');
  80. }
  81. function updateSelectionStates(dualListbox) {
  82. dualListbox.element.find('option').each(function(index, item) {
  83. var $item = $(item);
  84. if (typeof($item.data('original-index')) === 'undefined') {
  85. $item.data('original-index', dualListbox.elementCount++);
  86. }
  87. if (typeof($item.data('_selected')) === 'undefined') {
  88. $item.data('_selected', false);
  89. }
  90. });
  91. }
  92. function changeSelectionState(dualListbox, original_index, selected) {
  93. dualListbox.element.find('option').each(function(index, item) {
  94. var $item = $(item);
  95. if ($item.data('original-index') === original_index) {
  96. $item.prop('selected', selected);
  97. if(selected){
  98. $item.attr('data-sortindex', dualListbox.sortIndex);
  99. dualListbox.sortIndex++;
  100. } else {
  101. $item.removeAttr('data-sortindex');
  102. }
  103. }
  104. });
  105. }
  106. function formatString(s, args) {
  107. console.log(s, args);
  108. return s.replace(/{(\d+)}/g, function(match, number) {
  109. return typeof args[number] !== 'undefined' ? args[number] : match;
  110. });
  111. }
  112. function refreshInfo(dualListbox) {
  113. if (!dualListbox.settings.infoText) {
  114. return;
  115. }
  116. var visible1 = dualListbox.elements.select1.find('option').length,
  117. visible2 = dualListbox.elements.select2.find('option').length,
  118. all1 = dualListbox.element.find('option').length - dualListbox.selectedElements,
  119. all2 = dualListbox.selectedElements,
  120. content = '';
  121. if (all1 === 0) {
  122. content = dualListbox.settings.infoTextEmpty;
  123. } else if (visible1 === all1) {
  124. content = formatString(dualListbox.settings.infoText, [visible1, all1]);
  125. } else {
  126. content = formatString(dualListbox.settings.infoTextFiltered, [visible1, all1]);
  127. }
  128. dualListbox.elements.info1.html(content);
  129. dualListbox.elements.box1.toggleClass('filtered', !(visible1 === all1 || all1 === 0));
  130. if (all2 === 0) {
  131. content = dualListbox.settings.infoTextEmpty;
  132. } else if (visible2 === all2) {
  133. content = formatString(dualListbox.settings.infoText, [visible2, all2]);
  134. } else {
  135. content = formatString(dualListbox.settings.infoTextFiltered, [visible2, all2]);
  136. }
  137. dualListbox.elements.info2.html(content);
  138. dualListbox.elements.box2.toggleClass('filtered', !(visible2 === all2 || all2 === 0));
  139. }
  140. function refreshSelects(dualListbox) {
  141. dualListbox.selectedElements = 0;
  142. dualListbox.elements.select1.empty();
  143. dualListbox.elements.select2.empty();
  144. dualListbox.element.find('option').each(function(index, item) {
  145. var $item = $(item);
  146. if ($item.prop('selected')) {
  147. dualListbox.selectedElements++;
  148. dualListbox.elements.select2.append($item.clone(true).prop('selected', $item.data('_selected')));
  149. } else {
  150. dualListbox.elements.select1.append($item.clone(true).prop('selected', $item.data('_selected')));
  151. }
  152. });
  153. if (dualListbox.settings.showFilterInputs) {
  154. filter(dualListbox, 1);
  155. filter(dualListbox, 2);
  156. }
  157. refreshInfo(dualListbox);
  158. }
  159. function filter(dualListbox, selectIndex) {
  160. if (!dualListbox.settings.showFilterInputs) {
  161. return;
  162. }
  163. saveSelections(dualListbox, selectIndex);
  164. dualListbox.elements['select'+selectIndex].empty().scrollTop(0);
  165. var regex,
  166. allOptions = dualListbox.element.find('option'),
  167. options = dualListbox.element;
  168. if (selectIndex === 1) {
  169. options = allOptions.not(':selected');
  170. } else {
  171. options = options.find('option:selected');
  172. }
  173. try {
  174. regex = new RegExp($.trim(dualListbox.elements['filterInput'+selectIndex].val()), 'gi');
  175. }
  176. catch(e) {
  177. // a regex to match nothing
  178. regex = new RegExp('/a^/', 'gi');
  179. }
  180. options.each(function(index, item) {
  181. var $item = $(item),
  182. isFiltered = true;
  183. if (item.text.match(regex) || (dualListbox.settings.filterOnValues && $item.attr('value').match(regex) ) ) {
  184. isFiltered = false;
  185. dualListbox.elements['select'+selectIndex].append($item.clone(true).prop('selected', $item.data('_selected')));
  186. }
  187. allOptions.eq($item.data('original-index')).data('filtered'+selectIndex, isFiltered);
  188. });
  189. refreshInfo(dualListbox);
  190. }
  191. function saveSelections(dualListbox, selectIndex) {
  192. var options = dualListbox.element.find('option');
  193. dualListbox.elements['select'+selectIndex].find('option').each(function(index, item) {
  194. var $item = $(item);
  195. options.eq($item.data('original-index')).data('_selected', $item.prop('selected'));
  196. });
  197. }
  198. function sortOptionsByInputOrder(select){
  199. var selectopt = select.children('option');
  200. selectopt.sort(function(a,b){
  201. var an = parseInt(a.getAttribute('data-sortindex')),
  202. bn = parseInt(b.getAttribute('data-sortindex'));
  203. if(an > bn) {
  204. return 1;
  205. }
  206. if(an < bn) {
  207. return -1;
  208. }
  209. return 0;
  210. });
  211. selectopt.detach().appendTo(select);
  212. }
  213. function sortOptions(select, dualListbox) {
  214. select.find('option').sort(function(a, b) {
  215. return ($(a).data('original-index') > $(b).data('original-index')) ? 1 : -1;
  216. }).appendTo(select);
  217. // workaround for chromium bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1072475
  218. refreshSelects(dualListbox);
  219. }
  220. function clearSelections(dualListbox) {
  221. dualListbox.elements.select1.find('option').each(function() {
  222. dualListbox.element.find('option').data('_selected', false);
  223. });
  224. }
  225. function move(dualListbox) {
  226. if (dualListbox.settings.preserveSelectionOnMove === 'all' && !dualListbox.settings.moveOnSelect) {
  227. saveSelections(dualListbox, 1);
  228. saveSelections(dualListbox, 2);
  229. } else if (dualListbox.settings.preserveSelectionOnMove === 'moved' && !dualListbox.settings.moveOnSelect) {
  230. saveSelections(dualListbox, 1);
  231. }
  232. dualListbox.elements.select1.find('option:selected').each(function(index, item) {
  233. var $item = $(item);
  234. if (!$item.data('filtered1')) {
  235. changeSelectionState(dualListbox, $item.data('original-index'), true);
  236. }
  237. });
  238. refreshSelects(dualListbox);
  239. triggerChangeEvent(dualListbox);
  240. if(dualListbox.settings.sortByInputOrder){
  241. sortOptionsByInputOrder(dualListbox.elements.select2);
  242. } else {
  243. sortOptions(dualListbox.elements.select2, dualListbox);
  244. }
  245. }
  246. function remove(dualListbox) {
  247. if (dualListbox.settings.preserveSelectionOnMove === 'all' && !dualListbox.settings.moveOnSelect) {
  248. saveSelections(dualListbox, 1);
  249. saveSelections(dualListbox, 2);
  250. } else if (dualListbox.settings.preserveSelectionOnMove === 'moved' && !dualListbox.settings.moveOnSelect) {
  251. saveSelections(dualListbox, 2);
  252. }
  253. dualListbox.elements.select2.find('option:selected').each(function(index, item) {
  254. var $item = $(item);
  255. if (!$item.data('filtered2')) {
  256. changeSelectionState(dualListbox, $item.data('original-index'), false);
  257. }
  258. });
  259. refreshSelects(dualListbox);
  260. triggerChangeEvent(dualListbox);
  261. sortOptions(dualListbox.elements.select1, dualListbox);
  262. if(dualListbox.settings.sortByInputOrder){
  263. sortOptionsByInputOrder(dualListbox.elements.select2);
  264. }
  265. }
  266. function moveAll(dualListbox) {
  267. if (dualListbox.settings.preserveSelectionOnMove === 'all' && !dualListbox.settings.moveOnSelect) {
  268. saveSelections(dualListbox, 1);
  269. saveSelections(dualListbox, 2);
  270. } else if (dualListbox.settings.preserveSelectionOnMove === 'moved' && !dualListbox.settings.moveOnSelect) {
  271. saveSelections(dualListbox, 1);
  272. }
  273. dualListbox.element.find('option').each(function(index, item) {
  274. var $item = $(item);
  275. if (!$item.data('filtered1')) {
  276. $item.prop('selected', true);
  277. $item.attr('data-sortindex', dualListbox.sortIndex);
  278. dualListbox.sortIndex++;
  279. }
  280. });
  281. refreshSelects(dualListbox);
  282. triggerChangeEvent(dualListbox);
  283. }
  284. function removeAll(dualListbox) {
  285. if (dualListbox.settings.preserveSelectionOnMove === 'all' && !dualListbox.settings.moveOnSelect) {
  286. saveSelections(dualListbox, 1);
  287. saveSelections(dualListbox, 2);
  288. } else if (dualListbox.settings.preserveSelectionOnMove === 'moved' && !dualListbox.settings.moveOnSelect) {
  289. saveSelections(dualListbox, 2);
  290. }
  291. dualListbox.element.find('option').each(function(index, item) {
  292. var $item = $(item);
  293. if (!$item.data('filtered2')) {
  294. $item.prop('selected', false);
  295. $item.removeAttr('data-sortindex');
  296. }
  297. });
  298. refreshSelects(dualListbox);
  299. triggerChangeEvent(dualListbox);
  300. }
  301. function bindEvents(dualListbox) {
  302. dualListbox.elements.form.submit(function(e) {
  303. if (dualListbox.elements.filterInput1.is(':focus')) {
  304. e.preventDefault();
  305. dualListbox.elements.filterInput1.focusout();
  306. } else if (dualListbox.elements.filterInput2.is(':focus')) {
  307. e.preventDefault();
  308. dualListbox.elements.filterInput2.focusout();
  309. }
  310. });
  311. dualListbox.element.on('bootstrapDualListbox.refresh', function(e, mustClearSelections){
  312. dualListbox.refresh(mustClearSelections);
  313. });
  314. dualListbox.elements.filterClear1.on('click', function() {
  315. dualListbox.setNonSelectedFilter('', true);
  316. });
  317. dualListbox.elements.filterClear2.on('click', function() {
  318. dualListbox.setSelectedFilter('', true);
  319. });
  320. if (dualListbox.settings.eventMoveOverride === false) {
  321. dualListbox.elements.moveButton.on('click', function() {
  322. move(dualListbox);
  323. });
  324. }
  325. if (dualListbox.settings.eventMoveAllOverride === false) {
  326. dualListbox.elements.moveAllButton.on('click', function() {
  327. moveAll(dualListbox);
  328. });
  329. }
  330. if (dualListbox.settings.eventRemoveOverride === false) {
  331. dualListbox.elements.removeButton.on('click', function() {
  332. remove(dualListbox);
  333. });
  334. }
  335. if (dualListbox.settings.eventRemoveAllOverride === false) {
  336. dualListbox.elements.removeAllButton.on('click', function() {
  337. removeAll(dualListbox);
  338. });
  339. }
  340. dualListbox.elements.filterInput1.on('change keyup', function() {
  341. filter(dualListbox, 1);
  342. });
  343. dualListbox.elements.filterInput2.on('change keyup', function() {
  344. filter(dualListbox, 2);
  345. });
  346. }
  347. BootstrapDualListbox.prototype = {
  348. init: function () {
  349. // Add the custom HTML template
  350. this.container = $('' +
  351. '<div class="bootstrap-duallistbox-container row">' +
  352. ' <div class="box1 col-md-6">' +
  353. ' <label></label>' +
  354. ' <span class="info-container">' +
  355. ' <span class="info"></span>' +
  356. ' <button type="button" class="btn btn-sm clear1" style="float:right!important;"></button>' +
  357. ' </span>' +
  358. ' <input class="form-control filter" type="text">' +
  359. ' <div class="btn-group buttons">' +
  360. ' <button type="button" class="btn moveall"></button>' +
  361. ' <button type="button" class="btn move"></button>' +
  362. ' </div>' +
  363. ' <select multiple="multiple"></select>' +
  364. ' </div>' +
  365. ' <div class="box2 col-md-6">' +
  366. ' <label></label>' +
  367. ' <span class="info-container">' +
  368. ' <span class="info"></span>' +
  369. ' <button type="button" class="btn btn-sm clear2" style="float:right!important;"></button>' +
  370. ' </span>' +
  371. ' <input class="form-control filter" type="text">' +
  372. ' <div class="btn-group buttons">' +
  373. ' <button type="button" class="btn remove"></button>' +
  374. ' <button type="button" class="btn removeall"></button>' +
  375. ' </div>' +
  376. ' <select multiple="multiple"></select>' +
  377. ' </div>' +
  378. '</div>')
  379. .insertBefore(this.element);
  380. // Cache the inner elements
  381. this.elements = {
  382. originalSelect: this.element,
  383. box1: $('.box1', this.container),
  384. box2: $('.box2', this.container),
  385. filterInput1: $('.box1 .filter', this.container),
  386. filterInput2: $('.box2 .filter', this.container),
  387. filterClear1: $('.box1 .clear1', this.container),
  388. filterClear2: $('.box2 .clear2', this.container),
  389. label1: $('.box1 > label', this.container),
  390. label2: $('.box2 > label', this.container),
  391. info1: $('.box1 .info', this.container),
  392. info2: $('.box2 .info', this.container),
  393. select1: $('.box1 select', this.container),
  394. select2: $('.box2 select', this.container),
  395. moveButton: $('.box1 .move', this.container),
  396. removeButton: $('.box2 .remove', this.container),
  397. moveAllButton: $('.box1 .moveall', this.container),
  398. removeAllButton: $('.box2 .removeall', this.container),
  399. form: $($('.box1 .filter', this.container)[0].form)
  400. };
  401. // Set select IDs
  402. this.originalSelectName = this.element.attr('name') || '';
  403. var select1Id = 'bootstrap-duallistbox-nonselected-list_' + this.originalSelectName,
  404. select2Id = 'bootstrap-duallistbox-selected-list_' + this.originalSelectName;
  405. this.elements.select1.attr('id', select1Id);
  406. this.elements.select2.attr('id', select2Id);
  407. this.elements.label1.attr('for', select1Id);
  408. this.elements.label2.attr('for', select2Id);
  409. // Apply all settings
  410. this.selectedElements = 0;
  411. this.sortIndex = 0;
  412. this.elementCount = 0;
  413. this.setFilterTextClear(this.settings.filterTextClear);
  414. this.setFilterPlaceHolder(this.settings.filterPlaceHolder);
  415. this.setMoveSelectedLabel(this.settings.moveSelectedLabel);
  416. this.setMoveAllLabel(this.settings.moveAllLabel);
  417. this.setRemoveSelectedLabel(this.settings.removeSelectedLabel);
  418. this.setRemoveAllLabel(this.settings.removeAllLabel);
  419. this.setMoveOnSelect(this.settings.moveOnSelect);
  420. this.setMoveOnDoubleClick(this.settings.moveOnDoubleClick);
  421. this.setPreserveSelectionOnMove(this.settings.preserveSelectionOnMove);
  422. this.setSelectedListLabel(this.settings.selectedListLabel);
  423. this.setNonSelectedListLabel(this.settings.nonSelectedListLabel);
  424. this.setHelperSelectNamePostfix(this.settings.helperSelectNamePostfix);
  425. this.setSelectOrMinimalHeight(this.settings.selectorMinimalHeight);
  426. updateSelectionStates(this);
  427. this.setShowFilterInputs(this.settings.showFilterInputs);
  428. this.setNonSelectedFilter(this.settings.nonSelectedFilter);
  429. this.setSelectedFilter(this.settings.selectedFilter);
  430. this.setInfoText(this.settings.infoText);
  431. this.setInfoTextFiltered(this.settings.infoTextFiltered);
  432. this.setInfoTextEmpty(this.settings.infoTextEmpty);
  433. this.setFilterOnValues(this.settings.filterOnValues);
  434. this.setSortByInputOrder(this.settings.sortByInputOrder);
  435. this.setEventMoveOverride(this.settings.eventMoveOverride);
  436. this.setEventMoveAllOverride(this.settings.eventMoveAllOverride);
  437. this.setEventRemoveOverride(this.settings.eventRemoveOverride);
  438. this.setEventRemoveAllOverride(this.settings.eventRemoveAllOverride);
  439. this.setBtnClass(this.settings.btnClass);
  440. this.setBtnMoveText(this.settings.btnMoveText);
  441. this.setBtnRemoveText(this.settings.btnRemoveText);
  442. this.setBtnMoveAllText(this.settings.btnMoveAllText);
  443. this.setBtnRemoveAllText(this.settings.btnRemoveAllText);
  444. // Hide the original select
  445. this.element.hide();
  446. bindEvents(this);
  447. refreshSelects(this);
  448. return this.element;
  449. },
  450. setFilterTextClear: function(value, refresh) {
  451. this.settings.filterTextClear = value;
  452. this.elements.filterClear1.html(value);
  453. this.elements.filterClear2.html(value);
  454. if (refresh) {
  455. refreshSelects(this);
  456. }
  457. return this.element;
  458. },
  459. setFilterPlaceHolder: function(value, refresh) {
  460. this.settings.filterPlaceHolder = value;
  461. this.elements.filterInput1.attr('placeholder', value);
  462. this.elements.filterInput2.attr('placeholder', value);
  463. if (refresh) {
  464. refreshSelects(this);
  465. }
  466. return this.element;
  467. },
  468. setMoveSelectedLabel: function(value, refresh) {
  469. this.settings.moveSelectedLabel = value;
  470. this.elements.moveButton.attr('title', value);
  471. if (refresh) {
  472. refreshSelects(this);
  473. }
  474. return this.element;
  475. },
  476. setMoveAllLabel: function(value, refresh) {
  477. this.settings.moveAllLabel = value;
  478. this.elements.moveAllButton.attr('title', value);
  479. if (refresh) {
  480. refreshSelects(this);
  481. }
  482. return this.element;
  483. },
  484. setRemoveSelectedLabel: function(value, refresh) {
  485. this.settings.removeSelectedLabel = value;
  486. this.elements.removeButton.attr('title', value);
  487. if (refresh) {
  488. refreshSelects(this);
  489. }
  490. return this.element;
  491. },
  492. setRemoveAllLabel: function(value, refresh) {
  493. this.settings.removeAllLabel = value;
  494. this.elements.removeAllButton.attr('title', value);
  495. if (refresh) {
  496. refreshSelects(this);
  497. }
  498. return this.element;
  499. },
  500. setMoveOnSelect: function(value, refresh) {
  501. if (isBuggyAndroid) {
  502. value = true;
  503. }
  504. this.settings.moveOnSelect = value;
  505. if (this.settings.moveOnSelect) {
  506. this.container.addClass('moveonselect');
  507. var self = this;
  508. this.elements.select1.on('change', function() {
  509. move(self);
  510. });
  511. this.elements.select2.on('change', function() {
  512. remove(self);
  513. });
  514. this.elements.moveButton.detach();
  515. this.elements.removeButton.detach();
  516. } else {
  517. this.container.removeClass('moveonselect');
  518. this.elements.select1.off('change');
  519. this.elements.select2.off('change');
  520. this.elements.moveButton.insertAfter(this.elements.moveAllButton);
  521. this.elements.removeButton.insertBefore(this.elements.removeAllButton);
  522. }
  523. if (refresh) {
  524. refreshSelects(this);
  525. }
  526. return this.element;
  527. },
  528. setMoveOnDoubleClick: function(value, refresh) {
  529. if (isBuggyAndroid) {
  530. value = false;
  531. }
  532. this.settings.moveOnDoubleClick = value;
  533. if (this.settings.moveOnDoubleClick) {
  534. this.container.addClass('moveondoubleclick');
  535. var self = this;
  536. this.elements.select1.on('dblclick', function() {
  537. move(self);
  538. });
  539. this.elements.select2.on('dblclick', function() {
  540. remove(self);
  541. });
  542. } else {
  543. this.container.removeClass('moveondoubleclick');
  544. this.elements.select1.off('dblclick');
  545. this.elements.select2.off('dblclick');
  546. }
  547. if (refresh) {
  548. refreshSelects(this);
  549. }
  550. return this.element;
  551. },
  552. setPreserveSelectionOnMove: function(value, refresh) {
  553. // We are forcing to move on select and disabling preserveSelectionOnMove on Android
  554. if (isBuggyAndroid) {
  555. value = false;
  556. }
  557. this.settings.preserveSelectionOnMove = value;
  558. if (refresh) {
  559. refreshSelects(this);
  560. }
  561. return this.element;
  562. },
  563. setSelectedListLabel: function(value, refresh) {
  564. this.settings.selectedListLabel = value;
  565. if (value) {
  566. this.elements.label2.show().html(value);
  567. } else {
  568. this.elements.label2.hide().html(value);
  569. }
  570. if (refresh) {
  571. refreshSelects(this);
  572. }
  573. return this.element;
  574. },
  575. setNonSelectedListLabel: function(value, refresh) {
  576. this.settings.nonSelectedListLabel = value;
  577. if (value) {
  578. this.elements.label1.show().html(value);
  579. } else {
  580. this.elements.label1.hide().html(value);
  581. }
  582. if (refresh) {
  583. refreshSelects(this);
  584. }
  585. return this.element;
  586. },
  587. setHelperSelectNamePostfix: function(value, refresh) {
  588. this.settings.helperSelectNamePostfix = value;
  589. if (value) {
  590. this.elements.select1.attr('name', this.originalSelectName + value + '1');
  591. this.elements.select2.attr('name', this.originalSelectName + value + '2');
  592. } else {
  593. this.elements.select1.removeAttr('name');
  594. this.elements.select2.removeAttr('name');
  595. }
  596. if (refresh) {
  597. refreshSelects(this);
  598. }
  599. return this.element;
  600. },
  601. setSelectOrMinimalHeight: function(value, refresh) {
  602. this.settings.selectorMinimalHeight = value;
  603. var height = this.element.height();
  604. if (this.element.height() < value) {
  605. height = value;
  606. }
  607. this.elements.select1.height(height);
  608. this.elements.select2.height(height);
  609. if (refresh) {
  610. refreshSelects(this);
  611. }
  612. return this.element;
  613. },
  614. setShowFilterInputs: function(value, refresh) {
  615. if (!value) {
  616. this.setNonSelectedFilter('');
  617. this.setSelectedFilter('');
  618. refreshSelects(this);
  619. this.elements.filterInput1.hide();
  620. this.elements.filterInput2.hide();
  621. } else {
  622. this.elements.filterInput1.show();
  623. this.elements.filterInput2.show();
  624. }
  625. this.settings.showFilterInputs = value;
  626. if (refresh) {
  627. refreshSelects(this);
  628. }
  629. return this.element;
  630. },
  631. setNonSelectedFilter: function(value, refresh) {
  632. if (this.settings.showFilterInputs) {
  633. this.settings.nonSelectedFilter = value;
  634. this.elements.filterInput1.val(value);
  635. if (refresh) {
  636. refreshSelects(this);
  637. }
  638. return this.element;
  639. }
  640. },
  641. setSelectedFilter: function(value, refresh) {
  642. if (this.settings.showFilterInputs) {
  643. this.settings.selectedFilter = value;
  644. this.elements.filterInput2.val(value);
  645. if (refresh) {
  646. refreshSelects(this);
  647. }
  648. return this.element;
  649. }
  650. },
  651. setInfoText: function(value, refresh) {
  652. this.settings.infoText = value;
  653. if (value) {
  654. this.elements.info1.show();
  655. this.elements.info2.show();
  656. } else {
  657. this.elements.info1.hide();
  658. this.elements.info2.hide();
  659. }
  660. if (refresh) {
  661. refreshSelects(this);
  662. }
  663. return this.element;
  664. },
  665. setInfoTextFiltered: function(value, refresh) {
  666. this.settings.infoTextFiltered = value;
  667. if (refresh) {
  668. refreshSelects(this);
  669. }
  670. return this.element;
  671. },
  672. setInfoTextEmpty: function(value, refresh) {
  673. this.settings.infoTextEmpty = value;
  674. if (refresh) {
  675. refreshSelects(this);
  676. }
  677. return this.element;
  678. },
  679. setFilterOnValues: function(value, refresh) {
  680. this.settings.filterOnValues = value;
  681. if (refresh) {
  682. refreshSelects(this);
  683. }
  684. return this.element;
  685. },
  686. setSortByInputOrder: function(value, refresh){
  687. this.settings.sortByInputOrder = value;
  688. if (refresh) {
  689. refreshSelects(this);
  690. }
  691. return this.element;
  692. },
  693. setEventMoveOverride: function(value, refresh) {
  694. this.settings.eventMoveOverride = value;
  695. if (refresh) {
  696. refreshSelects(this);
  697. }
  698. return this.element;
  699. },
  700. setEventMoveAllOverride: function(value, refresh) {
  701. this.settings.eventMoveAllOverride = value;
  702. if (refresh) {
  703. refreshSelects(this);
  704. }
  705. return this.element;
  706. },
  707. setEventRemoveOverride: function(value, refresh) {
  708. this.settings.eventRemoveOverride = value;
  709. if (refresh) {
  710. refreshSelects(this);
  711. }
  712. return this.element;
  713. },
  714. setEventRemoveAllOverride: function(value, refresh) {
  715. this.settings.eventRemoveAllOverride = value;
  716. if (refresh) {
  717. refreshSelects(this);
  718. }
  719. return this.element;
  720. },
  721. setBtnClass: function(value, refresh) {
  722. this.settings.btnClass = value;
  723. this.elements.moveButton.attr('class', 'btn move').addClass(value);
  724. this.elements.removeButton.attr('class', 'btn remove').addClass(value);
  725. this.elements.moveAllButton.attr('class', 'btn moveall').addClass(value);
  726. this.elements.removeAllButton.attr('class', 'btn removeall').addClass(value);
  727. if (refresh) {
  728. refreshSelects(this);
  729. }
  730. return this.element;
  731. },
  732. setBtnMoveText: function(value, refresh) {
  733. this.settings.btnMoveText = value;
  734. this.elements.moveButton.html(value);
  735. if (refresh) {
  736. refreshSelects(this);
  737. }
  738. return this.element;
  739. },
  740. setBtnRemoveText: function(value, refresh) {
  741. this.settings.btnMoveText = value;
  742. this.elements.removeButton.html(value);
  743. if (refresh) {
  744. refreshSelects(this);
  745. }
  746. return this.element;
  747. },
  748. setBtnMoveAllText: function(value, refresh) {
  749. this.settings.btnMoveText = value;
  750. this.elements.moveAllButton.html(value);
  751. if (refresh) {
  752. refreshSelects(this);
  753. }
  754. return this.element;
  755. },
  756. setBtnRemoveAllText: function(value, refresh) {
  757. this.settings.btnMoveText = value;
  758. this.elements.removeAllButton.html(value);
  759. if (refresh) {
  760. refreshSelects(this);
  761. }
  762. return this.element;
  763. },
  764. getContainer: function() {
  765. return this.container;
  766. },
  767. refresh: function(mustClearSelections) {
  768. updateSelectionStates(this);
  769. if (!mustClearSelections) {
  770. saveSelections(this, 1);
  771. saveSelections(this, 2);
  772. } else {
  773. clearSelections(this);
  774. }
  775. refreshSelects(this);
  776. },
  777. destroy: function() {
  778. this.container.remove();
  779. this.element.show();
  780. $.data(this, 'plugin_' + pluginName, null);
  781. return this.element;
  782. }
  783. };
  784. // A really lightweight plugin wrapper around the constructor,
  785. // preventing against multiple instantiations
  786. $.fn[ pluginName ] = function (options) {
  787. var args = arguments;
  788. // Is the first parameter an object (options), or was omitted, instantiate a new instance of the plugin.
  789. if (options === undefined || typeof options === 'object') {
  790. return this.each(function () {
  791. // If this is not a select
  792. if (!$(this).is('select')) {
  793. $(this).find('select').each(function(index, item) {
  794. // For each nested select, instantiate the Dual List Box
  795. $(item).bootstrapDualListbox(options);
  796. });
  797. } else if (!$.data(this, 'plugin_' + pluginName)) {
  798. // Only allow the plugin to be instantiated once so we check that the element has no plugin instantiation yet
  799. // if it has no instance, create a new one, pass options to our plugin constructor,
  800. // and store the plugin instance in the elements jQuery data object.
  801. $.data(this, 'plugin_' + pluginName, new BootstrapDualListbox(this, options));
  802. }
  803. });
  804. // If the first parameter is a string and it doesn't start with an underscore or "contains" the `init`-function,
  805. // treat this as a call to a public method.
  806. } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
  807. // Cache the method call to make it possible to return a value
  808. var returns;
  809. this.each(function () {
  810. var instance = $.data(this, 'plugin_' + pluginName);
  811. // Tests that there's already a plugin-instance and checks that the requested public method exists
  812. if (instance instanceof BootstrapDualListbox && typeof instance[options] === 'function') {
  813. // Call the method of our plugin instance, and pass it the supplied arguments.
  814. returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
  815. }
  816. });
  817. // If the earlier cached method gives a value back return the value,
  818. // otherwise return this to preserve chainability.
  819. return returns !== undefined ? returns : this;
  820. }
  821. };
  822. }));