-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.popapilus.js
222 lines (171 loc) · 5.95 KB
/
jquery.popapilus.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
/*!
* Popapilus v.0.9 - 2013-10-03
* jQuery plugin for creating popups: modal windows, hints etc.
* http://github.com/dymio/popapilus
* (c) 2013 Ivan Dymkov ( http://dymio.net , @dymio )
* license: http://www.opensource.org/licenses/mit-license.php
*
* requires jQuery 1.7.1 or higher
*/
(function ( $, document, window ) {
// Plugin bridge
$.popapilus = function( options ) {
var opts = $.extend( {}, $.popapilus.defaults, options );
return new Popapilus( opts );
};
// Plugin defaults
$.popapilus.defaults = {
css_class: 'popapilus', // can't change after init
no_overlay: false, // can't change after init
overlay_css_class: 'popapilus_overlay', // can't change after init
overlay_z_index: 3998, // can't change after init
overlay_show_animation_speed: 0,
close_on_overlay_click: true,
fixed: true, // fixed or absolute position of block
z_index: 3999,
modal: true, // works only if no_overlay == false
centered: true, // always works by x axis but by y axis only if fixed
top: null, // don't works if centered && fixed
right: null, // works if not centered
bottom: null, // don't works if centered && fixed
left: null, // works if not centered
width: null,
height: null,
ignore_close_btn: false,
show_animation_speed: 0,
autoclose_time: 0
};
/** Overlay worker **/
function PopapilusOverlay( options ) {
var self = this;
var overlay = null;
var _is_visible = false;
this.onClickFunction = null;
this.show = function( show_options ) {
if ( _is_visible ) return false;
var opts = $.extend( {}, options, show_options );
overlay.fadeIn( opts.overlay_show_animation_speed );
_is_visible = true;
}
this.hide = function() {
if ( !_is_visible ) return false;
overlay.hide();
_is_visible = false;
}
this.init = function() {
$("body").append('<div class="' + options.overlay_css_class + '"></div>');
overlay = $("body ." + options.overlay_css_class + ":last");
overlay.css({
display: 'none',
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
'z-index': options.overlay_z_index
});
overlay.click(function(evnt) {
evnt.preventDefault();
if (self.onClickFunction) self.onClickFunction( evnt );
});
}
this.init();
}
/** Main worker**/
function Popapilus( options ) {
var self = this;
var overlay = null;
var holder = null;
var _is_visible = false;
var _session_opts = {};
this.__defineGetter__("isVisible", function() { return _is_visible; });
function setHolderStyles() {
var opts = _session_opts;
holder.attr('style', 'display: none;'); // flush old styles
holder.css({
position: opts.fixed ? 'fixed' : 'absolute',
'z-index': opts.z_index
});
if (opts.width !== null) holder.css('width', opts.width);
if (opts.height !== null) holder.css('height', opts.height);
if (!(opts.centered && opts.fixed)) {
if (opts.top !== null) holder.css('top', opts.top);
if (opts.bottom !== null) holder.css('bottom', opts.bottom);
}
if (!opts.centered) {
if (opts.right !== null) holder.css('right', opts.right);
if (opts.left !== null) holder.css('left', opts.left);
}
if (holder.css('top') === "auto") { holder.css('top', 0); }
}
function centerHolder() {
holder.css('left', Math.max(0, ($(window).width() / 2) - (holder.width() / 2)).toString() + 'px' ); // by x axis
if (_session_opts.fixed)
holder.css('top', Math.max(0, ($(window).height() / 2) - (holder.height() / 2)).toString() + 'px' ); // by y axis if position is 'fixed'
}
function _close() {
self.hide();
}
function inactivate() {
$(window).off('resize', centerHolder);
if (overlay) overlay.onClickFunction = null;
holder.find(".close").off('click', _close);
}
function fillWithData( data ) {
holder.empty();
holder.append(data);
}
function _showOverlay() {
if (_session_opts.modal && overlay) { overlay.show(_session_opts); }
}
this.show = function( data, show_options ) {
_session_opts = $.extend( {}, options, show_options );
if (_is_visible) { inactivate(); }
if (data) { fillWithData( data ); }
if (!_session_opts.ignore_close_btn) {
holder.find(".close").on('click', _close);
}
setHolderStyles();
if (_session_opts.close_on_overlay_click && overlay) {
overlay.onClickFunction = function(evnt) { self.hide(); }
}
_showOverlay();
if (_session_opts.centered) {
$(window).on('resize', centerHolder);
centerHolder();
}
if (!_is_visible) { holder.fadeIn(_session_opts.show_animation_speed); }
_is_visible = true;
if (_session_opts.autoclose_time) {
setTimeout(function() { self.hide(); }, _session_opts.autoclose_time);
}
}
this.showOverlay = function( show_options ) {
_session_opts = $.extend( {}, options, show_options );
_showOverlay();
}
this.setData = function( data ) {
if (data) { fillWithData( data ); }
}
this.hide = function() {
inactivate();
holder.hide();
if (overlay) overlay.hide();
_is_visible = false;
_session_option = options;
}
/*
* Initialize function
*/
function _init() {
if (!options.no_overlay) { overlay = new PopapilusOverlay(options); }
// create a popapilus DOM block
$("body").append('<div class="' + options.css_class + '"></div>');
holder = $("body ." + options.css_class + ":last");
self.hide();
setHolderStyles();
}
// rock'n'roll
_init();
}
}( jQuery, document, window ));