index.mjs 2.5 KB

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