2 * jQuery blockUI plugin
\r
3 * Version 2.14 (18-JAN-2009)
\r
4 * @requires jQuery v1.2.3 or later
\r
6 * Examples at: http://malsup.com/jquery/block/
\r
7 * Copyright (c) 2007-2008 M. Alsup
\r
8 * Dual licensed under the MIT and GPL licenses:
\r
9 * http://www.opensource.org/licenses/mit-license.php
\r
10 * http://www.gnu.org/licenses/gpl.html
\r
12 * Thanks to Amir-Hossein Sobhi for some excellent contributions!
\r
17 if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
\r
18 alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);
\r
22 // global $ methods for blocking/unblocking the entire page
\r
23 $.blockUI = function(opts) { install(window, opts); };
\r
24 $.unblockUI = function(opts) { remove(window, opts); };
\r
26 // convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
\r
27 $.growlUI = function(title, message, timeout) {
\r
28 var $m = $('<div class="growlUI"></div>');
\r
29 if (title) $m.append('<h1>'+title+'</h1>');
\r
30 if (message) $m.append('<h2>'+message+'</h2>');
\r
31 if (timeout == undefined) timeout = 3000;
\r
33 message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
\r
34 timeout: timeout, showOverlay: false,
\r
35 css: $.blockUI.defaults.growlCSS
\r
39 // plugin method for blocking element content
\r
40 $.fn.block = function(opts) {
\r
41 return this.each(function() {
\r
42 if ($.css(this,'position') == 'static')
\r
43 this.style.position = 'relative';
\r
44 if ($.browser.msie)
\r
45 this.style.zoom = 1; // force 'hasLayout'
\r
46 install(this, opts);
\r
50 // plugin method for unblocking element content
\r
51 $.fn.unblock = function(opts) {
\r
52 return this.each(function() {
\r
57 $.blockUI.version = 2.14; // 2nd generation blocking at no extra cost!
\r
59 // override these in your code to change the default behavior and style
\r
60 $.blockUI.defaults = {
\r
61 // message displayed when blocking (use null for no message)
\r
62 message: '<h1>Please wait...</h1>',
\r
64 // styles for the message when blocking; if you wish to disable
\r
65 // these and use an external stylesheet then do this in your code:
\r
66 // $.blockUI.defaults.css = {};
\r
73 textAlign: 'center',
\r
75 border: '3px solid #aaa',
\r
76 backgroundColor:'#fff',
\r
80 // styles for the overlay
\r
82 backgroundColor: '#000',
\r
86 // styles applied when using $.growlUI
\r
97 backgroundColor: '#000',
\r
98 '-webkit-border-radius': '10px',
\r
99 '-moz-border-radius': '10px'
\r
102 // z-index for the blocking overlay
\r
105 // set these to true to have the message automatically centered
\r
106 centerX: true, // <-- only effects element blocking (page block controlled via css above)
\r
109 // allow body element to be stetched in ie6; this makes blocking look better
\r
110 // on "short" pages. disable if you wish to prevent changes to the body height
\r
111 allowBodyStretch: true,
\r
113 // be default blockUI will supress tab navigation from leaving blocking content;
\r
114 constrainTabKey: true,
\r
116 // fadeIn time in millis; set to 0 to disable fadeIn on block
\r
119 // fadeOut time in millis; set to 0 to disable fadeOut on unblock
\r
122 // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
\r
125 // disable if you don't want to show the overlay
\r
128 // if true, focus will be placed in the first available input field when
\r
132 // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
\r
133 applyPlatformOpacityRules: true,
\r
135 // callback method invoked when unblocking has completed; the callback is
\r
136 // passed the element that has been unblocked (which is the window object for page
\r
137 // blocks) and the options that were passed to the unblock call:
\r
138 // onUnblock(element, options)
\r
141 // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
\r
142 quirksmodeOffsetHack: 4
\r
145 // private data and functions follow...
\r
147 var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent);
\r
148 var pageBlock = null;
\r
149 var pageBlockEls = [];
\r
151 function install(el, opts) {
\r
152 var full = (el == window);
\r
153 var msg = opts && opts.message !== undefined ? opts.message : undefined;
\r
154 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
155 opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
\r
156 var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
\r
157 msg = msg === undefined ? opts.message : msg;
\r
159 // remove the current block (if there is one)
\r
160 if (full && pageBlock)
\r
161 remove(window, {fadeOut:0});
\r
163 // if an existing element is being used as the blocking content then we capture
\r
164 // its current place in the DOM (and current display style) so we can restore
\r
165 // it when we unblock
\r
166 if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
\r
167 var node = msg.jquery ? msg[0] : msg;
\r
169 $(el).data('blockUI.history', data);
\r
171 data.parent = node.parentNode;
\r
172 data.display = node.style.display;
\r
173 data.position = node.style.position;
\r
175 data.parent.removeChild(node);
\r
178 var z = opts.baseZ;
\r
180 // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
\r
181 // layer1 is the iframe layer which is used to supress bleed through of underlying content
\r
182 // layer2 is the overlay layer which has opacity and a wait cursor
\r
183 // layer3 is the message content that is displayed while blocking
\r
185 var lyr1 = ($.browser.msie) ? $('<iframe class="blockUI" style="z-index:'+ z++ +';display:none;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;"></iframe>')
\r
186 : $('<div class="blockUI" style="display:none"></div>');
\r
187 var lyr2 = $('<div class="blockUI blockOverlay" style="z-index:'+ z++ +';display:none;cursor:wait;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
\r
188 var lyr3 = full ? $('<div class="blockUI blockMsg blockPage" style="z-index:'+z+';display:none;position:fixed"></div>')
\r
189 : $('<div class="blockUI blockMsg blockElement" style="z-index:'+z+';display:none;position:absolute"></div>');
\r
191 // if we have a message, style it
\r
195 // style the overlay
\r
196 if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform)))
\r
197 lyr2.css(opts.overlayCSS);
\r
198 lyr2.css('position', full ? 'fixed' : 'absolute');
\r
200 // make iframe layer transparent in IE
\r
201 if ($.browser.msie)
\r
202 lyr1.css('opacity','0.0');
\r
204 $([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
\r
206 // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
\r
207 var expr = $.browser.msie && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
\r
209 // give body 100% height
\r
210 if (full && opts.allowBodyStretch && $.boxModel)
\r
211 $('html,body').css('height','100%');
\r
213 // fix ie6 issue when blocked element has a border width
\r
214 if ((ie6 || !$.boxModel) && !full) {
\r
215 var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
\r
216 var fixT = t ? '(0 - '+t+')' : 0;
\r
217 var fixL = l ? '(0 - '+l+')' : 0;
\r
220 // simulate fixed position
\r
221 $.each([lyr1,lyr2,lyr3], function(i,o) {
\r
222 var s = o[0].style;
\r
223 s.position = 'absolute';
\r
225 full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')
\r
226 : s.setExpression('height','this.parentNode.offsetHeight + "px"');
\r
227 full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
\r
228 : s.setExpression('width','this.parentNode.offsetWidth + "px"');
\r
229 if (fixL) s.setExpression('left', fixL);
\r
230 if (fixT) s.setExpression('top', fixT);
\r
232 else if (opts.centerY) {
\r
233 if (full) s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"');
\r
236 else if (!opts.centerY && full) {
\r
237 var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;
\r
238 var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
\r
239 s.setExpression('top',expression);
\r
244 // show the message
\r
245 lyr3.append(msg);//.show();
\r
246 if (msg && (msg.jquery || msg.nodeType))
\r
250 if ($.browser.msie && opts.showOverlay)
\r
251 lyr1.fadeIn(opts.fadeIn);
\r
252 if (opts.showOverlay)
\r
253 lyr2.fadeIn(opts.fadeIn);
\r
254 lyr3.fadeIn(opts.fadeIn);
\r
257 if ($.browser.msie && opts.showOverlay)
\r
259 if (opts.showOverlay)
\r
264 // bind key and mouse events
\r
268 pageBlock = lyr3[0];
\r
269 pageBlockEls = $(':input:enabled:visible',pageBlock);
\r
270 if (opts.focusInput)
\r
271 setTimeout(focus, 20);
\r
274 center(lyr3[0], opts.centerX, opts.centerY);
\r
276 if (opts.timeout) {
\r
278 setTimeout(function() {
\r
279 full ? $.unblockUI(opts) : $(el).unblock(opts);
\r
284 // remove the block
\r
285 function remove(el, opts) {
\r
286 var full = el == window;
\r
287 var data = $(el).data('blockUI.history');
\r
288 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
289 bind(0, el, opts); // unbind events
\r
290 var els = full ? $('body').children().filter('.blockUI') : $('.blockUI', el);
\r
293 pageBlock = pageBlockEls = null;
\r
295 if (opts.fadeOut) {
\r
296 els.fadeOut(opts.fadeOut);
\r
297 setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
\r
300 reset(els, data, opts, el);
\r
303 // move blocking element back into the DOM where it started
\r
304 function reset(els,data,opts,el) {
\r
305 els.each(function(i,o) {
\r
306 // remove via DOM calls so we don't lose event handlers
\r
307 if (this.parentNode)
\r
308 this.parentNode.removeChild(this);
\r
311 if (data && data.el) {
\r
312 data.el.style.display = data.display;
\r
313 data.el.style.position = data.position;
\r
315 data.parent.appendChild(data.el);
\r
316 $(data.el).removeData('blockUI.history');
\r
319 if (typeof opts.onUnblock == 'function')
\r
320 opts.onUnblock(el,opts);
\r
323 // bind/unbind the handler
\r
324 function bind(b, el, opts) {
\r
325 var full = el == window, $el = $(el);
\r
327 // don't bother unbinding if there is nothing to unbind
\r
328 if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
\r
331 $el.data('blockUI.isBlocked', b);
\r
333 if (b && !opts.showOverlay) // don't prevent events when overlay not in use
\r
336 // bind anchors and inputs for mouse and key events
\r
337 var events = 'mousedown mouseup keydown keypress';
\r
338 b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
\r
341 // var $e = $('a,:input');
\r
342 // b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
\r
345 // event handler to suppress keyboard/mouse events when blocking
\r
346 function handler(e) {
\r
347 // allow tab navigation (conditionally)
\r
348 if (e.keyCode && e.keyCode == 9) {
\r
349 if (pageBlock && e.data.constrainTabKey) {
\r
350 var els = pageBlockEls;
\r
351 var fwd = !e.shiftKey && e.target == els[els.length-1];
\r
352 var back = e.shiftKey && e.target == els[0];
\r
354 setTimeout(function(){focus(back)},10);
\r
359 // allow events within the message content
\r
360 if ($(e.target).parents('div.blockMsg').length > 0)
\r
363 // allow events for content that is not being blocked
\r
364 return $(e.target).parents().children().filter('div.blockUI').length == 0;
\r
367 function focus(back) {
\r
368 if (!pageBlockEls)
\r
370 var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
\r
375 function center(el, x, y) {
\r
376 var p = el.parentNode, s = el.style;
\r
377 var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
\r
378 var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
\r
379 if (x) s.left = l > 0 ? (l+'px') : '0';
\r
380 if (y) s.top = t > 0 ? (t+'px') : '0';
\r
383 function sz(el, p) {
\r
384 return parseInt($.css(el,p))||0;
\r