forked from drublic/css-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
301 lines (241 loc) · 7.29 KB
/
modal.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
/*!
* CSS Modal
* http://drublic.github.com/css-modal
*
* @author Hans Christian Reinl - @drublic
* @version 1.1.0alpha
*/
(function (global) {
'use strict';
/*
* Storage for functions and attributes
*/
var modal = {
activeElement: undefined, // Store for currently active element
lastActive: undefined, // Store for last active elemet
stackedElements: [], // Store for stacked elements
// All elements that can get focus, can be tabbed in a modal
tabbableElements: 'a[href], area[href], input:not([disabled]),' +
'select:not([disabled]), textarea:not([disabled]),' +
'button:not([disabled]), iframe, object, embed, *[tabindex],' +
'*[contenteditable]',
/*
* Polyfill addEventListener for IE8 (only very basic)
* @param event {string} event type
* @param element {Node} node to fire event on
* @param callback {function} gets fired if event is triggered
*/
on: function (event, element, callback) {
if (element.addEventListener) {
element.addEventListener(event, callback, false);
} else {
element.attachEvent('on' + event, callback);
}
},
/*
* Convenience function to trigger event
* @param event {string} event type
* @param modal {string} id of modal that the event is triggerd on
*/
trigger: function (event, modal) {
var eventTrigger;
if (!window.CustomEvent) {
return;
}
eventTrigger = new CustomEvent(event, {
detail: {
'modal': modal
}
});
document.dispatchEvent(eventTrigger);
},
/*
* Convenience function to add a class to an element
* @param element {Node} element to add class to
* @param className {string}
*/
addClass: function (element, className) {
if (element && !element.className.match(className)) {
element.className += ' ' + className;
}
},
/*
* Convenience function to remove a class from an element
* @param element {Node} element to remove class off
* @param className {string}
*/
removeClass: function (element, className) {
element.className = element.className.replace(className, '').replace(' ', ' ');
},
/*
* Focus modal
*/
setFocus: function () {
if (modal.activeElement) {
// Set element with last focus
modal.lastActive = document.activeElement;
// New focussing
modal.activeElement.focus();
// Add handler to keep the focus
modal.keepFocus(modal.activeElement);
}
},
/*
* Unfocus
*/
removeFocus: function () {
if (modal.lastActive) {
modal.lastActive.focus();
}
},
/*
* Keep focus inside the modal
* @param element {node} element to keep focus in
*/
keepFocus: function (element) {
var allTabbableElements = element.querySelectorAll(modal.tabbableElements);
var firstTabbableElement = allTabbableElements[0];
var lastTabbableElement = allTabbableElements[allTabbableElements.length - 1];
var focusHandler = function (event) {
var keyCode = event.which || event.keyCode;
// TAB pressed
if (keyCode !== 9) {
return;
}
// Polyfill to prevent the default behavior of events
event.preventDefault = event.preventDefault || function () {
event.returnValue = false;
};
// Move focus to first element that can be tabbed if Shift isn't used
if (event.target === lastTabbableElement && !event.shiftKey) {
event.preventDefault();
firstTabbableElement.focus();
// Move focus to last element that can be tabbed if Shift is used
} else if (event.target === firstTabbableElement && event.shiftKey) {
event.preventDefault();
lastTabbableElement.focus();
}
};
modal.on('keydown', element, focusHandler);
},
/*
* Mark modal as active
* @param element {Node} element to set active
*/
setActive: function (element) {
modal.addClass(element, 'is-active');
modal.activeElement = element;
// Set the focus to the modal
modal.setFocus(element.id);
// Fire an event
modal.trigger('cssmodal:show', modal.activeElement);
},
/*
* Unset previous active modal
* @param isStacked {boolean} true if element is stacked above another
*/
unsetActive: function (isStacked) {
if (modal.activeElement) {
modal.removeClass(modal.activeElement, 'is-active');
// Fire an event
modal.trigger('cssmodal:hide', modal.activeElement);
// Unfocus
modal.removeFocus();
// Make modal stacked if needed
if (isStacked) {
modal.stackModal(modal.activeElement);
}
// If there are any stacked elements
if (!isStacked && modal.stackedElements.length > 0) {
modal.unstackModal();
}
// Reset active element
modal.activeElement = null;
}
},
/*
* Stackable modal
* @param stackableModal {node} element to be stacked
*/
stackModal: function (stackableModal) {
modal.addClass(stackableModal, 'is-stacked');
// Set modal as stacked
modal.stackedElements.push(modal.activeElement);
},
/*
* Reactivate stacked modal
*/
unstackModal: function () {
var stackedCount = modal.stackedElements.length;
var lastStacked = modal.stackedElements[stackedCount - 1];
modal.removeClass(lastStacked, 'is-stacked');
// Set hash to modal, activates the modal automatically
window.location.hash = lastStacked.id;
// Remove modal from stackedElements array
modal.stackedElements.splice(stackedCount - 1, 1);
},
/*
* When displaying modal, prevent background from scrolling
*/
mainHandler: function () {
var hash = window.location.hash.replace('#', '');
var modalElement = document.getElementById(hash);
var modalChild;
// If the hash element exists
if (modalElement) {
// Get first element in selected element
modalChild = modalElement.children[0];
// When we deal with a modal and body-class `has-overlay` is not set
if (modalChild && modalChild.className.match(/modal-inner/)) {
// Set an html class to prevent scrolling
modal.addClass(document.documentElement, 'has-overlay');
// Make previous element stackable
modal.unsetActive(true);
// Mark the active element
modal.setActive(modalElement);
}
} else {
modal.removeClass(document.documentElement, 'has-overlay');
// If activeElement is already defined, delete it
modal.unsetActive();
}
}
};
/*
* Hide overlay when ESC is pressed
*/
modal.on('keyup', document, function (event) {
var hash = window.location.hash.replace('#', '');
// If hash is not set
if (hash === '' || hash === '!') {
return;
}
// If key ESC is pressed
if (event.keyCode === 27) {
window.location.hash = '!';
if (modal.lastActive) {
return false;
}
// Unfocus
modal.removeFocus();
}
}, false);
/*
* Trigger main handler on load and hashchange
*/
modal.on('hashchange', window, modal.mainHandler);
modal.on('load', window, modal.mainHandler);
/*
* AMD, module loader, global registration
*/
// Expose modal for loaders that implement the Node module pattern.
if (typeof module === 'object' && module && typeof module.exports === 'object') {
module.exports = modal;
// Register as an AMD module
} else if (typeof define === 'function' && define.amd) {
define([], function () { return modal; });
// Export CSSModal into global space
} else if (typeof global === 'object' && typeof global.document === 'object') {
global.CSSModal = modal;
}
}(window));