-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpopelt-v1.0-source.js
353 lines (300 loc) · 9.24 KB
/
popelt-v1.0-source.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
/*=================================================================================================
* @name: Popelt
* @type: jQuery
* @author: (c) Elton Jain - @eltonjain
* @demo: http://welbour.com
* @version: 1.0
* @requires jQuery 1.9.1
*=================================================================================================*/
(function ($) {
// imaginary global variables in scope
var _popups_open = [],
_popups_open_count = 0,
_popups = null,
_popup_current_id = null;
function Popelt(params){
var _self = this;
//////////////////////////////////////
// VARIABLES
//////////////////////////////////////
var _d = $(document)
, _w = window
, _$w = $(_w)
, _wH = windowHeight()
, _wW = windowWidth()
, _prefix = '__pop'
, _btnCloseClass = 'pop-btn-close'
, _closeClass = 'pop-close'
, _popBtnClass = 'pop-btn'
, _popOverlayClass = 'pop-screen'
, _popClass = 'pop-wrap'
, _popContentClass = 'pop-content'
, _popActionBlockClass = 'pop-action-block'
, _popTitleClass = 'pop-title'
, _id
, _zIndex = 9000
;
//////////////////////////////////////
// DEFAULTS
//////////////////////////////////////
var defaults = {
modal : false
, title : false
, content : ''
, offsetTop : -1
, closeButton : true // X close button
, closeBtnTooltip : 'Close'
, width : 600
, closeClass : ''
, escClose : true
, focus : null // Element to focus
, contentType : false // [iframe, ajax, #elementId]
, loadUrl : false // iframe/ajax URL
, buttons : []
, fadeSpeed : 200
, iframeHeight : '300px'
, iframeWidth : '100%'
, maxHeight : false
, valign : true
, responsive : false
, overlayColor : false
, overlayOpacity: false
};
var o = $.extend(defaults, params);
_self.init = function(){
_popups = (_popups || 0) + 1;
_id = _prefix + _popups + '__';
_zIndex += _popups;
};
_self.setContent = function(content){
o.content = content;
return _self;
};
_self.showPopup = function(){
// CREATE TEMPLATE ONLY IF IT DOESN'T EXISTS
// CHECK WITH POPUP ID
if($('.'+_popClass + '.'+_id).length <= 0) {
// Create template
var popHtml, popTitle, popContent, popOverlay, actionPanel;
popOverlay = $('<div>').addClass(_popOverlayClass).addClass(_id).css('z-index',_zIndex);
if(o.overlayColor){popOverlay.css('background',o.overlayColor);}
if(o.overlayOpacity){popOverlay.css('opacity',o.overlayOpacity).css('filter','alpha('+o.overlayOpacity*10+')');}
popContent = $('<div>').addClass(_popContentClass);
if(o.title){
popTitle = $('<div>').addClass(_popTitleClass).text(o.title);
}
if(o.content){
popContent.append(o.content);
}
if(o.buttons.length){
actionPanel = $('<div>').addClass(_popActionBlockClass);
$.each(o.buttons, function( k, v ){
var classname = v.classname,
clickEvent = v.clickEvent;
if( $.isFunction(classname) ) {clickEvent = classname; classname = '';}
var btn = $('<button>')
.addClass(_popBtnClass).addClass(v.classname)
.text(v.label);
if(v.clickEvent) {btn.on('click', v.clickEvent);}
actionPanel.append(btn);
});
}
// Adding max height to content container
if(o.maxHeight){
popContent.css('max-height',o.maxHeight);
}
popBlock = $('<div>').addClass('pop-block');
if(o.closeButton !== false) {
var closeButton = $('<span>').addClass('pop-btn-close').html('×');
if(o.closeBtnTooltip){closeButton.attr('title',o.closeBtnTooltip);}
popBlock.append(closeButton);
}
popBlock.append(popTitle).append(popContent).append(actionPanel);
popContainer = $('<div>').addClass('pop-container').width(o.width).append(popBlock);
popContainer.css('margin','0 auto');
if(o.offsetTop >= 0){
popContainer.css('margin-top',o.offsetTop);
}
if(o.responsive === true){
popContainer.css('max-width','100%');
}
popHtml = $('<div>').addClass(_popClass).addClass(_id).css('z-index',_zIndex+1).append(popContainer);
$('body').append(popOverlay);
$('body').append(popHtml);
}
// if external content, first show popup and fetch and directly show data in dom element
if( o.contentType || o.loadUrl ) {
renderContent();
}
// Show popup and overlay
$('.'+_popOverlayClass + '.'+_id).show();
$('.'+_popClass + '.'+_id).show();
bindEvents();
recenter();
// Update number of open popups
_popups_open_count++;
_popups_open.push(_id);
_popup_current_id = _id;
// Adding noscroll to body
if($('body').not('.noscroll')){$('body').addClass('noscroll');}
return _self;
};
_self.addButton = function(label, classname, clickEvent){
var btn = {label: label};
if($.isFunction(classname)){
clickEvent = classname;
classname = '';
}
btn.classname = classname;
if($.isFunction(clickEvent)){
btn.clickEvent = clickEvent;
} else {
btn.clickEvent = _self.close; // if no event, trigger popup close
}
o.buttons.push(btn)
return btn;
};
// OK Button
_self.addOKButton = function(clickEvent){
_self.addButton('OK',clickEvent);
};
// Cancel Button
_self.addCancelButton = function(){
_self.addButton('Cancel',_self.close);
};
// Close Button
_self.addCloseButton = function(){
_self.addButton('Close',_self.close);
};
_self.closePopup = function(e){
// closing specific popup if given, else
popupId = _popup_current_id ? _popup_current_id : _id;
unbindEvents(popupId);
var popWrap = $('.'+_popClass + '.'+popupId),
popOverlay = $('.'+_popOverlayClass + '.'+popupId);
popWrap.fadeTo(o.fadeSpeed, 0, function() {
$(this).remove();
});
popOverlay.fadeTo(o.fadeSpeed, 0, function() {
$(this).remove();
});
_popups_open_count--;
_popups_open.pop();
_popup_current_id = _popups_open[_popups_open.length-1];
if(_popups_open_count == 0) $('body').removeClass('noscroll');
return false;
};
// Alias
_self.show = _self.showPopup;
_self.close = _self.closePopup;
_self.init();
return this;
//////////////////////////////////////
// PRIVATE FUNCTIONS
//////////////////////////////////////
function windowHeight(){
return _w.innerHeight || _$w.height();
}
function windowWidth(){
return _w.innerWidth || _$w.width();
}
function bindEvents(){
// bind close buttons and overlay
var popWrap = '.'+_popClass + '.'+_id,
popOverlay = "."+_popOverlayClass + '.'+_id;
$(popWrap).on('click', '.'+_closeClass, _self.close);
if(o.closeClass){
o.closeClass = o.closeClass.replace('.', '');
$(popWrap).on('click', '.'+o.closeClass, _self.close);
}
if(o.closeButton === true){
$(popWrap).on('click', '.'+_btnCloseClass, _self.close);
}
if(o.modal !== true){
$('body').on('click', popOverlay+', '+popWrap, _self.close);
$('body').on('click', popWrap + ' .pop-block', function(e){
e.stopPropagation();
});
}
if (o.escClose !== false) {
enableEscClose();
}
// recenter
_$w.on('resize', recenter);
}
function unbindEvents(popupId){
// bind close buttons and overlay
var popWrap = '.'+_popClass + '.'+popupId,
popOverlay = "."+_popOverlayClass + '.'+popupId;
// popwrap and overlay click
$(popOverlay+', '+popWrap).off('click');
// close buttons click
$(popWrap + '.'+_closeClass).off('click');
if(o.closeClass){
$(popWrap + '.' + o.closeClass.replace('.', '')).off('click');
}
//escape close
_d.off('keydown.'+popupId);
}
function enableEscClose(){
_d.on('keydown.'+_id, function(e) {
if (e.which == 27) { //escape
e.stopImmediatePropagation();
_self.close(_popup_current_id);
return false;
}
});
}
function updateDomContent(content){
// set content directly into dom wrapper
var contentContainer = '.'+_popClass + '.'+_id + ' .'+_popContentClass;
_self.setContent(content);
$(contentContainer).append(content);
enableFocus();
recenter();
}
function renderContent(){
if(o.contentType == 'ajax' && o.loadUrl){
// load URL via ajax
$.get(o.loadUrl, function(data){
var content = $('<div class="pop-ajax-wrapper"></div>').append(data);
updateDomContent(content);
return false;
});
} else if (o.contentType == 'iframe' && o.loadUrl){
var iframe = $('<iframe class="pop-iframe"></iframe>');
iframe.attr('src', o.loadUrl);
iframe.css({
'border': 0,
'width' : o.iframeWidth,
'height': o.iframeHeight
});
updateDomContent(iframe);
// dynamic height - coming soon
} else if (o.contentType.indexOf('#') == 0){ // if template from #elementId
var content = $(o.contentType).html();
updateDomContent(content);
}
}
function enableFocus(){
var popup = '.'+_popClass + ' ' + '.'+_popContentClass;
if(o.focus === true){
$(popup).find('input,textarea').first().focus();
}
}
function recenter(){
// if valign is not false and offsetTop is not given
if( (o.offsetTop !== '' && o.offsetTop >= 0) || o.valign === false ){
return false;
}
// do recenter
var contentWrap = '.'+_popClass+'.'+_id + ' .pop-block',
_content_w = $(contentWrap).outerWidth(),
_content_h = $(contentWrap).outerHeight();
_new_top = ((_wH - _content_h)/2)-20;
_new_top = _new_top > 0 ? _new_top : 0;
$('.'+_popClass+'.'+_id + ' .pop-container').css('margin-top', _new_top);
}
};
window.Popelt = Popelt;
} (jQuery));