Debian Codegraph @

closure-library/closure/goog/ui/progressbar.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Implementation of a progress bar.
17 *
18 * @author [email protected] (Erik Arvidsson)
19 * @see ../demos/progressbar.html
20 */
21
22
23 goog.provide('goog.ui.ProgressBar');
24 goog.provide('goog.ui.ProgressBar.Orientation');
25
26 goog.require('goog.a11y.aria');
27 goog.require('goog.asserts');
28 goog.require('goog.dom');
29 goog.require('goog.dom.TagName');
30 goog.require('goog.dom.classlist');
31 goog.require('goog.events');
32 goog.require('goog.events.EventType');
33 goog.require('goog.ui.Component');
34 goog.require('goog.ui.RangeModel');
35 goog.require('goog.userAgent');
36
37
38
39 /**
40 * This creates a progress bar object.
41 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
42 * @constructor
43 * @extends {goog.ui.Component}
44 */
45 goog.ui.ProgressBar = function(opt_domHelper) {
46 goog.ui.Component.call(this, opt_domHelper);
47
48 /** @type {?HTMLDivElement} */
49 this.thumbElement_;
50
51 /**
52 * The underlying data model for the progress bar.
53 * @type {goog.ui.RangeModel}
54 * @private
55 */
56 this.rangeModel_ = new goog.ui.RangeModel;
57 goog.events.listen(
58 this.rangeModel_, goog.ui.Component.EventType.CHANGE, this.handleChange_,
59 false, this);
60 };
61 goog.inherits(goog.ui.ProgressBar, goog.ui.Component);
62 goog.tagUnsealableClass(goog.ui.ProgressBar);
63
64
65 /**
66 * Enum for representing the orientation of the progress bar.
67 *
68 * @enum {string}
69 */
70 goog.ui.ProgressBar.Orientation = {
71 VERTICAL: 'vertical',
72 HORIZONTAL: 'horizontal'
73 };
74
75
76 /**
77 * Map from progress bar orientation to CSS class names.
78 * @type {!Object<string, string>}
79 * @private
80 */
81 goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_ = {};
82 goog.ui.ProgressBar
83 .ORIENTATION_TO_CSS_NAME_[goog.ui.ProgressBar.Orientation.VERTICAL] =
84 goog.getCssName('progress-bar-vertical');
85 goog.ui.ProgressBar
86 .ORIENTATION_TO_CSS_NAME_[goog.ui.ProgressBar.Orientation.HORIZONTAL] =
87 goog.getCssName('progress-bar-horizontal');
88
89
90 /**
91 * Creates the DOM nodes needed for the progress bar
92 * @override
93 */
94 goog.ui.ProgressBar.prototype.createDom = function() {
95 this.thumbElement_ = this.createThumb_();
96 this.setElementInternal(this.getDomHelper().createDom(
97 goog.dom.TagName.DIV,
98 goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[this.orientation_],
99 this.thumbElement_));
100 this.setValueState_();
101 this.setMinimumState_();
102 this.setMaximumState_();
103 };
104
105
106 /** @override */
107 goog.ui.ProgressBar.prototype.enterDocument = function() {
108 goog.ui.ProgressBar.superClass_.enterDocument.call(this);
109 this.attachEvents_();
110 this.updateUi_();
111
112 var element = this.getElement();
113 goog.asserts.assert(element, 'The progress bar DOM element cannot be null.');
114 // state live = polite will notify the user of updates,
115 // but will not interrupt ongoing feedback
116 goog.a11y.aria.setRole(element, 'progressbar');
117 goog.a11y.aria.setState(element, 'live', 'polite');
118 };
119
120
121 /** @override */
122 goog.ui.ProgressBar.prototype.exitDocument = function() {
123 goog.ui.ProgressBar.superClass_.exitDocument.call(this);
124 this.detachEvents_();
125 };
126
127
128 /**
129 * This creates the thumb element.
130 * @private
131 * @return {HTMLDivElement} The created thumb element.
132 */
133 goog.ui.ProgressBar.prototype.createThumb_ = function() {
134 return this.getDomHelper().createDom(
135 goog.dom.TagName.DIV, goog.getCssName('progress-bar-thumb'));
136 };
137
138
139 /**
140 * Adds the initial event listeners to the element.
141 * @private
142 */
143 goog.ui.ProgressBar.prototype.attachEvents_ = function() {
144 if (goog.userAgent.IE && goog.userAgent.VERSION < 7) {
145 goog.events.listen(
146 this.getElement(), goog.events.EventType.RESIZE, this.updateUi_, false,
147 this);
148 }
149 };
150
151
152 /**
153 * Removes the event listeners added by attachEvents_.
154 * @private
155 */
156 goog.ui.ProgressBar.prototype.detachEvents_ = function() {
157 if (goog.userAgent.IE && goog.userAgent.VERSION < 7) {
158 goog.events.unlisten(
159 this.getElement(), goog.events.EventType.RESIZE, this.updateUi_, false,
160 this);
161 }
162 };
163
164
165 /**
166 * Decorates an existing HTML DIV element as a progress bar input. If the
167 * element contains a child with a class name of 'progress-bar-thumb' that will
168 * be used as the thumb.
169 * @param {Element} element The HTML element to decorate.
170 * @override
171 */
172 goog.ui.ProgressBar.prototype.decorateInternal = function(element) {
173 goog.ui.ProgressBar.superClass_.decorateInternal.call(this, element);
174 goog.dom.classlist.add(
175 goog.asserts.assert(this.getElement()),
176 goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[this.orientation_]);
177
178 // find thumb
179 var thumb = goog.dom.getElementsByTagNameAndClass(
180 null, goog.getCssName('progress-bar-thumb'), this.getElement())[0];
181 if (!thumb) {
182 thumb = this.createThumb_();
183 this.getElement().appendChild(thumb);
184 }
185 this.thumbElement_ = /** @type {!HTMLDivElement} */ (thumb);
186 };
187
188
189 /**
190 * @return {number} The value.
191 */
192 goog.ui.ProgressBar.prototype.getValue = function() {
193 return this.rangeModel_.getValue();
194 };
195
196
197 /**
198 * Sets the value
199 * @param {number} v The value.
200 */
201 goog.ui.ProgressBar.prototype.setValue = function(v) {
202 this.rangeModel_.setValue(v);
203 if (this.getElement()) {
204 this.setValueState_();
205 }
206 };
207
208
209 /**
210 * Sets the state for a11y of the current value.
211 * @private
212 */
213 goog.ui.ProgressBar.prototype.setValueState_ = function() {
214 var element = this.getElement();
215 goog.asserts.assert(element, 'The progress bar DOM element cannot be null.');
216 goog.a11y.aria.setState(element, 'valuenow', this.getValue());
217 };
218
219
220 /**
221 * @return {number} The minimum value.
222 */
223 goog.ui.ProgressBar.prototype.getMinimum = function() {
224 return this.rangeModel_.getMinimum();
225 };
226
227
228 /**
229 * Sets the minimum number
230 * @param {number} v The minimum value.
231 */
232 goog.ui.ProgressBar.prototype.setMinimum = function(v) {
233 this.rangeModel_.setMinimum(v);
234 if (this.getElement()) {
235 this.setMinimumState_();
236 }
237 };
238
239
240 /**
241 * Sets the state for a11y of the minimum value.
242 * @private
243 */
244 goog.ui.ProgressBar.prototype.setMinimumState_ = function() {
245 var element = this.getElement();
246 goog.asserts.assert(element, 'The progress bar DOM element cannot be null.');
247 goog.a11y.aria.setState(element, 'valuemin', this.getMinimum());
248 };
249
250
251 /**
252 * @return {number} The maximum value.
253 */
254 goog.ui.ProgressBar.prototype.getMaximum = function() {
255 return this.rangeModel_.getMaximum();
256 };
257
258
259 /**
260 * Sets the maximum number
261 * @param {number} v The maximum value.
262 */
263 goog.ui.ProgressBar.prototype.setMaximum = function(v) {
264 this.rangeModel_.setMaximum(v);
265 if (this.getElement()) {
266 this.setMaximumState_();
267 }
268 };
269
270
271 /**
272 * Sets the state for a11y of the maximum valiue.
273 * @private
274 */
275 goog.ui.ProgressBar.prototype.setMaximumState_ = function() {
276 var element = this.getElement();
277 goog.asserts.assert(element, 'The progress bar DOM element cannot be null.');
278 goog.a11y.aria.setState(element, 'valuemax', this.getMaximum());
279 };
280
281
282 /**
283 *
284 * @type {goog.ui.ProgressBar.Orientation}
285 * @private
286 */
287 goog.ui.ProgressBar.prototype.orientation_ =
288 goog.ui.ProgressBar.Orientation.HORIZONTAL;
289
290
291 /**
292 * Call back when the internal range model changes
293 * @param {goog.events.Event} e The event object.
294 * @private
295 */
296 goog.ui.ProgressBar.prototype.handleChange_ = function(e) {
297 this.updateUi_();
298 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
299 };
300
301
302 /**
303 * This is called when we need to update the size of the thumb. This happens
304 * when first created as well as when the value and the orientation changes.
305 * @private
306 */
307 goog.ui.ProgressBar.prototype.updateUi_ = function() {
308 if (this.thumbElement_) {
309 var min = this.getMinimum();
310 var max = this.getMaximum();
311 var val = this.getValue();
312 var ratio = (val - min) / (max - min);
313 var size = Math.round(ratio * 100);
314 if (this.orientation_ == goog.ui.ProgressBar.Orientation.VERTICAL) {
315 // Note(arv): IE up to version 6 has some serious computation bugs when
316 // using percentages or bottom. We therefore first set the height to
317 // 100% and measure that and base the top and height on that size instead.
318 if (goog.userAgent.IE && goog.userAgent.VERSION < 7) {
319 this.thumbElement_.style.top = '0';
320 this.thumbElement_.style.height = '100%';
321 var h = this.thumbElement_.offsetHeight;
322 var bottom = Math.round(ratio * h);
323 this.thumbElement_.style.top = h - bottom + 'px';
324 this.thumbElement_.style.height = bottom + 'px';
325 } else {
326 this.thumbElement_.style.top = (100 - size) + '%';
327 this.thumbElement_.style.height = size + '%';
328 }
329 } else {
330 this.thumbElement_.style.width = size + '%';
331 }
332 }
333 };
334
335
336 /**
337 * This is called when we need to setup the UI sizes and positions. This
338 * happens when we create the element and when we change the orientation.
339 * @private
340 */
341 goog.ui.ProgressBar.prototype.initializeUi_ = function() {
342 var tStyle = this.thumbElement_.style;
343 if (this.orientation_ == goog.ui.ProgressBar.Orientation.VERTICAL) {
344 tStyle.left = '0';
345 tStyle.width = '100%';
346 } else {
347 tStyle.top = tStyle.left = '0';
348 tStyle.height = '100%';
349 }
350 };
351
352
353 /**
354 * Changes the orientation
355 * @param {goog.ui.ProgressBar.Orientation} orient The orientation.
356 */
357 goog.ui.ProgressBar.prototype.setOrientation = function(orient) {
358 if (this.orientation_ != orient) {
359 var oldCss =
360 goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[this.orientation_];
361 var newCss = goog.ui.ProgressBar.ORIENTATION_TO_CSS_NAME_[orient];
362 this.orientation_ = orient;
363
364 // Update the DOM
365 var element = this.getElement();
366 if (element) {
367 goog.dom.classlist.swap(element, oldCss, newCss);
368 this.initializeUi_();
369 this.updateUi_();
370 }
371 }
372 };
373
374
375 /**
376 * @return {goog.ui.ProgressBar.Orientation} The orientation of the
377 * progress bar.
378 */
379 goog.ui.ProgressBar.prototype.getOrientation = function() {
380 return this.orientation_;
381 };
382
383
384 /** @override */
385 goog.ui.ProgressBar.prototype.disposeInternal = function() {
386 this.detachEvents_();
387 goog.ui.ProgressBar.superClass_.disposeInternal.call(this);
388 this.thumbElement_ = null;
389 this.rangeModel_.dispose();
390 };
391
392
393 /**
394 * @return {?number} The step value used to determine how to round the value.
395 */
396 goog.ui.ProgressBar.prototype.getStep = function() {
397 return this.rangeModel_.getStep();
398 };
399
400
401 /**
402 * Sets the step value. The step value is used to determine how to round the
403 * value.
404 * @param {?number} step The step size.
405 */
406 goog.ui.ProgressBar.prototype.setStep = function(step) {
407 this.rangeModel_.setStep(step);
408 };