-
Notifications
You must be signed in to change notification settings - Fork 9
/
tools.expose.js
294 lines (226 loc) · 7.21 KB
/
tools.expose.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
/**
* tools.expose 1.0.5 - Make HTML elements stand out
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/expose.html
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Launch : June 2008
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
// static constructs
$.tools = $.tools || {};
$.tools.expose = {
version: '1.0.5',
conf: {
// mask settings
maskId: null,
maskClass: "expose_overlay",
exposedClass: "exposed",
loadSpeed: 'slow',
closeSpeed: 'fast',
closeOnClick: true,
closeOnEsc: true,
deferLoad: false,
// css settings
zIndex: 9998,
opacity: 0.8,
color: '#456',
api: false
}
};
/* one of the greatest headaches in the tool. finally made it */
function viewport() {
// the horror case
if ($.browser.msie) {
// if there are no scrollbars then use window.height
var d = $(document).height(), w = $(window).height();
return [
window.innerWidth || // ie7+
document.documentElement.clientWidth || // ie6
document.body.clientWidth, // ie6 quirks mode
d - w < 20 ? w : d
];
}
// other well behaving browsers
return [$(window).width(), $(document).height()];
}
function Expose(els, conf) {
// private variables
var self = this, $self = $(this), mask = null, loaded = false;
// bind all callbacks from configuration
$.each(conf, function(name, fn) {
if ($.isFunction(fn)) { $self.bind(name, fn); }
});
// adjust mask size when window is resized (or firebug is toggled)
$(window).resize(function() {
self.fit();
});
// public methods
$.extend(this, {
getMask: function() {
return mask;
},
getExposed: function() {
return els;
},
getConf: function() {
return conf;
},
isLoaded: function() {
return loaded;
},
load: function(e) {
// already loaded ?
if (loaded) { return self; }
origIndex = els.eq(0).css("zIndex");
els.eq(0).data("origIndex", origIndex);
// find existing mask
if (conf.maskId) { mask = $("#" + conf.maskId); }
// build the mask if it doesnt already exist
if (!mask || !mask.length) {
var size = viewport();
mask = $('<div/>').css({
position:'absolute',
top:0,
left:0,
display:'none',
opacity: 0
});
if (conf.maskId) { mask.attr("id", conf.maskId); }
// append the mask element to the end of the body
$("body").append(mask);
}
// wire up mask if its not already set up
if(!mask.data("initialized"))
{
// background color
var bg = mask.css("backgroundColor");
if (!bg || bg == 'transparent' || bg == 'rgba(0, 0, 0, 0)') {
mask.css("backgroundColor", conf.color);
}
// esc button
if (conf.closeOnEsc) {
$(document).bind("keydown.unexpose", function(evt) {
if (evt.keyCode == 27) {
self.close();
}
});
}
// mask click closes
if (conf.closeOnClick) {
mask.bind("click.unexpose", function(e) {
self.close(e);
});
}
// bind the mask close event
mask.bind("close", function(e) {
self.close(e.fast);
});
self.fit();
mask.data("initialized", true);
}
// possibility to cancel click action
e = e || $.Event();
e.type = "onBeforeLoad";
$self.trigger(e);
if (e.isDefaultPrevented()) { return self; }
// make sure element is positioned absolutely or relatively
$.each(els, function() {
var el = $(this);
if (!/relative|absolute|fixed/i.test(el.css("position"))) {
el.css("position", "relative");
}
});
// make elements sit on top of the mask
els.css({zIndex:Math.max(conf.zIndex + 1, origIndex == 'auto' ? 0 : origIndex)});
// reveal mask
var h = mask.height();
// set start opacity
var startOpacity = 0;
if(mask.is(":visible")) {
startOpacity = conf.opacity;
}
mask.css({opacity: startOpacity, display: 'block'}).fadeTo(conf.loadSpeed, conf.opacity, function() {
// sometimes IE6 misses the height property on fadeTo method
if (mask.height() != h) { mask.css("height", h); }
e.type = "onLoad";
$self.trigger(e);
});
els.addClass(conf.exposedClass);
loaded = true;
return self;
},
close: function(e) {
if (!loaded) { return self; }
e = e || $.Event();
e.type = "onBeforeClose";
$self.trigger(e);
if (e.isDefaultPrevented()) { return self; }
if(e.fast) {
mask.hide();
}
else {
mask.fadeOut(conf.closeSpeed, function() {
e.type = "onClose";
$self.trigger(e);
els.css({zIndex: $.browser.msie ? origIndex : null});
});
}
els.removeClass(conf.exposedClass)
loaded = false;
return self;
},
fit: function() {
if (mask) {
var size = viewport();
mask.css({ width: size[0], height: size[1]});
}
},
bind: function(name, fn) {
$self.bind(name, fn);
return self;
},
unbind: function(name) {
$self.unbind(name);
return self;
}
});
// callbacks
$.each("onBeforeLoad,onLoad,onBeforeClose,onClose".split(","), function(i, ev) {
self[ev] = function(fn) {
return self.bind(ev, fn);
};
});
}
// jQuery plugin implementation
$.fn.expose = function(conf) {
var el = this.eq(typeof conf == 'number' ? conf : 0).data("expose");
if (el) { return el; }
if (typeof conf == 'string') {
conf = {color: conf};
}
var globals = $.extend({}, $.tools.expose.conf);
conf = $.extend(globals, conf);
// construct exposes
this.each(function() {
var inner = $(this);
if(!inner.data("expose"))
{
inner.data("expose", new Expose(inner, conf))
}
if(conf.deferLoad) {
// defer loading for later
}
else {
// load it now
inner.data("expose").load();
}
});
return conf.api ? this.data("expose") : this;
};
})(jQuery);