1
0

select2-toggleselect.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /* global Select2 */
  11. /**
  12. * Select2 extension for toggling values in a multi-select dropdown
  13. */
  14. (function(Select2) {
  15. var Select2FindHighlightableChoices = Select2.class.multi.prototype.findHighlightableChoices;
  16. Select2.class.multi.prototype.findHighlightableChoices = function () {
  17. if (this.opts.toggleSelect) {
  18. return this.results.find('.select2-result-selectable:not(.select2-disabled)');
  19. }
  20. return Select2FindHighlightableChoices.apply(this, arguments);
  21. };
  22. var Select2TriggerSelect = Select2.class.multi.prototype.triggerSelect;
  23. Select2.class.multi.prototype.triggerSelect = function (data) {
  24. if (this.opts.toggleSelect && this.val().indexOf(this.id(data)) !== -1) {
  25. var self = this;
  26. var val = this.id(data);
  27. var selectionEls = this.container.find('.select2-search-choice').filter(function() {
  28. return (self.id($(this).data('select2-data')) === val);
  29. });
  30. if (this.unselect(selectionEls)) {
  31. // also unselect in dropdown
  32. this.results.find('.select2-result.select2-selected').each(function () {
  33. var $this = $(this);
  34. if (self.id($this.data('select2-data')) === val) {
  35. $this.removeClass('select2-selected');
  36. }
  37. });
  38. this.clearSearch();
  39. }
  40. return false;
  41. } else {
  42. return Select2TriggerSelect.apply(this, arguments);
  43. }
  44. };
  45. })(Select2);