2 * jQuery blockUI plugin
3 * Version 2.37 (29-JAN-2011)
4 * @requires jQuery v1.2.3 or later
6 * Examples at: http://malsup.com/jquery/block/
7 * Copyright (c) 2007-2010 M. Alsup
8 * Dual licensed under the MIT and GPL licenses:
9 * http://www.opensource.org/licenses/mit-license.php
10 * http://www.gnu.org/licenses/gpl.html
12 * Thanks to Amir-Hossein Sobhi for some excellent contributions!
17 if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
18 alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);
22 $.fn._fadeIn = $.fn.fadeIn;
24 var noOp = function() {};
26 // this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
27 // retarded userAgent strings on Vista)
28 var mode = document.documentMode || 0;
29 var setExpr = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);
30 var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
32 // global $ methods for blocking/unblocking the entire page
33 $.blockUI = function(opts) { install(window, opts); };
34 $.unblockUI = function(opts) { remove(window, opts); };
36 // convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
37 $.growlUI = function(title, message, timeout, onClose) {
38 var $m = $('<div class="growlUI"></div>');
39 if (title) $m.append('<h1>'+title+'</h1>');
40 if (message) $m.append('<h2>'+message+'</h2>');
41 if (timeout == undefined) timeout = 3000;
43 message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
44 timeout: timeout, showOverlay: false,
46 css: $.blockUI.defaults.growlCSS
50 // plugin method for blocking element content
51 $.fn.block = function(opts) {
52 return this.unblock({ fadeOut: 0 }).each(function() {
53 if ($.css(this,'position') == 'static')
54 this.style.position = 'relative';
56 this.style.zoom = 1; // force 'hasLayout'
61 // plugin method for unblocking element content
62 $.fn.unblock = function(opts) {
63 return this.each(function() {
68 $.blockUI.version = 2.37; // 2nd generation blocking at no extra cost!
70 // override these in your code to change the default behavior and style
71 $.blockUI.defaults = {
72 // message displayed when blocking (use null for no message)
73 message: '<h1>Please wait...</h1>',
75 title: null, // title string; only used when theme == true
76 draggable: true, // only used when theme == true (requires jquery-ui.js to be loaded)
78 theme: false, // set to true to use with jQuery UI themes
80 // styles for the message when blocking; if you wish to disable
81 // these and use an external stylesheet then do this in your code:
82 // $.blockUI.defaults.css = {};
91 border: '3px solid #aaa',
92 backgroundColor:'#fff',
96 // minimal style set used when themes are used
103 // styles for the overlay
105 backgroundColor: '#000',
110 // styles applied when using $.growlUI
121 backgroundColor: '#000',
122 '-webkit-border-radius': '10px',
123 '-moz-border-radius': '10px',
124 'border-radius': '10px'
127 // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
128 // (hat tip to Jorge H. N. de Vasconcelos)
129 iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
131 // force usage of iframe in non-IE browsers (handy for blocking applets)
134 // z-index for the blocking overlay
137 // set these to true to have the message automatically centered
138 centerX: true, // <-- only effects element blocking (page block controlled via css above)
141 // allow body element to be stetched in ie6; this makes blocking look better
142 // on "short" pages. disable if you wish to prevent changes to the body height
143 allowBodyStretch: true,
145 // enable if you want key and mouse events to be disabled for content that is blocked
148 // be default blockUI will supress tab navigation from leaving blocking content
149 // (if bindEvents is true)
150 constrainTabKey: true,
152 // fadeIn time in millis; set to 0 to disable fadeIn on block
155 // fadeOut time in millis; set to 0 to disable fadeOut on unblock
158 // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
161 // disable if you don't want to show the overlay
164 // if true, focus will be placed in the first available input field when
168 // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
169 applyPlatformOpacityRules: true,
171 // callback method invoked when fadeIn has completed and blocking message is visible
174 // callback method invoked when unblocking has completed; the callback is
175 // passed the element that has been unblocked (which is the window object for page
176 // blocks) and the options that were passed to the unblock call:
177 // onUnblock(element, options)
180 // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
181 quirksmodeOffsetHack: 4,
183 // class name of the message block
184 blockMsgClass: 'blockMsg'
187 // private data and functions follow...
189 var pageBlock = null;
190 var pageBlockEls = [];
192 function install(el, opts) {
193 var full = (el == window);
194 var msg = opts && opts.message !== undefined ? opts.message : undefined;
195 opts = $.extend({}, $.blockUI.defaults, opts || {});
196 opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
197 var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
198 var themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
199 msg = msg === undefined ? opts.message : msg;
201 // remove the current block (if there is one)
202 if (full && pageBlock)
203 remove(window, {fadeOut:0});
205 // if an existing element is being used as the blocking content then we capture
206 // its current place in the DOM (and current display style) so we can restore
207 // it when we unblock
208 if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
209 var node = msg.jquery ? msg[0] : msg;
211 $(el).data('blockUI.history', data);
213 data.parent = node.parentNode;
214 data.display = node.style.display;
215 data.position = node.style.position;
217 data.parent.removeChild(node);
222 // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
223 // layer1 is the iframe layer which is used to supress bleed through of underlying content
224 // layer2 is the overlay layer which has opacity and a wait cursor (by default)
225 // layer3 is the message content that is displayed while blocking
227 var lyr1 = ($.browser.msie || opts.forceIframe)
228 ? $('<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="'+opts.iframeSrc+'"></iframe>')
229 : $('<div class="blockUI" style="display:none"></div>');
230 var lyr2 = $('<div class="blockUI blockOverlay" style="z-index:'+ (z++) +';display:none;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
233 if (opts.theme && full) {
234 s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:fixed">' +
235 '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || ' ')+'</div>' +
236 '<div class="ui-widget-content ui-dialog-content"></div>' +
239 else if (opts.theme) {
240 s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:absolute">' +
241 '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || ' ')+'</div>' +
242 '<div class="ui-widget-content ui-dialog-content"></div>' +
246 s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage" style="z-index:'+z+';display:none;position:fixed"></div>';
249 s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement" style="z-index:'+z+';display:none;position:absolute"></div>';
253 // if we have a message, style it
257 lyr3.addClass('ui-widget-content');
264 if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform)))
265 lyr2.css(opts.overlayCSS);
266 lyr2.css('position', full ? 'fixed' : 'absolute');
268 // make iframe layer transparent in IE
269 if ($.browser.msie || opts.forceIframe)
270 lyr1.css('opacity',0.0);
272 //$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
273 var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);
274 $.each(layers, function() {
278 if (opts.theme && opts.draggable && $.fn.draggable) {
280 handle: '.ui-dialog-titlebar',
285 // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
286 var expr = setExpr && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
288 // give body 100% height
289 if (full && opts.allowBodyStretch && $.boxModel)
290 $('html,body').css('height','100%');
292 // fix ie6 issue when blocked element has a border width
293 if ((ie6 || !$.boxModel) && !full) {
294 var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
295 var fixT = t ? '(0 - '+t+')' : 0;
296 var fixL = l ? '(0 - '+l+')' : 0;
299 // simulate fixed position
300 $.each([lyr1,lyr2,lyr3], function(i,o) {
302 s.position = 'absolute';
304 full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')
305 : s.setExpression('height','this.parentNode.offsetHeight + "px"');
306 full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
307 : s.setExpression('width','this.parentNode.offsetWidth + "px"');
308 if (fixL) s.setExpression('left', fixL);
309 if (fixT) s.setExpression('top', fixT);
311 else if (opts.centerY) {
312 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"');
315 else if (!opts.centerY && full) {
316 var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;
317 var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
318 s.setExpression('top',expression);
326 lyr3.find('.ui-widget-content').append(msg);
329 if (msg.jquery || msg.nodeType)
333 if (($.browser.msie || opts.forceIframe) && opts.showOverlay)
334 lyr1.show(); // opacity is zero
336 var cb = opts.onBlock ? opts.onBlock : noOp;
337 var cb1 = (opts.showOverlay && !msg) ? cb : noOp;
338 var cb2 = msg ? cb : noOp;
339 if (opts.showOverlay)
340 lyr2._fadeIn(opts.fadeIn, cb1);
342 lyr3._fadeIn(opts.fadeIn, cb2);
345 if (opts.showOverlay)
353 // bind key and mouse events
358 pageBlockEls = $(':input:enabled:visible',pageBlock);
360 setTimeout(focus, 20);
363 center(lyr3[0], opts.centerX, opts.centerY);
367 var to = setTimeout(function() {
368 full ? $.unblockUI(opts) : $(el).unblock(opts);
370 $(el).data('blockUI.timeout', to);
375 function remove(el, opts) {
376 var full = (el == window);
378 var data = $el.data('blockUI.history');
379 var to = $el.data('blockUI.timeout');
382 $el.removeData('blockUI.timeout');
384 opts = $.extend({}, $.blockUI.defaults, opts || {});
385 bind(0, el, opts); // unbind events
388 if (full) // crazy selector to handle odd field errors in ie6/7
389 els = $('body').children().filter('.blockUI').add('body > .blockUI');
391 els = $('.blockUI', el);
394 pageBlock = pageBlockEls = null;
397 els.fadeOut(opts.fadeOut);
398 setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
401 reset(els, data, opts, el);
404 // move blocking element back into the DOM where it started
405 function reset(els,data,opts,el) {
406 els.each(function(i,o) {
407 // remove via DOM calls so we don't lose event handlers
409 this.parentNode.removeChild(this);
412 if (data && data.el) {
413 data.el.style.display = data.display;
414 data.el.style.position = data.position;
416 data.parent.appendChild(data.el);
417 $(el).removeData('blockUI.history');
420 if (typeof opts.onUnblock == 'function')
421 opts.onUnblock(el,opts);
424 // bind/unbind the handler
425 function bind(b, el, opts) {
426 var full = el == window, $el = $(el);
428 // don't bother unbinding if there is nothing to unbind
429 if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
432 $el.data('blockUI.isBlocked', b);
434 // don't bind events when overlay is not in use or if bindEvents is false
435 if (!opts.bindEvents || (b && !opts.showOverlay))
438 // bind anchors and inputs for mouse and key events
439 var events = 'mousedown mouseup keydown keypress';
440 b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
443 // var $e = $('a,:input');
444 // b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
447 // event handler to suppress keyboard/mouse events when blocking
448 function handler(e) {
449 // allow tab navigation (conditionally)
450 if (e.keyCode && e.keyCode == 9) {
451 if (pageBlock && e.data.constrainTabKey) {
452 var els = pageBlockEls;
453 var fwd = !e.shiftKey && e.target === els[els.length-1];
454 var back = e.shiftKey && e.target === els[0];
456 setTimeout(function(){focus(back)},10);
462 // allow events within the message content
463 if ($(e.target).parents('div.' + opts.blockMsgClass).length > 0)
466 // allow events for content that is not being blocked
467 return $(e.target).parents().children().filter('div.blockUI').length == 0;
470 function focus(back) {
473 var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
478 function center(el, x, y) {
479 var p = el.parentNode, s = el.style;
480 var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
481 var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
482 if (x) s.left = l > 0 ? (l+'px') : '0';
483 if (y) s.top = t > 0 ? (t+'px') : '0';
487 return parseInt($.css(el,p))||0;