jquery.blockUI.js 12 KB

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