{"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;CACJ,SAAS,iBAAiB,cAAc,MAAmB,aAAa,EAAG;CAC3E,SAAS,iBAAiB,YAAY,MAAkB;EACtD,IAAI,YAAY;GACd,KAAK,MAAM,YAAY,SAAS,QAAQ,EACtC,KAAK,MAAM,EAAE,qBAAqB,UAChC,gBAAgB,GAAiB,WAAW;GAGhD,aAAa,KAAA;;GAEf;;AAGJ,SAAS,sBACP,IACA,SACiB;CACjB,IAAI,WAA0B,EAAE;CAChC,IAAI,QAAQ,QAAQ,IAAI,EACtB,WAAW,QAAQ;MACd,IAAI,UAAU,QAAQ,IAAI,EAE/B,SAAS,KAAK,QAAQ,IAA8B;CAEtD,OAAO,SAAU,SAAS,WAAW;EACnC,MAAM,YACJ,QAAQ,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