123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import { isClient } from '@vueuse/core';
- import { isArray } from '@vue/shared';
- import { isElement } from '../../utils/types.mjs';
- const nodeList = /* @__PURE__ */ new Map();
- if (isClient) {
- let startClick;
- document.addEventListener("mousedown", (e) => startClick = e);
- document.addEventListener("mouseup", (e) => {
- if (startClick) {
- for (const handlers of nodeList.values()) {
- for (const { documentHandler } of handlers) {
- documentHandler(e, startClick);
- }
- }
- startClick = void 0;
- }
- });
- }
- function createDocumentHandler(el, binding) {
- let excludes = [];
- if (isArray(binding.arg)) {
- excludes = binding.arg;
- } else if (isElement(binding.arg)) {
- excludes.push(binding.arg);
- }
- return function(mouseup, mousedown) {
- const popperRef = binding.instance.popperRef;
- const mouseUpTarget = mouseup.target;
- const mouseDownTarget = mousedown == null ? void 0 : mousedown.target;
- const isBound = !binding || !binding.instance;
- const isTargetExists = !mouseUpTarget || !mouseDownTarget;
- const isContainedByEl = el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
- const isSelf = el === mouseUpTarget;
- const isTargetExcluded = excludes.length && excludes.some((item) => item == null ? void 0 : item.contains(mouseUpTarget)) || excludes.length && excludes.includes(mouseDownTarget);
- const isContainedByPopper = popperRef && (popperRef.contains(mouseUpTarget) || popperRef.contains(mouseDownTarget));
- if (isBound || isTargetExists || isContainedByEl || isSelf || isTargetExcluded || isContainedByPopper) {
- return;
- }
- binding.value(mouseup, mousedown);
- };
- }
- const ClickOutside = {
- beforeMount(el, binding) {
- if (!nodeList.has(el)) {
- nodeList.set(el, []);
- }
- nodeList.get(el).push({
- documentHandler: createDocumentHandler(el, binding),
- bindingFn: binding.value
- });
- },
- updated(el, binding) {
- if (!nodeList.has(el)) {
- nodeList.set(el, []);
- }
- const handlers = nodeList.get(el);
- const oldHandlerIndex = handlers.findIndex((item) => item.bindingFn === binding.oldValue);
- const newHandler = {
- documentHandler: createDocumentHandler(el, binding),
- bindingFn: binding.value
- };
- if (oldHandlerIndex >= 0) {
- handlers.splice(oldHandlerIndex, 1, newHandler);
- } else {
- handlers.push(newHandler);
- }
- },
- unmounted(el) {
- nodeList.delete(el);
- }
- };
- export { ClickOutside as default };
- //# sourceMappingURL=index.mjs.map
|