Project

General

Profile

Download (18.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/*!
2
 * jQuery blockUI plugin
3
 * Version 2.53 (01-NOV-2012)
4
 * @requires jQuery v1.3 or later
5
 *
6
 * Examples at: http://malsup.com/jquery/block/
7
 * Copyright (c) 2007-2012 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

    
15
;(function() {
16
"use strict";
17

    
18
	function setup($) {
19
		if (/^1\.(0|1|2)/.test($.fn.jquery)) {
20
			/*global alert:true */
21
			alert('blockUI requires jQuery v1.3 or later!  You are using v' + $.fn.jquery);
22
			return;
23
		}
24

    
25
		$.fn._fadeIn = $.fn.fadeIn;
26

    
27
		var noOp = $.noop || function() {};
28

    
29
		// this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
30
		// retarded userAgent strings on Vista)
31
		var msie = /MSIE/.test(navigator.userAgent);
32
		var ie6  = /MSIE 6.0/.test(navigator.userAgent);
33
		var mode = document.documentMode || 0;
34
		// var setExpr = msie && (($.browser.version < 8 && !mode) || mode < 8);
35
		var setExpr = $.isFunction( document.createElement('div').style.setExpression );
36

    
37
		// global $ methods for blocking/unblocking the entire page
38
		$.blockUI   = function(opts) { install(window, opts); };
39
		$.unblockUI = function(opts) { remove(window, opts); };
40

    
41
		// convenience method for quick growl-like notifications  (http://www.google.com/search?q=growl)
42
		$.growlUI = function(title, message, timeout, onClose) {
43
			var $m = $('<div class="growlUI"></div>');
44
			if (title) $m.append('<h1>'+title+'</h1>');
45
			if (message) $m.append('<h2>'+message+'</h2>');
46
			if (timeout === undefined) timeout = 3000;
47
			$.blockUI({
48
				message: $m, fadeIn: 700, fadeOut: 1000, centerY: false,
49
				timeout: timeout, showOverlay: false,
50
				onUnblock: onClose,
51
				css: $.blockUI.defaults.growlCSS
52
			});
53
		};
54

    
55
		// plugin method for blocking element content
56
		$.fn.block = function(opts) {
57
			var fullOpts = $.extend({}, $.blockUI.defaults, opts || {});
58
			this.each(function() {
59
				var $el = $(this);
60
				if (fullOpts.ignoreIfBlocked && $el.data('blockUI.isBlocked'))
61
					return;
62
				$el.unblock({ fadeOut: 0 });
63
			});
64

    
65
			return this.each(function() {
66
				if ($.css(this,'position') == 'static')
67
					this.style.position = 'relative';
68
				this.style.zoom = 1; // force 'hasLayout' in ie
69
				install(this, opts);
70
			});
71
		};
72

    
73
		// plugin method for unblocking element content
74
		$.fn.unblock = function(opts) {
75
			return this.each(function() {
76
				remove(this, opts);
77
			});
78
		};
79

    
80
		$.blockUI.version = 2.53; // 2nd generation blocking at no extra cost!
81

    
82
		// override these in your code to change the default behavior and style
83
		$.blockUI.defaults = {
84
			// message displayed when blocking (use null for no message)
85
			message:  '<h1>Please wait...</h1>',
86

    
87
			title: null,		// title string; only used when theme == true
88
			draggable: true,	// only used when theme == true (requires jquery-ui.js to be loaded)
89

    
90
			theme: false, // set to true to use with jQuery UI themes
91

    
92
			// styles for the message when blocking; if you wish to disable
93
			// these and use an external stylesheet then do this in your code:
94
			// $.blockUI.defaults.css = {};
95
			css: {
96
				padding:	0,
97
				margin:		0,
98
				width:		'30%',
99
				top:		'40%',
100
				left:		'35%',
101
				textAlign:	'center',
102
				color:		'#000',
103
				border:		'3px solid #aaa',
104
				backgroundColor:'#fff',
105
				cursor:		'wait'
106
			},
107

    
108
			// minimal style set used when themes are used
109
			themedCSS: {
110
				width:	'30%',
111
				top:	'40%',
112
				left:	'35%'
113
			},
114

    
115
			// styles for the overlay
116
			overlayCSS:  {
117
				backgroundColor:	'#000',
118
				opacity:				0.6,
119
				cursor:				'wait'
120
			},
121

    
122
			// style to replace wait cursor before unblocking to correct issue
123
			// of lingering wait cursor
124
			cursorReset: 'default',
125

    
126
			// styles applied when using $.growlUI
127
			growlCSS: {
128
				width:		'350px',
129
				top:		'10px',
130
				left:		'',
131
				right:		'10px',
132
				border:		'none',
133
				padding:	'5px',
134
				opacity:	0.6,
135
				cursor:		'default',
136
				color:		'#fff',
137
				backgroundColor: '#000',
138
				'-webkit-border-radius':'10px',
139
				'-moz-border-radius':	'10px',
140
				'border-radius':		'10px'
141
			},
142

    
143
			// IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
144
			// (hat tip to Jorge H. N. de Vasconcelos)
145
			/*jshint scripturl:true */
146
			iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
147

    
148
			// force usage of iframe in non-IE browsers (handy for blocking applets)
149
			forceIframe: false,
150

    
151
			// z-index for the blocking overlay
152
			baseZ: 1000,
153

    
154
			// set these to true to have the message automatically centered
155
			centerX: true, // <-- only effects element blocking (page block controlled via css above)
156
			centerY: true,
157

    
158
			// allow body element to be stetched in ie6; this makes blocking look better
159
			// on "short" pages.  disable if you wish to prevent changes to the body height
160
			allowBodyStretch: true,
161

    
162
			// enable if you want key and mouse events to be disabled for content that is blocked
163
			bindEvents: true,
164

    
165
			// be default blockUI will supress tab navigation from leaving blocking content
166
			// (if bindEvents is true)
167
			constrainTabKey: true,
168

    
169
			// fadeIn time in millis; set to 0 to disable fadeIn on block
170
			fadeIn:  200,
171

    
172
			// fadeOut time in millis; set to 0 to disable fadeOut on unblock
173
			fadeOut:  400,
174

    
175
			// time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
176
			timeout: 0,
177

    
178
			// disable if you don't want to show the overlay
179
			showOverlay: true,
180

    
181
			// if true, focus will be placed in the first available input field when
182
			// page blocking
183
			focusInput: true,
184

    
185
			// suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
186
			// no longer needed in 2012
187
			// applyPlatformOpacityRules: true,
188

    
189
			// callback method invoked when fadeIn has completed and blocking message is visible
190
			onBlock: null,
191

    
192
			// callback method invoked when unblocking has completed; the callback is
193
			// passed the element that has been unblocked (which is the window object for page
194
			// blocks) and the options that were passed to the unblock call:
195
			//	onUnblock(element, options)
196
			onUnblock: null,
197

    
198
			// callback method invoked when the overlay area is clicked.
199
			// setting this will turn the cursor to a pointer, otherwise cursor defined in overlayCss will be used.
200
			onOverlayClick: null,
201

    
202
			// don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
203
			quirksmodeOffsetHack: 4,
204

    
205
			// class name of the message block
206
			blockMsgClass: 'blockMsg',
207

    
208
			// if it is already blocked, then ignore it (don't unblock and reblock)
209
			ignoreIfBlocked: false
210
		};
211

    
212
		// private data and functions follow...
213

    
214
		var pageBlock = null;
215
		var pageBlockEls = [];
216

    
217
		function install(el, opts) {
218
			var css, themedCSS;
219
			var full = (el == window);
220
			var msg = (opts && opts.message !== undefined ? opts.message : undefined);
221
			opts = $.extend({}, $.blockUI.defaults, opts || {});
222

    
223
			if (opts.ignoreIfBlocked && $(el).data('blockUI.isBlocked'))
224
				return;
225

    
226
			opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
227
			css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
228
			if (opts.onOverlayClick)
229
				opts.overlayCSS.cursor = 'pointer';
230

    
231
			themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
232
			msg = msg === undefined ? opts.message : msg;
233

    
234
			// remove the current block (if there is one)
235
			if (full && pageBlock)
236
				remove(window, {fadeOut:0});
237

    
238
			// if an existing element is being used as the blocking content then we capture
239
			// its current place in the DOM (and current display style) so we can restore
240
			// it when we unblock
241
			if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
242
				var node = msg.jquery ? msg[0] : msg;
243
				var data = {};
244
				$(el).data('blockUI.history', data);
245
				data.el = node;
246
				data.parent = node.parentNode;
247
				data.display = node.style.display;
248
				data.position = node.style.position;
249
				if (data.parent)
250
					data.parent.removeChild(node);
251
			}
252

    
253
			$(el).data('blockUI.onUnblock', opts.onUnblock);
254
			var z = opts.baseZ;
255

    
256
			// blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
257
			// layer1 is the iframe layer which is used to supress bleed through of underlying content
258
			// layer2 is the overlay layer which has opacity and a wait cursor (by default)
259
			// layer3 is the message content that is displayed while blocking
260
			var lyr1, lyr2, lyr3, s;
261
			if (msie || opts.forceIframe)
262
				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>');
263
			else
264
				lyr1 = $('<div class="blockUI" style="display:none"></div>');
265

    
266
			if (opts.theme)
267
				lyr2 = $('<div class="blockUI blockOverlay ui-widget-overlay" style="z-index:'+ (z++) +';display:none"></div>');
268
			else
269
				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>');
270

    
271
			if (opts.theme && full) {
272
				s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:fixed">';
273
				if ( opts.title ) {
274
					s += '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>';
275
				}
276
				s += '<div class="ui-widget-content ui-dialog-content"></div>';
277
				s += '</div>';
278
			}
279
			else if (opts.theme) {
280
				s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:absolute">';
281
				if ( opts.title ) {
282
					s += '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>';
283
				}  
284
				s += '<div class="ui-widget-content ui-dialog-content"></div>';
285
				s += '</div>';
286
			}
287
			else if (full) {
288
				s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage" style="z-index:'+(z+10)+';display:none;position:fixed"></div>';
289
			}
290
			else {
291
				s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement" style="z-index:'+(z+10)+';display:none;position:absolute"></div>';
292
			}
293
			lyr3 = $(s);
294

    
295
			// if we have a message, style it
296
			if (msg) {
297
				if (opts.theme) {
298
					lyr3.css(themedCSS);
299
					lyr3.addClass('ui-widget-content');
300
				}
301
				else
302
					lyr3.css(css);
303
			}
304

    
305
			// style the overlay
306
			if (!opts.theme /*&& (!opts.applyPlatformOpacityRules)*/)
307
				lyr2.css(opts.overlayCSS);
308
			lyr2.css('position', full ? 'fixed' : 'absolute');
309

    
310
			// make iframe layer transparent in IE
311
			if (msie || opts.forceIframe)
312
				lyr1.css('opacity',0.0);
313

    
314
			//$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
315
			var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);
316
			$.each(layers, function() {
317
				this.appendTo($par);
318
			});
319

    
320
			if (opts.theme && opts.draggable && $.fn.draggable) {
321
				lyr3.draggable({
322
					handle: '.ui-dialog-titlebar',
323
					cancel: 'li'
324
				});
325
			}
326

    
327
			// ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
328
			var expr = setExpr && (!$.support.boxModel || $('object,embed', full ? null : el).length > 0);
329
			if (ie6 || expr) {
330
				// give body 100% height
331
				if (full && opts.allowBodyStretch && $.support.boxModel)
332
					$('html,body').css('height','100%');
333

    
334
				// fix ie6 issue when blocked element has a border width
335
				if ((ie6 || !$.support.boxModel) && !full) {
336
					var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
337
					var fixT = t ? '(0 - '+t+')' : 0;
338
					var fixL = l ? '(0 - '+l+')' : 0;
339
				}
340

    
341
				// simulate fixed position
342
				$.each(layers, function(i,o) {
343
					var s = o[0].style;
344
					s.position = 'absolute';
345
					if (i < 2) {
346
						if (full)
347
							s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.support.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"');
348
						else
349
							s.setExpression('height','this.parentNode.offsetHeight + "px"');
350
						if (full)
351
							s.setExpression('width','jQuery.support.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"');
352
						else
353
							s.setExpression('width','this.parentNode.offsetWidth + "px"');
354
						if (fixL) s.setExpression('left', fixL);
355
						if (fixT) s.setExpression('top', fixT);
356
					}
357
					else if (opts.centerY) {
358
						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"');
359
						s.marginTop = 0;
360
					}
361
					else if (!opts.centerY && full) {
362
						var top = (opts.css && opts.css.top) ? parseInt(opts.css.top, 10) : 0;
363
						var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
364
						s.setExpression('top',expression);
365
					}
366
				});
367
			}
368

    
369
			// show the message
370
			if (msg) {
371
				if (opts.theme)
372
					lyr3.find('.ui-widget-content').append(msg);
373
				else
374
					lyr3.append(msg);
375
				if (msg.jquery || msg.nodeType)
376
					$(msg).show();
377
			}
378

    
379
			if ((msie || opts.forceIframe) && opts.showOverlay)
380
				lyr1.show(); // opacity is zero
381
			if (opts.fadeIn) {
382
				var cb = opts.onBlock ? opts.onBlock : noOp;
383
				var cb1 = (opts.showOverlay && !msg) ? cb : noOp;
384
				var cb2 = msg ? cb : noOp;
385
				if (opts.showOverlay)
386
					lyr2._fadeIn(opts.fadeIn, cb1);
387
				if (msg)
388
					lyr3._fadeIn(opts.fadeIn, cb2);
389
			}
390
			else {
391
				if (opts.showOverlay)
392
					lyr2.show();
393
				if (msg)
394
					lyr3.show();
395
				if (opts.onBlock)
396
					opts.onBlock();
397
			}
398

    
399
			// bind key and mouse events
400
			bind(1, el, opts);
401

    
402
			if (full) {
403
				pageBlock = lyr3[0];
404
				pageBlockEls = $(':input:enabled:visible',pageBlock);
405
				if (opts.focusInput)
406
					setTimeout(focus, 20);
407
			}
408
			else
409
				center(lyr3[0], opts.centerX, opts.centerY);
410

    
411
			if (opts.timeout) {
412
				// auto-unblock
413
				var to = setTimeout(function() {
414
					if (full)
415
						$.unblockUI(opts);
416
					else
417
						$(el).unblock(opts);
418
				}, opts.timeout);
419
				$(el).data('blockUI.timeout', to);
420
			}
421
		}
422

    
423
		// remove the block
424
		function remove(el, opts) {
425
			var full = (el == window);
426
			var $el = $(el);
427
			var data = $el.data('blockUI.history');
428
			var to = $el.data('blockUI.timeout');
429
			if (to) {
430
				clearTimeout(to);
431
				$el.removeData('blockUI.timeout');
432
			}
433
			opts = $.extend({}, $.blockUI.defaults, opts || {});
434
			bind(0, el, opts); // unbind events
435

    
436
			if (opts.onUnblock === null) {
437
				opts.onUnblock = $el.data('blockUI.onUnblock');
438
				$el.removeData('blockUI.onUnblock');
439
			}
440

    
441
			var els;
442
			if (full) // crazy selector to handle odd field errors in ie6/7
443
				els = $('body').children().filter('.blockUI').add('body > .blockUI');
444
			else
445
				els = $el.find('>.blockUI');
446

    
447
			// fix cursor issue
448
			if ( opts.cursorReset ) {
449
				if ( els.length > 1 )
450
					els[1].style.cursor = opts.cursorReset;
451
				if ( els.length > 2 )
452
					els[2].style.cursor = opts.cursorReset;
453
			}
454

    
455
			if (full)
456
				pageBlock = pageBlockEls = null;
457

    
458
			if (opts.fadeOut) {
459
				els.fadeOut(opts.fadeOut);
460
				setTimeout(function() { reset(els,data,opts,el); }, opts.fadeOut);
461
			}
462
			else
463
				reset(els, data, opts, el);
464
		}
465

    
466
		// move blocking element back into the DOM where it started
467
		function reset(els,data,opts,el) {
468
			els.each(function(i,o) {
469
				// remove via DOM calls so we don't lose event handlers
470
				if (this.parentNode)
471
					this.parentNode.removeChild(this);
472
			});
473

    
474
			if (data && data.el) {
475
				data.el.style.display = data.display;
476
				data.el.style.position = data.position;
477
				if (data.parent)
478
					data.parent.appendChild(data.el);
479
				$(el).removeData('blockUI.history');
480
			}
481

    
482
			if (typeof opts.onUnblock == 'function')
483
				opts.onUnblock(el,opts);
484

    
485
			// fix issue in Safari 6 where block artifacts remain until reflow
486
			var body = $(document.body), w = body.width(), cssW = body[0].style.width;
487
			body.width(w-1).width(w);
488
			body[0].style.width = cssW;
489
		}
490

    
491
		// bind/unbind the handler
492
		function bind(b, el, opts) {
493
			var full = el == window, $el = $(el);
494

    
495
			// don't bother unbinding if there is nothing to unbind
496
			if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
497
				return;
498

    
499
			$el.data('blockUI.isBlocked', b);
500

    
501
			// don't bind events when overlay is not in use or if bindEvents is false
502
			if (!opts.bindEvents || (b && !opts.showOverlay))
503
				return;
504

    
505
			// bind anchors and inputs for mouse and key events
506
			var events = 'mousedown mouseup keydown keypress touchstart touchend touchmove';
507
			if (b)
508
				$(document).bind(events, opts, handler);
509
			else
510
				$(document).unbind(events, handler);
511

    
512
		// former impl...
513
		//		var $e = $('a,:input');
514
		//		b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
515
		}
516

    
517
		// event handler to suppress keyboard/mouse events when blocking
518
		function handler(e) {
519
			// allow tab navigation (conditionally)
520
			if (e.keyCode && e.keyCode == 9) {
521
				if (pageBlock && e.data.constrainTabKey) {
522
					var els = pageBlockEls;
523
					var fwd = !e.shiftKey && e.target === els[els.length-1];
524
					var back = e.shiftKey && e.target === els[0];
525
					if (fwd || back) {
526
						setTimeout(function(){focus(back);},10);
527
						return false;
528
					}
529
				}
530
			}
531
			var opts = e.data;
532
			var target = $(e.target);
533
			if (target.hasClass('blockOverlay') && opts.onOverlayClick)
534
				opts.onOverlayClick();
535

    
536
			// allow events within the message content
537
			if (target.parents('div.' + opts.blockMsgClass).length > 0)
538
				return true;
539

    
540
			// allow events for content that is not being blocked
541
			return target.parents().children().filter('div.blockUI').length === 0;
542
		}
543

    
544
		function focus(back) {
545
			if (!pageBlockEls)
546
				return;
547
			var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
548
			if (e)
549
				e.focus();
550
		}
551

    
552
		function center(el, x, y) {
553
			var p = el.parentNode, s = el.style;
554
			var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
555
			var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
556
			if (x) s.left = l > 0 ? (l+'px') : '0';
557
			if (y) s.top  = t > 0 ? (t+'px') : '0';
558
		}
559

    
560
		function sz(el, p) {
561
			return parseInt($.css(el,p),10)||0;
562
		}
563

    
564
	}
565

    
566

    
567
	/*global define:true */
568
	if (typeof define === 'function' && define.amd && define.amd.jQuery) {
569
		define(['jquery'], setup);
570
	} else {
571
		setup(jQuery);
572
	}
573

    
574
})();
(2-2/3)