-
Notifications
You must be signed in to change notification settings - Fork 9
/
tools.overlay.js
339 lines (257 loc) · 8.23 KB
/
tools.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
/**
* tools.overlay 1.1.2 - Overlay HTML with eye candy.
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/overlay.html
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Launch : March 2008
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
// static constructs
$.tools = $.tools || {};
$.tools.overlay = {
version: '1.1.2',
addEffect: function(name, loadFn, closeFn) {
effects[name] = [loadFn, closeFn];
},
conf: {
top: '10%',
left: 'center',
absolute: false,
speed: 'normal',
closeSpeed: 'fast',
effect: 'default',
close: null,
oneInstance: true,
closeOnClick: true,
closeOnEsc: true,
api: false,
expose: null,
// target element to be overlayed. by default taken from [rel]
target: null
}
};
var effects = {};
// the default effect. nice and easy!
$.tools.overlay.addEffect('default',
/*
onLoad/onClose functions must be called otherwise none of the
user supplied callback methods won't be called
*/
function(onLoad) {
this.getOverlay().fadeIn(this.getConf().speed, onLoad);
}, function(onClose) {
this.getOverlay().fadeOut(this.getConf().closeSpeed, onClose);
}
);
var instances = [];
function Overlay(trigger, conf) {
// private variables
var self = this,
$self = $(this),
w = $(window),
closers,
overlay,
opened,
expose = conf.expose && $.tools.expose.version;
// get overlay and triggerr
var jq = conf.target || trigger.attr("rel");
overlay = jq ? $(jq) : null || trigger;
// overlay not found. cannot continue
if (!overlay.length) { throw "Could not find Overlay: " + jq; }
// if trigger is given - assign it's click event
if (trigger && trigger.index(overlay) == -1) {
trigger.click(function(e) {
self.load(e);
return e.preventDefault();
});
}
// bind all callbacks from configuration
$.each(conf, function(name, fn) {
if ($.isFunction(fn)) { $self.bind(name, fn); }
});
// API methods
$.extend(self, {
load: function(e) {
// can be opened only once
if (self.isOpened()) { return self; }
// find the effect
var eff = effects[conf.effect];
if (!eff) { throw "Overlay: cannot find effect : \"" + conf.effect + "\""; }
// close other instances?
if (conf.oneInstance) {
$.each(instances, function() {
this.close(e);
});
}
// onBeforeLoad
e = e || $.Event();
e.type = "onBeforeLoad";
$self.trigger(e);
if (e.isDefaultPrevented()) { return self; }
// opened
opened = true;
// possible expose effect
if (expose) { overlay.expose().load(e); }
// calculate end position
var top = conf.top;
var left = conf.left;
// get overlay dimensions
var oWidth = overlay.outerWidth({margin:true});
var oHeight = overlay.outerHeight({margin:true});
if (typeof top == 'string') {
top = top == 'center' ? Math.max((w.height() - oHeight) / 2, 0) :
parseInt(top, 10) / 100 * w.height();
}
if (left == 'center') { left = Math.max((w.width() - oWidth) / 2, 0); }
if (!conf.absolute) {
top += w.scrollTop();
left += w.scrollLeft();
}
// position overlay
overlay.css({top: top, left: left, position: 'absolute'});
// onStart
e.type = "onStart";
$self.trigger(e);
// load effect
eff[0].call(self, function() {
if (opened) {
e.type = "onLoad";
$self.trigger(e);
}
});
// when window is clicked outside overlay, we close
if (conf.closeOnClick) {
$(document).bind("click.overlay", function(e) {
if (!self.isOpened()) { return; }
var et = $(e.target);
if (et.parents(overlay).length > 1) { return; }
$.each(instances, function() {
this.close(e);
});
});
}
// keyboard::escape
if (conf.closeOnEsc) {
// one callback is enough if multiple instances are loaded simultaneously
$(document).unbind("keydown.overlay").bind("keydown.overlay", function(e) {
if (e.keyCode == 27) {
$.each(instances, function() {
this.close(e);
});
}
});
}
return self;
},
close: function(e) {
if (!self.isOpened()) { return self; }
e = e || $.Event();
e.type = "onBeforeClose";
$self.trigger(e);
if (e.isDefaultPrevented()) { return; }
opened = false;
// close effect
effects[conf.effect][1].call(self, function() {
e.type = "onClose";
$self.trigger(e);
});
// if all instances are closed then we unbind the keyboard / clicking actions
var allClosed = true;
$.each(instances, function() {
if (this.isOpened()) { allClosed = false; }
});
if (allClosed) {
$(document).unbind("click.overlay").unbind("keydown.overlay");
}
return self;
},
// @deprecated
getContent: function() {
return overlay;
},
getOverlay: function() {
return overlay;
},
getTrigger: function() {
return trigger;
},
getClosers: function() {
return closers;
},
isOpened: function() {
return opened;
},
// manipulate start, finish and speeds
getConf: function() {
return conf;
},
// bind
bind: function(name, fn) {
$self.bind(name, fn);
return self;
},
// unbind
unbind: function(name) {
$self.unbind(name);
return self;
}
});
// callbacks
$.each("onBeforeLoad,onStart,onLoad,onBeforeClose,onClose".split(","), function(i, ev) {
self[ev] = function(fn) {
return self.bind(ev, fn);
};
});
// exposing effect
if (expose) {
// expose configuration
if (typeof conf.expose == 'string') { conf.expose = {color: conf.expose}; }
$.extend(conf.expose, {
api: true,
closeOnClick: conf.closeOnClick,
// only overlay control's the esc button
closeOnEsc: false
});
// initialize expose api
var ex = overlay.expose(conf.expose);
ex.onBeforeClose(function(e) {
self.close(e);
});
self.onClose(function(e) {
ex.close(e);
});
}
// close button
closers = overlay.find(conf.close || ".close");
if (!closers.length && !conf.close) {
closers = $('<div class="close"></div>');
overlay.prepend(closers);
}
closers.click(function(e) {
self.close(e);
});
}
// jQuery plugin initialization
$.fn.overlay = function(conf) {
// already constructed --> return API
var el = this.eq(typeof conf == 'number' ? conf : 0).data("overlay");
if (el) { return el; }
if ($.isFunction(conf)) {
conf = {onBeforeLoad: conf};
}
var globals = $.extend({}, $.tools.overlay.conf);
conf = $.extend(true, globals, conf);
this.each(function() {
el = new Overlay($(this), conf);
instances.push(el);
$(this).data("overlay", el);
});
return conf.api ? el: this;
};
})(jQuery);