jquery.bootstrap-duallistbox.js 26 KB

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