Update v-click-outside-x.js
This commit is contained in:
parent
4c249f6e34
commit
99fcf3b4b9
1 changed files with 16 additions and 7 deletions
|
@ -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,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue