forked from dinbror/bpopup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.bpopup.js
326 lines (308 loc) · 11.1 KB
/
jquery.bpopup.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
/*===================================================================================================================
* @name: bPopup
* @type: jQuery
* @author: (c) Bjoern Klinggaard - @bklinggaard
* @demo: http://dinbror.dk/bpopup
* @version: 0.9.3
* @requires jQuery 1.4.3
* todo: refactor
*==================================================================================================================*/
;(function($) {
$.fn.bPopup = function(options, callback) {
if ($.isFunction(options)) {
callback = options;
options = null;
}
// OPTIONS
var o = $.extend({}, $.fn.bPopup.defaults, options);
// HIDE SCROLLBAR?
if (!o.scrollBar)
$('html').css('overflow', 'hidden');
// VARIABLES
var $popup = this
, d = $(document)
, w = $(window)
, prefix = '__b-popup'
, isIOS6X = (/OS 6(_\d)+/i).test(navigator.userAgent) // Used for a temporary fix for ios6 timer bug when using zoom/scroll
, buffer = 20
, popups = 0
, closing
, id
, inside
, fixedVPos
, fixedHPos
, fixedPosStyle
, vPos
, hPos
, height
, width
, debounce
;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTION - call it: $(element).bPopup().close();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
$popup.close = function() {
o = this.data('bPopup');
id = prefix + w.data('bPopup') + '__';
close();
};
$popup.reposition = function() {
reposition();
}
return $popup.each(function() {
if ($(this).data('bPopup')) return; //POPUP already exists?
init();
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS - PRIVATE
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function init() {
triggerCall(o.onOpen);
popups = (w.data('bPopup') || 0) + 1, id = prefix + popups + '__',fixedVPos = o.position[1] !== 'auto', fixedHPos = o.position[0] !== 'auto', fixedPosStyle = o.positionStyle === 'fixed', height = $popup.outerHeight(true), width = $popup.outerWidth(true);
o.loadUrl ? createContent() : open();
}
function createContent() {
o.contentContainer = $(o.contentContainer || $popup);
switch (o.content) {
case ('iframe'):
var iframe = $('<iframe class="b-iframe" scrolling="no" frameborder="0"></iframe>');
iframe.appendTo(o.contentContainer);
height = $popup.outerHeight(true);
width = $popup.outerWidth(true);
open();
iframe.attr('src', o.loadUrl); // setting iframe src after open due IE9 bug
triggerCall(o.loadCallback);
break;
case ('image'):
open();
$('<img />')
.load(function() {
triggerCall(o.loadCallback);
recenter($(this));
}).attr('src', o.loadUrl).hide().appendTo(o.contentContainer);
break;
default:
open();
$('<div class="b-ajax-wrapper"></div>')
.load(o.loadUrl, o.loadData, function(){
triggerCall(o.loadCallback);
recenter($(this));
}).hide().appendTo(o.contentContainer);
break;
}
}
function open(){
// MODAL OVERLAY
if (o.modal) {
$('<div class="b-modal '+id+'"></div>')
.css({backgroundColor: o.modalColor, position: 'fixed', top: 0, right:0, bottom:0, left: 0, opacity: 0, zIndex: o.zIndex + popups})
.appendTo(o.appendTo)
.fadeTo(o.speed, o.opacity);
}
// POPUP
calPosition();
$popup
.data('bPopup', o).data('id',id)
.css({ 'left': o.transition === 'slideIn' ? (hPos + width) *-1 : getLeft(!(!o.follow[0] && fixedHPos || fixedPosStyle)), 'position': o.positionStyle || 'absolute', 'top': o.transition === 'slideDown' ? (vPos + width) *-1 : getTop(!(!o.follow[1] && fixedVPos || fixedPosStyle)), 'z-index': o.zIndex + popups + 1 })
.each(function() {
if(o.appending) {
$(this).appendTo(o.appendTo);
}
});
doTransition(true);
}
function close() {
if (o.modal) {
$('.b-modal.'+$popup.data('id'))
.fadeTo(o.speed, 0, function() {
$(this).remove();
});
}
// Clean up
unbindEvents();
// Close trasition
doTransition();
return false; // Prevent default
}
function reposition() {
inside = insideWindow();
if(inside){
clearTimeout(debounce);
debounce = setTimeout(function(){
calPosition();
$popup
.dequeue()
.each(function() {
if(fixedPosStyle) {
$(this).css({ 'left': hPos, 'top': vPos });
}
else {
$(this).animate({ 'left': o.follow[0] ? getLeft(true) : 'auto', 'top': o.follow[1] ? getTop(true) : 'auto' }, o.followSpeed, o.followEasing);
}
});
}, 50);
}
}
//Eksperimental
function recenter(content){
var _width = content.width(), _height = content.height(), css = {};
o.contentContainer.css({height:_height,width:_width});
if (_height >= $popup.height()){
css.height = $popup.height();
}
if(_width >= $popup.width()){
css.width = $popup.width();
}
height = $popup.outerHeight(true)
, width = $popup.outerWidth(true);
calPosition();
o.contentContainer.css({height:'auto',width:'auto'});
css.left = getLeft(!(!o.follow[0] && fixedHPos || fixedPosStyle)),
css.top = getTop(!(!o.follow[1] && fixedVPos || fixedPosStyle));
$popup
.animate(
css
, 250
, function() {
content.show();
inside = insideWindow();
}
);
}
function bindEvents() {
w.data('bPopup', popups);
$popup.on('click', '.bClose, .' + o.closeClass, close); // legacy, still supporting the close class bClose
if (o.modalClose) {
$('.b-modal.'+id).css('cursor', 'pointer').bind('click', close);
}
// Temporary disabling scroll/resize events on devices with IOS6+
// due to a bug where events are dropped after pinch to zoom
if (!isIOS6X && (o.follow[0] || o.follow[1])) {
w.bind('scroll.'+id, function() {
if(inside){
$popup
.dequeue()
.animate({ 'left': o.follow[0] ? getLeft(!fixedPosStyle) : 'auto', 'top': o.follow[1] ? getTop(!fixedPosStyle) : 'auto' }, o.followSpeed, o.followEasing);
}
}).bind('resize.'+id, function() {
reposition();
});
}
if (o.escClose) {
d.bind('keydown.'+id, function(e) {
if (e.which == 27) { //escape
var topZIndex = o.zIndex + $(window).data('bPopup') + 1;
if (o.escCloseAll || $popup.css("zIndex") == topZIndex) {
close();
}
}
});
}
}
function unbindEvents() {
if (!o.scrollBar) {
$('html').css('overflow', 'auto');
}
$('.b-modal.'+id).unbind('click');
d.unbind('keydown.'+id);
w.unbind('.'+id).data('bPopup', (w.data('bPopup')-1 > 0) ? w.data('bPopup')-1 : null);
$popup.off('click', '.bClose, .' + o.closeClass).data('bPopup', null);
}
function doTransition(open) {
switch (o.transition) {
case "slideIn":
$popup
.css({display: 'block',opacity: 1})
.animate({
left: open ? getLeft(!(!o.follow[0] && fixedHPos || fixedPosStyle)) : d.scrollLeft() - (width || $popup.outerWidth(true)) - 200
},o.speed, o.easing, function(){onCompleteCallback(open);});
break;
case "slideDown":
$popup
.css({display: 'block',opacity: 1})
.animate({
top: open ? getTop(!(!o.follow[1] && fixedVPos || fixedPosStyle)) : d.scrollTop() - (height || $popup.outerHeight(true)) - 200
},o.speed, o.easing, function(){onCompleteCallback(open);});
break;
default:
//Typing 1 and 0 to ensure opacity 1 and not 0.9999998
$popup.stop().fadeTo(o.speed, open ? 1 : 0, function(){onCompleteCallback(open);});
}
}
function onCompleteCallback(open){
if(open){
bindEvents();
triggerCall(callback);
} else {
$popup.hide();
triggerCall(o.onClose);
if (o.loadUrl) {
o.contentContainer.empty();
$popup.css({height: 'auto', width: 'auto'});
}
}
}
function getLeft(includeScroll){
return includeScroll ? hPos + d.scrollLeft() : hPos;
}
function getTop(includeScroll){
return includeScroll ? vPos + d.scrollTop() : vPos;
}
function triggerCall(func) {
$.isFunction(func) && func.call($popup);
}
function calPosition(){
vPos = fixedVPos ? o.position[1] : getYCoord()
, hPos = fixedHPos ? o.position[0] : getXCoord()
, inside = insideWindow();
}
function getYCoord(){
var y = (((windowHeight()- $popup.outerHeight(true)) / 2) - o.amsl);
return (y < buffer ? buffer : y);
}
function getXCoord(){
return ((windowWidth() - $popup.outerWidth(true)) / 2);
}
function insideWindow(){
return windowHeight() > $popup.outerHeight(true)+buffer && windowWidth() > $popup.outerWidth(true)+buffer;
}
function windowHeight(){
return window.innerHeight || w.height();
}
function windowWidth(){
return window.innerWidth || w.width();
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFAULT VALUES
////////////////////////////////////////////////////////////////////////////////////////////////////////////
$.fn.bPopup.defaults = {
amsl: 50
, appending: true
, appendTo: 'body'
, closeClass: 'b-close'
, content: 'ajax' // ajax, iframe or image
, contentContainer: false
, easing: 'swing'
, escClose: true
, escCloseAll: true
, follow: [true, true] // x, y
, followEasing: 'swing'
, followSpeed: 500
, loadCallback: false
, loadData: false
, loadUrl: false
, modal: true
, modalClose: true
, modalColor: '#000'
, onClose: false
, onOpen: false
, opacity: 0.7
, position: ['auto', 'auto'] // x, y,
, positionStyle: 'absolute'// absolute or fixed
, scrollBar: true
, speed: 250 // open & close speed
, transition: 'fadeIn' //transitions: fadeIn, slideDown, slideIn
, zIndex: 9997 // popup gets z-index 9999, modal overlay 9998
};
})(jQuery);