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