primevue-mirror/components/utils/ZIndexUtils.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

function handler() {
let zIndexes = [];
const generateZIndex = (key, autoZIndex, baseZIndex = 999) => {
const lastZIndex = getLastZIndex(key, autoZIndex, baseZIndex);
const newZIndex = lastZIndex.value + (lastZIndex.key === key ? 0 : baseZIndex) + 1;
zIndexes.push({ key, value: newZIndex });
2022-09-14 11:26:01 +00:00
return newZIndex;
2022-09-14 11:26:01 +00:00
};
const revertZIndex = (zIndex) => {
2022-09-14 11:26:01 +00:00
zIndexes = zIndexes.filter((obj) => obj.value !== zIndex);
};
const getCurrentZIndex = (key, autoZIndex) => {
return getLastZIndex(key, autoZIndex).value;
};
const getLastZIndex = (key, autoZIndex, baseZIndex = 0) => {
return [...zIndexes].reverse().find((obj) => (autoZIndex ? true : obj.key === key)) || { key, value: baseZIndex };
2022-09-14 11:26:01 +00:00
};
const getZIndex = (el) => {
2022-09-14 11:26:01 +00:00
return el ? parseInt(el.style.zIndex, 10) || 0 : 0;
};
return {
get: getZIndex,
set: (key, el, baseZIndex) => {
if (el) {
el.style.zIndex = String(generateZIndex(key, true, baseZIndex));
}
},
clear: (el) => {
if (el) {
revertZIndex(getZIndex(el));
el.style.zIndex = '';
}
},
getCurrent: (key) => getCurrentZIndex(key, true)
};
}
2022-09-14 11:26:01 +00:00
export default handler();