index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. require('../../utils/index.js');
  4. var core = require('@vueuse/core');
  5. var types = require('../../utils/types.js');
  6. const nodeList = /* @__PURE__ */ new Map();
  7. if (core.isClient) {
  8. let startClick;
  9. document.addEventListener("mousedown", (e) => startClick = e);
  10. document.addEventListener("mouseup", (e) => {
  11. if (startClick) {
  12. for (const handlers of nodeList.values()) {
  13. for (const { documentHandler } of handlers) {
  14. documentHandler(e, startClick);
  15. }
  16. }
  17. startClick = void 0;
  18. }
  19. });
  20. }
  21. function createDocumentHandler(el, binding) {
  22. let excludes = [];
  23. if (Array.isArray(binding.arg)) {
  24. excludes = binding.arg;
  25. } else if (types.isElement(binding.arg)) {
  26. excludes.push(binding.arg);
  27. }
  28. return function(mouseup, mousedown) {
  29. const popperRef = binding.instance.popperRef;
  30. const mouseUpTarget = mouseup.target;
  31. const mouseDownTarget = mousedown == null ? void 0 : mousedown.target;
  32. const isBound = !binding || !binding.instance;
  33. const isTargetExists = !mouseUpTarget || !mouseDownTarget;
  34. const isContainedByEl = el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
  35. const isSelf = el === mouseUpTarget;
  36. const isTargetExcluded = excludes.length && excludes.some((item) => item == null ? void 0 : item.contains(mouseUpTarget)) || excludes.length && excludes.includes(mouseDownTarget);
  37. const isContainedByPopper = popperRef && (popperRef.contains(mouseUpTarget) || popperRef.contains(mouseDownTarget));
  38. if (isBound || isTargetExists || isContainedByEl || isSelf || isTargetExcluded || isContainedByPopper) {
  39. return;
  40. }
  41. binding.value(mouseup, mousedown);
  42. };
  43. }
  44. const ClickOutside = {
  45. beforeMount(el, binding) {
  46. if (!nodeList.has(el)) {
  47. nodeList.set(el, []);
  48. }
  49. nodeList.get(el).push({
  50. documentHandler: createDocumentHandler(el, binding),
  51. bindingFn: binding.value
  52. });
  53. },
  54. updated(el, binding) {
  55. if (!nodeList.has(el)) {
  56. nodeList.set(el, []);
  57. }
  58. const handlers = nodeList.get(el);
  59. const oldHandlerIndex = handlers.findIndex((item) => item.bindingFn === binding.oldValue);
  60. const newHandler = {
  61. documentHandler: createDocumentHandler(el, binding),
  62. bindingFn: binding.value
  63. };
  64. if (oldHandlerIndex >= 0) {
  65. handlers.splice(oldHandlerIndex, 1, newHandler);
  66. } else {
  67. handlers.push(newHandler);
  68. }
  69. },
  70. unmounted(el) {
  71. nodeList.delete(el);
  72. }
  73. };
  74. exports["default"] = ClickOutside;
  75. //# sourceMappingURL=index.js.map