-
Notifications
You must be signed in to change notification settings - Fork 0
/
popups.js
237 lines (196 loc) · 7.76 KB
/
popups.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const stateHidden = 0;
const stateShown = 1;
const popupList = {}
let popupTop = 0
let curActor = 0
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function registerPopup(popup) {
const id = popupTop++
popupList[id] = {
popup,
state: stateHidden,
action: {},
ownerStates: {}, ownerActions: {},
openedArgElements: [],
__stateChangeCallbacks: [],
set onStateChange(callback) { this.__stateChangeCallbacks.push(callback) },
}
popup.safeZone.setAttribute('data-popup-id', id)
return id
}
function unregisterPopup(id) {
delete popupList[id]
}
function addOpenedArgumentToElement(id, name, element) {
if(name.includes('$')) {
console.error('popup open argument name `' + name + '` contains special symbols')
name.replace('$', '!@#%^&*()')
}
popupList[id].openedArgElements.push([element, '$' + name + '$']);
}
function addOwner(owner, id) {
popupList[id].ownerStates[owner] = stateHidden;
}
async function updatePopupAfterMs(owner, id, state, delay) {
const item = popupList[id]
const action = item.ownerActions[owner]
if(delay <= 0) { updatePopup(owner, id, state); return }
const end = new Date()
end.setMilliseconds(end.getMilliseconds() + delay)
if(action && action.desired === state && end >= action.time) return
else if(!action && item.ownerStates[owner] === state) return
const me = curActor++
item.ownerActions[owner] = { actor: me, owner: owner, desired: state, time: end }
//console.log('popup ' + id + ' ' + owner + ' started ' + (state === stateShown ? 'showing' : 'hiding'))
await timeout(delay)
const newAction = item.ownerActions[owner]
if(newAction && newAction.actor === me) updatePopup(owner, id, state);
}
function updatePopup(owner, id, newState) {
const item = popupList[id]
if(item.ownerStates[owner] == undefined) return;
item.ownerStates[owner] = newState
delete item.ownerActions[owner]
const oldState = item.state
if(newState == stateHidden) {
item.state = stateHidden
for(const owner in item.ownerStates) if(item.ownerStates[owner] === stateShown) {
item.state = stateShown
break
}
}
else item.state = newState
if(item.state === oldState) return
const opened = item.state === stateShown
item.popup.element.setAttribute('data-shown', opened)
for(let i = 0; i < popupList[id].openedArgElements.length; i++) {
const [el, name] = popupList[id].openedArgElements[i]
if(el) {
let attrs = el.getAttribute('data-popup-opened') ?? ''
if(opened && !attrs.includes(name)) attrs = attrs + name
else if(!opened && attrs.includes(name)) attrs = attrs.replace(name, '')
if(attrs != '') el.setAttribute('data-popup-opened', attrs)
else el.removeAttribute('data-popup-opened')
}
}
//console.log('popup ' + id + (opened ? ' opened' : ' hidden'))
try {
for(let i = 0; i < item.__stateChangeCallbacks.length; i++) {
try { item.__stateChangeCallbacks[i](id) } catch(e) { console.error(e) }
}
} catch(e) { console.error(e) }
}
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function popupAddHoverClick(id, onElement, whenToggled) {
addOwner('hover', id)
addOwner('click', id)
addOwner('focus', id)
addOwner('safe zone', id)
const item = popupList[id]
const popupEl = item.popup.element
popupEl.addEventListener('focusin', () => {
updatePopup('focus', id, stateShown)
})
popupEl.addEventListener('focusout', () => {
if(popupEl.contains(document.activeElement)) return; /*switching tabs or something*/
updatePopup('focus', id, stateHidden)
})
let keepPopupOpen = false, ignoreHover = false
addClick(onElement, () => {
keepPopupOpen = !keepPopupOpen;
if(!window.matchMedia('(pointer: fine)').matches) {
ignoreHover = true
updatePopup('hover', id, stateHidden)
}
else ignoreHover = false
if(keepPopupOpen) updatePopup('click', id, stateShown)
else updatePopup('click', id, stateHidden)
whenToggled(keepPopupOpen)
})
//check if element is already under the mouse
if(onElement.matches(':hover')/*I hope this is equivalent to mouseenter*/) {
if(!ignoreHover) updatePopupAfterMs('hover', id, stateShown, 300)
}
onElement.addEventListener('mouseenter', () => {
if(!ignoreHover) updatePopupAfterMs('hover', id, stateShown, 300)
})
onElement.addEventListener('mouseleave', () => {
if(!ignoreHover) updatePopupAfterMs('hover', id, stateHidden, 500)
})
item.onStateChange = (id) => fixPopupXPosition(id)
}
window.addEventListener('mousemove', function(ev) {
if(popupList.length === 0) return
if(!window.matchMedia('(pointer: fine)').matches) {
for(const i in popupList) {
updatePopup('safe zone', i, stateHidden)
}
return
}
const x = ev.clientX
const y = ev.clientY
for(const i in popupList) {
const pp = popupList[i]
if(pp.state !== stateShown) /*
should this be removed? Popup has hiding transition,
and while it is playing the popup is technically hidden
so the user can't hover back on it to reopen it.
But neither feels good. And this variant is more safe,
because the safe area migth not collapse some day,
and this check would prevent a completely hidden popup from
showing up when the mouse is over it
*/ continue;
const bs = pp.popup.safeZone.getBoundingClientRect()
const hovered = x === clamp(x, bs.left, bs.right)
&& y === clamp(y, bs.top, bs.bottom)
if(hovered) updatePopup('safe zone', i, stateShown)
else updatePopupAfterMs('safe zone', i, stateHidden, 500)
}
})
function isHidden(elem) { //not position: fixed !
//jQuery
return !( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
}
//does not really belong to popups, fix for popups being outside of screen bounds + 'overflow: hidden' on body
//It doesn't work properly because it is not called when popup's position or size changes, so it may
//still go out of bounds.
//TODO: move popups to separate div and put two 1px divs in popup container instead of popup itself,
//one positioned normally and another absolutly on top of it and use IntersectionObserver to detect
//position changes + ResizeObserver for the popup
window.addEventListener('resize', function() {
for(const i in popupList) fixPopupXPosition(i)
})
function fixPopupXPosition(id) {
const windowWidth = window.innerWidth
const horPadding = windowWidth * 0.02
const allowedWidth = windowWidth - horPadding*2
const pp = popupList[id]
const popup = pp.popup.popup
if(isHidden(popup)) return
//if(pp.state !== stateShown) continue; //hiding transition would probably break?
pp.popup.safeZone.style.transform = ''
popup.style.width = ''
popup.style.overflowX = ''
const bs = popup.getBoundingClientRect()
const popupWidth = bs.right - bs.left
if(popupWidth > allowedWidth) {
pp.popup.safeZone.style.transform = `translateX(${(windowWidth - bs.right - bs.left)*0.5}px)`
popup.style.width = allowedWidth + 'px'
popup.style.overflowX = 'scroll'
return
}
const offR = horPadding + allowedWidth - bs.right
if(offR < 0) {
pp.popup.safeZone.style.transform = `translateX(${offR}px)`
return
}
const offL = horPadding - bs.left
if(offL > 0) {
pp.popup.safeZone.style.transform = `translateX(${offL}px)`
return
}
}