blockUI.js 12 KB

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