This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
/
modal.js
321 lines (259 loc) · 9.12 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function Modal(api)
{
var self = this,
options = api.options.show.modal,
elems = api.elements,
tooltip = elems.tooltip,
overlaySelector = '#qtip-overlay',
globalNamespace = '.qtipmodal',
namespace = globalNamespace + api.id,
attr = 'is-modal-qtip',
docBody = $(document.body),
focusableSelector = PLUGINS.modal.focusable.join(','),
focusableElems = {}, overlay;
// Setup option set checks
api.checks.modal = {
'^show.modal.(on|blur)$': function() {
// Initialise
self.init();
// Show the modal if not visible already and tooltip is visible
elems.overlay.toggle( tooltip.is(':visible') );
},
'^content.text$': updateFocusable
};
function updateFocusable() {
focusableElems = $(focusableSelector, tooltip).not('[disabled]').map(function() {
return typeof this.focus === 'function' ? this : null;
});
}
function focusInputs(blurElems) {
// Blurring body element in IE causes window.open windows to unfocus!
if(focusableElems.length < 1 && blurElems.length) { blurElems.not('body').blur(); }
// Focus the inputs
else { focusableElems.first().focus(); }
}
function stealFocus(event) {
var target = $(event.target),
container = target.closest('.qtip'),
targetOnTop;
// Determine if input container target is above this
targetOnTop = container.length < 1 ? FALSE :
(parseInt(container[0].style.zIndex, 10) > parseInt(tooltip[0].style.zIndex, 10));
// If we're showing a modal, but focus has landed on an input below
// this modal, divert focus to the first visible input in this modal
// or if we can't find one... the tooltip itself
if(!targetOnTop && ($(event.target).closest(selector)[0] !== tooltip[0])) {
focusInputs(target);
}
}
$.extend(self, {
init: function()
{
// If modal is disabled... return
if(!options.on) { return self; }
// Create the overlay if needed
overlay = self.create();
// Add unique attribute so we can grab modal tooltips easily via a selector
tooltip.attr(attr, TRUE)
// Set z-index
.css('z-index', PLUGINS.modal.zindex + $(selector+'['+attr+']').length)
// Remove previous bound events in globalNamespace
.unbind(globalNamespace).unbind(namespace)
// Apply our show/hide/focus modal events
.bind('tooltipshow'+globalNamespace+' tooltiphide'+globalNamespace, function(event, api, duration) {
var oEvent = event.originalEvent;
// Make sure mouseout doesn't trigger a hide when showing the modal and mousing onto backdrop
if(event.target === tooltip[0]) {
if(oEvent && event.type === 'tooltiphide' && /mouse(leave|enter)/.test(oEvent.type) && $(oEvent.relatedTarget).closest(overlay[0]).length) {
try { event.preventDefault(); } catch(e) {}
}
else if(!oEvent || (oEvent && !oEvent.solo)) {
self[ event.type.replace('tooltip', '') ](event, duration);
}
}
})
// Adjust modal z-index on tooltip focus
.bind('tooltipfocus'+globalNamespace, function(event) {
// If focus was cancelled before it reearch us, don't do anything
if(event.isDefaultPrevented() || event.target !== tooltip[0]) { return; }
var qtips = $(selector).filter('['+attr+']'),
// Keep the modal's lower than other, regular qtips
newIndex = PLUGINS.modal.zindex + qtips.length,
curIndex = parseInt(tooltip[0].style.zIndex, 10);
// Set overlay z-index
overlay[0].style.zIndex = newIndex - 2;
// Reduce modal z-index's and keep them properly ordered
qtips.each(function() {
if(this.style.zIndex > curIndex) {
this.style.zIndex -= 1;
}
});
// Fire blur event for focused tooltip
qtips.end().filter('.' + focusClass).qtip('blur', event.originalEvent);
// Set the new z-index
tooltip.addClass(focusClass)[0].style.zIndex = newIndex;
// Prevent default handling
try { event.preventDefault(); } catch(e) {}
})
// Focus any other visible modals when this one hides
.bind('tooltiphide'+globalNamespace, function(event) {
if(event.target === tooltip[0]) {
$('[' + attr + ']').filter(':visible').not(tooltip).last().qtip('focus', event);
}
});
// Apply keyboard "Escape key" close handler
if(options.escape) {
$(document).unbind(namespace).bind('keydown'+namespace, function(event) {
if(event.keyCode === 27 && tooltip.hasClass(focusClass)) {
api.hide(event);
}
});
}
// Apply click handler for blur option
if(options.blur) {
elems.overlay.unbind(namespace).bind('click'+namespace, function(event) {
if(tooltip.hasClass(focusClass)) { api.hide(event); }
});
}
// Update focusable elements
updateFocusable();
return self;
},
create: function()
{
var elem = $(overlaySelector);
// Return if overlay is already rendered
if(elem.length) {
// Modal overlay should always be below all tooltips if possible
return (elems.overlay = elem.insertAfter( $(selector).last() ));
}
// Create document overlay
overlay = elems.overlay = $('<div />', {
id: overlaySelector.substr(1),
html: '<div></div>',
mousedown: function() { return FALSE; }
})
.hide()
.insertAfter( $(selector).last() );
// Update position on window resize or scroll
function resize() {
overlay.css({
height: $(window).height(),
width: $(window).width()
});
}
$(window).unbind(globalNamespace).bind('resize'+globalNamespace, resize);
resize(); // Fire it initially too
return overlay;
},
toggle: function(event, state, duration)
{
// Make sure default event hasn't been prevented
if(event && event.isDefaultPrevented()) { return self; }
var effect = options.effect,
type = state ? 'show': 'hide',
visible = overlay.is(':visible'),
modals = $('[' + attr + ']').filter(':visible').not(tooltip),
zindex;
// Create our overlay if it isn't present already
if(!overlay) { overlay = self.create(); }
// Prevent modal from conflicting with show.solo, and don't hide backdrop is other modals are visible
if((overlay.is(':animated') && visible === state) || (!state && modals.length)) { return self; }
// State specific...
if(state) {
// Set position
overlay.css({ left: 0, top: 0 });
// Toggle backdrop cursor style on show
overlay.toggleClass('blurs', options.blur);
// IF the modal can steal the focus
if(options.stealfocus !== FALSE) {
// Make sure we can't focus anything outside the tooltip
docBody.bind('focusin'+namespace, stealFocus);
// Blur the current item and focus anything in the modal we an
focusInputs( $('body *') );
}
}
else {
// Undelegate focus handler
docBody.unbind('focusin'+namespace);
}
// Stop all animations
overlay.stop(TRUE, FALSE);
// Use custom function if provided
if($.isFunction(effect)) {
effect.call(overlay, state);
}
// If no effect type is supplied, use a simple toggle
else if(effect === FALSE) {
overlay[ type ]();
}
// Use basic fade function
else {
overlay.fadeTo( parseInt(duration, 10) || 90, state ? 1 : 0, function() {
if(!state) { $(this).hide(); }
});
}
// Reset position on hide
if(!state) {
overlay.queue(function(next) {
overlay.css({ left: '', top: '' });
next();
});
}
return self;
},
show: function(event, duration) { return self.toggle(event, TRUE, duration); },
hide: function(event, duration) { return self.toggle(event, FALSE, duration); },
destroy: function()
{
var delBlanket = overlay;
if(delBlanket) {
// Check if any other modal tooltips are present
delBlanket = $('[' + attr + ']').not(tooltip).length < 1;
// Remove overlay if needed
if(delBlanket) {
elems.overlay.remove();
$(document).unbind(globalNamespace);
}
else {
elems.overlay.unbind(globalNamespace+api.id);
}
// Undelegate focus handler
docBody.undelegate('*', 'focusin'+namespace);
}
// Remove bound events
return tooltip.removeAttr(attr).unbind(globalNamespace);
}
});
self.init();
}
PLUGINS.modal = function(api) {
var self = api.plugins.modal;
return 'object' === typeof self ? self : (api.plugins.modal = new Modal(api));
};
// Plugin needs to be initialized on render
PLUGINS.modal.initialize = 'render';
// Setup sanitiztion rules
PLUGINS.modal.sanitize = function(opts) {
if(opts.show) {
if(typeof opts.show.modal !== 'object') { opts.show.modal = { on: !!opts.show.modal }; }
else if(typeof opts.show.modal.on === 'undefined') { opts.show.modal.on = TRUE; }
}
};
// Base z-index for all modal tooltips (use qTip core z-index as a base)
PLUGINS.modal.zindex = QTIP.zindex + 1000;
// Defines the selector used to select all 'focusable' elements within the modal when using the show.modal.stealfocus option.
// Selectors initially taken from http://stackoverflow.com/questions/7668525/is-there-a-jquery-selector-to-get-all-elements-that-can-get-focus
PLUGINS.modal.focusable = ['a[href]', 'area[href]', 'input', 'select', 'textarea', 'button', 'iframe', 'object', 'embed', '[tabindex]', '[contenteditable]'];
// Extend original api defaults
$.extend(TRUE, QTIP.defaults, {
show: {
modal: {
on: FALSE,
effect: TRUE,
blur: TRUE,
stealfocus: TRUE,
escape: TRUE
}
}
});