Update v-click-outside-x.js

This commit is contained in:
梁灏 2019-09-17 18:15:55 +08:00
parent 4c249f6e34
commit 99fcf3b4b9

View file

@ -104,17 +104,27 @@ export const directive = Object.defineProperties(
value: nonCaptureEventHandler, value: nonCaptureEventHandler,
}, },
/**
* 注意这里的 arg 修改为 capture这样可以动态设置原先的事件作为 modifiers
* */
bind: { bind: {
value: function bind(el, binding) { value: function bind(el, binding) {
if (typeof binding.value !== 'function') { if (typeof binding.value !== 'function') {
throw new TypeError('Binding value must be a function.'); throw new TypeError('Binding value must be a function.');
} }
const arg = binding.arg || CLICK; let eventType;
const modifiers = binding.modifiers;
if (modifiers.click) eventType = 'click';
else if (modifiers.mousedown) eventType = 'mousedown';
else if (modifiers.touchstart) eventType = 'touchstart';
else eventType = CLICK;
const useCapture = binding.arg;
const normalisedBinding = { const normalisedBinding = {
...binding, ...binding,
...{ ...{
arg,
modifiers: { modifiers: {
...{ ...{
capture: false, capture: false,
@ -126,17 +136,16 @@ export const directive = Object.defineProperties(
}, },
}; };
const useCapture = normalisedBinding.modifiers.capture;
const instances = useCapture ? captureInstances : nonCaptureInstances; const instances = useCapture ? captureInstances : nonCaptureInstances;
if (!Array.isArray(instances[arg])) { if (!Array.isArray(instances[eventType])) {
instances[arg] = []; instances[eventType] = [];
} }
if (instances[arg].push({el, binding: normalisedBinding}) === 1) { if (instances[eventType].push({el, binding: normalisedBinding}) === 1) {
if (typeof document === 'object' && document) { if (typeof document === 'object' && document) {
document.addEventListener( document.addEventListener(
arg, eventType,
getEventHandler(useCapture), getEventHandler(useCapture),
useCapture, useCapture,
); );