|
1 /* |
|
2 * jQuery Progress Bar plugin |
|
3 * Version 2.0 (06/22/2009) |
|
4 * @requires jQuery v1.2.1 or later |
|
5 * |
|
6 * Copyright (c) 2008 Gary Teo |
|
7 * http://t.wits.sg |
|
8 |
|
9 USAGE: |
|
10 $(".someclass").progressBar(); |
|
11 $("#progressbar").progressBar(); |
|
12 $("#progressbar").progressBar(45); // percentage |
|
13 $("#progressbar").progressBar({showText: false }); // percentage with config |
|
14 $("#progressbar").progressBar(45, {showText: false }); // percentage with config |
|
15 */ |
|
16 (function ($) { |
|
17 |
|
18 $.extend({ |
|
19 |
|
20 progressBar: new function () { |
|
21 |
|
22 this.defaults = { |
|
23 steps: 20, // steps taken to reach target |
|
24 step_duration: 20, |
|
25 max: 100, // Upon 100% i'd assume, but configurable |
|
26 showText: true, // show text with percentage in next to the progressbar? - default : true |
|
27 textFormat: 'percentage', // Or otherwise, set to 'fraction' |
|
28 width: 120, // Width of the progressbar - don't forget to adjust your image too!!! // Image to use in the progressbar. Can be a single image too: 'images/progressbg_green.gif' |
|
29 height: 12, // Height of the progressbar - don't forget to adjust your image too!!! |
|
30 callback: null, // Calls back with the config object that has the current percentage, target percentage, current image, etc |
|
31 boxImage: '/--static--/ztfy.jqueryui/img/progressbar.gif', // boxImage : image around the progress bar |
|
32 barImage: { |
|
33 0: '/--static--/ztfy.jqueryui/img/progressbg_red.gif', |
|
34 30: '/--static--/ztfy.jqueryui/img/progressbg_orange.gif', |
|
35 70: '/--static--/ztfy.jqueryui/img/progressbg_green.gif' |
|
36 }, |
|
37 // Internal use |
|
38 running_value: 0, |
|
39 value: 0, |
|
40 image: null |
|
41 }; |
|
42 |
|
43 /* public methods */ |
|
44 this.construct = function (arg1, arg2) { |
|
45 var argvalue = null; |
|
46 var argconfig = null; |
|
47 |
|
48 if (arg1 != null) { |
|
49 if (!isNaN(arg1)) { |
|
50 argvalue = arg1; |
|
51 if (arg2 != null) { |
|
52 argconfig = arg2; |
|
53 } |
|
54 } else { |
|
55 argconfig = arg1; |
|
56 } |
|
57 } |
|
58 |
|
59 return this.each(function (child) { |
|
60 var pb = this; |
|
61 var config = this.config; |
|
62 |
|
63 if (argvalue != null && this.bar != null && this.config != null) { |
|
64 this.config.value = argvalue |
|
65 if (argconfig != null) |
|
66 pb.config = $.extend(this.config, argconfig); |
|
67 config = pb.config; |
|
68 } else { |
|
69 var $this = $(this); |
|
70 var config = $.extend({}, $.progressBar.defaults, argconfig); |
|
71 config.id = $this.attr('id') ? $this.attr('id') : Math.ceil(Math.random() * 100000); // random id, if none provided |
|
72 |
|
73 if (argvalue == null) |
|
74 argvalue = $this.html().replace("%", "") // parse percentage |
|
75 |
|
76 config.value = argvalue; |
|
77 config.running_value = 0; |
|
78 config.image = getBarImage(config); |
|
79 |
|
80 $this.html(""); |
|
81 var bar = document.createElement('img'); |
|
82 var text = document.createElement('span'); |
|
83 var $bar = $(bar); |
|
84 var $text = $(text); |
|
85 pb.bar = $bar; |
|
86 |
|
87 $bar.attr('id', config.id + "_pbImage"); |
|
88 $text.attr('id', config.id + "_pbText"); |
|
89 $text.html(getText(config)); |
|
90 $bar.attr('title', getText(config)); |
|
91 $bar.attr('alt', getText(config)); |
|
92 $bar.attr('src', config.boxImage); |
|
93 $bar.attr('width', config.width); |
|
94 $bar.css("width", config.width + "px"); |
|
95 $bar.css("height", config.height + "px"); |
|
96 $bar.css("background-image", "url(" + config.image + ")"); |
|
97 $bar.css("background-position", ((config.width * -1)) + 'px 50%'); |
|
98 $bar.css("padding", "0"); |
|
99 $bar.css("margin", "0"); |
|
100 $this.append($bar); |
|
101 $this.append($text); |
|
102 } |
|
103 |
|
104 function getPercentage(config) { |
|
105 return config.running_value * 100 / config.max; |
|
106 } |
|
107 |
|
108 function getBarImage(config) { |
|
109 var image = config.barImage; |
|
110 if (typeof(config.barImage) == 'object') { |
|
111 for (var i in config.barImage) { |
|
112 if (getPercentage(config) >= parseInt(i)) { |
|
113 image = config.barImage[i]; |
|
114 } else { |
|
115 break; |
|
116 } |
|
117 } |
|
118 } |
|
119 return image; |
|
120 } |
|
121 |
|
122 function getText(config) { |
|
123 if (config.showText) { |
|
124 if (config.textFormat == 'percentage') { |
|
125 return " " + Math.round(config.running_value) + "%"; |
|
126 } else if (config.textFormat == 'fraction') { |
|
127 return " " + config.running_value + '/' + config.max; |
|
128 } |
|
129 } |
|
130 } |
|
131 |
|
132 config.increment = Math.round((config.value - config.running_value) / config.steps); |
|
133 if (config.increment < 0) |
|
134 config.increment *= -1; |
|
135 if (config.increment < 1) |
|
136 config.increment = 1; |
|
137 |
|
138 var t = setInterval(function () { |
|
139 var pixels = config.width / 100; // Define how many pixels go into 1% |
|
140 var stop = false; |
|
141 |
|
142 if (config.running_value > config.value) { |
|
143 if (config.running_value - config.increment < config.value) { |
|
144 config.running_value = config.value; |
|
145 } else { |
|
146 config.running_value -= config.increment; |
|
147 } |
|
148 } |
|
149 else if (config.running_value < config.value) { |
|
150 if (config.running_value + config.increment > config.value) { |
|
151 config.running_value = config.value; |
|
152 } else { |
|
153 config.running_value += config.increment; |
|
154 } |
|
155 } |
|
156 |
|
157 if (config.running_value == config.value) |
|
158 clearInterval(t); |
|
159 |
|
160 var $bar = $("#" + config.id + "_pbImage"); |
|
161 var $text = $("#" + config.id + "_pbText"); |
|
162 var image = getBarImage(config); |
|
163 if (image != config.image) { |
|
164 $bar.css("background-image", "url(" + image + ")"); |
|
165 config.image = image; |
|
166 } |
|
167 $bar.css("background-position", (((config.width * -1)) + (getPercentage(config) * pixels)) + 'px 50%'); |
|
168 $bar.attr('title', getText(config)); |
|
169 $text.html(getText(config)); |
|
170 |
|
171 if (config.callback != null && typeof(config.callback) == 'function') |
|
172 config.callback(config); |
|
173 |
|
174 pb.config = config; |
|
175 }, config.step_duration); |
|
176 }); |
|
177 }; |
|
178 |
|
179 this.submit = function (form) { |
|
180 var files = $('input:file', form); |
|
181 if (files.length > 0) { |
|
182 var frame = $('iframe[name=progress]', form); |
|
183 if (frame.length > 0) { |
|
184 var uuid = ""; |
|
185 for (var i = 0; i < 32; i++) { |
|
186 uuid += Math.floor(Math.random() * 16).toString(16); |
|
187 } |
|
188 var action = $(form).attr("action"); |
|
189 if (old_id = /X-Progress-ID=([^&]+)/.exec(action)) { |
|
190 action = action.replace(old_id[1], uuid); |
|
191 } else { |
|
192 action = action + "?X-Progress-ID=" + uuid; |
|
193 } |
|
194 $(form).attr("action", action); |
|
195 var progress = $(frame).get(0).contentWindow.updateUploadProgress; |
|
196 if (progress) { |
|
197 progress(uuid); |
|
198 return uuid; |
|
199 } |
|
200 } |
|
201 } |
|
202 } |
|
203 } |
|
204 }); |
|
205 |
|
206 $.fn.extend({ |
|
207 progressBar: $.progressBar.construct |
|
208 }); |
|
209 |
|
210 })(jQuery); |