forked from defunkt/facebox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
protofacebox.js
205 lines (171 loc) · 6.58 KB
/
protofacebox.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
var ProtoFacebox = Class.create({
initialize: function(options) {
var defaults = {
opacity : 0,
overlay : false,
loadingImage : 'loading.gif',
closeImage : 'closelabel.gif',
imageTypes : [ 'png', 'jpg', 'jpeg', 'gif' ],
faceboxHtml : '\
<div id="protofacebox" style="display:none;"> \
<div class="popup"> \
<table id="protofacebox_table"> \
<tbody> \
<tr> \
<td class="tl"/><td class="b"/><td class="tr"/> \
</tr> \
<tr> \
<td class="b"/> \
<td id="protofacebox_body"> \
<div id="protofacebox_content"> \
</div> \
<div class="footer"> \
<a href="#" id="protofacebox_close"> \
<img src="/facebox/closelabel.gif" title="close" id="protofacebox_close_image" /> \
</a> \
</div> \
</td> \
<td class="b"/> \
</tr> \
<tr> \
<td class="bl"/><td class="b"/><td class="br"/> \
</tr> \
</tbody> \
</table> \
</div> \
</div>'
};
this.settings = Object.extend(defaults, options || { });
console.log(this);
this.init();
if (this.settings) {
this.loading();
if (this.settings.ajax) this.fillFaceboxFromAjax(this.settings.ajax);
else if (this.settings.image) this.fillFaceboxFromImage(this.settings.image);
else if (this.settings.callback && Object.isFunction(this.settings.callback)) this.settings.callback.call(this);
else if (this.settings.div) this.reveal($(this.settings.div));
else if (this.settings.markup) this.reveal(this.settings.markup);
else console.log("Data type unknown. Can't display anything");
}
},
loading: function() {
if ($('protofacebox_loading') != null) return true;
this.showOverlay();
$('protofacebox_content').update();
$('protofacebox_body').insert('<div id="protofacebox_loading"><img src="' + this.settings.loadingImage + '" /></div>');
var top = getPageScroll()[1] + (getPageHeight() / 10);
var left = document.width / 2 - 205;
$('protofacebox').setStyle({
'top': top + 'px',
'left': left + 'px'
}).show();
// $(document).bind('keydown.facebox', function(e) {
// if (e.keyCode == 27) $.facebox.close()
// return true
// })
$(document).fire('ProtoFacebox:loading');
},
reveal: function(data) {
$(document).fire('ProtoFacebox:beforeReveal');
if (this.settings.class) $('protofacebox_content').addClassName(this.settings.class);
$('protofacebox_content').insert(data);
if ($('protofacebox_loading')) $('protofacebox_loading').remove();
$('protofacebox_body').appear();
$('protofacebox').setStyle({left: document.width / 2 - ($('protofacebox_table').getDimensions().width / 2)});
$(document).fire('ProtoFacebox:reveal')
$(document).fire('ProtoFacebox:afterReveal');
},
close: function() {
// $(document).unbind('keydown.facebox')
$('protofacebox').fade();
this.hideOverlay();
if ($('protofacebox_loading') != null) $('protofacebox_loading').remove();
if (this.settings.class) $('protofacebox_content').removeClassName(this.settings.class);
$(document).fire('ProtoFacebox:close');
return false;
},
init: function(settings) {
if (this.settings.inited) return true;
else this.settings.inited = true;
$(document).fire('ProtoFacebox:init');
var imageTypes = this.settings.imageTypes.join('|');
this.settings.imageTypesRegexp = new RegExp('\.(' + imageTypes + ')$', 'i');
if (settings)
extend(this.settings, settings);
$$('body')[0].insert(this.settings.faceboxHtml);
var preload = [ new Image(), new Image() ];
preload[0].src = this.settings.closeImage;
preload[1].src = this.settings.loadingImage;
$$('#protofacebox .b:first', '#protofacebox .bl', '#protofacebox .br', '#protofacebox .tl', '#protofacebox .tr').each(function(e) {
preload.push(new Image());
preload.slice(-1).src = e.getStyle('background-image').replace(/url\((.+)\)/, '$1');
})
$('protofacebox_close').observe('click', this.close.bind(this));
$('protofacebox_close_image').setAttribute('src', this.settings.closeImage);
},
skipOverlay: function() {
return this.settings.overlay == false || this.settings.opacity === null
},
showOverlay: function() {
if (this.skipOverlay()) return;
if ($('protofacebox_overlay') == null)
$$("body")[0].insert('<div id="protofacebox_overlay" class="protofacebox_hide"></div>');
$('protofacebox_overlay').hide();
$('protofacebox_overlay').addClassName("protofacebox_overlayBG")
.setOpacity(0.5)
.observe('click', function() { $(document).fire('ProtoFacebox:close') })
.appear({to: this.settings.opacity, duration: 5});
return false;
},
hideOverlay: function() {
if (this.skipOverlay()) return;
$('protofacebox_overlay').fade({duration: 0.2});
$("protofacebox_overlay").removeClassName("protofacebox_overlayBG");
$("protofacebox_overlay").addClassName("protofacebox_hide");
$("protofacebox_overlay").remove();
return false;
},
fillFaceboxFromAjax: function(url) {
new Ajax.Request(url, {
method: 'get',
onSuccess: function(transport) {
this.reveal(transport.responseText);
}.bind(this)
});
},
fillFaceboxFromImage: function(href) {
var image = new Image();
var f = function() {
this.reveal('<div class="image"><img src="' + image.src + '" /></div>');
}
image.onload = f.bind(this)
image.src = href;
}
});
// getPageScroll() by quirksmode.com
function getPageScroll() {
var xScroll, yScroll;
if (self.pageYOffset) {
yScroll = self.pageYOffset;
xScroll = self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
yScroll = document.documentElement.scrollTop;
xScroll = document.documentElement.scrollLeft;
} else if (document.body) {// all other Explorers
yScroll = document.body.scrollTop;
xScroll = document.body.scrollLeft;
}
return new Array(xScroll,yScroll);
}
// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
var windowHeight;
if (self.innerHeight) { // all except Explorer
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowHeight = document.body.clientHeight;
}
return windowHeight;
}