bootstrap-duallistbox.js 32 KB

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