-
Notifications
You must be signed in to change notification settings - Fork 6
/
facebox.js
189 lines (165 loc) · 5.78 KB
/
facebox.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
/* Facebox for Prototype, version 2.0
* By Robert Gaal - http://wakoopa.com
*
* Heavily based on Facebox by Chris Wanstrath - http://famspam.com/facebox
* First ported to Prototype by Phil Burrows - http://blog.philburrows.com
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Need help? Join the Google Groups mailing list:
* http://groups.google.com/group/facebox/
*
* Dependencies: prototype & script.aculo.us + images & CSS files from original facebox
* Usage: Append 'rel="facebox"' to an element to call it inside a so-called facebox
*
*--------------------------------------------------------------------------*/
var Facebox = Class.create({
initialize : function(extra_set){
this.settings = {
loading_image : '/facebox/loading.gif',
close_image : '/facebox/closelabel.gif',
image_types : new RegExp('\.' + ['png', 'jpg', 'jpeg', 'gif'].join('|') + '$', 'i'),
inited : true,
facebox_html : '\
<div id="facebox" style="display:none;"> \
<div class="popup"> \
<table id="facebox_table"> \
<tbody> \
<tr> \
<td class="tl"/><td class="b"/><td class="tr"/> \
</tr> \
<tr> \
<td class="b"/> \
<td class="body"> \
<div class="content"> \
</div> \
<div class="footer"> \
<a href="#" class="close"> \
<img src="/facebox/closelabel.gif" title="close" class="close_image" /> \
</a> \
</div> \
</td> \
<td class="b"/> \
</tr> \
<tr> \
<td class="bl"/><td class="b"/><td class="br"/> \
</tr> \
</tbody> \
</table> \
</div> \
</div>'
};
if (extra_set) Object.extend(this.settings, extra_set);
$(document.body).insert({bottom: this.settings.facebox_html});
this.preload = [ new Image(), new Image() ];
this.preload[0].src = this.settings.close_image;
this.preload[1].src = this.settings.loading_image;
f = this;
$$('#facebox .b:first, #facebox .bl, #facebox .br, #facebox .tl, #facebox .tr').each(function(elem){
f.preload.push(new Image());
f.preload.slice(-1).src = elem.getStyle('background-image').replace(/url\((.+)\)/, '$1');
});
this.facebox = $('facebox');
this.keyPressListener = this.watchKeyPress.bindAsEventListener(this);
this.watchClickEvents();
fb = this;
Event.observe($$('#facebox .close').first(), 'click', function(e){
Event.stop(e);
fb.close()
});
Event.observe($$('#facebox .close_image').first(), 'click', function(e){
Event.stop(e);
fb.close()
});
},
watchKeyPress : function(e){
// Close if espace is pressed or if there's a click outside of the facebox
if (e.keyCode == 27 || !Event.element(e).descendantOf(this.facebox)) this.close();
},
watchClickEvents : function(e){
var f = this;
$$('a[rel=facebox]').each(function(elem,i){
Event.observe(elem, 'click', function(e){
Event.stop(e);
f.click_handler(elem, e);
});
});
},
loading : function() {
if ($$('#facebox .loading').length == 1) return true;
contentWrapper = $$('#facebox .content').first();
contentWrapper.childElements().each(function(elem, i){
elem.remove();
});
contentWrapper.insert({bottom: '<div class="loading"><img src="'+this.settings.loading_image+'"/></div>'});
var pageScroll = document.viewport.getScrollOffsets();
this.facebox.setStyle({
'top': pageScroll.top + (document.viewport.getHeight() / 10) + 'px',
'left': document.viewport.getWidth() / 2 - (this.facebox.getWidth() / 2) + 'px'
});
Event.observe(document, 'keypress', this.keyPressListener);
Event.observe(document, 'click', this.keyPressListener);
},
reveal : function(data, klass){
this.loading();
load = $$('#facebox .loading').first();
if(load) load.remove();
contentWrapper = $$('#facebox .content').first();
if (klass) contentWrapper.addClassName(klass);
contentWrapper.insert({bottom: data});
$$('#facebox .body').first().childElements().each(function(elem,i){
elem.show();
});
if(!this.facebox.visible()) new Effect.Appear(this.facebox, {duration: .3});
this.facebox.setStyle({
'left': document.viewport.getWidth() / 2 - (this.facebox.getWidth() / 2) + 'px'
});
Event.observe(document, 'keypress', this.keyPressListener);
Event.observe(document, 'click', this.keyPressListener);
},
close : function(){
new Effect.Fade('facebox', {duration: .3});
},
click_handler : function(elem, e){
this.loading();
Event.stop(e);
// support for rel="facebox[.inline_popup]" syntax, to add a class
var klass = elem.rel.match(/facebox\[\.(\w+)\]/);
if (klass) klass = klass[1];
new Effect.Appear(this.facebox, {duration: .3});
if (elem.href.match(/#/)){
var url = window.location.href.split('#')[0];
var target = elem.href.replace(url+'#','');
// var data = $$(target).first();
var d = $(target);
// create a new element so as to not delete the original on close()
var data = new Element(d.tagName);
data.innerHTML = d.innerHTML;
this.reveal(data, klass);
} else if (elem.href.match(this.settings.image_types)) {
var image = new Image();
fb = this;
image.onload = function() {
fb.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
}
image.src = elem.href;
} else {
var fb = this;
var url = elem.href;
new Ajax.Request(url, {
method : 'get',
onFailure : function(transport){
fb.reveal(transport.responseText, klass);
},
onSuccess : function(transport){
fb.reveal(transport.responseText, klass);
}
});
}
}
});
var facebox;
document.observe('dom:loaded', function(){
facebox = new Facebox();
});