winbox-ui-next
Version:
We Design Vue 2.0: A Vue.js 3 UI Library
87 lines (86 loc) • 2.04 kB
JavaScript
;
var vue = require("vue");
const POPUP_BASE_Z_INDEX = 1e3;
const MESSAGE_BASE_Z_INDEX = 5e3;
const Z_INDEX_STEP = 1;
class PopupManager {
constructor() {
this.popupStack = {
popup: new Set(),
dialog: new Set(),
message: new Set()
};
this.getNextZIndex = (type) => {
if (type === "message") {
return MESSAGE_BASE_Z_INDEX + this.popupStack.message.size * Z_INDEX_STEP;
}
return POPUP_BASE_Z_INDEX + this.popupStack.popup.size * Z_INDEX_STEP;
};
this.add = (id, type) => {
this.popupStack[type].add(id);
if (type === "dialog") {
this.popupStack.popup.add(id);
}
return this.getNextZIndex(type);
};
this.delete = (id, type) => {
this.popupStack[type].delete(id);
if (type === "dialog") {
this.popupStack.popup.delete(id);
}
};
this.isLastDialog = (id) => {
if (this.popupStack.dialog.size > 1) {
const array = Array.from(this.popupStack.dialog);
return id === array[array.length - 1];
}
return true;
};
}
}
const popupManager = new PopupManager();
function usePopupManager(type, {
visible,
runOnMounted
} = {}) {
var _a, _b;
const id = (_b = (_a = vue.getCurrentInstance()) == null ? void 0 : _a.uid) != null ? _b : Date.now();
const zIndex = vue.ref(0);
const open = () => {
zIndex.value = popupManager.add(id, type);
};
const close = () => {
popupManager.delete(id, type);
};
const isLastDialog = () => {
if (type === "dialog") {
return popupManager.isLastDialog(id);
}
return false;
};
vue.watch(() => visible == null ? void 0 : visible.value, (visible2) => {
if (visible2) {
open();
} else {
close();
}
}, {
immediate: true
});
if (runOnMounted) {
vue.onMounted(() => {
open();
});
vue.onBeforeUnmount(() => {
close();
});
}
return {
id,
zIndex,
open,
close,
isLastDialog
};
}
module.exports = usePopupManager;