Commit a7a777dd authored by qingwei.li's avatar qingwei.li

optimize clickoutside

parent 96ff5e1d
import { on, off } from 'wind-dom/src/event'; import { on } from 'wind-dom/src/event';
const nodeList = [];
on(document, 'click', e => {
nodeList.forEach(node => node[clickoutsideContext].documentHandler(e));
});
/** /**
* v-clickoutside * v-clickoutside
* @desc 点击元素外面才会触发的事件 * @desc 点击元素外面才会触发的事件
...@@ -12,6 +16,7 @@ const clickoutsideContext = '@@clickoutsideContext'; ...@@ -12,6 +16,7 @@ const clickoutsideContext = '@@clickoutsideContext';
export default { export default {
bind(el, binding, vnode) { bind(el, binding, vnode) {
const id = nodeList.push(el) - 1;
const documentHandler = function(e) { const documentHandler = function(e) {
if (!vnode.context || if (!vnode.context ||
el.contains(e.target) || el.contains(e.target) ||
...@@ -24,11 +29,11 @@ export default { ...@@ -24,11 +29,11 @@ export default {
} }
}; };
el[clickoutsideContext] = { el[clickoutsideContext] = {
id,
documentHandler, documentHandler,
methodName: binding.expression, methodName: binding.expression,
bindingFn: binding.value bindingFn: binding.value
}; };
on(document, 'click', documentHandler);
}, },
update(el, binding) { update(el, binding) {
...@@ -37,7 +42,7 @@ export default { ...@@ -37,7 +42,7 @@ export default {
}, },
unbind(el) { unbind(el) {
off(document, 'click', el[clickoutsideContext].documentHandler); nodeList.splice(el[clickoutsideContext].id, 1);
}, },
install(Vue) { install(Vue) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment