jquery.blockUI.js 18 KB

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