1
0

select2-toggleselect.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * SPDX-FileCopyrightText: 2016 ownCloud Inc.
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. /* global Select2 */
  6. /**
  7. * Select2 extension for toggling values in a multi-select dropdown
  8. */
  9. (function(Select2) {
  10. var Select2FindHighlightableChoices = Select2.class.multi.prototype.findHighlightableChoices;
  11. Select2.class.multi.prototype.findHighlightableChoices = function () {
  12. if (this.opts.toggleSelect) {
  13. return this.results.find('.select2-result-selectable:not(.select2-disabled)');
  14. }
  15. return Select2FindHighlightableChoices.apply(this, arguments);
  16. };
  17. var Select2TriggerSelect = Select2.class.multi.prototype.triggerSelect;
  18. Select2.class.multi.prototype.triggerSelect = function (data) {
  19. if (this.opts.toggleSelect && this.val().indexOf(this.id(data)) !== -1) {
  20. var self = this;
  21. var val = this.id(data);
  22. var selectionEls = this.container.find('.select2-search-choice').filter(function() {
  23. return (self.id($(this).data('select2-data')) === val);
  24. });
  25. if (this.unselect(selectionEls)) {
  26. // also unselect in dropdown
  27. this.results.find('.select2-result.select2-selected').each(function () {
  28. var $this = $(this);
  29. if (self.id($this.data('select2-data')) === val) {
  30. $this.removeClass('select2-selected');
  31. }
  32. });
  33. this.clearSearch();
  34. }
  35. return false;
  36. } else {
  37. return Select2TriggerSelect.apply(this, arguments);
  38. }
  39. };
  40. })(Select2);