-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
178 lines (156 loc) · 3.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
let winDom: Document | null = null
let bodyEl: HTMLElement | null = null
let storeEl: HTMLElement | null = null
let storeId = 'z-index-manage'
let styleEl: HTMLStyleElement | null = null
let styleId = 'z-index-style'
let storeMainKey: 'm' = 'm'
let storeSubKey: 's' = 's'
const storeData = {
m: 1000,
s: 1000
}
function getDocument () {
if (!winDom) {
if (typeof document !== 'undefined') {
winDom = document
}
}
return winDom
}
function getBody () {
if (winDom && !bodyEl) {
bodyEl= winDom.body || winDom.getElementsByTagName('body')[0]
}
return bodyEl
}
function getDomMaxZIndex () {
let max = 0
const dom = getDocument()
if (dom) {
const body = getBody()
if (body) {
const allElem = body.getElementsByTagName('*')
for(let i = 0; i < allElem.length; i++) {
const elem = allElem[i] as HTMLElement
if (elem && elem.style && elem.nodeType === 1) {
const zIndex = elem.style.zIndex
if (zIndex && /^\d+$/.test(zIndex)) {
max = Math.max(max, Number(zIndex))
}
}
}
}
}
return max
}
function getStyle () {
if (!styleEl) {
const dom = getDocument()
if (dom) {
styleEl = dom.getElementById(styleId) as HTMLStyleElement
if (!styleEl) {
styleEl = dom.createElement('style')
styleEl.id = styleId
dom.getElementsByTagName('head')[0].appendChild(styleEl)
}
}
}
return styleEl
}
function updateVar () {
let styEl = getStyle()
if (styEl) {
let prefixes = '--dom-'
let propKey = '-z-index'
styEl.innerHTML = ':root{' + prefixes + 'main' + propKey + ':' + getCurrent() + ';' + prefixes + 'sub' + propKey + ':' + getSubCurrent() + '}'
}
}
function getStoreDom () {
if (!storeEl) {
const dom = getDocument()
if (dom) {
storeEl = dom.getElementById(storeId)
if (!storeEl) {
const body = getBody()
if (body) {
storeEl = dom.createElement('div')
storeEl.id = storeId
storeEl.style.display = 'none'
body.appendChild(storeEl)
setCurrent(storeData.m)
setSubCurrent(storeData.s)
}
}
}
}
return storeEl
}
function createSetHandle (key: keyof (typeof storeData)) {
return function (value: number) {
if (value) {
value = Number(value)
storeData[key] = value
let el = getStoreDom()
if (el) {
if (el.dataset) {
el.dataset[key] = value + ''
} else {
el.setAttribute('data-' + key, value + '')
}
}
}
updateVar()
return storeData[key]
}
}
export const setCurrent = createSetHandle(storeMainKey)
function createGetHandle (key: keyof (typeof storeData), nextMethod: () => number) {
return function getCurrent (currZindex?: number) {
let zIndex
let el = getStoreDom()
if (el) {
const domVal = el.dataset ? el.dataset[key] : el.getAttribute('data-' + key)
if (domVal) {
zIndex = Number(domVal)
}
}
if (!zIndex) {
zIndex = storeData[key]
}
if (currZindex) {
if (Number(currZindex) < zIndex) {
return nextMethod()
}
return currZindex
}
return zIndex
}
}
export const getCurrent = createGetHandle(storeMainKey, getNext)
export function getNext () {
return setCurrent(getCurrent() + 1)
}
export const setSubCurrent = createSetHandle(storeSubKey)
const _getSubCurrent = createGetHandle(storeSubKey, getSubNext)
export function getSubCurrent () {
return getCurrent() + _getSubCurrent()
}
export function getSubNext () {
setSubCurrent(_getSubCurrent() + 1)
return getSubCurrent()
}
/**
* Web common z-index style management
*/
const DomZIndex = {
setCurrent,
getCurrent,
getNext,
setSubCurrent,
getSubCurrent,
getSubNext,
getMax: getDomMaxZIndex
}
updateVar()
export default DomZIndex