ivue-material-plus
Version:
A high quality UI components Library with Vue.js
115 lines (113 loc) • 3.31 kB
JavaScript
const CLICK = "click";
const captureInstances = /* @__PURE__ */ Object.create(null);
const nonCaptureInstances = /* @__PURE__ */ Object.create(null);
const instancesList = [captureInstances, nonCaptureInstances];
const commonHandler = function _onCommonHandler(context, instances, event) {
const { target } = event;
const item = function _item(item2) {
const { el } = item2;
if (el !== target && !el.contains(target)) {
const { binding } = item2;
if (binding.modifiers.stop) {
event.stopPropagation();
}
if (binding.modifiers.prevent) {
event.preventDefault();
}
binding.value.call(context, event);
}
};
const key = function _key(eventName) {
return instances[eventName].forEach(item);
};
Object.keys(instances).forEach(key);
};
const captureEventHandler = function onCaptureEvent(event) {
commonHandler(this, captureInstances, event);
};
const nonCaptureEventHandler = function onNonCaptureEvent(event) {
commonHandler(this, nonCaptureInstances, event);
};
const getEventHandler = function _getEventHandler(useCapture) {
return useCapture ? captureEventHandler : nonCaptureEventHandler;
};
function inserted(el, binding) {
if (typeof binding.value !== "function") {
throw new TypeError("Binding value must be a function");
}
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 = {
...binding,
...{
modifiers: {
...{
capture: false,
prevent: false,
stop: false
},
...binding.modifiers
}
}
};
const instances = useCapture ? captureInstances : nonCaptureInstances;
if (!Array.isArray(instances[eventType])) {
instances[eventType] = [];
}
if (instances[eventType].push({
el,
binding: normalisedBinding
}) === 1) {
if (typeof document === "object" && document) {
document.addEventListener(
eventType,
getEventHandler(useCapture),
useCapture
);
}
}
}
function unbind(el) {
const compareElements = function _compareElements(item) {
return item.el !== el;
};
const instances = function _instances(instances2) {
const instancesKeys = Object.keys(instances2);
if (instancesKeys.length) {
const useCapture = instances2 === captureInstances;
const keys = function _keys(eventName) {
const newInstance = instances2[eventName].filter(compareElements);
if (newInstance.length) {
instances2[eventName] = newInstance;
} else {
if (typeof document === "object" && document) {
document.removeEventListener(
eventName,
getEventHandler(useCapture),
useCapture
);
}
delete instances2[eventName];
}
};
instancesKeys.forEach(keys);
}
};
instancesList.forEach(instances);
}
var ClickOutside = {
beforeMount: inserted,
unmounted: unbind
};
export { ClickOutside as default };
//# sourceMappingURL=click-outside.mjs.map