select2-toggleselect.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if (!Select2) return;
  11. var Select2FindHighlightableChoices = Select2.class.multi.prototype.findHighlightableChoices;
  12. Select2.class.multi.prototype.findHighlightableChoices = function () {
  13. if (this.opts.toggleSelect) {
  14. return this.results.find('.select2-result-selectable:not(.select2-disabled)');
  15. }
  16. return Select2FindHighlightableChoices.apply(this, arguments);
  17. };
  18. var Select2TriggerSelect = Select2.class.multi.prototype.triggerSelect;
  19. Select2.class.multi.prototype.triggerSelect = function (data) {
  20. if (this.opts.toggleSelect && this.val().indexOf(this.id(data)) !== -1) {
  21. var self = this;
  22. var val = this.id(data);
  23. var selectionEls = this.container.find('.select2-search-choice').filter(function() {
  24. return (self.id($(this).data('select2-data')) === val);
  25. });
  26. if (this.unselect(selectionEls)) {
  27. // also unselect in dropdown
  28. this.results.find('.select2-result.select2-selected').each(function () {
  29. var $this = $(this);
  30. if (self.id($this.data('select2-data')) === val) {
  31. $this.removeClass('select2-selected');
  32. }
  33. });
  34. this.clearSearch();
  35. }
  36. return false;
  37. } else {
  38. return Select2TriggerSelect.apply(this, arguments);
  39. }
  40. };
  41. })(window.Select2);