-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.js
268 lines (184 loc) · 6.1 KB
/
main.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
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
/**
* Creates an element from a HTML string.
* @param {String} html
* @param {?Boolean} children - Return all children instead of the first one.
* @returns {Node}
*/
const toElement = function(html, children = false) {
const elem = document.createElement('div')
elem.innerHTML = html.trim()
return children === true ? elem.children : elem.firstChild
}
/**
* Validates and converts content.
* @param {Node|String} content
* @returns {Array} content - Validated content.
*/
const validateContent = function(content) {
const isString = typeof content === 'string'
const isHTMLElement = content instanceof HTMLElement === true
if (isString === false && isHTMLElement === false) {
throw new Error('Content must be a DOM element/node or string')
}
if (isString === true) {
// String
return Array.from(toElement(content, true))
} else if (content.tagName === 'TEMPLATE') {
// Template
return [ content.content.cloneNode(true) ]
} else {
// HTMLElement
return Array.from(content.children)
}
}
/**
* Validates options and sets defaults for undefined properties.
* @param {?Object} opts
* @returns {Object} opts - Validated options.
*/
const validateOptions = function(opts = {}) {
opts = Object.assign({}, opts)
if (opts.closable == null) opts.closable = true
if (opts.className == null) opts.className = ''
if (opts.onShow == null) opts.onShow = () => {}
if (opts.onClose == null) opts.onClose = () => {}
if (typeof opts.closable !== 'boolean') throw new Error('Property `closable` must be a boolean')
if (typeof opts.className !== 'string') throw new Error('Property `className` must be a string')
if (typeof opts.onShow !== 'function') throw new Error('Property `onShow` must be a function')
if (typeof opts.onClose !== 'function') throw new Error('Property `onClose` must be a function')
return opts
}
/**
* Checks if an element's first child has a specific tag.
* @param {Node} elem
* @param {String} tag
* @returns {Boolean} containsTag
*/
const containsTag = function(elem, tag) {
const children = elem.children
return (children.length === 1 && children[0].tagName === tag)
}
/**
* Checks if a given or any lightbox is visible.
* @param {?Node} elem
* @returns {Boolean} visible
*/
export const visible = function(elem) {
elem = elem || document.querySelector('.basicLightbox')
return (elem != null && elem.ownerDocument.body.contains(elem) === true)
}
/**
* Creates a lightbox element.
* @param {Array} content
* @param {Object} opts
* @returns {Node} elem
*/
const render = function(content, opts) {
const elem = toElement(`
<div class="basicLightbox ${ opts.className }">
<div class="basicLightbox__placeholder" role="dialog"></div>
</div>
`)
const placeholder = elem.querySelector('.basicLightbox__placeholder')
// Move content into lightbox placeholder
content.forEach((child) => placeholder.appendChild(child))
// Check if placeholder contains a tag that requires a special treatment
const img = containsTag(placeholder, 'IMG')
const video = containsTag(placeholder, 'VIDEO')
const iframe = containsTag(placeholder, 'IFRAME')
// Add special treatment class when it only contains an image, a video or iframe.
// This class is necessary to center the image, video or iframe.
if (img === true) elem.classList.add('basicLightbox--img')
if (video === true) elem.classList.add('basicLightbox--video')
if (iframe === true) elem.classList.add('basicLightbox--iframe')
return elem
}
/**
* Shows a lightbox by appending an element to the DOM.
* @param {Node} elem
* @param {Function} next - The callback that gets executed when the lightbox starts to show up.
* @returns {Boolean} success
*/
const show = function(elem, next) {
document.body.appendChild(elem)
// Wait a while to ensure that the class change triggers the animation
setTimeout(() => {
requestAnimationFrame(() => {
elem.classList.add('basicLightbox--visible')
return next()
})
}, 10)
return true
}
/**
* Closes a lightbox by fading the element out and by removing the element from the DOM.
* @param {Node} elem
* @param {Function} next - The callback that gets executed when the lightbox is fully closed.
* @returns {Boolean} success
*/
const close = function(elem, next) {
elem.classList.remove('basicLightbox--visible')
setTimeout(() => {
// Don't continue to remove lightbox when element missing
if (visible(elem) === false) return next()
elem.parentElement.removeChild(elem)
return next()
}, 410)
return true
}
/**
* Creats a new instance.
* @param {Node|String} content
* @param {?Object} opts
* @returns {Object} instance
*/
export const create = function(content, opts) {
content = validateContent(content)
opts = validateOptions(opts)
// Render the lightbox element
const elem = render(content, opts)
// Returns the lightbox element
const _element = () => {
return elem
}
// Check if the lightbox is attached to the DOM
const _visible = () => {
return visible(elem)
}
// Show the lightbox
const _show = (next) => {
// Run onShow callback and stop execution when function returns false
if (opts.onShow(instance) === false) return false
// Show the lightbox
return show(elem, () => {
// Continue with the callback when available
if (typeof next === 'function') return next(instance)
})
}
// Hide the lightbox
const _close = (next) => {
// Run onClose callback and stop execution when function returns false
if (opts.onClose(instance) === false) return false
return close(elem, () => {
// Continue with the callback when available
if (typeof next === 'function') return next(instance)
})
}
// Close lightbox when clicking the background
if (opts.closable === true) elem.addEventListener('click', (e) => {
// If e.target is not the same element as elem,
// then the user clicked a descendant of the element.
if (e.target !== elem) return
// Close lightbox with the instance function
_close()
})
// Assign instance to a variable so the instance can be used
// elsewhere in the current function.
const instance = {
element: _element,
visible: _visible,
show: _show,
close: _close
}
return instance
}