You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
6.4 KiB

{"version":3,"file":"index.mjs","sources":["../../../../../packages/directives/click-outside/index.ts"],"sourcesContent":["import { isClient, isElement } from '@element-plus/utils'\n\nimport type {\n ComponentPublicInstance,\n DirectiveBinding,\n ObjectDirective,\n} from 'vue'\n\ntype DocumentHandler = <T extends MouseEvent>(mouseup: T, mousedown: T) => void\ntype FlushList = Map<\n HTMLElement,\n {\n documentHandler: DocumentHandler\n bindingFn: (...args: unknown[]) => unknown\n }[]\n>\n\nconst nodeList: FlushList = new Map()\n\nif (isClient) {\n let startClick: MouseEvent | undefined\n document.addEventListener('mousedown', (e: MouseEvent) => (startClick = e))\n document.addEventListener('mouseup', (e: MouseEvent) => {\n if (startClick) {\n for (const handlers of nodeList.values()) {\n for (const { documentHandler } of handlers) {\n documentHandler(e as MouseEvent, startClick)\n }\n }\n startClick = undefined\n }\n })\n}\n\nfunction createDocumentHandler(\n el: HTMLElement,\n binding: DirectiveBinding\n): DocumentHandler {\n let excludes: HTMLElement[] = []\n if (Array.isArray(binding.arg)) {\n excludes = binding.arg\n } else if (isElement(binding.arg)) {\n // due to current implementation on binding type is wrong the type casting is necessary here\n excludes.push(binding.arg as unknown as HTMLElement)\n }\n return function (mouseup, mousedown) {\n const popperRef = (\n binding.instance as ComponentPublicInstance<{\n popperRef: HTMLElement\n }>\n ).popperRef\n const mouseUpTarget = mouseup.target as Node\n const mouseDownTarget = mousedown?.target as Node\n const isBound = !binding || !binding.instance\n const isTargetExists = !mouseUpTarget || !mouseDownTarget\n const isContainedByEl =\n el.contains(mouseUpTarget) || el.contains(mouseDownTarget)\n const isSelf = el === mouseUpTarget\n\n const isTargetExcluded =\n (excludes.length &&\n excludes.some((item) => item?.contains(mouseUpTarget))) ||\n (excludes.length && excludes.includes(mouseDownTarget as HTMLElement))\n const isContainedByPopper =\n popperRef &&\n (popperRef.contains(mouseUpTarget) || popperRef.contains(mouseDownTarget))\n if (\n isBound ||\n isTargetExists ||\n isContainedByEl ||\n isSelf ||\n isTargetExcluded ||\n isContainedByPopper\n ) {\n return\n }\n binding.value(mouseup, mousedown)\n }\n}\n\nconst ClickOutside: ObjectDirective = {\n beforeMount(el: HTMLElement, binding: DirectiveBinding) {\n // there could be multiple handlers on the element\n if (!nodeList.has(el)) {\n nodeList.set(el, [])\n }\n\n nodeList.get(el)!.push({\n documentHandler: createDocumentHandler(el, binding),\n bindingFn: binding.value,\n })\n },\n updated(el: HTMLElement, binding: DirectiveBinding) {\n if (!nodeList.has(el)) {\n nodeList.set(el, [])\n }\n\n const handlers = nodeList.get(el)!\n const oldHandlerIndex = handlers.findIndex(\n (item) => item.bindingFn === binding.oldValue\n )\n const newHandler = {\n documentHandler: createDocumentHandler(el, binding),\n bindingFn: binding.value,\n }\n\n if (oldHandlerIndex >= 0) {\n // replace the old handler to the new handler\n handlers.splice(oldHandlerIndex, 1, newHandler)\n } else {\n handlers.push(newHandler)\n }\n },\n unmounted(el: HTMLElement) {\n // remove all listeners when a component unmounted\n nodeList.delete(el)\n },\n}\n\nexport default ClickOutside\n"],"names":[],"mappings":";;;;AACA,MAAM,QAAQ,mBAAmB,IAAI,GAAG,EAAE,CAAC;AAC3C,IAAI,QAAQ,EAAE;AACd,EAAE,IAAI,UAAU,CAAC;AACjB,EAAE,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,CAAC,CAAC,KAAK,UAAU,GAAG,CAAC,CAAC,CAAC;AAChE,EAAE,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK;AAC9C,IAAI,IAAI,UAAU,EAAE;AACpB,MAAM,KAAK,MAAM,QAAQ,IAAI,QAAQ,CAAC,MAAM,EAAE,EAAE;AAChD,QAAQ,KAAK,MAAM,EAAE,eAAe,EAAE,IAAI,QAAQ,EAAE;AACpD,UAAU,eAAe,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;AACzC,SAAS;AACT,OAAO;AACP,MAA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