| 1 |
- {"version":3,"file":"index.mjs","names":[],"sources":["../../../../../packages/directives/click-outside/index.ts"],"sourcesContent":["import { isArray, 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 (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<HTMLElement, any> = {\n beforeMount(el, binding) {\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, binding) {\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) {\n // remove all listeners when a component unmounted\n nodeList.delete(el)\n },\n}\n\nexport default ClickOutside\n"],"mappings":";;;;AAiBA,MAAM,2BAAsB,IAAI,KAAK;AAErC,IAAI,UAAU;CACZ,IAAI;AACJ,UAAS,iBAAiB,cAAc,MAAmB,aAAa,EAAG;AAC3E,UAAS,iBAAiB,YAAY,MAAkB;AACtD,MAAI,YAAY;AACd,QAAK,MAAM,YAAY,SAAS,QAAQ,CACtC,MAAK,MAAM,EAAE,qBAAqB,SAChC,iBAAgB,GAAiB,WAAW;AAGhD,gBAAa;;GAEf;;AAGJ,SAAS,sBACP,IACA,SACiB;CACjB,IAAI,WAA0B,EAAE;AAChC,KAAI,QAAQ,QAAQ,IAAI,CACtB,YAAW,QAAQ;UACV,UAAU,QAAQ,IAAI,CAE/B,UAAS,KAAK,QAAQ,IAA8B;AAEtD,QAAO,SAAU,SAAS,WAAW;EACnC,MAAM,YACJ,QAAQ,SAGR;EACF,MAAM,gBAAgB,QAAQ;EAC9B,MAAM,kBAAkB,WAAW;EACnC,MAAM,UAAU,CAAC,WAAW,CAAC,QAAQ;EACrC,MAAM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