selectize-option-click.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import $ from 'jquery';
  2. import Selectize from 'selectize';
  3. Selectize.define('option_click', function(options) {
  4. const self = this;
  5. const setup = self.setup;
  6. this.setup = function() {
  7. setup.apply(self, arguments);
  8. let clicking = false;
  9. // Detect click on a .clickable
  10. self.$dropdown_content.on('mousedown click', function(e) {
  11. const target = $(e.target);
  12. if (target.hasClass('clickable') || target.closest('.clickable').length) {
  13. if (e.type === 'mousedown') {
  14. clicking = true;
  15. self.isFocused = false; // awful hack to defuse the document mousedown listener
  16. } else {
  17. self.isFocused = true;
  18. setTimeout(function() {
  19. clicking = false; // wait until blur has been preempted
  20. });
  21. }
  22. } else { // cleanup in case user right-clicked or dragged off the element
  23. clicking = false;
  24. self.isFocused = true;
  25. }
  26. });
  27. // Intercept default handlers
  28. self.$dropdown.off('mousedown click', '[data-selectable]').on('mousedown click', '[data-selectable]', function() {
  29. if (!clicking) {
  30. return self.onOptionSelect.apply(self, arguments);
  31. }
  32. });
  33. self.$control_input.off('blur').on('blur', function() {
  34. if (!clicking) {
  35. return self.onBlur.apply(self, arguments);
  36. }
  37. });
  38. };
  39. });