2 * jQuery blockUI plugin
\r
3 * Version 2.28 (02-DEC-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 $.fn._fadeIn = $.fn.fadeIn;
\r
24 // this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
\r
25 // retarded userAgent strings on Vista)
\r
26 var mode = document.documentMode || 0;
\r
27 var setExpr = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);
\r
28 var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
\r
30 // global $ methods for blocking/unblocking the entire page
\r
31 $.blockUI = function(opts) { install(window, opts); };
\r
32 $.unblockUI = function(opts) { remove(window, opts); };
\r
34 // convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
\r
35 $.growlUI = function(title, message, timeout, onClose) {
\r
36 var $m = $('<div class="growlUI"></div>');
\r
37 if (title) $m.append('<h1>'+title+'</h1>');
\r
38 if (message) $m.append('<h2>'+message+'</h2>');
\r
39 if (timeout == undefined) timeout = 3000;
\r
41 message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
\r
42 timeout: timeout, showOverlay: false,
\r
43 onUnblock: onClose,
\r
44 css: $.blockUI.defaults.growlCSS
\r
48 // plugin method for blocking element content
\r
49 $.fn.block = function(opts) {
\r
50 return this.unblock({ fadeOut: 0 }).each(function() {
\r
51 if ($.css(this,'position') == 'static')
\r
52 this.style.position = 'relative';
\r
54 this.style.zoom = 1; // force 'hasLayout'
\r
55 install(this, opts);
\r
59 // plugin method for unblocking element content
\r
60 $.fn.unblock = function(opts) {
\r
61 return this.each(function() {
\r
66 $.blockUI.version = 2.28; // 2nd generation blocking at no extra cost!
\r
68 // override these in your code to change the default behavior and style
\r
69 $.blockUI.defaults = {
\r
70 // message displayed when blocking (use null for no message)
\r
71 message: '<h1>Please wait...</h1>',
\r
73 title: null, // title string; only used when theme == true
\r
74 draggable: true, // only used when theme == true (requires jquery-ui.js to be loaded)
\r
76 theme: false, // set to true to use with jQuery UI themes
\r
78 // styles for the message when blocking; if you wish to disable
\r
79 // these and use an external stylesheet then do this in your code:
\r
80 // $.blockUI.defaults.css = {};
\r
87 textAlign: 'center',
\r
89 border: '3px solid #aaa',
\r
90 backgroundColor:'#fff',
\r
94 // minimal style set used when themes are used
\r
101 // styles for the overlay
\r
103 backgroundColor: '#000',
\r
108 // styles applied when using $.growlUI
\r
119 backgroundColor: '#000',
\r
120 '-webkit-border-radius': '10px',
\r
121 '-moz-border-radius': '10px'
\r
124 // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
\r
125 // (hat tip to Jorge H. N. de Vasconcelos)
\r
126 iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
\r
128 // force usage of iframe in non-IE browsers (handy for blocking applets)
\r
129 forceIframe: false,
\r
131 // z-index for the blocking overlay
\r
134 // set these to true to have the message automatically centered
\r
135 centerX: true, // <-- only effects element blocking (page block controlled via css above)
\r
138 // allow body element to be stetched in ie6; this makes blocking look better
\r
139 // on "short" pages. disable if you wish to prevent changes to the body height
\r
140 allowBodyStretch: true,
\r
142 // enable if you want key and mouse events to be disabled for content that is blocked
\r
145 // be default blockUI will supress tab navigation from leaving blocking content
\r
146 // (if bindEvents is true)
\r
147 constrainTabKey: true,
\r
149 // fadeIn time in millis; set to 0 to disable fadeIn on block
\r
152 // fadeOut time in millis; set to 0 to disable fadeOut on unblock
\r
155 // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
\r
158 // disable if you don't want to show the overlay
\r
161 // if true, focus will be placed in the first available input field when
\r
165 // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
\r
166 applyPlatformOpacityRules: true,
\r
168 // callback method invoked when unblocking has completed; the callback is
\r
169 // passed the element that has been unblocked (which is the window object for page
\r
170 // blocks) and the options that were passed to the unblock call:
\r
171 // onUnblock(element, options)
\r
174 // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
\r
175 quirksmodeOffsetHack: 4
\r
178 // private data and functions follow...
\r
180 var pageBlock = null;
\r
181 var pageBlockEls = [];
\r
183 function install(el, opts) {
\r
184 var full = (el == window);
\r
185 var msg = opts && opts.message !== undefined ? opts.message : undefined;
\r
186 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
187 opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
\r
188 var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
\r
189 var themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
\r
190 msg = msg === undefined ? opts.message : msg;
\r
192 // remove the current block (if there is one)
\r
193 if (full && pageBlock)
\r
194 remove(window, {fadeOut:0});
\r
196 // if an existing element is being used as the blocking content then we capture
\r
197 // its current place in the DOM (and current display style) so we can restore
\r
198 // it when we unblock
\r
199 if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
\r
200 var node = msg.jquery ? msg[0] : msg;
\r
202 $(el).data('blockUI.history', data);
\r
204 data.parent = node.parentNode;
\r
205 data.display = node.style.display;
\r
206 data.position = node.style.position;
\r
208 data.parent.removeChild(node);
\r
211 var z = opts.baseZ;
\r
213 // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
\r
214 // layer1 is the iframe layer which is used to supress bleed through of underlying content
\r
215 // layer2 is the overlay layer which has opacity and a wait cursor (by default)
\r
216 // layer3 is the message content that is displayed while blocking
\r
218 var lyr1 = ($.browser.msie || opts.forceIframe)
\r
219 ? $('<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>')
\r
220 : $('<div class="blockUI" style="display:none"></div>');
\r
221 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>');
\r
224 if (opts.theme && full) {
\r
225 var s = '<div class="blockUI blockMsg blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:fixed">' +
\r
226 '<div class="ui-widget-header ui-dialog-titlebar blockTitle">'+(opts.title || ' ')+'</div>' +
\r
227 '<div class="ui-widget-content ui-dialog-content"></div>' +
\r
232 lyr3 = full ? $('<div class="blockUI blockMsg blockPage" style="z-index:'+z+';display:none;position:fixed"></div>')
\r
233 : $('<div class="blockUI blockMsg blockElement" style="z-index:'+z+';display:none;position:absolute"></div>');
\r
236 // if we have a message, style it
\r
239 lyr3.css(themedCSS);
\r
240 lyr3.addClass('ui-widget-content');
\r
246 // style the overlay
\r
247 if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform)))
\r
248 lyr2.css(opts.overlayCSS);
\r
249 lyr2.css('position', full ? 'fixed' : 'absolute');
\r
251 // make iframe layer transparent in IE
\r
252 if ($.browser.msie || opts.forceIframe)
\r
253 lyr1.css('opacity',0.0);
\r
255 $([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
\r
257 if (opts.theme && opts.draggable && $.fn.draggable) {
\r
259 handle: '.ui-dialog-titlebar',
\r
264 // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
\r
265 var expr = setExpr && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
\r
267 // give body 100% height
\r
268 if (full && opts.allowBodyStretch && $.boxModel)
\r
269 $('html,body').css('height','100%');
\r
271 // fix ie6 issue when blocked element has a border width
\r
272 if ((ie6 || !$.boxModel) && !full) {
\r
273 var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
\r
274 var fixT = t ? '(0 - '+t+')' : 0;
\r
275 var fixL = l ? '(0 - '+l+')' : 0;
\r
278 // simulate fixed position
\r
279 $.each([lyr1,lyr2,lyr3], function(i,o) {
\r
280 var s = o[0].style;
\r
281 s.position = 'absolute';
\r
283 full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')
\r
284 : s.setExpression('height','this.parentNode.offsetHeight + "px"');
\r
285 full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
\r
286 : s.setExpression('width','this.parentNode.offsetWidth + "px"');
\r
287 if (fixL) s.setExpression('left', fixL);
\r
288 if (fixT) s.setExpression('top', fixT);
\r
290 else if (opts.centerY) {
\r
291 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
294 else if (!opts.centerY && full) {
\r
295 var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;
\r
296 var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
\r
297 s.setExpression('top',expression);
\r
302 // show the message
\r
305 lyr3.find('.ui-widget-content').append(msg);
\r
308 if (msg.jquery || msg.nodeType)
\r
312 if (($.browser.msie || opts.forceIframe) && opts.showOverlay)
\r
313 lyr1.show(); // opacity is zero
\r
315 if (opts.showOverlay)
\r
316 lyr2._fadeIn(opts.fadeIn);
\r
318 lyr3.fadeIn(opts.fadeIn);
\r
321 if (opts.showOverlay)
\r
327 // bind key and mouse events
\r
331 pageBlock = lyr3[0];
\r
332 pageBlockEls = $(':input:enabled:visible',pageBlock);
\r
333 if (opts.focusInput)
\r
334 setTimeout(focus, 20);
\r
337 center(lyr3[0], opts.centerX, opts.centerY);
\r
339 if (opts.timeout) {
\r
341 var to = setTimeout(function() {
\r
342 full ? $.unblockUI(opts) : $(el).unblock(opts);
\r
344 $(el).data('blockUI.timeout', to);
\r
348 // remove the block
\r
349 function remove(el, opts) {
\r
350 var full = (el == window);
\r
352 var data = $el.data('blockUI.history');
\r
353 var to = $el.data('blockUI.timeout');
\r
356 $el.removeData('blockUI.timeout');
\r
358 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
359 bind(0, el, opts); // unbind events
\r
362 if (full) // crazy selector to handle odd field errors in ie6/7
\r
363 els = $('body').children().filter('.blockUI').add('body > .blockUI');
\r
365 els = $('.blockUI', el);
\r
368 pageBlock = pageBlockEls = null;
\r
370 if (opts.fadeOut) {
\r
371 els.fadeOut(opts.fadeOut);
\r
372 setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
\r
375 reset(els, data, opts, el);
\r
378 // move blocking element back into the DOM where it started
\r
379 function reset(els,data,opts,el) {
\r
380 els.each(function(i,o) {
\r
381 // remove via DOM calls so we don't lose event handlers
\r
382 if (this.parentNode)
\r
383 this.parentNode.removeChild(this);
\r
386 if (data && data.el) {
\r
387 data.el.style.display = data.display;
\r
388 data.el.style.position = data.position;
\r
390 data.parent.appendChild(data.el);
\r
391 $(el).removeData('blockUI.history');
\r
394 if (typeof opts.onUnblock == 'function')
\r
395 opts.onUnblock(el,opts);
\r
398 // bind/unbind the handler
\r
399 function bind(b, el, opts) {
\r
400 var full = el == window, $el = $(el);
\r
402 // don't bother unbinding if there is nothing to unbind
\r
403 if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
\r
406 $el.data('blockUI.isBlocked', b);
\r
408 // don't bind events when overlay is not in use or if bindEvents is false
\r
409 if (!opts.bindEvents || (b && !opts.showOverlay))
\r
412 // bind anchors and inputs for mouse and key events
\r
413 var events = 'mousedown mouseup keydown keypress';
\r
414 b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
\r
417 // var $e = $('a,:input');
\r
418 // b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
\r
421 // event handler to suppress keyboard/mouse events when blocking
\r
422 function handler(e) {
\r
423 // allow tab navigation (conditionally)
\r
424 if (e.keyCode && e.keyCode == 9) {
\r
425 if (pageBlock && e.data.constrainTabKey) {
\r
426 var els = pageBlockEls;
\r
427 var fwd = !e.shiftKey && e.target == els[els.length-1];
\r
428 var back = e.shiftKey && e.target == els[0];
\r
430 setTimeout(function(){focus(back)},10);
\r
435 // allow events within the message content
\r
436 if ($(e.target).parents('div.blockMsg').length > 0)
\r
439 // allow events for content that is not being blocked
\r
440 return $(e.target).parents().children().filter('div.blockUI').length == 0;
\r
443 function focus(back) {
\r
446 var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
\r
451 function center(el, x, y) {
\r
452 var p = el.parentNode, s = el.style;
\r
453 var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
\r
454 var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
\r
455 if (x) s.left = l > 0 ? (l+'px') : '0';
\r
456 if (y) s.top = t > 0 ? (t+'px') : '0';
\r
459 function sz(el, p) {
\r
460 return parseInt($.css(el,p))||0;
\r