@opensig/opendesign
Version:
<p align="center"><img src="../docs/public/opendesign-logo-light.png"/></p> <h1 align="center">opendesign</h1> <p align="center">一个 Vue 3 组件库</p> <p align="center"><b>皮肤可定制,使用 TypeScript</b></p>
87 lines (86 loc) • 2.19 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const is = require("../_utils/is.js");
const elList = /* @__PURE__ */ new Map();
const elListFast = /* @__PURE__ */ new Map();
let isBindEvent = false;
const Event = {
start: is.isTouchDevice ? "touchstart" : "mousedown",
end: is.isTouchDevice ? "touchend" : "mouseup"
};
function addListener(el, fn, params) {
const list = (params == null ? void 0 : params.fast) ? elListFast : elList;
if (!list.has(el)) {
list.set(el, []);
}
const handlers = list.get(el);
if (handlers) {
handlers.push({
handler: fn,
exception: params == null ? void 0 : params.exception
});
}
}
function removeListener(el, listener) {
if (listener) {
const handlers = elList.get(el);
if (!handlers) {
return;
}
const idx = handlers.findIndex((item) => item.handler === listener);
if (idx > -1) {
handlers.splice(idx, 1);
}
} else {
elList.delete(el);
}
}
function bindEvents() {
if (!isBindEvent) {
let isOutSide = false;
const runHandlers = (list, e) => {
list.forEach((handlers, el) => {
if (!el.contains(e.target)) {
handlers.forEach((item) => {
if (!item.exception || !item.exception(e)) {
item.handler();
}
});
}
});
};
window.addEventListener(Event.start, (e) => {
runHandlers(elListFast, e);
const keys = Array.from(elList.keys());
isOutSide = false;
keys.some((el) => {
if (!el.contains(e.target)) {
const handlers = elList.get(el);
if (!handlers) {
isOutSide = true;
} else {
isOutSide = handlers.some((item) => {
return !item.exception || !item.exception(e);
});
}
}
return isOutSide;
});
});
window.addEventListener(Event.end, (e) => {
if (!isOutSide) {
return;
}
runHandlers(elList, e);
});
isBindEvent = true;
}
}
function useOutClick() {
bindEvents();
return {
addListener,
removeListener
};
}
exports.useOutClick = useOutClick;