jquery.blockUI.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*!
  2. * jQuery blockUI plugin
  3. * Version 2.39 (23-MAY-2011)
  4. * @requires jQuery v1.2.3 or later
  5. *
  6. * Examples at: http://malsup.com/jquery/block/
  7. * Copyright (c) 2007-2010 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. /*
  16. if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
  17. alert('blockUI requires jQuery v1.2.3 or later! You are using v' + $.fn.jquery);
  18. return;
  19. }
  20. */
  21. $.fn._fadeIn = $.fn.fadeIn;
  22. var noOp = function() {};
  23. // this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
  24. // retarded userAgent strings on Vista)
  25. var mode = document.documentMode || 0;
  26. var setExpr = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);
  27. var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) && !mode;
  28. // global $ methods for blocking/unblocking the entire page
  29. $.blockUI = function(opts) { install(window, opts); };
  30. $.unblockUI = function(opts) { remove(window, opts); };
  31. // convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
  32. $.growlUI = function(title, message, timeout, onClose) {
  33. var $m = $('<div class="growlUI"></div>');
  34. if (title) $m.append('<h1>'+title+'</h1>');
  35. if (message) $m.append('<h2>'+message+'</h2>');
  36. if (timeout == undefined) timeout = 3000;
  37. $.blockUI({
  38. message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
  39. timeout: timeout, showOverlay: false,
  40. onUnblock: onClose,
  41. css: $.blockUI.defaults.growlCSS
  42. });
  43. };
  44. // plugin method for blocking element content
  45. $.fn.block = function(opts) {
  46. return this.unblock({ fadeOut: 0 }).each(function() {
  47. if ($.css(this,'position') == 'static')
  48. this.style.position = 'relative';
  49. if ($.browser.msie)
  50. this.style.zoom = 1; // force 'hasLayout'
  51. install(this, opts);
  52. });
  53. };
  54. // plugin method for unblocking element content
  55. $.fn.unblock = function(opts) {
  56. return this.each(function() {
  57. remove(this, opts);
  58. });
  59. };
  60. $.blockUI.version = 2.39; // 2nd generation blocking at no extra cost!
  61. // override these in your code to change the default behavior and style
  62. $.blockUI.defaults = {
  63. // message displayed when blocking (use null for no message)
  64. message: '<h1>Please wait...</h1>',
  65. title: null, // title string; only used when theme == true
  66. draggable: true, // only used when theme == true (requires jquery-ui.js to be loaded)
  67. theme: false, // set to true to use with jQuery UI themes
  68. // styles for the message when blocking; if you wish to disable
  69. // these and use an external stylesheet then do this in your code:
  70. // $.blockUI.defaults.css = {};
  71. css: {
  72. padding: 0,
  73. margin: 0,
  74. width: '30%',
  75. top: '40%',
  76. left: '35%',
  77. textAlign: 'center',
  78. color: '#000',
  79. border: '3px solid #aaa',
  80. backgroundColor:'#fff',
  81. cursor: 'wait'
  82. },
  83. // minimal style set used when themes are used
  84. themedCSS: {
  85. width: '30%',
  86. top: '40%',
  87. left: '35%'
  88. },
  89. // styles for the overlay
  90. overlayCSS: {
  91. backgroundColor: '#000',
  92. opacity: 0.6,
  93. cursor: 'wait'
  94. },
  95. // styles applied when using $.growlUI
  96. growlCSS: {
  97. width: '350px',
  98. top: '10px',
  99. left: '',
  100. right: '10px',
  101. border: 'none',
  102. padding: '5px',
  103. opacity: 0.6,
  104. cursor: 'default',
  105. color: '#fff',
  106. backgroundColor: '#000',
  107. '-webkit-border-radius': '10px',
  108. '-moz-border-radius': '10px',
  109. 'border-radius': '10px'
  110. },
  111. // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
  112. // (hat tip to Jorge H. N. de Vasconcelos)
  113. iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
  114. // force usage of iframe in non-IE browsers (handy for blocking applets)
  115. forceIframe: false,
  116. // z-index for the blocking overlay
  117. baseZ: 1000,
  118. // set these to true to have the message automatically centered
  119. centerX: true, // <-- only effects element blocking (page block controlled via css above)
  120. centerY: true,
  121. // allow body element to be stetched in ie6; this makes blocking look better
  122. // on "short" pages. disable if you wish to prevent changes to the body height
  123. allowBodyStretch: true,
  124. // enable if you want key and mouse events to be disabled for content that is blocked
  125. bindEvents: true,
  126. // be default blockUI will supress tab navigation from leaving blocking content
  127. // (if bindEvents is true)
  128. constrainTabKey: true,
  129. // fadeIn time in millis; set to 0 to disable fadeIn on block
  130. fadeIn: 200,
  131. // fadeOut time in millis; set to 0 to disable fadeOut on unblock
  132. fadeOut: 400,
  133. // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
  134. timeout: 0,
  135. // disable if you don't want to show the overlay
  136. showOverlay: true,
  137. // if true, focus will be placed in the first available input field when
  138. // page blocking
  139. focusInput: true,
  140. // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
  141. applyPlatformOpacityRules: true,
  142. // callback method invoked when fadeIn has completed and blocking message is visible
  143. onBlock: null,
  144. // callback method invoked when unblocking has completed; the callback is
  145. // passed the element that has been unblocked (which is the window object for page
  146. // blocks) and the options that were passed to the unblock call:
  147. // onUnblock(element, options)
  148. onUnblock: null,
  149. // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
  150. quirksmodeOffsetHack: 4,
  151. // class name of the message block
  152. blockMsgClass: 'blockMsg'
  153. };
  154. // private data and functions follow...
  155. var pageBlock = null;
  156. var pageBlockEls = [];
  157. function install(el, opts) {
  158. var full = (el == window);
  159. var msg = opts && opts.message !== undefined ? opts.message : undefined;
  160. opts = $.extend({}, $.blockUI.defaults, opts || {});
  161. opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
  162. var css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
  163. var themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
  164. msg = msg === undefined ? opts.message : msg;
  165. // remove the current block (if there is one)
  166. if (full && pageBlock)
  167. remove(window, {fadeOut:0});
  168. // if an existing element is being used as the blocking content then we capture
  169. // its current place in the DOM (and current display style) so we can restore
  170. // it when we unblock
  171. if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
  172. var node = msg.jquery ? msg[0] : msg;
  173. var data = {};
  174. $(el).data('blockUI.history', data);
  175. data.el = node;
  176. data.parent = node.parentNode;
  177. data.display = node.style.display;
  178. data.position = node.style.position;
  179. if (data.parent)
  180. data.parent.removeChild(node);
  181. }
  182. $(el).data('blockUI.onUnblock', opts.onUnblock);
  183. var z = opts.baseZ;
  184. // blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
  185. // layer1 is the iframe layer which is used to supress bleed through of underlying content
  186. // layer2 is the overlay layer which has opacity and a wait cursor (by default)
  187. // layer3 is the message content that is displayed while blocking
  188. var lyr1 = ($.browser.msie || opts.forceIframe)
  189. ? $('<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>')
  190. : $('<div class="blockUI" style="display:none"></div>');
  191. var lyr2 = opts.theme
  192. ? $('<div class="blockUI blockOverlay ui-widget-overlay" style="z-index:'+ (z++) +';display:none"></div>')
  193. : $('<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>');
  194. var lyr3, s;
  195. if (opts.theme && full) {
  196. s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:fixed">' +
  197. '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>' +
  198. '<div class="ui-widget-content ui-dialog-content"></div>' +
  199. '</div>';
  200. }
  201. else if (opts.theme) {
  202. s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:absolute">' +
  203. '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>' +
  204. '<div class="ui-widget-content ui-dialog-content"></div>' +
  205. '</div>';
  206. }
  207. else if (full) {
  208. s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage" style="z-index:'+(z+10)+';display:none;position:fixed"></div>';
  209. }
  210. else {
  211. s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement" style="z-index:'+(z+10)+';display:none;position:absolute"></div>';
  212. }
  213. lyr3 = $(s);
  214. // if we have a message, style it
  215. if (msg) {
  216. if (opts.theme) {
  217. lyr3.css(themedCSS);
  218. lyr3.addClass('ui-widget-content');
  219. }
  220. else
  221. lyr3.css(css);
  222. }
  223. // style the overlay
  224. if (!opts.theme && (!opts.applyPlatformOpacityRules || !($.browser.mozilla && /Linux/.test(navigator.platform))))
  225. lyr2.css(opts.overlayCSS);
  226. lyr2.css('position', full ? 'fixed' : 'absolute');
  227. // make iframe layer transparent in IE
  228. if ($.browser.msie || opts.forceIframe)
  229. lyr1.css('opacity',0.0);
  230. //$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
  231. var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);
  232. $.each(layers, function() {
  233. this.appendTo($par);
  234. });
  235. if (opts.theme && opts.draggable && $.fn.draggable) {
  236. lyr3.draggable({
  237. handle: '.ui-dialog-titlebar',
  238. cancel: 'li'
  239. });
  240. }
  241. // ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
  242. var expr = setExpr && (!$.boxModel || $('object,embed', full ? null : el).length > 0);
  243. if (ie6 || expr) {
  244. // give body 100% height
  245. if (full && opts.allowBodyStretch && $.boxModel)
  246. $('html,body').css('height','100%');
  247. // fix ie6 issue when blocked element has a border width
  248. if ((ie6 || !$.boxModel) && !full) {
  249. var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
  250. var fixT = t ? '(0 - '+t+')' : 0;
  251. var fixL = l ? '(0 - '+l+')' : 0;
  252. }
  253. // simulate fixed position
  254. $.each([lyr1,lyr2,lyr3], function(i,o) {
  255. var s = o[0].style;
  256. s.position = 'absolute';
  257. if (i < 2) {
  258. full ? s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"')
  259. : s.setExpression('height','this.parentNode.offsetHeight + "px"');
  260. full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
  261. : s.setExpression('width','this.parentNode.offsetWidth + "px"');
  262. if (fixL) s.setExpression('left', fixL);
  263. if (fixT) s.setExpression('top', fixT);
  264. }
  265. else if (opts.centerY) {
  266. 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"');
  267. s.marginTop = 0;
  268. }
  269. else if (!opts.centerY && full) {
  270. var top = (opts.css && opts.css.top) ? parseInt(opts.css.top) : 0;
  271. var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
  272. s.setExpression('top',expression);
  273. }
  274. });
  275. }
  276. // show the message
  277. if (msg) {
  278. if (opts.theme)
  279. lyr3.find('.ui-widget-content').append(msg);
  280. else
  281. lyr3.append(msg);
  282. if (msg.jquery || msg.nodeType)
  283. $(msg).show();
  284. }
  285. if (($.browser.msie || opts.forceIframe) && opts.showOverlay)
  286. lyr1.show(); // opacity is zero
  287. if (opts.fadeIn) {
  288. var cb = opts.onBlock ? opts.onBlock : noOp;
  289. var cb1 = (opts.showOverlay && !msg) ? cb : noOp;
  290. var cb2 = msg ? cb : noOp;
  291. if (opts.showOverlay)
  292. lyr2._fadeIn(opts.fadeIn, cb1);
  293. if (msg)
  294. lyr3._fadeIn(opts.fadeIn, cb2);
  295. }
  296. else {
  297. if (opts.showOverlay)
  298. lyr2.show();
  299. if (msg)
  300. lyr3.show();
  301. if (opts.onBlock)
  302. opts.onBlock();
  303. }
  304. // bind key and mouse events
  305. bind(1, el, opts);
  306. if (full) {
  307. pageBlock = lyr3[0];
  308. pageBlockEls = $(':input:enabled:visible',pageBlock);
  309. if (opts.focusInput)
  310. setTimeout(focus, 20);
  311. }
  312. else
  313. center(lyr3[0], opts.centerX, opts.centerY);
  314. if (opts.timeout) {
  315. // auto-unblock
  316. var to = setTimeout(function() {
  317. full ? $.unblockUI(opts) : $(el).unblock(opts);
  318. }, opts.timeout);
  319. $(el).data('blockUI.timeout', to);
  320. }
  321. };
  322. // remove the block
  323. function remove(el, opts) {
  324. var full = (el == window);
  325. var $el = $(el);
  326. var data = $el.data('blockUI.history');
  327. var to = $el.data('blockUI.timeout');
  328. if (to) {
  329. clearTimeout(to);
  330. $el.removeData('blockUI.timeout');
  331. }
  332. opts = $.extend({}, $.blockUI.defaults, opts || {});
  333. bind(0, el, opts); // unbind events
  334. if (opts.onUnblock === null) {
  335. opts.onUnblock = $el.data('blockUI.onUnblock');
  336. $el.removeData('blockUI.onUnblock');
  337. }
  338. var els;
  339. if (full) // crazy selector to handle odd field errors in ie6/7
  340. els = $('body').children().filter('.blockUI').add('body > .blockUI');
  341. else
  342. els = $('.blockUI', el);
  343. if (full)
  344. pageBlock = pageBlockEls = null;
  345. if (opts.fadeOut) {
  346. els.fadeOut(opts.fadeOut);
  347. setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
  348. }
  349. else
  350. reset(els, data, opts, el);
  351. };
  352. // move blocking element back into the DOM where it started
  353. function reset(els,data,opts,el) {
  354. els.each(function(i,o) {
  355. // remove via DOM calls so we don't lose event handlers
  356. if (this.parentNode)
  357. this.parentNode.removeChild(this);
  358. });
  359. if (data && data.el) {
  360. data.el.style.display = data.display;
  361. data.el.style.position = data.position;
  362. if (data.parent)
  363. data.parent.appendChild(data.el);
  364. $(el).removeData('blockUI.history');
  365. }
  366. if (typeof opts.onUnblock == 'function')
  367. opts.onUnblock(el,opts);
  368. };
  369. // bind/unbind the handler
  370. function bind(b, el, opts) {
  371. var full = el == window, $el = $(el);
  372. // don't bother unbinding if there is nothing to unbind
  373. if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
  374. return;
  375. if (!full)
  376. $el.data('blockUI.isBlocked', b);
  377. // don't bind events when overlay is not in use or if bindEvents is false
  378. if (!opts.bindEvents || (b && !opts.showOverlay))
  379. return;
  380. // bind anchors and inputs for mouse and key events
  381. var events = 'mousedown mouseup keydown keypress';
  382. b ? $(document).bind(events, opts, handler) : $(document).unbind(events, handler);
  383. // former impl...
  384. // var $e = $('a,:input');
  385. // b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
  386. };
  387. // event handler to suppress keyboard/mouse events when blocking
  388. function handler(e) {
  389. // allow tab navigation (conditionally)
  390. if (e.keyCode && e.keyCode == 9) {
  391. if (pageBlock && e.data.constrainTabKey) {
  392. var els = pageBlockEls;
  393. var fwd = !e.shiftKey && e.target === els[els.length-1];
  394. var back = e.shiftKey && e.target === els[0];
  395. if (fwd || back) {
  396. setTimeout(function(){focus(back)},10);
  397. return false;
  398. }
  399. }
  400. }
  401. var opts = e.data;
  402. // allow events within the message content
  403. if ($(e.target).parents('div.' + opts.blockMsgClass).length > 0)
  404. return true;
  405. // allow events for content that is not being blocked
  406. return $(e.target).parents().children().filter('div.blockUI').length == 0;
  407. };
  408. function focus(back) {
  409. if (!pageBlockEls)
  410. return;
  411. var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
  412. if (e)
  413. e.focus();
  414. };
  415. function center(el, x, y) {
  416. var p = el.parentNode, s = el.style;
  417. var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
  418. var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
  419. if (x) s.left = l > 0 ? (l+'px') : '0';
  420. if (y) s.top = t > 0 ? (t+'px') : '0';
  421. };
  422. function sz(el, p) {
  423. return parseInt($.css(el,p))||0;
  424. };
  425. })(jQuery);