/bi-platform-v2-plugin/cdf/js/jquery.blockUI.js

http://pentaho-cdf.googlecode.com/ · JavaScript · 487 lines · 329 code · 74 blank · 84 comment · 131 complexity · 5263735d1adbeb0418471ba500794a8b MD5 · raw file

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