2020-08-19 08:36:03 +08:00
|
|
|
import { on } from '@element-plus/utils/dom'
|
2020-09-16 14:49:21 +08:00
|
|
|
import isServer from '@element-plus/utils/isServer'
|
2020-08-19 08:36:03 +08:00
|
|
|
|
2021-08-24 13:36:48 +08:00
|
|
|
import type {
|
|
|
|
ComponentPublicInstance,
|
|
|
|
DirectiveBinding,
|
|
|
|
ObjectDirective,
|
|
|
|
} from 'vue'
|
|
|
|
import type { Nullable } from '@element-plus/utils/types'
|
2020-08-19 08:36:03 +08:00
|
|
|
|
2020-08-28 10:47:02 +08:00
|
|
|
type DocumentHandler = <T extends MouseEvent>(mouseup: T, mousedown: T) => void;
|
2020-08-19 08:36:03 +08:00
|
|
|
type FlushList = Map<
|
2021-08-27 17:46:06 +08:00
|
|
|
HTMLElement,
|
|
|
|
{
|
|
|
|
documentHandler: DocumentHandler
|
|
|
|
bindingFn: (...args: unknown[]) => unknown
|
|
|
|
}[]
|
2020-08-19 08:36:03 +08:00
|
|
|
>;
|
|
|
|
|
|
|
|
const nodeList: FlushList = new Map()
|
|
|
|
|
2020-08-28 10:47:02 +08:00
|
|
|
let startClick: MouseEvent
|
2020-08-19 08:36:03 +08:00
|
|
|
|
|
|
|
if (!isServer) {
|
2020-08-28 10:47:02 +08:00
|
|
|
on(document, 'mousedown', (e: MouseEvent) => (startClick = e))
|
|
|
|
on(document, 'mouseup', (e: MouseEvent) => {
|
2021-06-25 17:10:29 +08:00
|
|
|
for (const handlers of nodeList.values()) {
|
|
|
|
for (const { documentHandler } of handlers) {
|
|
|
|
documentHandler(e, startClick)
|
|
|
|
}
|
2020-08-19 08:36:03 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDocumentHandler(
|
|
|
|
el: HTMLElement,
|
|
|
|
binding: DirectiveBinding,
|
|
|
|
): DocumentHandler {
|
2020-08-28 10:47:02 +08:00
|
|
|
let excludes: HTMLElement[] = []
|
|
|
|
if (Array.isArray(binding.arg)) {
|
|
|
|
excludes = binding.arg
|
2021-06-25 17:10:29 +08:00
|
|
|
} else if (binding.arg as unknown instanceof HTMLElement) {
|
2020-08-28 10:47:02 +08:00
|
|
|
// due to current implementation on binding type is wrong the type casting is necessary here
|
|
|
|
excludes.push(binding.arg as unknown as HTMLElement)
|
|
|
|
}
|
2020-08-19 08:36:03 +08:00
|
|
|
return function(mouseup, mousedown) {
|
|
|
|
const popperRef = (binding.instance as ComponentPublicInstance<{
|
|
|
|
popperRef: Nullable<HTMLElement>
|
|
|
|
}>).popperRef
|
2020-08-28 10:47:02 +08:00
|
|
|
const mouseUpTarget = mouseup.target as Node
|
2021-04-06 13:28:57 +08:00
|
|
|
const mouseDownTarget = mousedown?.target as Node
|
2020-08-28 10:47:02 +08:00
|
|
|
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?.contains(mouseUpTarget))
|
|
|
|
) || (
|
|
|
|
excludes.length && excludes.includes(mouseDownTarget as HTMLElement)
|
|
|
|
)
|
|
|
|
const isContainedByPopper = (
|
|
|
|
popperRef &&
|
|
|
|
(
|
|
|
|
popperRef.contains(mouseUpTarget) ||
|
|
|
|
popperRef.contains(mouseDownTarget)
|
|
|
|
)
|
|
|
|
)
|
2020-08-19 08:36:03 +08:00
|
|
|
if (
|
2020-08-28 10:47:02 +08:00
|
|
|
isBound ||
|
|
|
|
isTargetExists ||
|
|
|
|
isContainedByEl ||
|
|
|
|
isSelf ||
|
|
|
|
isTargetExcluded ||
|
|
|
|
isContainedByPopper
|
2020-08-19 08:36:03 +08:00
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-24 17:29:19 +08:00
|
|
|
binding.value(mouseup, mousedown)
|
2020-08-19 08:36:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClickOutside: ObjectDirective = {
|
|
|
|
beforeMount(el, binding) {
|
2021-06-25 17:10:29 +08:00
|
|
|
// there could be multiple handlers on the element
|
|
|
|
if (!nodeList.has(el)) {
|
|
|
|
nodeList.set(el, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeList.get(el).push({
|
2020-08-19 08:36:03 +08:00
|
|
|
documentHandler: createDocumentHandler(el, binding),
|
|
|
|
bindingFn: binding.value,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
updated(el, binding) {
|
2021-06-25 17:10:29 +08:00
|
|
|
if (!nodeList.has(el)) {
|
|
|
|
nodeList.set(el, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
const handlers = nodeList.get(el)
|
|
|
|
const oldHandlerIndex = handlers.findIndex(item => (item.bindingFn === binding.oldValue))
|
|
|
|
const newHandler = {
|
2020-08-19 08:36:03 +08:00
|
|
|
documentHandler: createDocumentHandler(el, binding),
|
|
|
|
bindingFn: binding.value,
|
2021-06-25 17:10:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oldHandlerIndex >= 0) {
|
|
|
|
// replace the old handler to the new handler
|
|
|
|
handlers.splice(oldHandlerIndex, 1, newHandler)
|
|
|
|
} else {
|
|
|
|
handlers.push(newHandler)
|
|
|
|
}
|
2020-08-19 08:36:03 +08:00
|
|
|
},
|
|
|
|
unmounted(el) {
|
2021-06-25 17:10:29 +08:00
|
|
|
// remove all listeners when a component unmounted
|
2020-08-19 08:36:03 +08:00
|
|
|
nodeList.delete(el)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClickOutside
|