modal.js
12.2 KB
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
/**
* Modal jquery code
* 11/13/2021 - do not scroll to first focusable field
* 8/2/2021 - add code to allow timer modal to optionally display only once via the use of a cookie
* 8/17/2021 - do not allow timer modal to display if another modal already open
* 11/1/2021 - when setting focus on first element do not scroll
* 2/15/2022 - disable close modal on escape if disabled in the frontend
* 4/5/2022 - Do not scroll when returning focus on modal close
* 5/5/2022 - Allow for modals to call other modals
* 6/28/2022 - Allow for checking url for text before triggering modal
* 11/22/2022 - custom events for open and close
* 1/11/2023 - Provide ability to manually trigger initialization of modals on page. Also only initialize modals when document is ready.
*/
var bodModalBlock = function(){
"use strict";
var $ = jQuery.noConflict();
const focusableElements =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
var bodModalCount = 0; // global count of modals
var bodModalActive = false;
var bodModals = [];
var initElements = function(){
bodModalCount = 0; // global count of modals
bodModalActive = false;
bodModals = [];
// if we have already inited this type of element
$('.wp-block-bod-modal-block').each(function(){
// for the identified modal we add an instance of the class BodModal
// to the data element
// $(this).data('bod-block-popup', new BodModal(this));
bodModals.push(new BodModal(this));
});
};
var checkForCookie = function(cookieName){
let name = cookieName + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// class used to manipulate the modal, there will be a seperate instance of the
// class for each modal on the page
// container - DOM object for modal
var BodModal = function (container) {
// convert passed in DOM modal container to jquery object so we can apply jquery methods
this.$container = $(container);
this.$trigger = this.$container.find('.bod-block-popup-trigger');
this.$overlay = this.$container.find('.bod-block-popup-overlay');
// Markup the aria labels
bodModalCount ++; // get modal number to append to get unique id
this.$container.find('.bod-block-popup-wrap').attr('aria-labelledby','bodModalAriaTitle' + bodModalCount);
this.$container.find('.bod-modal-title').attr('id' , 'bodModalAriaTitle' + bodModalCount);
this.$container.find('.bod-block-popup-wrap').attr('aria-describedby','bodModalAriaContent' + bodModalCount);
this.$container.find('.bod-modal-content').attr('id' , 'bodModalAriaContent' + bodModalCount);
// bind our functions to this so we use current rather than event version of this
this.show = this.show.bind(this);
this.afterShow = this.afterShow.bind(this);
this.hide = this.hide.bind(this);
this.keyPress = this.keyPress.bind(this);
// we need a timer for fading in and out the modal and for show on page load
this.timer = null;
// assign trigger event depending upon trigger type which is
// stored has a class
if (this.$trigger.hasClass('type_selector')) {
var triggerSelector = this.$trigger.attr('data-selector');
if (triggerSelector) {
$('.' + triggerSelector).css('cursor','pointer').on('click', this.show); // attach click event
}
} else if (this.$trigger.hasClass('type_load')) {
// we have show on page load
// need to load the data-once attribute which will be either no , or yes
var loadOnce = this.$trigger.attr('data-once');
var triggerTimer = true;
if (loadOnce) {
if (loadOnce === 'yes') {
let modalId = this.$trigger.attr('data-id');
if (!modalId) modalId= "";
let modalOnce = checkForCookie ("bodModalOnce" + modalId);
if (modalOnce) triggerTimer = false;
}
}
// check if we need to check for text in url to enable modal
var loadurl = this.$trigger.attr('data-urltrig');
if (loadurl) {
if (window.location.href.indexOf(loadurl)===-1) triggerTimer = false;
}
if (triggerTimer) {
// we need to extract the delay amd set a timeout using it
var loadDelay = this.$trigger.attr('data-delay');
isNaN(parseInt(loadDelay)) ? loadDelay = 1000 : loadDelay = parseInt(loadDelay);
// if we only want to show the modal once we exact the UID provided when the modal created (optional), and the number of days we wait until we show the
// modal again (this is added to the cookie)
if (loadOnce) {
let modalId = this.$trigger.attr('data-id');
if (!modalId) modalId= "";
let noShowDays = this.$trigger.attr('data-days');
isNaN(parseInt(noShowDays)) ? noShowDays = 30 : noShowDays = parseInt(noShowDays);
this.timer = setTimeout(this.show, loadDelay, loadOnce, modalId , noShowDays);
} else {
this.timer = setTimeout(this.show, loadDelay);
}
}
} else { // must be button, text, or image trigger
this.$trigger.on('click', this.show); // attach click event
}
// setup the overlay and content wrap, put them into a jquery object and
// attach the hide event for the click
// check to see if we have disabled the close on overlay click in the frontend
var disableOverlayClose = this.$overlay.attr('data-disabled-overlay-close');
if (!disableOverlayClose) {
this.$overlay.on('click' , this.hide);
} else {
if (disableOverlayClose === 'false') {
this.$overlay.on('click' , this.hide);
}
}
// check to see if we have disabled the close on escape key press in the frontend
var disableEscapeClose = this.$overlay.attr('data-disabled-escape-close');
if (!disableEscapeClose) {
this.disableEscapeClose = false;
} else {
if (disableEscapeClose === 'true') {
this.disableEscapeClose = true;
} else {
this.disableEscapeClose = false;
}
}
this.$modalWrap = this.$container.find('.bod-block-popup-wrap');
this.$modal = this.$container.find('.bod-block-popup');
this.$modalcloser = this.$container.find('.bod-block-popup-closer')
.on('click' , this.hide);
this.$titlecloser = this.$container.find('.bod-block-title-closer')
.on('click' , this.hide);
this.$btncloser = this.$container.find('.bod-block-close-btn .bod-btn')
.on('click' , this.hide);
this.$modalToModal = this.$container.find('.bod-modal-to-modal')
.on('click' , this.hide);
this.$escCloser = $(document).on('keydown' , this.keyPress);
// Javascript modal wrap
this.modalWrap = container.querySelector('.bod-block-popup-wrap');
// capture first, all and last focusable elements so we can enforce modal focus trap for ADA
this.focusableContent = this.modalWrap.querySelectorAll(focusableElements);
if (this.focusableContent) {
this.firstFocusableElement = this.focusableContent[0]; // get first element to be focused inside modal
this.lastFocusableElement = this.focusableContent[this.focusableContent.length - 1]; // get last element to be focused inside modal
}
this.modalOpen = false;
}
BodModal.prototype = {
show: function(loadOnce , modalId, noShowDays ){
$(document).trigger("bod-modal-before-open",this.$modalWrap);
// if we try and show a modal but one is already open then return without showing the new one
if (this.$trigger.hasClass('type_load') && bodModalActive === true) {
return;
}
bodModalActive = true;
if (loadOnce) {
// check if we need to set a cookie to stop modal being shown mmore than once
if (loadOnce !== 'no') {
// flag set to say we only show once so set the cookie
const d = new Date();
d.setTime(d.getTime() + (noShowDays*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
if (modalId) {
var cookie = "bodModalOnce" + modalId;
} else {
var cookie = "bodModalOnce";
}
document.cookie = cookie + "=" + loadOnce + ";" + expires + ";path=/";
}
}
// start to clearing any timeouts
clearTimeout(this.timer);
// when we show the modal we move it to the body DOM so that
// nothing else impacts it
this.$overlay.appendTo(document.body).show();
this.$modalWrap.appendTo(document.body).show();
// even though we have hit it with a show the opacticy still makes it
// invisible. We wait to ensure the modal is ready then then add
// an active class with triggers the opacity transition
this.timer = setTimeout(this.afterShow, 25);
},
afterShow: function(){
clearTimeout(this.timer);
// Lets check what transition we are going to use
if (this.transition = this.$modal.attr('data-transition')) {
if (this.transition == 'left') {
this.$modal.addClass('slide-right');
} else if (this.transition == 'right') {
this.$modal.addClass('slide-left');
} else if (this.transition == 'bottom') {
this.$modal.addClass('slide-up');
} else if (this.transition == 'top') {
this.$modal.addClass('slide-down');
} else {
this.$modal.addClass('fade');
};
} else {
this.$modal.addClass('fade');
};
this.$overlay.addClass('active');
this.$modalWrap.addClass('active');
this.$modalWrap.attr('aria-modal','true');
this.modalOpen = true;
this.triggerElement = document.activeElement;
if (this.firstFocusableElement) {
this.firstFocusableElement.focus({
preventScroll: true
});
} else {
document.activeElement.blur();
}
$(document).trigger("bod-modal-after-open",this.$modalWrap);
},
hide: function() {
if ($(document).trigger("bod-modal-before-close",this.$modalWrap).data('cancel')){
return;
};
this.$overlay.removeClass('active');
this.$modalWrap.removeClass('active');
// add the overlay and modal wrap back to the container and hide it
this.$overlay.appendTo(this.$container).hide();
this.$modalWrap.appendTo(this.$container).hide();
this.$modalWrap.attr('aria-modal','false');
bodModalActive = false;
this.modalOpen = false;
$(document).trigger("bod-modal-after-close",this.$modalWrap);
// return focus to element that was active before modal called
// but do not scroll to it
if (this.triggerElement) this.triggerElement.focus(
{
preventScroll: true
}
);
},
keyPress: function(e) {
if ( e.keyCode === 27 && !this.disableEscapeClose) { // ESC
this.hide();
}
// here we do the ADA focus trap but only if the modal is open
if (this.modalOpen) {
// code to enforce focus trap for ADA
let isTabPressed = e.key === 'Tab' || e.keyCode === 9;
// if keypress is not tab do nothing
if (!isTabPressed) {
return;
}
// Do we have at least one element in the modal that can receive focus
if (this.firstFocusableElement) {
if (e.shiftKey) { // if shift key pressed for shift + tab combination
if (document.activeElement === this.firstFocusableElement) {
this.lastFocusableElement.focus(); // add focus for the last focusable element
e.preventDefault();
}
} else { // if tab key is pressed
if (document.activeElement === this.lastFocusableElement) { // if focused has reached to last focusable element then focus first focusable element after pressing tab
this.firstFocusableElement.focus(); // add focus for the first focusable element
e.preventDefault();
}
}
} else {
// if we are here the modal is open and the tab key has been pressed
// but there are no elements in the modal that can receive focus.
// Therefore we do not want to provide focus to any element outside
// the modal.
e.preventDefault();
}
}
},
}
return {
initModal : initElements
}
};
var bodModal = bodModalBlock();
jQuery(document).ready(function( $ ) {
bodModal.initModal();
});
// This is an example for the custom events, this one returns cancel=true so modal will not close
//(function($){
//$(document).on("bod-modal-before-close", function(e, modalData){
// $(this).data('cancel', true);
// this.$modalData = $(modalData);
// this.$modalData.addClass('testbeforeclose');
// alert("added before close");
//});
//})(jQuery);
// This is an example of how to manually trigger initialization 10 secs after page is ready
//jQuery(document).ready(function( $ ) {
// setTimeout(bodModal.initModal,10000);
//});