-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
jquery.the-modal.js
259 lines (219 loc) · 6.62 KB
/
jquery.the-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
/**
* The Modal jQuery plugin
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @link https://github.com/samdark/the-modal
* @version 0.0.14
*/
;(function($, window, document, undefined) {
"use strict";
/*jshint smarttabs:true*/
// :focusable expression, needed for tabindexes in modal
$.extend($.expr[':'],{
focusable: function(element){
var map, mapName, img,
nodeName = element.nodeName.toLowerCase(),
isTabIndexNotNaN = !isNaN($.attr(element,'tabindex'));
if ('area' === nodeName) {
map = element.parentNode;
mapName = map.name;
if (!element.href || !mapName || map.nodeName.toLowerCase() !== 'map') {
return false;
}
img = $('img[usemap=#' + mapName + ']')[0];
return !!img && visible(img);
}
var result = isTabIndexNotNaN;
if (/input|select|textarea|button|object/.test(nodeName)) {
result = !element.disabled;
} else if ('a' === nodeName) {
result = element.href || isTabIndexNotNaN;
}
return result && visible(element);
function visible(element) {
return $.expr.filters.visible(element) &&
!$(element).parents().addBack().filter(function() {
return $.css(this,'visibility') === 'hidden';
}).length;
}
}
});
var pluginNamespace = 'the-modal',
// global defaults
defaults = {
lockClass: 'themodal-lock',
overlayClass: 'themodal-overlay',
closeOnEsc: true,
closeOnOverlayClick: true,
onBeforeClose: null,
onClose: null,
onOpen: null,
cloning: true
};
var oMargin = {};
var ieBodyTopMargin = 0;
function isIE() {
return ((navigator.appName == 'Microsoft Internet Explorer') ||
(navigator.userAgent.match(/MSIE\s+\d+\.\d+/)) ||
(navigator.userAgent.match(/Trident\/\d+\.\d+/)));
}
function lockContainer(options, overlay) {
var body = $('body');
var oWidth = body.outerWidth(true);
body.addClass(options.lockClass);
var sbWidth = body.outerWidth(true) - oWidth;
if (isIE()) {
ieBodyTopMargin = body.css('margin-top');
body.css('margin-top', 0);
}
if (sbWidth != 0) {
var tags = $('html, body');
tags.each(function () {
var $this = $(this);
oMargin[$this.prop('tagName').toLowerCase()] = parseInt($this.css('margin-right'));
});
$('html').css('margin-right', oMargin['html'] + sbWidth);
overlay.css('left', 0 - sbWidth);
}
}
function unlockContainer(options) {
if (isIE()) {
$('body').css('margin-top', ieBodyTopMargin);
}
var body = $('body');
var oWidth = body.outerWidth(true);
body.removeClass(options.lockClass);
var sbWidth = body.outerWidth(true) - oWidth;
if (sbWidth != 0) {
$('html, body').each(function () {
var $this = $(this);
$this.css('margin-right', oMargin[$this.prop('tagName').toLowerCase()])
});
}
}
function init(els, options) {
var modalOptions = options;
if(els.length) {
els.each(function(){
$(this).data(pluginNamespace+'.options', modalOptions);
});
} else {
$.extend(defaults, modalOptions);
}
// on Ctrl+A click fire `onSelectAll` event
$(window).bind('keydown',function(e){
if (!(e.ctrlKey && e.keyCode == 65)) {
return true;
}
if ( $('input:focus, textarea:focus').length > 0 ) {
return true;
}
var selectAllEvent = new $.Event('onSelectAll');
selectAllEvent.parentEvent = e;
$(window).trigger(selectAllEvent);
return true;
});
els.bind('keydown',function(e){
var modalFocusableElements = $(':focusable',$(this));
if(modalFocusableElements.filter(':last').is(':focus') && (e.which || e.keyCode) == 9){
e.preventDefault();
modalFocusableElements.filter(':first').focus();
}
});
return {
open: function(options) {
var el = els.get(0),
localOptions = $.extend({}, defaults, $(el).data(pluginNamespace+'.options'), options);
// close modal if opened
if($('.'+localOptions.overlayClass).length) {
$.modal().close();
}
var overlay = $('<div/>').addClass(localOptions.overlayClass).prependTo('body');
overlay.data(pluginNamespace+'.options', localOptions);
lockContainer(localOptions, overlay);
if(el) {
var openedModalElement = null;
if (!localOptions.cloning) {
overlay.data(pluginNamespace+'.el', el);
$(el).data(pluginNamespace+'.parent', $(el).parent());
openedModalElement = $(el).appendTo(overlay).show();
} else {
openedModalElement = $(el).clone(true).appendTo(overlay).show();
}
}
if(localOptions.closeOnEsc) {
$(document).bind('keyup.'+pluginNamespace, function(e){
if(e.keyCode === 27) {
$.modal().close(localOptions);
}
});
}
if(localOptions.closeOnOverlayClick) {
$('.' + localOptions.overlayClass).on('click.' + pluginNamespace, function(e){
if (e.target.className == localOptions.overlayClass){
$.modal().close(localOptions);
}
});
}
$(document).bind('touchmove.'+pluginNamespace,function(e){
if(!$(e).parents('.' + localOptions.overlayClass)) {
e.preventDefault();
}
});
if(el) {
$(window).bind('onSelectAll',function(e){
e.parentEvent.preventDefault();
var range = null,
selection = null,
selectionElement = openedModalElement.get(0);
if (document.body.createTextRange) { //ms
range = document.body.createTextRange();
range.moveToElementText(selectionElement);
range.select();
} else if (window.getSelection) { //all others
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(selectionElement);
selection.removeAllRanges();
selection.addRange(range);
}
});
}
if(localOptions.onOpen) {
localOptions.onOpen(overlay, localOptions);
}
},
close: function(options) {
var el = els.get(0),
localOptions = $.extend({}, defaults, $(el).data(pluginNamespace+'.options'), options);
var overlay = $('.' + localOptions.overlayClass);
if ($.isFunction(localOptions.onBeforeClose)) {
if (localOptions.onBeforeClose(overlay, localOptions) === false) {
return;
}
}
if (!localOptions.cloning) {
if (!el) {
el = overlay.data(pluginNamespace+'.el');
}
$(el).hide().appendTo($(el).data(pluginNamespace+'.parent'));
}
overlay.remove();
unlockContainer(localOptions);
if(localOptions.closeOnEsc) {
$(document).unbind('keyup.'+pluginNamespace);
}
$(window).unbind('onSelectAll');
if(localOptions.onClose) {
localOptions.onClose(overlay, localOptions);
}
}
};
}
$.modal = function(options){
return init($(), options);
};
$.fn.modal = function(options) {
return init(this, options);
};
})(jQuery, window, document);