2021-04-13 13:59:08 +00:00
|
|
|
function handler() {
|
|
|
|
let zIndexes = [];
|
|
|
|
|
2023-01-18 15:40:42 +00:00
|
|
|
const generateZIndex = (key, autoZIndex, baseZIndex = 999) => {
|
|
|
|
const lastZIndex = getLastZIndex(key, autoZIndex, baseZIndex);
|
2023-01-17 09:52:01 +00:00
|
|
|
const newZIndex = lastZIndex.value + (lastZIndex.key === key ? 0 : baseZIndex) + 1;
|
2021-04-13 13:59:08 +00:00
|
|
|
|
|
|
|
zIndexes.push({ key, value: newZIndex });
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2021-04-13 13:59:08 +00:00
|
|
|
return newZIndex;
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2021-04-13 13:59:08 +00:00
|
|
|
|
|
|
|
const revertZIndex = (zIndex) => {
|
2022-09-14 11:26:01 +00:00
|
|
|
zIndexes = zIndexes.filter((obj) => obj.value !== zIndex);
|
|
|
|
};
|
2021-04-13 13:59:08 +00:00
|
|
|
|
2023-01-18 15:40:42 +00:00
|
|
|
const getCurrentZIndex = (key, autoZIndex) => {
|
|
|
|
return getLastZIndex(key, autoZIndex).value;
|
2023-01-17 09:52:01 +00:00
|
|
|
};
|
|
|
|
|
2023-01-18 15:40:42 +00:00
|
|
|
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
|
|
|
};
|
2021-04-13 13:59:08 +00:00
|
|
|
|
|
|
|
const getZIndex = (el) => {
|
2022-09-14 11:26:01 +00:00
|
|
|
return el ? parseInt(el.style.zIndex, 10) || 0 : 0;
|
|
|
|
};
|
2021-04-13 13:59:08 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
get: getZIndex,
|
|
|
|
set: (key, el, baseZIndex) => {
|
|
|
|
if (el) {
|
2023-01-18 15:40:42 +00:00
|
|
|
el.style.zIndex = String(generateZIndex(key, true, baseZIndex));
|
2021-04-13 13:59:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
clear: (el) => {
|
|
|
|
if (el) {
|
|
|
|
revertZIndex(getZIndex(el));
|
|
|
|
el.style.zIndex = '';
|
|
|
|
}
|
|
|
|
},
|
2023-01-18 15:40:42 +00:00
|
|
|
getCurrent: (key) => getCurrentZIndex(key, true)
|
2021-04-13 13:59:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
export default handler();
|