PageRenderTime 366ms CodeModel.GetById 140ms app.highlight 84ms RepoModel.GetById 138ms app.codeStats 0ms

/kai/public/javascripts/jquery.blockUI.js

https://bitbucket.org/bbangert/kai/
JavaScript | 312 lines | 197 code | 49 blank | 66 comment | 80 complexity | 72351d3b8e5b079c231ae20825407fef MD5 | raw file
  1/*
  2 * jQuery blockUI plugin
  3 * Version 2.08 (06/11/2008)
  4 * @requires jQuery v1.2.3 or later
  5 *
  6 * Examples at: http://malsup.com/jquery/block/
  7 * Copyright (c) 2007-2008 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
 11 * 
 12 * Thanks to Amir-Hossein Sobhi for some excellent contributions!
 13 */
 14
 15;(function($) {
 16
 17if (/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);
 19    return;
 20}
 21
 22// global $ methods for blocking/unblocking the entire page
 23$.blockUI   = function(opts) { install(window, opts); };
 24$.unblockUI = function(opts) { remove(window, opts); };
 25
 26// plugin method for blocking element content
 27$.fn.block = function(opts) {
 28    return this.each(function() {
 29        if ($.css(this,'position') == 'static')
 30            this.style.position = 'relative';
 31        if ($.browser.msie) 
 32            this.style.zoom = 1; // force 'hasLayout'
 33        install(this, opts);
 34    });
 35};
 36
 37// plugin method for unblocking element content
 38$.fn.unblock = function(opts) {
 39    return this.each(function() {
 40        remove(this, opts);
 41    });
 42};
 43
 44$.blockUI.version = 2.08; // 2nd generation blocking at no extra cost!
 45
 46// override these in your code to change the default behavior and style
 47$.blockUI.defaults = {
 48    // message displayed when blocking (use null for no message)
 49    message:  '<h1>Please wait...</h1>',
 50    
 51    // styles for the message when blocking; if you wish to disable
 52    // these and use an external stylesheet then do this in your code:
 53    // $.blockUI.defaults.css = {};
 54    css: { 
 55        padding:        0,
 56        margin:         0,
 57        width:          '30%', 
 58        top:            '40%', 
 59        left:           '35%', 
 60        textAlign:      'center', 
 61        color:          '#000', 
 62        border:         '3px solid #aaa',
 63        backgroundColor:'#fff',
 64        cursor:         'wait'
 65    },
 66    
 67    // styles for the overlay
 68    overlayCSS:  { 
 69        backgroundColor:'#000', 
 70        opacity:        '0.6' 
 71    },
 72    
 73    // z-index for the blocking overlay
 74    baseZ: 1000,
 75    
 76    // set these to true to have the message automatically centered
 77    centerX: true, // <-- only effects element blocking (page block controlled via css above)
 78    centerY: true,
 79    
 80    // allow body element to be stetched in ie6; this makes blocking look better
 81    // on "short" pages.  disable if you wish to prevent changes to the body height
 82    allowBodyStretch: true,
 83    
 84    // be default blockUI will supress tab navigation from leaving blocking content;
 85    constrainTabKey: true,
 86    
 87    // fadeOut time in millis; set to 0 to disable fadeout on unblock
 88    fadeOut:  400,
 89    
 90    // if true, focus will be placed in the first available input field when
 91    // page blocking
 92    focusInput: true,
 93    
 94    // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
 95    applyPlatformOpacityRules: true,
 96    
 97    // callback method invoked when unblocking has completed; the callback is
 98    // passed the element that has been unblocked (which is the window object for page
 99    // blocks) and the options that were passed to the unblock call:
100    //     onUnblock(element, options)
101    onUnblock: null
102};
103
104// private data and functions follow...
105
106var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent);
107var pageBlock = null;
108var pageBlockEls = [];
109
110function install(el, opts) {
111    var full = (el == window);
112    var msg = opts && opts.message !== undefined ? opts.message : undefined;
113    opts = $.extend({}, $.blockUI.defaults, opts || {});
114    opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
115    var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
116    msg = msg === undefined ? opts.message : msg;
117
118    // remove the current block (if there is one)
119    if (full && pageBlock) 
120        remove(window, {fadeOut:0}); 
121    
122    // if an existing element is being used as the blocking content then we capture
123    // its current place in the DOM (and current display style) so we can restore
124    // it when we unblock
125    if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
126        var node = msg.jquery ? msg[0] : msg;
127        var data = {};
128        $(el).data('blockUI.history', data);
129        data.el = node;
130        data.parent = node.parentNode;
131        data.display = node.style.display;
132        data.position = node.style.position;
133        data.parent.removeChild(node);
134    }
135    
136    var z = opts.baseZ;
137    
138    // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
139    // layer1 is the iframe layer which is used to supress bleed through of underlying content
140    // layer2 is the overlay layer which has opacity and a wait cursor
141    // layer3 is the message content that is displayed while blocking
142    
143    var lyr1 = ($.browser.msie) ? $('<iframe class="blockUI" style="z-index:'+ z++ +';border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;"></iframe>')
144                                : $('<div class="blockUI" style="display:none"></div>');
145    var lyr2 = $('<div class="blockUI" style="z-index:'+ z++ +';cursor:wait;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
146    var lyr3 = full ? $('<div class="blockUI blockMsg blockPage" style="z-index:'+z+';position:fixed"></div>')
147                    : $('<div class="blockUI blockMsg blockElement" style="z-index:'+z+';display:none;position:absolute"></div>');
148
149    // if we have a message, style it
150    if (msg) 
151        lyr3.css(css);
152
153    // style the overlay
154    if (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform))) 
155        lyr2.css(opts.overlayCSS);
156    lyr2.css('position', full ? 'fixed' : 'absolute');
157    
158    // make iframe layer transparent in IE
159    if ($.browser.msie) 
160        lyr1.css('opacity','0.0');
161
162    $([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
163    
164    // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
165    var expr = $.browser.msie && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
166    if (ie6 || expr) {
167        // give body 100% height
168        if (full && opts.allowBodyStretch && $.boxModel)
169            $('html,body').css('height','100%');
170
171        // fix ie6 issue when blocked element has a border width
172        if ((ie6 || !$.boxModel) && !full) {
173            var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
174            var fixT = t ? '(0 - '+t+')' : 0;
175            var fixL = l ? '(0 - '+l+')' : 0;
176        }
177
178        // simulate fixed position
179        $.each([lyr1,lyr2,lyr3], function(i,o) {
180            var s = o[0].style;
181            s.position = 'absolute';
182            if (i < 2) {
183                full ? s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"')
184                     : s.setExpression('height','this.parentNode.offsetHeight + "px"');
185                full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
186                     : s.setExpression('width','this.parentNode.offsetWidth + "px"');
187                if (fixL) s.setExpression('left', fixL);
188                if (fixT) s.setExpression('top', fixT);
189            }
190            else if (opts.centerY) {
191                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"');
192                s.marginTop = 0;
193            }
194        });
195    }
196    
197    // show the message
198    lyr3.append(msg).show();
199    if (msg && (msg.jquery || msg.nodeType))
200        $(msg).show();
201
202    // bind key and mouse events
203    bind(1, el, opts);
204        
205    if (full) {
206        pageBlock = lyr3[0];
207        pageBlockEls = $(':input:enabled:visible',pageBlock);
208        if (opts.focusInput)
209            setTimeout(focus, 20);
210    }
211    else
212        center(lyr3[0], opts.centerX, opts.centerY);
213};
214
215// remove the block
216function remove(el, opts) {
217    var full = el == window;
218    var data = $(el).data('blockUI.history');
219    opts = $.extend({}, $.blockUI.defaults, opts || {});
220    bind(0, el, opts); // unbind events
221    var els = full ? $('body').children().filter('.blockUI') : $('.blockUI', el);
222    
223    if (full) 
224        pageBlock = pageBlockEls = null;
225
226    if (opts.fadeOut) {
227        els.fadeOut(opts.fadeOut);
228        setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
229    }
230    else
231        reset(els, data, opts, el);
232};
233
234// move blocking element back into the DOM where it started
235function reset(els,data,opts,el) {
236    els.each(function(i,o) {
237        // remove via DOM calls so we don't lose event handlers
238        if (this.parentNode) 
239            this.parentNode.removeChild(this);
240    });
241    if (data && data.el) {
242        data.el.style.display = data.display;
243        data.el.style.position = data.position;
244        data.parent.appendChild(data.el);
245        $(data.el).removeData('blockUI.history');
246    }
247    if (typeof opts.onUnblock == 'function')
248        opts.onUnblock(el,opts);
249};
250
251// bind/unbind the handler
252function bind(b, el, opts) {
253    var full = el == window, $el = $(el);
254    
255    // don't bother unbinding if there is nothing to unbind
256    if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked'))) 
257        return;
258    if (!full) 
259        $el.data('blockUI.isBlocked', b);
260        
261    // bind anchors and inputs for mouse and key events
262    var events = 'mousedown mouseup keydown keypress click';
263    b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
264
265// former impl...
266//    var $e = $('a,:input');
267//    b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
268};
269
270// event handler to suppress keyboard/mouse events when blocking
271function handler(e) {
272    // allow tab navigation (conditionally)
273    if (e.keyCode && e.keyCode == 9) {
274        if (pageBlock && e.data.constrainTabKey) {
275            var els = pageBlockEls;
276            var fwd = !e.shiftKey && e.target == els[els.length-1];
277            var back = e.shiftKey && e.target == els[0];
278            if (fwd || back) {
279                setTimeout(function(){focus(back)},10);
280                return false;
281            }
282        }
283    }
284    // allow events within the message content
285    if ($(e.target).parents('div.blockMsg').length > 0)
286        return true;
287        
288    // allow events for content that is not being blocked
289    return $(e.target).parents().children().filter('div.blockUI').length == 0;
290};
291
292function focus(back) {
293    if (!pageBlockEls) 
294        return;
295    var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
296    if (e) 
297        e.focus();
298};
299
300function center(el, x, y) {
301    var p = el.parentNode, s = el.style;
302    var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
303    var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
304    if (x) s.left = l > 0 ? (l+'px') : '0';
305    if (y) s.top  = t > 0 ? (t+'px') : '0';
306};
307
308function sz(el, p) { 
309    return parseInt($.css(el,p))||0; 
310};
311
312})(jQuery);