-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodally.mjs
459 lines (375 loc) · 14.4 KB
/
modally.mjs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import {
getScrollbarWidth,
disableScroll,
enableScroll,
shallowMerge,
parseDOM,
stringToType,
css,
isString,
detachElement,
isEmpty,
fadeIn,
fadeOut,
hashChange,
getHashProperties,
isFunction,
transformDashToCamelCase,
transformCamelCaseToDash,
serializeUrlParameters,
pick,
RE_VIDEO,
RE_YOUTUBE,
RE_VIMEO,
on,
} from "book-of-spells"
export class Modal {
constructor(id, contentElement, options = {}, modallyInstance) {
this.id = id
this.element = contentElement
this.modallyInstance = modallyInstance
this.opened = false
// https://www.youtube.com/watch?v=gJ-WmYn_9GE
this.videoRegEx = {};
this.videoRegEx.YOUTUBE = RE_YOUTUBE
this.videoRegEx.VIMEO = RE_VIMEO
this.videoRegEx.VIDEO = RE_VIDEO
//TODO: add support for brightcove and cloudfront. or not?
this.options = {
disableScroll: true,
landing: document.body,
maxWidth: 'none',
classes: '',
verticalAlign: 'middle',
closeParent: false,
closeOthers: false,
enableHashChange: true,
closeOthersOnHashChange: false,
updateHash: false,
image: false,
video: false,
autoplay: true,
scrollToTop: true,
template: `
<div class="modally-wrap">
<div class="modally-table">
<div class="modally-cell">
<div class="modally-underlay modally-close"></div>
<div class="modally" role="dialog" aria-modal="true">
<button tabindex="1" class="modally-close modally-close-button" aria-label="Close">×</button>
<div class="modally-content"></div>
</div>
</div>
</div>
</div>`,
}
const landing = this.options.hasOwnProperty('landing') && this.options.landing instanceof HTMLElement ? this.options.landing : document.querySelector(this.options.landing)
if (!landing) return
// transform camelCase to dash-case
if (options) {
const tempOptions = {}
for (const k in options) {
const key = /^[a-z0-9]+$/i.test(k) ? k : transformDashToCamelCase(k)
tempOptions[key] = options[k]
}
options = tempOptions
}
shallowMerge(this.options, options)
this.template = this.options.template instanceof Element || this.options.template instanceof NodeList ? this.options.template : parseDOM(this.options.template)
if (this.element) {
for (const k in this.options) {
const key = /^[a-z0-9]+$/.test(k) ? k : transformCamelCaseToDash(k)
if (this.element.hasAttribute(`modally-${key}`)) {
this.options[k] = stringToType(this.element.getAttribute(`modally-${key}`))
}
}
if (this.element.hasAttribute('id')) this.element.removeAttribute('id')
}
this.template.setAttribute('id', this.id)
const modallyElement = this.template.querySelector('.modally')
if (modallyElement) {
css(modallyElement, {
'maxWidth': `${this.options.maxWidth}px`
})
}
const modallyCellElement = this.template.querySelector('.modally-cell')
if (modallyCellElement) {
css(modallyCellElement, {
'verticalAlign': this.options.verticalAlign
})
}
if (!isEmpty(this.options.classes))
this.template.classList.add(this.options.classes)
// Setup modal types. There are 3 types: video, image and default (no type).
if (this.options.video) this.setupVideoLanding()
else if (this.options.image) this.setupImageLanding()
else {
if (this.element) {
const ghost = detachElement(this.element)
this.template.querySelector('.modally-content').appendChild(ghost)
}
}
if (this.element) {
this.element.style.display = ''
this.element.removeAttribute('hidden')
}
this.template.querySelectorAll('.modally-close').forEach((el) => {
el.addEventListener('click', (e) => {
e.preventDefault()
this.modallyInstance.close(this, e.target)
})
})
landing.appendChild(this.template)
this.zIndex = window.getComputedStyle(this.template).zIndex
}
setupVideoLanding() {
const spacer = parseDOM('<svg aria-hidden="true" style="width: 100%; height: auto; display: block;" width="1920" height="1080"></svg>')
const ymod = this.options.autoplay ? 'autoplay=1&' : ''
const vmod = this.options.autoplay ? 'autoplay=1' : ''
const vidmod = this.options.autoplay ? ' autoplay' : ''
const embeds = parseDOM(`
<div hidden>
<iframe hidden class="youtube embed-template template" data-src="https://www.youtube.com/embed/{ID}?${ymod}autohide=1&fs=1&rel=0&hd=1&wmode=opaque&enablejsapi=1" type="text/html" width="1920" height="1080" allow="autoplay" frameborder="0" vspace="0" hspace="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" scrolling="auto"></iframe>
<iframe hidden class="vimeo embed-template template" title="vimeo-player" data-src="https://player.vimeo.com/video/{ID}?${vmod}" type="text/html" width="1920" height="1080" allow="autoplay; allowfullscreen" rameborder="0" allowfullscreen=""></iframe>
<video hidden height="1920" width="1080" class="video embed-template template" data-src="{ID}" controls playsinline${vidmod}></video>
</div>
`)
const landing = parseDOM('<div class="iframe-landing"></div>')
landing.appendChild(spacer)
this.template.querySelector('.modally-content').appendChild(landing)
this.template.appendChild(embeds)
this.template.classList.add('video-embed')
}
setupImageLanding() {
const spacer = parseDOM('<div class="image-landing"><img style="width: 100%; height: auto;" decoding="async" loading="lazy" alt=""></div>')
this.template.querySelector('.modally-content').appendChild(spacer)
this.template.classList.add('image-embed')
}
getVideoId(link) {
for (const k in this.videoRegEx) {
const match = link.match(this.videoRegEx[k])
if (match) {
const type = k.toLowerCase()
return {
type: type,
id: type === 'video' ? link : match[1]
}
}
this.videoRegEx[k].lastIndex = 0
}
return false
}
mountVideo(link) {
const vidData = this.getVideoId(link)
let template = this.template.querySelector(`.embed-template.template.${vidData.type}`)
const landing = this.template.querySelector('.modally-content .iframe-landing')
if (!template || !landing) return
template = template.cloneNode(true)
template.setAttribute('src', template.getAttribute('data-src').replace('{ID}', vidData.id))
template.style.display = 'block'
template.removeAttribute('hidden')
landing.appendChild(template)
}
unmountVideo() {
const iframe = this.template.querySelector('.modally-content iframe, .modally-content video')
if (iframe) iframe.remove()
}
mountImage(link, width, height, srcset) {
const img = this.template.querySelector('.modally-content img')
if (!img) return
img.setAttribute('src', link)
img.style.display = 'block'
img.removeAttribute('hidden')
if (srcset) img.setAttribute('srcset', srcset)
if (width) img.setAttribute('width', width)
if (height) img.setAttribute('height', height)
}
open(target, callback) {
if (this.opened) return
this.opened = true
const dataset = target instanceof HTMLElement ? target.dataset : target // target can either be an element with a dataset or a dataset object itself. This way an object can be passed from the url hash variables
if (this.options.video && dataset && dataset.hasOwnProperty('video')) {
this.mountVideo(dataset.video)
}
if (this.options.image && dataset && dataset.hasOwnProperty('image')) {
this.mountImage(dataset.image, dataset.width, dataset.height, dataset.srcset)
}
document.body.classList.add(`modally-${this.id}`)
fadeIn(this.template, () => {
if (isFunction(callback)) callback(this)
}, (elem) => {
if (this.options.scrollToTop) elem.scrollTop = 0
})
return true
}
close(target, callback) {
if (!this.opened) return
this.opened = false
fadeOut(this.template, () => {
if (this.options.video) this.unmountVideo()
document.body.classList.remove(`modally-${this.id}`)
if (isFunction(callback)) callback(this)
css(this.template, {
'zIndex': this.zIndex
})
})
return true
}
dispatchEvents(eventName, target) {
this.target = target
if (this.element) this.element.dispatchEvent(new CustomEvent(`modally:${eventName}`, { detail: this }))
this.template.dispatchEvent(new CustomEvent(`modally:${eventName}`, { detail: this }))
document.dispatchEvent(new CustomEvent(`modally:${eventName}`, { detail: this }))
document.dispatchEvent(new CustomEvent(`modally:${eventName}:${this.id}`, { detail: this }))
}
}
export class Modally {
constructor(options) {
this.index = {}
this.opened = []
this.options = {
selector: null
}
this.scrollbarWidth = getScrollbarWidth()
if (options) shallowMerge(this.options, options)
if (this.options.selector) {
const elements = document.querySelectorAll(this.options.selector)
elements.forEach((el) => {
if (el.hasAttribute('id')) {
// TODO: what if the selector is not only one class, but an attribute? Maybe we don't need to remove anything?
const className = this.options.selector.replace('.', '')
el.classList.remove(className)
const options = { ...this.options, element: el }
delete options.selector // TODO: use book of spells reject to remove the selector property and hash settings
this.add(el.getAttribute('id'), options)
}
})
}
on('[target^="_modal"]', 'click', (e, target) => {
if (!target || target.matches('[disabled]')) return
const targetQuery = target.getAttribute('target').replace('_modal:', '')
const targetQueryParts = targetQuery.split(':')
const href = target.getAttribute('href')
let id, modal
if (href && href.length && href !== '#') {
e.preventDefault()
id = href.replace('#', '')
modal = this.get(id)
if (modal.options.updateHash) {
const getAttributes = pick(target.dataset, ['image', 'video', 'width', 'height', 'srcset']) // TODO: these should be defined in the modal option, one can pass the data attributes and do simple templating in the modal
const hash = serializeUrlParameters(getAttributes)
window.location.hash = `#${id}${hash.length ? `&${hash}` : ''}`
}
}
if (targetQueryParts.length > 1) {
id = targetQueryParts[1]
modal = this.get(id)
}
if (targetQueryParts[0] === 'close') return this.close(modal, target)
if (modal) this.open(modal, target)
})
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
this.close()
}
})
// TODO: This should be a setting also for the modally instance, and there should be a setting to close all modals on hash change regardless if the modal exists or not, clear the hash on modal close. If there is a parent update the hash to the has of the parent... etc...
this.initHashCheck()
}
modallyHashCheck(hash) {
const hashProperties = getHashProperties(hash)
for (const id in hashProperties) {
if (hashProperties[id] !== undefined) continue // skip empty value hash properties
const modal = this.get(id)
if (!modal) continue
if (modal.options.closeOthersOnHashChange) this.closeAll()
if (modal.options.enableHashChange) this.open(id, hashProperties)
}
}
modallyInitialHashCheck(id) {
if (this.options.closeOthersOnHashChange) this.closeAll()
const hashProperties = getHashProperties()
if (!hashProperties.hasOwnProperty(id)) return
const modal = this.get(id)
if (!modal) return
if (modal.options.closeOthersOnHashChange) this.closeAll()
if (modal.options.enableHashChange) this.open(id, hashProperties)
}
add(id, options) {
if (!id) return
if (!options) options = {}
const optionsClone = {...this.options}
delete optionsClone.selector
shallowMerge(optionsClone, options)
options = optionsClone
let element
if (!options.element) {
if (id instanceof HTMLElement) {
element = id
id = id.getAttribute('id')
} else {
element = document.getElementById(id)
}
} else {
element = options.element
delete options.element
}
if (!element && options.selector) {
element = isString(options.selector) ? document.querySelector(options.selector) : options.selector
delete options.selector
}
const modal = new Modal(id, element, options, this)
this.index[id] = modal
modal.dispatchEvents('added')
if (modal.options.enableHashChange) this.modallyInitialHashCheck(id)
return modal
}
get(id) {
return this.index[id]
}
isOpen(id) {
return this.get(id).opened
}
open(id, target) {
const modal = id instanceof Modal ? id : this.get(id)
if (!modal) return
if (modal.options.closeParent) this.close()
if (modal.options.closeOthers) this.closeAll()
if (modal.opened) return
modal.dispatchEvents('open', target)
if (!this.opened.length && modal.options.disableScroll) disableScroll(this.scrollbarWidth)
if (!this.opened.length) document.body.classList.add('modally-open')
css(modal.template, {
'zIndex': modal.zIndex + this.opened.length
})
const isOpened = modal.open(target, () => {
modal.dispatchEvents('opened', target)
})
if (!isOpened) return
this.opened.push(modal)
}
close(id, target) {
if (!id && this.opened.length) {
id = this.opened[this.opened.length - 1]
}
const modal = id instanceof Modal ? id : this.get(id)
if (!modal) return
const isClosed = modal.close(target, () => {
if (!this.opened.length && modal.options.disableScroll) enableScroll(this.scrollbarWidth)
if (!this.opened.length) document.body.classList.remove('modally-open')
modal.dispatchEvents('closed', target)
})
if (!isClosed) return
this.opened.pop()
modal.dispatchEvents('close', target)
}
closeAll() {
[...this.opened].forEach((modal) => this.close(modal))
}
initHashCheck() {
hashChange((hash) => {
this.modallyHashCheck(hash)
}, 'modallyHashCheckListenerInitialized')
}
}
export default Modally