2 * jQuery blockUI plugin
\r
3 * Version 2.31 (06-JAN-2010)
\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 var noOp = function() {};
\r
26 // this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
\r
27 // retarded userAgent strings on Vista)
\r
28 var mode = document.documentMode || 0;
\r
29 var setExpr = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);
\r
30 var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
\r
32 // global $ methods for blocking/unblocking the entire page
\r
33 $.blockUI = function(opts) { install(window, opts); };
\r
34 $.unblockUI = function(opts) { remove(window, opts); };
\r
36 // convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
\r
37 $.growlUI = function(title, message, timeout, onClose) {
\r
38 var $m = $('<div class="growlUI"></div>');
\r
39 if (title) $m.append('<h1>'+title+'</h1>');
\r
40 if (message) $m.append('<h2>'+message+'</h2>');
\r
41 if (timeout == undefined) timeout = 3000;
\r
43 message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
\r
44 timeout: timeout, showOverlay: false,
\r
45 onUnblock: onClose,
\r
46 css: $.blockUI.defaults.growlCSS
\r
50 // plugin method for blocking element content
\r
51 $.fn.block = function(opts) {
\r
52 return this.unblock({ fadeOut: 0 }).each(function() {
\r
53 if ($.css(this,'position') == 'static')
\r
54 this.style.position = 'relative';
\r
56 this.style.zoom = 1; // force 'hasLayout'
\r
57 install(this, opts);
\r
61 // plugin method for unblocking element content
\r
62 $.fn.unblock = function(opts) {
\r
63 return this.each(function() {
\r
68 $.blockUI.version = 2.31; // 2nd generation blocking at no extra cost!
\r
70 // override these in your code to change the default behavior and style
\r
71 $.blockUI.defaults = {
\r
72 // message displayed when blocking (use null for no message)
\r
73 message: '<h1>Please wait...</h1>',
\r
75 title: null, // title string; only used when theme == true
\r
76 draggable: true, // only used when theme == true (requires jquery-ui.js to be loaded)
\r
78 theme: false, // set to true to use with jQuery UI themes
\r
80 // styles for the message when blocking; if you wish to disable
\r
81 // these and use an external stylesheet then do this in your code:
\r
82 // $.blockUI.defaults.css = {};
\r
89 textAlign: 'center',
\r
91 border: '3px solid #aaa',
\r
92 backgroundColor:'#fff',
\r
96 // minimal style set used when themes are used
\r
103 // styles for the overlay
\r
105 backgroundColor: '#000',
\r
110 // styles applied when using $.growlUI
\r
121 backgroundColor: '#000',
\r
122 '-webkit-border-radius': '10px',
\r
123 '-moz-border-radius': '10px'
\r
126 // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
\r
127 // (hat tip to Jorge H. N. de Vasconcelos)
\r
128 iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
\r
130 // force usage of iframe in non-IE browsers (handy for blocking applets)
\r
131 forceIframe: false,
\r
133 // z-index for the blocking overlay
\r
136 // set these to true to have the message automatically centered
\r
137 centerX: true, // <-- only effects element blocking (page block controlled via css above)
\r
140 // allow body element to be stetched in ie6; this makes blocking look better
\r
141 // on "short" pages. disable if you wish to prevent changes to the body height
\r
142 allowBodyStretch: true,
\r
144 // enable if you want key and mouse events to be disabled for content that is blocked
\r
147 // be default blockUI will supress tab navigation from leaving blocking content
\r
148 // (if bindEvents is true)
\r
149 constrainTabKey: true,
\r
151 // fadeIn time in millis; set to 0 to disable fadeIn on block
\r
154 // fadeOut time in millis; set to 0 to disable fadeOut on unblock
\r
157 // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
\r
160 // disable if you don't want to show the overlay
\r
163 // if true, focus will be placed in the first available input field when
\r
167 // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
\r
168 applyPlatformOpacityRules: true,
\r
170 // callback method invoked when fadeIn has completed and blocking message is visible
\r
173 // callback method invoked when unblocking has completed; the callback is
\r
174 // passed the element that has been unblocked (which is the window object for page
\r
175 // blocks) and the options that were passed to the unblock call:
\r
176 // onUnblock(element, options)
\r
179 // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
\r
180 quirksmodeOffsetHack: 4
\r
183 // private data and functions follow...
\r
185 var pageBlock = null;
\r
186 var pageBlockEls = [];
\r
188 function install(el, opts) {
\r
189 var full = (el == window);
\r
190 var msg = opts && opts.message !== undefined ? opts.message : undefined;
\r
191 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
192 opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
\r
193 var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
\r
194 var themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
\r
195 msg = msg === undefined ? opts.message : msg;
\r
197 // remove the current block (if there is one)
\r
198 if (full && pageBlock)
\r
199 remove(window, {fadeOut:0});
\r
201 // if an existing element is being used as the blocking content then we capture
\r
202 // its current place in the DOM (and current display style) so we can restore
\r
203 // it when we unblock
\r
204 if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
\r
205 var node = msg.jquery ? msg[0] : msg;
\r
207 $(el).data('blockUI.history', data);
\r
209 data.parent = node.parentNode;
\r
210 data.display = node.style.display;
\r
211 data.position = node.style.position;
\r
213 data.parent.removeChild(node);
\r
216 var z = opts.baseZ;
\r
218 // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
\r
219 // layer1 is the iframe layer which is used to supress bleed through of underlying content
\r
220 // layer2 is the overlay layer which has opacity and a wait cursor (by default)
\r
221 // layer3 is the message content that is displayed while blocking
\r
223 var lyr1 = ($.browser.msie || opts.forceIframe)
\r
224 ? $('<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
225 : $('<div class="blockUI" style="display:none"></div>');
\r
226 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
229 if (opts.theme && full) {
\r
230 var s = '<div class="blockUI blockMsg blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+z+';display:none;position:fixed">' +
\r
231 '<div class="ui-widget-header ui-dialog-titlebar blockTitle">'+(opts.title || ' ')+'</div>' +
\r
232 '<div class="ui-widget-content ui-dialog-content"></div>' +
\r
237 lyr3 = full ? $('<div class="blockUI blockMsg blockPage" style="z-index:'+z+';display:none;position:fixed"></div>')
\r
238 : $('<div class="blockUI blockMsg blockElement" style="z-index:'+z+';display:none;position:absolute"></div>');
\r
241 // if we have a message, style it
\r
244 lyr3.css(themedCSS);
\r
245 lyr3.addClass('ui-widget-content');
\r
251 // style the overlay
\r
252 if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform)))
\r
253 lyr2.css(opts.overlayCSS);
\r
254 lyr2.css('position', full ? 'fixed' : 'absolute');
\r
256 // make iframe layer transparent in IE
\r
257 if ($.browser.msie || opts.forceIframe)
\r
258 lyr1.css('opacity',0.0);
\r
260 //$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
\r
261 var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);
\r
262 $.each(layers, function() {
\r
263 this.appendTo($par);
\r
266 if (opts.theme && opts.draggable && $.fn.draggable) {
\r
268 handle: '.ui-dialog-titlebar',
\r
273 // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
\r
274 var expr = setExpr && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
\r
276 // give body 100% height
\r
277 if (full && opts.allowBodyStretch && $.boxModel)
\r
278 $('html,body').css('height','100%');
\r
280 // fix ie6 issue when blocked element has a border width
\r
281 if ((ie6 || !$.boxModel) && !full) {
\r
282 var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
\r
283 var fixT = t ? '(0 - '+t+')' : 0;
\r
284 var fixL = l ? '(0 - '+l+')' : 0;
\r
287 // simulate fixed position
\r
288 $.each([lyr1,lyr2,lyr3], function(i,o) {
\r
289 var s = o[0].style;
\r
290 s.position = 'absolute';
\r
292 full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')
\r
293 : s.setExpression('height','this.parentNode.offsetHeight + "px"');
\r
294 full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
\r
295 : s.setExpression('width','this.parentNode.offsetWidth + "px"');
\r
296 if (fixL) s.setExpression('left', fixL);
\r
297 if (fixT) s.setExpression('top', fixT);
\r
299 else if (opts.centerY) {
\r
300 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
303 else if (!opts.centerY && full) {
\r
304 var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;
\r
305 var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
\r
306 s.setExpression('top',expression);
\r
311 // show the message
\r
314 lyr3.find('.ui-widget-content').append(msg);
\r
317 if (msg.jquery || msg.nodeType)
\r
321 if (($.browser.msie || opts.forceIframe) && opts.showOverlay)
\r
322 lyr1.show(); // opacity is zero
\r
324 var cb = opts.onBlock ? opts.onBlock : noOp;
\r
325 var cb1 = (opts.showOverlay && !msg) ? cb : noOp;
\r
326 var cb2 = msg ? cb : noOp;
\r
327 if (opts.showOverlay)
\r
328 lyr2._fadeIn(opts.fadeIn, cb1);
\r
330 lyr3._fadeIn(opts.fadeIn, cb2);
\r
333 if (opts.showOverlay)
\r
341 // bind key and mouse events
\r
345 pageBlock = lyr3[0];
\r
346 pageBlockEls = $(':input:enabled:visible',pageBlock);
\r
347 if (opts.focusInput)
\r
348 setTimeout(focus, 20);
\r
351 center(lyr3[0], opts.centerX, opts.centerY);
\r
353 if (opts.timeout) {
\r
355 var to = setTimeout(function() {
\r
356 full ? $.unblockUI(opts) : $(el).unblock(opts);
\r
358 $(el).data('blockUI.timeout', to);
\r
362 // remove the block
\r
363 function remove(el, opts) {
\r
364 var full = (el == window);
\r
366 var data = $el.data('blockUI.history');
\r
367 var to = $el.data('blockUI.timeout');
\r
370 $el.removeData('blockUI.timeout');
\r
372 opts = $.extend({}, $.blockUI.defaults, opts || {});
\r
373 bind(0, el, opts); // unbind events
\r
376 if (full) // crazy selector to handle odd field errors in ie6/7
\r
377 els = $('body').children().filter('.blockUI').add('body > .blockUI');
\r
379 els = $('.blockUI', el);
\r
382 pageBlock = pageBlockEls = null;
\r
384 if (opts.fadeOut) {
\r
385 els.fadeOut(opts.fadeOut);
\r
386 setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
\r
389 reset(els, data, opts, el);
\r
392 // move blocking element back into the DOM where it started
\r
393 function reset(els,data,opts,el) {
\r
394 els.each(function(i,o) {
\r
395 // remove via DOM calls so we don't lose event handlers
\r
396 if (this.parentNode)
\r
397 this.parentNode.removeChild(this);
\r
400 if (data && data.el) {
\r
401 data.el.style.display = data.display;
\r
402 data.el.style.position = data.position;
\r
404 data.parent.appendChild(data.el);
\r
405 $(el).removeData('blockUI.history');
\r
408 if (typeof opts.onUnblock == 'function')
\r
409 opts.onUnblock(el,opts);
\r
412 // bind/unbind the handler
\r
413 function bind(b, el, opts) {
\r
414 var full = el == window, $el = $(el);
\r
416 // don't bother unbinding if there is nothing to unbind
\r
417 if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
\r
420 $el.data('blockUI.isBlocked', b);
\r
422 // don't bind events when overlay is not in use or if bindEvents is false
\r
423 if (!opts.bindEvents || (b && !opts.showOverlay))
\r
426 // bind anchors and inputs for mouse and key events
\r
427 var events = 'mousedown mouseup keydown keypress';
\r
428 b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
\r
431 // var $e = $('a,:input');
\r
432 // b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
\r
435 // event handler to suppress keyboard/mouse events when blocking
\r
436 function handler(e) {
\r
437 // allow tab navigation (conditionally)
\r
438 if (e.keyCode && e.keyCode == 9) {
\r
439 if (pageBlock && e.data.constrainTabKey) {
\r
440 var els = pageBlockEls;
\r
441 var fwd = !e.shiftKey && e.target == els[els.length-1];
\r
442 var back = e.shiftKey && e.target == els[0];
\r
444 setTimeout(function(){focus(back)},10);
\r
449 // allow events within the message content
\r
450 if ($(e.target).parents('div.blockMsg').length > 0)
\r
453 // allow events for content that is not being blocked
\r
454 return $(e.target).parents().children().filter('div.blockUI').length == 0;
\r
457 function focus(back) {
\r
460 var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
\r
465 function center(el, x, y) {
\r
466 var p = el.parentNode, s = el.style;
\r
467 var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
\r
468 var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
\r
469 if (x) s.left = l > 0 ? (l+'px') : '0';
\r
470 if (y) s.top = t > 0 ? (t+'px') : '0';
\r
473 function sz(el, p) {
\r
474 return parseInt($.css(el,p))||0;
\r