forked from foundation/foundation-sites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.reveal.js
632 lines (563 loc) · 17.4 KB
/
foundation.reveal.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
'use strict';
import $ from 'jquery';
import { Plugin } from './foundation.core.plugin';
import { onLoad } from './foundation.core.utils';
import { Keyboard } from './foundation.util.keyboard';
import { MediaQuery } from './foundation.util.mediaQuery';
import { Motion } from './foundation.util.motion';
import { Triggers } from './foundation.util.triggers';
import { Touch } from './foundation.util.touch'
/**
* Reveal module.
* @module foundation.reveal
* @requires foundation.util.keyboard
* @requires foundation.util.touch
* @requires foundation.util.triggers
* @requires foundation.util.mediaQuery
* @requires foundation.util.motion if using animations
*/
class Reveal extends Plugin {
/**
* Creates a new instance of Reveal.
* @class
* @name Reveal
* @param {jQuery} element - jQuery object to use for the modal.
* @param {Object} options - optional parameters.
*/
_setup(element, options) {
this.$element = element;
this.options = $.extend({}, Reveal.defaults, this.$element.data(), options);
this.className = 'Reveal'; // ie9 back compat
this._init();
// Touch and Triggers init are idempotent, just need to make sure they are initialized
Touch.init($);
Triggers.init($);
Keyboard.register('Reveal', {
'ESCAPE': 'close',
});
}
/**
* Initializes the modal by adding the overlay and close buttons, (if selected).
* @private
*/
_init() {
MediaQuery._init();
this.id = this.$element.attr('id');
this.isActive = false;
this.cached = {mq: MediaQuery.current};
this.$anchor = $(`[data-open="${this.id}"]`).length ? $(`[data-open="${this.id}"]`) : $(`[data-toggle="${this.id}"]`);
this.$anchor.attr({
'aria-controls': this.id,
'aria-haspopup': true,
'tabindex': 0
});
if (this.options.fullScreen || this.$element.hasClass('full')) {
this.options.fullScreen = true;
this.options.overlay = false;
}
if (this.options.overlay && !this.$overlay) {
this.$overlay = this._makeOverlay(this.id);
}
this.$element.attr({
'role': 'dialog',
'aria-hidden': true,
'data-yeti-box': this.id,
'data-resize': this.id
});
if(this.$overlay) {
this.$element.detach().appendTo(this.$overlay);
} else {
this.$element.detach().appendTo($(this.options.appendTo));
this.$element.addClass('without-overlay');
}
this._events();
if (this.options.deepLink && window.location.hash === ( `#${this.id}`)) {
this.onLoadListener = onLoad($(window), () => this.open());
}
}
/**
* Creates an overlay div to display behind the modal.
* @private
*/
_makeOverlay() {
var additionalOverlayClasses = '';
if (this.options.additionalOverlayClasses) {
additionalOverlayClasses = ' ' + this.options.additionalOverlayClasses;
}
return $('<div></div>')
.addClass('reveal-overlay' + additionalOverlayClasses)
.appendTo(this.options.appendTo);
}
/**
* Updates position of modal
* TODO: Figure out if we actually need to cache these values or if it doesn't matter
* @private
*/
_updatePosition() {
var width = this.$element.outerWidth();
var outerWidth = $(window).width();
var height = this.$element.outerHeight();
var outerHeight = $(window).height();
var left, top = null;
if (this.options.hOffset === 'auto') {
left = parseInt((outerWidth - width) / 2, 10);
} else {
left = parseInt(this.options.hOffset, 10);
}
if (this.options.vOffset === 'auto') {
if (height > outerHeight) {
top = parseInt(Math.min(100, outerHeight / 10), 10);
} else {
top = parseInt((outerHeight - height) / 4, 10);
}
} else if (this.options.vOffset !== null) {
top = parseInt(this.options.vOffset, 10);
}
if (top !== null) {
this.$element.css({top: top + 'px'});
}
// only worry about left if we don't have an overlay or we have a horizontal offset,
// otherwise we're perfectly in the middle
if (!this.$overlay || (this.options.hOffset !== 'auto')) {
this.$element.css({left: left + 'px'});
this.$element.css({margin: '0px'});
}
}
/**
* Adds event handlers for the modal.
* @private
*/
_events() {
var _this = this;
this.$element.on({
'open.zf.trigger': this.open.bind(this),
'close.zf.trigger': (event, $element) => {
if ((event.target === _this.$element[0]) ||
($(event.target).parents('[data-closable]')[0] === $element)) { // only close reveal when it's explicitly called
return this.close.apply(this);
}
},
'toggle.zf.trigger': this.toggle.bind(this),
'resizeme.zf.trigger': function() {
_this._updatePosition();
}
});
if (this.options.closeOnClick && this.options.overlay) {
this.$overlay.off('.zf.reveal').on('click.zf.dropdown tap.zf.dropdown', function(e) {
if (e.target === _this.$element[0] ||
$.contains(_this.$element[0], e.target) ||
!$.contains(document, e.target)) {
return;
}
_this.close();
});
}
if (this.options.deepLink) {
$(window).on(`hashchange.zf.reveal:${this.id}`, this._handleState.bind(this));
}
}
/**
* Handles modal methods on back/forward button clicks or any other event that triggers hashchange.
* @private
*/
_handleState(e) {
if(window.location.hash === ( '#' + this.id) && !this.isActive){ this.open(); }
else{ this.close(); }
}
/**
* Disables the scroll when Reveal is shown to prevent the background from shifting
* @param {number} scrollTop - Scroll to visually apply, window current scroll by default
*/
_disableScroll(scrollTop) {
scrollTop = scrollTop || $(window).scrollTop();
if ($(document).height() > $(window).height()) {
$("html")
.css("top", -scrollTop);
}
}
/**
* Reenables the scroll when Reveal closes
* @param {number} scrollTop - Scroll to restore, html "top" property by default (as set by `_disableScroll`)
*/
_enableScroll(scrollTop) {
scrollTop = scrollTop || parseInt($("html").css("top"));
if ($(document).height() > $(window).height()) {
$("html")
.css("top", "");
$(window).scrollTop(-scrollTop);
}
}
/**
* Opens the modal controlled by `this.$anchor`, and closes all others by default.
* @function
* @fires Reveal#closeme
* @fires Reveal#open
*/
open() {
// either update or replace browser history
const hash = `#${this.id}`;
if (this.options.deepLink && window.location.hash !== hash) {
if (window.history.pushState) {
if (this.options.updateHistory) {
window.history.pushState({}, '', hash);
} else {
window.history.replaceState({}, '', hash);
}
} else {
window.location.hash = hash;
}
}
// Remember anchor that opened it to set focus back later, have general anchors as fallback
this.$activeAnchor = $(document.activeElement).is(this.$anchor) ? $(document.activeElement) : this.$anchor;
this.isActive = true;
// Make elements invisible, but remove display: none so we can get size and positioning
this.$element
.css({ 'visibility': 'hidden' })
.show()
.scrollTop(0);
if (this.options.overlay) {
this.$overlay.css({'visibility': 'hidden'}).show();
}
this._updatePosition();
this.$element
.hide()
.css({ 'visibility': '' });
if(this.$overlay) {
this.$overlay.css({'visibility': ''}).hide();
if(this.$element.hasClass('fast')) {
this.$overlay.addClass('fast');
} else if (this.$element.hasClass('slow')) {
this.$overlay.addClass('slow');
}
}
if (!this.options.multipleOpened) {
/**
* Fires immediately before the modal opens.
* Closes any other modals that are currently open
* @event Reveal#closeme
*/
this.$element.trigger('closeme.zf.reveal', this.id);
}
this._disableScroll();
var _this = this;
// Motion UI method of reveal
if (this.options.animationIn) {
function afterAnimation(){
_this.$element
.attr({
'aria-hidden': false,
'tabindex': -1
})
.focus();
_this._addGlobalClasses();
Keyboard.trapFocus(_this.$element);
}
if (this.options.overlay) {
Motion.animateIn(this.$overlay, 'fade-in');
}
Motion.animateIn(this.$element, this.options.animationIn, () => {
if(this.$element) { // protect against object having been removed
this.focusableElements = Keyboard.findFocusable(this.$element);
afterAnimation();
}
});
}
// jQuery method of reveal
else {
if (this.options.overlay) {
this.$overlay.show(0);
}
this.$element.show(this.options.showDelay);
}
// handle accessibility
this.$element
.attr({
'aria-hidden': false,
'tabindex': -1
})
.focus();
Keyboard.trapFocus(this.$element);
this._addGlobalClasses();
this._addGlobalListeners();
/**
* Fires when the modal has successfully opened.
* @event Reveal#open
*/
this.$element.trigger('open.zf.reveal');
}
/**
* Adds classes and listeners on document required by open modals.
*
* The following classes are added and updated:
* - `.is-reveal-open` - Prevents the scroll on document
* - `.zf-has-scroll` - Displays a disabled scrollbar on document if required like if the
* scroll was not disabled. This prevent a "shift" of the page content due
* the scrollbar disappearing when the modal opens.
*
* @private
*/
_addGlobalClasses() {
const updateScrollbarClass = () => {
$('html').toggleClass('zf-has-scroll', !!($(document).height() > $(window).height()));
};
this.$element.on('resizeme.zf.trigger.revealScrollbarListener', () => updateScrollbarClass());
updateScrollbarClass();
$('html').addClass('is-reveal-open');
}
/**
* Removes classes and listeners on document that were required by open modals.
* @private
*/
_removeGlobalClasses() {
this.$element.off('resizeme.zf.trigger.revealScrollbarListener');
$('html').removeClass('is-reveal-open');
$('html').removeClass('zf-has-scroll');
}
/**
* Adds extra event handlers for the body and window if necessary.
* @private
*/
_addGlobalListeners() {
var _this = this;
if(!this.$element) { return; } // If we're in the middle of cleanup, don't freak out
this.focusableElements = Keyboard.findFocusable(this.$element);
if (!this.options.overlay && this.options.closeOnClick && !this.options.fullScreen) {
$('body').on('click.zf.dropdown tap.zf.dropdown', function(e) {
if (e.target === _this.$element[0] ||
$.contains(_this.$element[0], e.target) ||
!$.contains(document, e.target)) { return; }
_this.close();
});
}
if (this.options.closeOnEsc) {
$(window).on('keydown.zf.reveal', function(e) {
Keyboard.handleKey(e, 'Reveal', {
close: function() {
if (_this.options.closeOnEsc) {
_this.close();
}
}
});
});
}
}
/**
* Closes the modal.
* @function
* @fires Reveal#closed
*/
close() {
if (!this.isActive || !this.$element.is(':visible')) {
return false;
}
var _this = this;
// Motion UI method of hiding
if (this.options.animationOut) {
if (this.options.overlay) {
Motion.animateOut(this.$overlay, 'fade-out');
}
Motion.animateOut(this.$element, this.options.animationOut, finishUp);
}
// jQuery method of hiding
else {
this.$element.hide(this.options.hideDelay);
if (this.options.overlay) {
this.$overlay.hide(0, finishUp);
}
else {
finishUp();
}
}
// Conditionals to remove extra event listeners added on open
if (this.options.closeOnEsc) {
$(window).off('keydown.zf.reveal');
}
if (!this.options.overlay && this.options.closeOnClick) {
$('body').off('click.zf.dropdown tap.zf.dropdown');
}
this.$element.off('keydown.zf.reveal');
function finishUp() {
// Get the current top before the modal is closed and restore the scroll after.
// TODO: use component properties instead of HTML properties
// See https://github.com/zurb/foundation-sites/pull/10786
var scrollTop = parseInt($("html").css("top"));
if ($('.reveal:visible').length === 0) {
_this._removeGlobalClasses(); // also remove .is-reveal-open from the html element when there is no opened reveal
}
Keyboard.releaseFocus(_this.$element);
_this.$element.attr('aria-hidden', true);
_this._enableScroll(scrollTop);
/**
* Fires when the modal is done closing.
* @event Reveal#closed
*/
_this.$element.trigger('closed.zf.reveal');
}
/**
* Resets the modal content
* This prevents a running video to keep going in the background
*/
if (this.options.resetOnClose) {
this.$element.html(this.$element.html());
}
this.isActive = false;
// If deepLink and we did not switched to an other modal...
if (_this.options.deepLink && window.location.hash === `#${this.id}`) {
// Remove the history hash
if (window.history.replaceState) {
const urlWithoutHash = window.location.pathname + window.location.search;
if (this.options.updateHistory) {
window.history.pushState({}, '', urlWithoutHash); // remove the hash
} else {
window.history.replaceState('', document.title, urlWithoutHash);
}
} else {
window.location.hash = '';
}
}
this.$activeAnchor.focus();
}
/**
* Toggles the open/closed state of a modal.
* @function
*/
toggle() {
if (this.isActive) {
this.close();
} else {
this.open();
}
};
/**
* Destroys an instance of a modal.
* @function
*/
_destroy() {
if (this.options.overlay) {
this.$element.appendTo($(this.options.appendTo)); // move $element outside of $overlay to prevent error unregisterPlugin()
this.$overlay.hide().off().remove();
}
this.$element.hide().off();
this.$anchor.off('.zf');
$(window).off(`.zf.reveal:${this.id}`)
if (this.onLoadListener) $(window).off(this.onLoadListener);
if ($('.reveal:visible').length === 0) {
this._removeGlobalClasses(); // also remove .is-reveal-open from the html element when there is no opened reveal
}
};
}
Reveal.defaults = {
/**
* Motion-UI class to use for animated elements. If none used, defaults to simple show/hide.
* @option
* @type {string}
* @default ''
*/
animationIn: '',
/**
* Motion-UI class to use for animated elements. If none used, defaults to simple show/hide.
* @option
* @type {string}
* @default ''
*/
animationOut: '',
/**
* Time, in ms, to delay the opening of a modal after a click if no animation used.
* @option
* @type {number}
* @default 0
*/
showDelay: 0,
/**
* Time, in ms, to delay the closing of a modal after a click if no animation used.
* @option
* @type {number}
* @default 0
*/
hideDelay: 0,
/**
* Allows a click on the body/overlay to close the modal.
* @option
* @type {boolean}
* @default true
*/
closeOnClick: true,
/**
* Allows the modal to close if the user presses the `ESCAPE` key.
* @option
* @type {boolean}
* @default true
*/
closeOnEsc: true,
/**
* If true, allows multiple modals to be displayed at once.
* @option
* @type {boolean}
* @default false
*/
multipleOpened: false,
/**
* Distance, in pixels, the modal should push down from the top of the screen.
* @option
* @type {number|string}
* @default auto
*/
vOffset: 'auto',
/**
* Distance, in pixels, the modal should push in from the side of the screen.
* @option
* @type {number|string}
* @default auto
*/
hOffset: 'auto',
/**
* Allows the modal to be fullscreen, completely blocking out the rest of the view. JS checks for this as well.
* @option
* @type {boolean}
* @default false
*/
fullScreen: false,
/**
* Allows the modal to generate an overlay div, which will cover the view when modal opens.
* @option
* @type {boolean}
* @default true
*/
overlay: true,
/**
* Allows the modal to remove and reinject markup on close. Should be true if using video elements w/o using provider's api, otherwise, videos will continue to play in the background.
* @option
* @type {boolean}
* @default false
*/
resetOnClose: false,
/**
* Link the location hash to the modal.
* Set the location hash when the modal is opened/closed, and open/close the modal when the location changes.
* @option
* @type {boolean}
* @default false
*/
deepLink: false,
/**
* If `deepLink` is enabled, update the browser history with the open modal
* @option
* @default false
*/
updateHistory: false,
/**
* Allows the modal to append to custom div.
* @option
* @type {string}
* @default "body"
*/
appendTo: "body",
/**
* Allows adding additional class names to the reveal overlay.
* @option
* @type {string}
* @default ''
*/
additionalOverlayClasses: ''
};
export {Reveal};