forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathmaterialbox.ts
446 lines (414 loc) · 14.1 KB
/
materialbox.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
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
import { Utils } from './utils';
import { BaseOptions, Component, InitElements, MElement } from './component';
export interface MaterialboxOptions extends BaseOptions {
/**
* Transition in duration in milliseconds.
* @default 275
*/
inDuration: number;
/**
* Transition out duration in milliseconds.
* @default 200
*/
outDuration: number;
/**
* Callback function called before materialbox is opened.
* @default null
*/
onOpenStart: (el: Element) => void;
/**
* Callback function called after materialbox is opened.
* @default null
*/
onOpenEnd: (el: Element) => void;
/**
* Callback function called before materialbox is closed.
* @default null
*/
onCloseStart: (el: Element) => void;
/**
* Callback function called after materialbox is closed.
* @default null
*/
onCloseEnd: (el: Element) => void;
}
const _defaults: MaterialboxOptions = {
inDuration: 275,
outDuration: 200,
onOpenStart: null,
onOpenEnd: null,
onCloseStart: null,
onCloseEnd: null
};
export class Materialbox extends Component<MaterialboxOptions> {
/** If the materialbox overlay is showing. */
overlayActive: boolean;
/** If the materialbox is no longer being animated. */
doneAnimating: boolean;
/** Caption, if specified. */
caption: string;
/** Original width of image. */
originalWidth: number;
/** Original height of image. */
originalHeight: number;
private originInlineStyles: string;
private placeholder: HTMLElement;
private _changedAncestorList: HTMLElement[];
private newHeight: number;
private newWidth: number;
private windowWidth: number;
private windowHeight: number;
private attrWidth: string;
private attrHeight: string;
private _overlay: HTMLElement;
private _photoCaption: HTMLElement;
constructor(el: HTMLElement, options: Partial<MaterialboxOptions>) {
super(el, options, Materialbox);
this.el['M_Materialbox'] = this;
this.options = {
...Materialbox.defaults,
...options
};
this.overlayActive = false;
this.doneAnimating = true;
this.placeholder = document.createElement('div');
this.placeholder.classList.add('material-placeholder');
this.originalWidth = 0;
this.originalHeight = 0;
this.originInlineStyles = this.el.getAttribute('style');
this.caption = this.el.getAttribute('data-caption') || '';
this.el.tabIndex = 0;
// Wrap
this.el.before(this.placeholder);
this.placeholder.append(this.el);
this._setupEventHandlers();
}
static get defaults(): MaterialboxOptions {
return _defaults;
}
/**
* Initializes instance of MaterialBox.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLElement, options?: Partial<MaterialboxOptions>): Materialbox;
/**
* Initializes instances of MaterialBox.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: InitElements<MElement>, options?: Partial<MaterialboxOptions>): Materialbox[];
/**
* Initializes instances of MaterialBox.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: HTMLElement | InitElements<MElement>,
options: Partial<MaterialboxOptions> = {}
): Materialbox | Materialbox[] {
return super.init(els, options, Materialbox);
}
static getInstance(el: HTMLElement): Materialbox {
return el['M_Materialbox'];
}
destroy() {
this._removeEventHandlers();
this.el['M_Materialbox'] = undefined;
// Unwrap image
//this.placeholder.after(this.el).remove();
this.placeholder.remove();
this.el.removeAttribute('style');
}
private _setupEventHandlers() {
this.el.addEventListener('click', this._handleMaterialboxClick);
this.el.addEventListener('keypress', this._handleMaterialboxKeypress);
}
private _removeEventHandlers() {
this.el.removeEventListener('click', this._handleMaterialboxClick);
this.el.removeEventListener('keypress', this._handleMaterialboxKeypress);
}
private _handleMaterialboxClick = () => {
this._handleMaterialboxToggle();
};
private _handleMaterialboxKeypress = (e: KeyboardEvent) => {
if (Utils.keys.ENTER.includes(e.key)) {
this._handleMaterialboxToggle();
}
};
private _handleMaterialboxToggle = () => {
// If already modal, return to original
if (this.doneAnimating === false || (this.overlayActive && this.doneAnimating)) this.close();
else this.open();
};
private _handleWindowScroll = () => {
if (this.overlayActive) this.close();
};
private _handleWindowResize = () => {
if (this.overlayActive) this.close();
};
private _handleWindowEscape = (e: KeyboardEvent) => {
if (Utils.keys.ESC.includes(e.key) && this.doneAnimating && this.overlayActive) this.close();
};
private _makeAncestorsOverflowVisible() {
this._changedAncestorList = [];
let ancestor = this.placeholder.parentNode;
while (ancestor !== null && ancestor !== document) {
const curr = <HTMLElement>ancestor;
if (curr.style.overflow !== 'visible') {
curr.style.overflow = 'visible';
this._changedAncestorList.push(curr);
}
ancestor = ancestor.parentNode;
}
}
private _offset(el: HTMLElement) {
const box = el.getBoundingClientRect();
const docElem = document.documentElement;
return {
top: box.top + window.scrollY - docElem.clientTop,
left: box.left + window.scrollX - docElem.clientLeft
};
}
private _updateVars(): void {
this.windowWidth = window.innerWidth;
this.windowHeight = window.innerHeight;
this.caption = this.el.getAttribute('data-caption') || '';
}
// Image
private _animateImageIn(): void {
this.el.style.maxHeight = this.newHeight.toString() + 'px';
this.el.style.maxWidth = this.newWidth.toString() + 'px';
const duration = this.options.inDuration;
// from
this.el.style.transition = 'none';
this.el.style.height = this.originalHeight + 'px';
this.el.style.width = this.originalWidth + 'px';
setTimeout(() => {
// easeOutQuad
this.el.style.transition = `height ${duration}ms ease,
width ${duration}ms ease,
left ${duration}ms ease,
top ${duration}ms ease
`;
// to
this.el.style.height = this.newHeight + 'px';
this.el.style.width = this.newWidth + 'px';
this.el.style.left =
Utils.getDocumentScrollLeft() +
this.windowWidth / 2 -
this._offset(this.placeholder).left -
this.newWidth / 2 +
'px';
this.el.style.top =
Utils.getDocumentScrollTop() +
this.windowHeight / 2 -
this._offset(this.placeholder).top -
this.newHeight / 2 +
'px';
}, 1);
setTimeout(() => {
this.doneAnimating = true;
if (typeof this.options.onOpenEnd === 'function') this.options.onOpenEnd.call(this, this.el);
}, duration);
/*
anim({
targets: this.el, // image
height: [this.originalHeight, this.newHeight],
width: [this.originalWidth, this.newWidth],
left:
Utils.getDocumentScrollLeft() +
this.windowWidth / 2 -
this._offset(this.placeholder).left -
this.newWidth / 2,
top:
Utils.getDocumentScrollTop() +
this.windowHeight / 2 -
this._offset(this.placeholder).top -
this.newHeight / 2,
duration: this.options.inDuration,
easing: 'easeOutQuad',
complete: () => {
this.doneAnimating = true;
if (typeof this.options.onOpenEnd === 'function') this.options.onOpenEnd.call(this, this.el);
}
});
*/
}
private _animateImageOut(): void {
const duration = this.options.outDuration;
// easeOutQuad
this.el.style.transition = `height ${duration}ms ease,
width ${duration}ms ease,
left ${duration}ms ease,
top ${duration}ms ease
`;
// to
this.el.style.height = this.originalWidth + 'px';
this.el.style.width = this.originalWidth + 'px';
this.el.style.left = '0';
this.el.style.top = '0';
setTimeout(() => {
this.placeholder.style.height = '';
this.placeholder.style.width = '';
this.placeholder.style.position = '';
this.placeholder.style.top = '';
this.placeholder.style.left = '';
// Revert to width or height attribute
if (this.attrWidth) this.el.setAttribute('width', this.attrWidth.toString());
if (this.attrHeight) this.el.setAttribute('height', this.attrHeight.toString());
this.el.removeAttribute('style');
if (this.originInlineStyles) this.el.setAttribute('style', this.originInlineStyles);
// Remove class
this.el.classList.remove('active');
this.doneAnimating = true;
// Remove overflow overrides on ancestors
this._changedAncestorList.forEach((anchestor) => (anchestor.style.overflow = ''));
// onCloseEnd callback
if (typeof this.options.onCloseEnd === 'function')
this.options.onCloseEnd.call(this, this.el);
}, duration);
}
// Caption
private _addCaption(): void {
this._photoCaption = document.createElement('div');
this._photoCaption.classList.add('materialbox-caption');
this._photoCaption.innerText = this.caption;
document.body.append(this._photoCaption);
this._photoCaption.style.display = 'inline';
// Animate
this._photoCaption.style.transition = 'none';
this._photoCaption.style.opacity = '0';
const duration = this.options.inDuration;
setTimeout(() => {
this._photoCaption.style.transition = `opacity ${duration}ms ease`;
this._photoCaption.style.opacity = '1';
}, 1);
}
private _removeCaption(): void {
const duration = this.options.outDuration;
this._photoCaption.style.transition = `opacity ${duration}ms ease`;
this._photoCaption.style.opacity = '0';
setTimeout(() => {
this._photoCaption.remove();
}, duration);
}
// Overlay
private _addOverlay(): void {
this._overlay = document.createElement('div');
this._overlay.id = 'materialbox-overlay';
this._overlay.addEventListener(
'click',
() => {
if (this.doneAnimating) this.close();
},
{ once: true }
);
// Put before in origin image to preserve z-index layering.
this.el.before(this._overlay);
// Set dimensions if needed
const overlayOffset = this._overlay.getBoundingClientRect();
this._overlay.style.width = this.windowWidth + 'px';
this._overlay.style.height = this.windowHeight + 'px';
this._overlay.style.left = -1 * overlayOffset.left + 'px';
this._overlay.style.top = -1 * overlayOffset.top + 'px';
// Animate
this._overlay.style.transition = 'none';
this._overlay.style.opacity = '0';
const duration = this.options.inDuration;
setTimeout(() => {
this._overlay.style.transition = `opacity ${duration}ms ease`;
this._overlay.style.opacity = '1';
}, 1);
}
private _removeOverlay(): void {
const duration = this.options.outDuration;
this._overlay.style.transition = `opacity ${duration}ms ease`;
this._overlay.style.opacity = '0';
setTimeout(() => {
this.overlayActive = false;
this._overlay.remove();
}, duration);
}
/**
* Open materialbox.
*/
open = () => {
this._updateVars();
this.originalWidth = this.el.getBoundingClientRect().width;
this.originalHeight = this.el.getBoundingClientRect().height;
// Set states
this.doneAnimating = false;
this.el.classList.add('active');
this.overlayActive = true;
// onOpenStart callback
if (typeof this.options.onOpenStart === 'function')
this.options.onOpenStart.call(this, this.el);
// Set positioning for placeholder
this.placeholder.style.width = this.placeholder.getBoundingClientRect().width + 'px';
this.placeholder.style.height = this.placeholder.getBoundingClientRect().height + 'px';
this.placeholder.style.position = 'relative';
this.placeholder.style.top = '0';
this.placeholder.style.left = '0';
this._makeAncestorsOverflowVisible();
// Set css on origin
this.el.style.position = 'absolute';
this.el.style.zIndex = '1000';
this.el.style.willChange = 'left, top, width, height';
// Change from width or height attribute to css
this.attrWidth = this.el.getAttribute('width');
this.attrHeight = this.el.getAttribute('height');
if (this.attrWidth) {
this.el.style.width = this.attrWidth + 'px';
this.el.removeAttribute('width');
}
if (this.attrHeight) {
this.el.style.width = this.attrHeight + 'px';
this.el.removeAttribute('height');
}
this._addOverlay();
// Add and animate caption if it exists
if (this.caption !== '') this._addCaption();
// Resize Image
const widthPercent = this.originalWidth / this.windowWidth;
const heightPercent = this.originalHeight / this.windowHeight;
this.newWidth = 0;
this.newHeight = 0;
if (widthPercent > heightPercent) {
// Width first
const ratio = this.originalHeight / this.originalWidth;
this.newWidth = this.windowWidth * 0.9;
this.newHeight = this.windowWidth * 0.9 * ratio;
} else {
// Height first
const ratio = this.originalWidth / this.originalHeight;
this.newWidth = this.windowHeight * 0.9 * ratio;
this.newHeight = this.windowHeight * 0.9;
}
this._animateImageIn();
// Handle Exit triggers
window.addEventListener('scroll', this._handleWindowScroll);
window.addEventListener('resize', this._handleWindowResize);
window.addEventListener('keyup', this._handleWindowEscape);
};
/**
* Close materialbox.
*/
close = () => {
this._updateVars();
this.doneAnimating = false;
// onCloseStart callback
if (typeof this.options.onCloseStart === 'function')
this.options.onCloseStart.call(this, this.el);
//anim.remove(this.el);
//anim.remove(this._overlay);
//if (this.caption !== '') anim.remove(this._photoCaption);
// disable exit handlers
window.removeEventListener('scroll', this._handleWindowScroll);
window.removeEventListener('resize', this._handleWindowResize);
window.removeEventListener('keyup', this._handleWindowEscape);
this._removeOverlay();
this._animateImageOut();
if (this.caption !== '') this._removeCaption();
};
}