1
|
/*
|
2
|
* jQuery-busy v1.0.3
|
3
|
* Copyright 2010 Tomasz Szymczyszyn
|
4
|
*
|
5
|
* Examples available at:
|
6
|
* http://kammerer.boo.pl/code/jquery-busy
|
7
|
*
|
8
|
* This plug-in is 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
|
(function($) {
|
13
|
// Helper object factory
|
14
|
function Busy(options) {
|
15
|
this.options = $.extend({}, Busy.defaults, options);
|
16
|
};
|
17
|
|
18
|
// Remembers currently "busied" targets along with options
|
19
|
Busy.instances = [];
|
20
|
|
21
|
Busy.repositionAll = function() {
|
22
|
for (var i = 0; i < Busy.instances.length; i++) {
|
23
|
if (! Busy.instances[i])
|
24
|
continue;
|
25
|
|
26
|
var options = Busy.instances[i].options;
|
27
|
new Busy(options).positionImg($(Busy.instances[i].target), $.data(Busy.instances[i].target, "busy"), options.position);
|
28
|
}
|
29
|
};
|
30
|
|
31
|
Busy.prototype.hide = function(targets) {
|
32
|
targets.each(function() {
|
33
|
var busyImg = $.data(this, "busy");
|
34
|
if (busyImg)
|
35
|
busyImg.remove();
|
36
|
|
37
|
$(this).css("visibility", "");
|
38
|
|
39
|
$.data(this, "busy", null);
|
40
|
for (var i = 0; i < Busy.instances.length; i++)
|
41
|
if (Busy.instances[i] != null && Busy.instances[i].target == this)
|
42
|
Busy.instances[i] = null;
|
43
|
});
|
44
|
};
|
45
|
|
46
|
Busy.prototype.show = function(targets) {
|
47
|
var that = this;
|
48
|
|
49
|
targets.each(function() {
|
50
|
if ($.data(this, "busy"))
|
51
|
return;
|
52
|
|
53
|
var target = $(this);
|
54
|
|
55
|
var busyImg = that.buildImg();
|
56
|
busyImg.css("visibility", "hidden");
|
57
|
busyImg.load(function() {
|
58
|
that.positionImg(target, busyImg, that.options.position);
|
59
|
busyImg.css("visibility", "");
|
60
|
});
|
61
|
|
62
|
$("body").append(busyImg);
|
63
|
|
64
|
if (that.options.hide)
|
65
|
target.css("visibility", "hidden");
|
66
|
|
67
|
$.data(this, "busy", busyImg);
|
68
|
Busy.instances.push({ target : this, options : that.options });
|
69
|
});
|
70
|
};
|
71
|
|
72
|
Busy.prototype.preload = function() {
|
73
|
var busyImg = this.buildImg();
|
74
|
busyImg.css("visibility", "hidden");
|
75
|
busyImg.load(function() {
|
76
|
$(this).remove();
|
77
|
});
|
78
|
|
79
|
$("body").append(busyImg);
|
80
|
};
|
81
|
|
82
|
// Creates image node, wraps it in $ object and returns.
|
83
|
Busy.prototype.buildImg = function() {
|
84
|
var html = "<img src='" + this.options.img + "' alt='" + this.options.alt + "' title='" + this.options.title + "'";
|
85
|
|
86
|
if (this.options.width)
|
87
|
html += " width='" + this.options.width + "'";
|
88
|
|
89
|
if (this.options.height)
|
90
|
html += " height='" + this.options.height + "'";
|
91
|
|
92
|
html += " />";
|
93
|
|
94
|
return $(html);
|
95
|
};
|
96
|
|
97
|
Busy.prototype.positionImg = function(target, busyImg, position) {
|
98
|
var targetPosition = target.offset();
|
99
|
var targetWidth = target.outerWidth();
|
100
|
var targetHeight = target.outerHeight();
|
101
|
|
102
|
var busyWidth = busyImg.outerWidth();
|
103
|
var busyHeight = busyImg.outerHeight();
|
104
|
|
105
|
if (position == "left") {
|
106
|
var busyLeft = targetPosition.left - busyWidth - this.options.offset;
|
107
|
}
|
108
|
else if (position == "right") {
|
109
|
var busyLeft = targetPosition.left + targetWidth + this.options.offset;
|
110
|
}
|
111
|
else {
|
112
|
var busyLeft = targetPosition.left + (targetWidth - busyWidth) / 2.0;
|
113
|
}
|
114
|
|
115
|
var busyTop = targetPosition.top + (targetHeight - busyHeight) / 2.0;
|
116
|
|
117
|
busyImg.css("position", "absolute");
|
118
|
busyImg.css("left", busyLeft + "px");
|
119
|
busyImg.css("top", busyTop + "px");
|
120
|
};
|
121
|
|
122
|
Busy.defaults = {
|
123
|
img : 'busy.gif',
|
124
|
alt : 'Please wait...',
|
125
|
title : 'Please wait...',
|
126
|
hide : true,
|
127
|
position : 'center',
|
128
|
zIndex : 1001,
|
129
|
width : null,
|
130
|
height : null,
|
131
|
offset : 10
|
132
|
};
|
133
|
|
134
|
$.fn.busy = function(options, defaults) {
|
135
|
if ($.inArray(options, ["clear", "hide", "remove"]) != -1) {
|
136
|
// Hide busy image(s)
|
137
|
new Busy(options).hide($(this));
|
138
|
}
|
139
|
else if (options == "defaults") {
|
140
|
// Overwrite defaults
|
141
|
$.extend(Busy.defaults, defaults || {});
|
142
|
}
|
143
|
else if (options == "preload") {
|
144
|
// Preload busy image
|
145
|
new Busy(options).preload();
|
146
|
}
|
147
|
else if (options == "reposition") {
|
148
|
// Update positions of all existing busy images
|
149
|
Busy.repositionAll();
|
150
|
}
|
151
|
else {
|
152
|
// Show busy image(s)
|
153
|
new Busy(options).show($(this));
|
154
|
return $(this);
|
155
|
}
|
156
|
};
|
157
|
})(jQuery);
|