OnlyOffice在线文档

jquery.blockUI.js 18KB

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