PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/media/js/multiup/jquery.blockUI.js

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