-
Notifications
You must be signed in to change notification settings - Fork 2
/
ender.overlay.js
560 lines (481 loc) · 12.1 KB
/
ender.overlay.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*!
* Ender-Overlay: Highly Customizable Overlay for Ender
* copyright Andras Nemeseri @nemeseri 2012 | License MIT
* https://github.com/nemeseri/ender-overlay
*/
!function ($) {
var is,
transition;
// from valentine
is = {
fun: function (f) {
return typeof f === 'function';
},
arr: function (ar) {
return ar instanceof Array;
},
obj: function (o) {
return o instanceof Object && !is.fun(o) && !is.arr(o);
}
};
/*
Based on Bootstrap
Mozilla and Webkit support only
*/
transition = (function () {
var st = document.createElement('div').style,
transitionEnd = 'TransitionEnd',
transitionProp = 'Transition',
support = st.transition !== undefined ||
st.WebkitTransition !== undefined ||
st.MozTransition !== undefined;
return support && {
prop: (function () {
if (st.WebkitTransition !== undefined) {
transitionProp = 'WebkitTransition';
} else if (st.MozTransition !== undefined) {
transitionProp = 'MozTransition';
}
return transitionProp;
}()),
end: (function () {
if (st.WebkitTransition !== undefined) {
transitionEnd = 'webkitTransitionEnd';
} else if (st.MozTransition !== undefined) {
transitionEnd = 'transitionend';
}
return transitionEnd;
}())
};
}());
function extend() {
// based on jQuery deep merge
var options, name, src, copy, clone,
target = arguments[0], i = 1, length = arguments.length;
for (; i < length; i += 1) {
if ((options = arguments[i]) !== null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue;
}
if (copy && (is.obj(copy))) {
clone = src && is.obj(src) ? src : {};
target[name] = extend(clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
}
function clone(obj) {
if (null === obj || 'object' !== typeof obj) {
return obj;
}
var copy = obj.constructor(),
attr;
for (attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = obj[attr];
}
}
return copy;
}
// from jquery
function proxy(fn, context) {
var slice = Array.prototype.slice,
args = slice.call(arguments, 2);
return function () {
return fn.apply(context, args.concat(slice.call(arguments)));
};
}
function animate(options) {
var el = options.el,
complete = options.complete ? options.complete : function () {},
animation,
dummy;
// no animation obj OR animation is not available,
// fallback to css and call the callback
if (! options.animation ||
! (el.animate || (options.css3transition && transition))) {
el.css(options.fallbackCss);
complete();
return;
}
// we will animate, apply start CSS
if (options.animStartCss) {
if (options.animStartCss.opacity === 0) {
options.animStartCss.opacity = 0.01; // ie quirk
}
el.css(options.animStartCss);
}
animation = options.animation;
// css3 setted, if available apply the css
if (options.css3transition && transition) {
dummy = el[0].offsetWidth; // force reflow; source: bootstrap
el[0].style[transition.prop] = 'all ' + animation.duration + 'ms ease-out';
// takaritas
delete animation.duration;
el.css(animation);
//el.unbind(transition.end);
el.on(transition.end, function () {
// delete transition properties and events
el.off(transition.end);
el[0].style[transition.prop] = 'none';
complete();
});
} else if (window.ender) {
// use morpheus
el.animate(extend(animation, {'complete': complete}));
} else {
// use animate from jquery
el.animate(animation, animation.duration, 'swing', complete);
}
}
/*
OverlayMask Constructor
*/
function OverlayMask(settings) {
this.init(settings);
}
OverlayMask.prototype = {
init: function (options) {
this.options = {
id: 'ender-overlay-mask',
zIndex: 9998,
opacity: 0.6,
color: "#777"
};
extend(this.options, options || {});
var $mask = $('#' + this.options.id),
opt = this.options;
if (! $mask.length) {
$mask = $('<div></div>')
.attr('id', this.options.id)
.css({
display: 'none',
position: 'absolute',
top: 0,
left: 0
})
.appendTo('body');
}
this.$mask = $mask;
},
show: function () {
// apply instance mask options
var opt = this.options,
docSize = this.getDocSize(),
animObj = false;
this.$mask.css({
zIndex: opt.zIndex,
backgroundColor: opt.color,
width: docSize.width,
height: docSize.height
});
if (opt.durationIn) {
animObj = {
opacity: opt.opacity,
duration: opt.durationIn
};
}
animate({
el: this.$mask,
animStartCss: {
opacity: 0.01, // ie quirk
display: 'block'
},
animation: animObj,
fallbackCss: {display: 'block', opacity: opt.opacity},
css3transition: opt.css3transition
});
},
hide: function () {
var opt = this.options,
self = this,
animObj = false;
if (opt.durationOut) {
animObj = {
opacity: 0,
duration: opt.durationOut
};
}
animate({
el: this.$mask,
animation: animObj,
complete: function () {
self.$mask.css({display: 'none'});
},
fallbackCss: {display: 'none'},
css3transition: opt.css3transition
});
},
getDocSize: function () {
if (window.ender) { // ender
return {
width: $.doc().width,
height: $.doc().height
};
} else if (window.Zepto) {
// zepto caches the document height..
var doc = window.document,
html = doc.documentElement,
ie = /msie/i.test(navigator.userAgent),
vp = {
width: ie ? html.clientWidth : self.innerWidth,
height: ie ? html.clientHeight : self.innerHeight
}
return {
width: Math.max(doc.body.scrollWidth, html.scrollWidth, vp.width),
height: Math.max(doc.body.scrollHeight, html.scrollHeight, vp.height)
};
} else { // jquery / zepto
return {
width: $(document).width(),
height: $(document).height()
};
}
},
getMask: function () {
return this.$mask;
}
};
/*
Overlay Constructor
*/
function Overlay(el, settings) {
this.init(el, settings);
// only return the API
// instead of this
return this.getApi();
}
Overlay.prototype = {
init: function ($el, options) {
this.options = {
top: 80,
position: 'absolute',
cssClass: 'ender-overlay',
close: '.close',
trigger: null,
zIndex: 9999,
showMask: true,
closeOnEsc: true,
closeOnMaskClick: true,
autoOpen: false,
allowMultipleDisplay: false,
// morpheus required for JS fallback
css3transition: false, // experimental
// start values before animation
startAnimationCss: {
opacity: 0.01 // ie quirk
},
// morpheus animation options
animationIn: {
opacity: 1,
duration: 250
},
animationOut: {
opacity: 0,
duration: 250
},
mask: {},
onBeforeOpen: function () {},
onBeforeClose: function () {},
onOpen: function () {},
onClose: function () {}
};
this.setOptions(options);
this.$overlay = $el.css({
display: 'none'
});
if (this.options.showMask) {
this.mask = new OverlayMask(this.options.mask);
}
// prevent multiple event binding
if (! this.$overlay.attr('data-overlayloaded')) {
this.attachEvents();
this.$overlay.attr('data-overlayloaded', 1);
}
if (this.options.autoOpen) {
this.open();
}
},
attachEvents: function () {
var self = this,
opt = this.options;
// Bind open method to trigger's click event
if (opt.trigger && $(opt.trigger).length) {
$(opt.trigger).on('click', function (e) {
e.preventDefault();
self.open();
});
}
this.$overlay.on('click', opt.close, function (e) {
e.preventDefault();
self.close();
});
// attach event listeners
$(document).on('ender-overlay.close', function () {
self.close();
});
$(document).on('ender-overlay.closeOverlay', function () {
self.close(true);
});
if (opt.closeOnEsc) {
$(document).on('keyup', function (e) {
self.onKeyUp(e);
});
}
if (this.mask && opt.closeOnMaskClick) {
this.mask.getMask().on('click', function () {
self.close();
});
}
},
setupOverlay: function () {
var opt = this.options,
topPos = opt.top,
scrollTop = $(window).scrollTop(),
overlayWidth = this.$overlay.width();
// setup overlay
this.$overlay
.addClass(opt.cssClass)
.appendTo('body');
if (opt.position === 'absolute') {
topPos += scrollTop;
}
// width is not defined explicitly
// so we try to find out
if (overlayWidth === 0) {
this.$overlay.css({
display: 'block',
position: 'absolute',
left: -9999
});
overlayWidth = this.$overlay.width();
}
this.$overlay.css({
display: 'none',
position: opt.position,
top: topPos,
left: '50%',
zIndex: opt.zIndex,
marginLeft: overlayWidth / 2 * -1
});
},
open: function (dontOpenMask) {
var opt = this.options,
self = this,
animationIn = opt.animationIn ? clone(opt.animationIn) : false,
api = this.getApi();
if (this.$overlay.css('display') === 'block' ||
opt.onBeforeOpen(api) === false) {
return;
}
this.setupOverlay();
if (! opt.allowMultipleDisplay) {
$(document).trigger('ender-overlay.closeOverlay');
}
animate({
el: this.$overlay,
animStartCss: extend({display: 'block'}, opt.startAnimationCss),
animation: animationIn,
complete: function () {
if (animationIn && animationIn.opacity === 1) {
self.$overlay.css({ 'filter': '' }); // ie quirk
}
self.options.onOpen(api);
},
fallbackCss: {display: 'block', opacity: 1},
css3transition: opt.css3transition
});
if (this.mask &&
typeof dontOpenMask === 'undefined') {
this.mask.show();
}
},
close: function (dontHideMask) {
var opt = this.options,
self = this,
animationOut = opt.animationOut ? clone(opt.animationOut) : false,
api = this.getApi();
if (opt.onBeforeClose(api) === false ||
this.$overlay.css('display') !== 'block') {
return;
}
animate({
el: this.$overlay,
animation: animationOut,
complete: function () {
self.$overlay.css({display: 'none'});
self.options.onClose(api);
},
fallbackCss: {display: 'none', opacity: 0},
css3transition: opt.css3transition
});
if (this.mask &&
typeof dontHideMask === 'undefined') {
this.mask.hide();
}
},
onKeyUp: function (e) {
if (e.keyCode === 27 &&
this.$overlay.css('display') !== 'none') {
this.close();
}
},
getOverlay: function () {
return this.$overlay;
},
getOptions: function () {
return this.options;
},
setOptions: function (options) {
extend(this.options, options || {});
var opt = this.options;
if (opt.animationIn === 'none') {
opt.animationIn = false;
}
if (opt.animationOut === 'none') {
opt.animationOut = false;
}
if (opt.showMask) {
// If there is no explicit duration set for OverlayMask
// set it from overlay animation
if (! opt.mask.durationIn && opt.animationIn && opt.animationIn.duration) {
opt.mask.durationIn = opt.animationIn.duration;
}
if (! opt.mask.durationOut && opt.animationOut && opt.animationOut.duration) {
opt.mask.durationOut = opt.animationOut.duration;
}
// no animation
if (! opt.mask.durationIn && ! opt.animationIn) {
opt.mask.durationIn = 0;
}
if (! opt.mask.durationOut && ! opt.animationOut) {
opt.mask.durationOut = 0;
}
if (typeof opt.mask.css3transition !== 'boolean') {
opt.mask.css3transition = opt.css3transition;
}
}
},
getApi: function () {
return {
open: proxy(this.open, this),
close: proxy(this.close, this),
getOverlay: proxy(this.getOverlay, this),
getOptions: proxy(this.getOptions, this),
setOptions: proxy(this.setOptions, this)
};
}
};
$.fn.overlay = function (options) {
var el = $(this).first();
return new Overlay(el, options);
};
}(window.ender || window.jQuery || window.Zepto);