-
Notifications
You must be signed in to change notification settings - Fork 49
/
jqModal.js
400 lines (335 loc) · 11.5 KB
/
jqModal.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
/*
* jqModal - Minimalist Modaling with jQuery
*
* Copyright (c) 2007-2016 Brice Burgess @IceburgBrice
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* $Version: 1.4.2 (2016.04.16 +r27)
* Requires: jQuery 1.2.3+
*/
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
/**
* Initialize elements as "modals". Modals typically are popup dialogs,
* notices, modal windows, &c.
*
* @name jqm
* @param options user defined options, augments defaults.
* @type jQuery
* @cat Plugins/jqModal
*/
$.fn.jqm=function(options){
return this.each(function(){
var jqm = $(this).data('jqm') || $.extend({ID: I++}, $.jqm.params),
o = $.extend(jqm,options);
// add/extend options to modal and mark as initialized
$(this).data('jqm',o).addClass('jqm-init')[0]._jqmID = o.ID;
// ... Attach events to trigger showing of this modal
$(this).jqmAddTrigger(o.trigger);
});
};
/**
* Matching modals will have their jqmShow() method fired by attaching a
* onClick event to elements matching `trigger`.
*
* @name jqmAddTrigger
* @param trigger a a string selector, jQuery collection, or DOM element.
*/
$.fn.jqmAddTrigger=function(trigger){
if(trigger){
return this.each(function(){
if (!addTrigger($(this), 'jqmShow', trigger)) {
err("jqmAddTrigger must be called on initialized modals");
}
});
}
};
/**
* Matching modals will have their jqmHide() method fired by attaching an
* onClick event to elements matching `trigger`.
*
* @name jqmAddClose
* @param trigger a string selector, jQuery collection, or DOM element.
*/
$.fn.jqmAddClose=function(trigger){
if(trigger){
return this.each(function(){
if(!addTrigger($(this), 'jqmHide', trigger)) {
err ("jqmAddClose must be called on initialized modals");
}
});
}
};
/**
* Open matching modals (if not shown)
*/
$.fn.jqmShow=function(trigger){
return this.each(function(){
if(!this._jqmShown) { show($(this), trigger); }
});
};
/**
* Close matching modals
*/
$.fn.jqmHide=function(trigger){
return this.each(function(){
if(this._jqmShown) { hide($(this), trigger); }
});
};
// utility functions
var
err = function(msg){
if(window.console && window.console.error) { window.console.error(msg); }
}, show = function(m, t){
/**
* m = modal element (as jQuery object)
* t = triggering element
*
* o = options
* z = z-index of modal
* v = overlay element (as jQuery object)
* h = hash (for jqModal <= r15 compatibility)
*/
t = t || window.event;
var o = m.data('jqm'),
z = (parseInt(m.css('z-index'),10)) || 3000,
v = $('<div></div>').addClass(o.overlayClass).css({
height:'100%',
width:'100%',
position:'fixed',
left:0,
top:0,
'z-index':z-1,
opacity:o.overlay/100
}),
// maintain legacy "hash" construct
h = {w: m, c: o, o: v, t: t};
m.css('z-index',z);
if(o.ajax){
var target = o.target || m,
url = o.ajax;
target = (typeof target === 'string') ? $(target,m) : $(target);
if(url.substr(0,1) === '@') {
url = $(t).attr(url.substring(1));
}
// load remote contents
target.load(url,function(){
if(o.onLoad) {
o.onLoad.call(this,h);
}
parseModalContents.call(m,o,target);
});
// show modal
if(o.ajaxText) {
target.html(o.ajaxText);
}
open(h);
}
else { open(h); }
}, hide = function(m, t){
/**
* m = modal element (as jQuery object)
* t = triggering element
*
* o = options
* h = hash (for jqModal <= r15 compatibility)
*/
t = t || window.event;
var o = m.data('jqm'),
// maintain legacy "hash" construct
h = {w: m, c: o, o: m.data('jqmv'), t: t};
close(h);
}, onShow = function(hash){
// onShow callback. Responsible for showing a modal and overlay.
// return false to stop opening modal.
// hash object;
// w: (jQuery object) The modal element
// c: (object) The modal's options object
// o: (jQuery object) The overlay element
// t: (DOM object) The triggering element
// if overlay not disabled, prepend to body
if(hash.c.overlay > 0) {
hash.o.prependTo('body');
}
// make modal visible
hash.w.show();
// call focusFunc (attempts to focus on first input in modal)
$.jqm.focusFunc(hash.w,true);
return true;
}, onHide = function(hash){
// onHide callback. Responsible for hiding a modal and overlay.
// return false to stop closing modal.
// hash object;
// w: (jQuery object) The modal element
// c: (object) The modal's options object
// o: (jQuery object) The overlay element
// t: (DOM object) The triggering element
// hide modal and if overlay, remove overlay.
if(hash.w.hide() && hash.o) {
hash.o.remove();
}
return true;
}, addTrigger = function(m, key, trigger){
// addTrigger: Adds a jqmShow/jqmHide (key) event click on modal (m)
// to all elements that match trigger string (trigger)
var jqm = m.data('jqm');
if(jqm) {
return $(trigger).each(function(){
this[key] = this[key] || [];
// register this modal with this trigger only once
if($.inArray(jqm.ID,this[key]) < 0) {
this[key].push(jqm.ID);
// register trigger click event for this modal
// allows cancellation of show/hide event from
$(this).click(function(e){
if(!e.isDefaultPrevented()) { m[key](this); }
return false;
});
}
});
}
}, open = function(h){
// open: executes the onOpen callback + performs common tasks if successful
// transform legacy hash into new var shortcuts
var m = h.w,
v = h.o,
o = h.c;
// execute onShow callback
if(o.onShow(h) !== false){
// mark modal as shown
m[0]._jqmShown = true;
// if modal:true dialog
// Bind the Keep Focus Function [F] if no other Modals are active
// else,
// trigger closing of dialog when overlay is clicked
if(o.modal){
if(!ActiveModals[0]){ F('bind'); }
ActiveModals.push(m[0]);
}
else {
m.jqmAddClose(v);
}
// parse modal contents and add behavior
parseModalContents.call(m,o);
// if toTop is true and overlay exists;
// remember modal DOM position with <span> placeholder element, and move
// the modal to a direct child of the body tag (after overlyay)
if(o.toTop && v) {
m.before('<span id="jqmP'+o.ID+'"></span>').insertAfter(v);
}
// remember overlay (for closing function)
m.data('jqmv',v);
// close modal if the esc key is pressed and closeOnEsc is set to true
m.unbind("keydown",$.jqm.closeOnEscFunc);
if(o.closeOnEsc) {
m.attr("tabindex", 0).bind("keydown",$.jqm.closeOnEscFunc).focus();
}
}
}, close = function(h){
// close: executes the onHide callback + performs common tasks if successful
// transform legacy hash into new var shortcuts
var m = h.w,
v = h.o,
o = h.c;
// execute onHide callback
if(o.onHide(h) !== false){
// mark modal as !shown
m[0]._jqmShown = false;
// If modal, remove from modal stack.
// If no modals in modal stack, unbind the Keep Focus Function
if(o.modal){
ActiveModals.pop();
if(!ActiveModals[0]) {
F('unbind');
}
}
// IF toTop was passed and an overlay exists;
// Move modal back to its "remembered" position.
if(o.toTop && v) {
$('#jqmP'+o.ID).after(m).remove();
}
}
}, F = function(t){
// F: The Keep Focus Function (for modal: true dialos)
// Binds or Unbinds (t) the Focus Examination Function (X)
$(document)[t]("keypress keydown mousedown",X);
}, X = function(e){
// X: The Focus Examination Function (for modal: true dialogs)
var targetModal = $(e.target).data('jqm') ||
$(e.target).parents('.jqm-init:first').data('jqm');
var activeModal = ActiveModals[ActiveModals.length-1];
// allow bubbling if event target is within active modal dialog
return (targetModal && targetModal.ID === activeModal._jqmID) ?
true : $.jqm.focusFunc(activeModal,e);
}, parseModalContents = function(o, context){
context = context || this;
// Attach events to elements inside the modal matching closingClass
if(o.closeClass) {
this.jqmAddClose($('.' + o.closeClass,context));
}
},
I = 0, // modal ID increment (for nested modals)
ActiveModals = []; // array of active modals
// $.jqm, overridable defaults
$.jqm = {
/**
* default options
*
* (Integer) overlay - [0-100] Translucency percentage (opacity) of the body covering overlay. Set to 0 for NO overlay, and up to 100 for a 100% opaque overlay.
* (String) overlayClass - Applied to the body covering overlay. Useful for controlling overlay look (tint, background-image, &c) with CSS.
* (String) closeClass - Children of the modal element matching `closeClass` will fire the onHide event (to close the modal).
* (Mixed) trigger - Matching elements will fire the onShow event (to display the modal). Trigger can be a selector String, a jQuery collection of elements, a DOM element, or a False boolean.
* (String) ajax - URL to load content from via an AJAX request. False to disable ajax. If ajax begins with a "@", the URL is extracted from the attribute of the triggering element (e.g. use '@data-url' for; <a href="#" class="jqModal" data-url="modal.html">...)
* (Mixed) target - Children of the modal element to load the ajax response into. If false, modal content will be overwritten by ajax response. Useful for retaining modal design.
* Target may be a selector string, jQuery collection of elements, or a DOM element -- and MUST exist as a child of the modal element.
* (String) ajaxText - Text shown while waiting for ajax return. Replaces HTML content of `target` element.
* (Boolean) modal - If true, user interactivity will be locked to the modal window until closed.
* (Boolean) toTop - If true, modal will be posistioned as a first child of the BODY element when opened, and its DOM posistion restored when closed. Useful for overcoming z-Index container issues.
* (Function) onShow - User defined callback function fired when modal opened.
* (Function) onHide - User defined callback function fired when modal closed.
* (Function) onLoad - User defined callback function fired when ajax content loads.
*/
params: {
overlay: 50,
overlayClass: 'jqmOverlay',
closeClass: 'jqmClose',
closeOnEsc: false,
trigger: '.jqModal',
ajax: false,
target: false,
ajaxText: '',
modal: false,
toTop: false,
onShow: onShow,
onHide: onHide,
onLoad: false
},
// focusFunc is fired:
// a) when a modal:true dialog is shown,
// b) when an event occurs outside an active modal:true dialog
// It is passed the active modal:true dialog as well as event
focusFunc: function(activeModal, e) {
// if the event occurs outside the activeModal, focus on first element
if(e) { $(':input:visible:first',activeModal).focus(); }
// lock interactions to the activeModal
return false;
},
// closeOnEscFunc is attached to modals where closeOnEsc param true.
closeOnEscFunc: function(e){
if (e.keyCode === 27) {
$(this).jqmHide();
return false;
}
}
};
return $.jqm;
}));