| 1 |
// Copyright 2007 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 basic slider control. |
| 17 |
* |
| 18 |
* Models a control that allows to select a sub-range within a given |
| 19 |
* range of values using two thumbs. The underlying range is modeled |
| 20 |
* as a range model, where the min thumb points to value of the |
| 21 |
* rangemodel, and the max thumb points to value + extent of the range |
| 22 |
* model. |
| 23 |
* |
| 24 |
* The currently selected range is exposed through methods |
| 25 |
* getValue() and getExtent(). |
| 26 |
* |
| 27 |
* The reason for modelling the basic slider state as value + extent is |
| 28 |
* to be able to capture both, a two-thumb slider to select a range, and |
| 29 |
* a single-thumb slider to just select a value (in the latter case, extent |
| 30 |
* is always zero). We provide subclasses (twothumbslider.js and slider.js) |
| 31 |
* that model those special cases of this control. |
| 32 |
* |
| 33 |
* All rendering logic is left out, so that the subclasses can define |
| 34 |
* their own rendering. To do so, the subclasses overwrite: |
| 35 |
* - createDom |
| 36 |
* - decorateInternal |
| 37 |
* - getCssClass |
| 38 |
* |
| 39 |
* @author [email protected] (Erik Arvidsson) |
| 40 |
*/ |
| 41 |
|
| 42 |
goog.provide('goog.ui.SliderBase'); |
| 43 |
goog.provide('goog.ui.SliderBase.AnimationFactory'); |
| 44 |
goog.provide('goog.ui.SliderBase.Orientation'); |
| 45 |
|
| 46 |
goog.require('goog.Timer'); |
| 47 |
goog.require('goog.a11y.aria'); |
| 48 |
goog.require('goog.a11y.aria.Role'); |
| 49 |
goog.require('goog.a11y.aria.State'); |
| 50 |
goog.require('goog.array'); |
| 51 |
goog.require('goog.asserts'); |
| 52 |
goog.require('goog.dom'); |
| 53 |
goog.require('goog.dom.TagName'); |
| 54 |
goog.require('goog.dom.classlist'); |
| 55 |
goog.require('goog.events'); |
| 56 |
goog.require('goog.events.EventType'); |
| 57 |
goog.require('goog.events.KeyCodes'); |
| 58 |
goog.require('goog.events.KeyHandler'); |
| 59 |
goog.require('goog.events.MouseWheelHandler'); |
| 60 |
goog.require('goog.functions'); |
| 61 |
goog.require('goog.fx.AnimationParallelQueue'); |
| 62 |
goog.require('goog.fx.Dragger'); |
| 63 |
goog.require('goog.fx.Transition'); |
| 64 |
goog.require('goog.fx.dom.ResizeHeight'); |
| 65 |
goog.require('goog.fx.dom.ResizeWidth'); |
| 66 |
goog.require('goog.fx.dom.Slide'); |
| 67 |
goog.require('goog.math'); |
| 68 |
goog.require('goog.math.Coordinate'); |
| 69 |
goog.require('goog.style'); |
| 70 |
goog.require('goog.style.bidi'); |
| 71 |
goog.require('goog.ui.Component'); |
| 72 |
goog.require('goog.ui.RangeModel'); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* This creates a SliderBase object. |
| 78 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 79 |
* @param {(function(number):?string)=} opt_labelFn An optional function mapping |
| 80 |
* slider values to a description of the value. |
| 81 |
* @constructor |
| 82 |
* @extends {goog.ui.Component} |
| 83 |
*/ |
| 84 |
goog.ui.SliderBase = function(opt_domHelper, opt_labelFn) { |
| 85 |
goog.ui.Component.call(this, opt_domHelper); |
| 86 |
|
| 87 |
/** |
| 88 |
* The factory to use to generate additional animations when animating to a |
| 89 |
* new value. |
| 90 |
* @type {goog.ui.SliderBase.AnimationFactory} |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
this.additionalAnimations_ = null; |
| 94 |
|
| 95 |
/** |
| 96 |
* The model for the range of the slider. |
| 97 |
* @protected {!goog.ui.RangeModel} |
| 98 |
*/ |
| 99 |
this.rangeModel = new goog.ui.RangeModel(); |
| 100 |
|
| 101 |
/** |
| 102 |
* A function mapping slider values to text description. |
| 103 |
* @private {function(number):?string} |
| 104 |
*/ |
| 105 |
this.labelFn_ = opt_labelFn || goog.functions.NULL; |
| 106 |
|
| 107 |
// Don't use getHandler because it gets cleared in exitDocument. |
| 108 |
goog.events.listen( |
| 109 |
this.rangeModel, goog.ui.Component.EventType.CHANGE, |
| 110 |
this.handleRangeModelChange, false, this); |
| 111 |
}; |
| 112 |
goog.inherits(goog.ui.SliderBase, goog.ui.Component); |
| 113 |
goog.tagUnsealableClass(goog.ui.SliderBase); |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Event types used to listen for dragging events. Note that extent drag events |
| 118 |
* are also sent for single-thumb sliders, since the one thumb controls both |
| 119 |
* value and extent together; in this case, they can simply be ignored. |
| 120 |
* @enum {string} |
| 121 |
*/ |
| 122 |
goog.ui.SliderBase.EventType = { |
| 123 |
/** User started dragging the value thumb */ |
| 124 |
DRAG_VALUE_START: goog.events.getUniqueId('dragvaluestart'), |
| 125 |
/** User is done dragging the value thumb */ |
| 126 |
DRAG_VALUE_END: goog.events.getUniqueId('dragvalueend'), |
| 127 |
/** User started dragging the extent thumb */ |
| 128 |
DRAG_EXTENT_START: goog.events.getUniqueId('dragextentstart'), |
| 129 |
/** User is done dragging the extent thumb */ |
| 130 |
DRAG_EXTENT_END: goog.events.getUniqueId('dragextentend'), |
| 131 |
// Note that the following two events are sent twice, once for the value |
| 132 |
// dragger, and once of the extent dragger. If you need to differentiate |
| 133 |
// between the two, or if your code relies on receiving a single event per |
| 134 |
// START/END event, it should listen to one of the VALUE/EXTENT-specific |
| 135 |
// events. |
| 136 |
/** User started dragging a thumb */ |
| 137 |
DRAG_START: goog.events.getUniqueId('dragstart'), |
| 138 |
/** User is done dragging a thumb */ |
| 139 |
DRAG_END: goog.events.getUniqueId('dragend'), |
| 140 |
/** Animation on the value thumb ends */ |
| 141 |
ANIMATION_END: goog.events.getUniqueId('animationend') |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Enum for representing the orientation of the slider. |
| 147 |
* |
| 148 |
* @enum {string} |
| 149 |
*/ |
| 150 |
goog.ui.SliderBase.Orientation = { |
| 151 |
VERTICAL: 'vertical', |
| 152 |
HORIZONTAL: 'horizontal' |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Orientation of the slider. |
| 158 |
* @type {goog.ui.SliderBase.Orientation} |
| 159 |
* @private |
| 160 |
*/ |
| 161 |
goog.ui.SliderBase.prototype.orientation_ = |
| 162 |
goog.ui.SliderBase.Orientation.HORIZONTAL; |
| 163 |
|
| 164 |
|
| 165 |
/** @private {goog.fx.AnimationParallelQueue} */ |
| 166 |
goog.ui.SliderBase.prototype.currentAnimation_; |
| 167 |
|
| 168 |
|
| 169 |
/** @private {!goog.Timer} */ |
| 170 |
goog.ui.SliderBase.prototype.incTimer_; |
| 171 |
|
| 172 |
|
| 173 |
/** @private {boolean} */ |
| 174 |
goog.ui.SliderBase.prototype.incrementing_; |
| 175 |
|
| 176 |
|
| 177 |
/** @private {number} */ |
| 178 |
goog.ui.SliderBase.prototype.lastMousePosition_; |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* When the user holds down the mouse on the slider background, the closest |
| 183 |
* thumb will move in "lock-step" towards the mouse. This number indicates how |
| 184 |
* long each step should take (in milliseconds). |
| 185 |
* @type {number} |
| 186 |
* @private |
| 187 |
*/ |
| 188 |
goog.ui.SliderBase.MOUSE_DOWN_INCREMENT_INTERVAL_ = 200; |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* How long the animations should take (in milliseconds). |
| 193 |
* @type {number} |
| 194 |
* @private |
| 195 |
*/ |
| 196 |
goog.ui.SliderBase.ANIMATION_INTERVAL_ = 100; |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* The minThumb dom-element, pointing to the start of the selected range. |
| 201 |
* @type {HTMLDivElement} |
| 202 |
* @protected |
| 203 |
*/ |
| 204 |
goog.ui.SliderBase.prototype.valueThumb; |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* The maxThumb dom-element, pointing to the end of the selected range. |
| 209 |
* @type {HTMLDivElement} |
| 210 |
* @protected |
| 211 |
*/ |
| 212 |
goog.ui.SliderBase.prototype.extentThumb; |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* The dom-element highlighting the selected range. |
| 217 |
* @type {HTMLDivElement} |
| 218 |
* @protected |
| 219 |
*/ |
| 220 |
goog.ui.SliderBase.prototype.rangeHighlight; |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* The thumb that we should be moving (only relevant when timed move is active). |
| 225 |
* @type {HTMLDivElement} |
| 226 |
* @private |
| 227 |
*/ |
| 228 |
goog.ui.SliderBase.prototype.thumbToMove_; |
| 229 |
|
| 230 |
|
| 231 |
/** |
| 232 |
* The object handling keyboard events. |
| 233 |
* @type {goog.events.KeyHandler} |
| 234 |
* @private |
| 235 |
*/ |
| 236 |
goog.ui.SliderBase.prototype.keyHandler_; |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* The object handling mouse wheel events. |
| 241 |
* @type {goog.events.MouseWheelHandler} |
| 242 |
* @private |
| 243 |
*/ |
| 244 |
goog.ui.SliderBase.prototype.mouseWheelHandler_; |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* The Dragger for dragging the valueThumb. |
| 249 |
* @type {goog.fx.Dragger} |
| 250 |
* @private |
| 251 |
*/ |
| 252 |
goog.ui.SliderBase.prototype.valueDragger_; |
| 253 |
|
| 254 |
|
| 255 |
/** |
| 256 |
* The Dragger for dragging the extentThumb. |
| 257 |
* @type {goog.fx.Dragger} |
| 258 |
* @private |
| 259 |
*/ |
| 260 |
goog.ui.SliderBase.prototype.extentDragger_; |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* If we are currently animating the thumb. |
| 265 |
* @private |
| 266 |
* @type {boolean} |
| 267 |
*/ |
| 268 |
goog.ui.SliderBase.prototype.isAnimating_ = false; |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* Whether clicking on the backgtround should move directly to that point. |
| 273 |
* @private |
| 274 |
* @type {boolean} |
| 275 |
*/ |
| 276 |
goog.ui.SliderBase.prototype.moveToPointEnabled_ = false; |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* The amount to increment/decrement for page up/down as well as when holding |
| 281 |
* down the mouse button on the background. |
| 282 |
* @private |
| 283 |
* @type {number} |
| 284 |
*/ |
| 285 |
goog.ui.SliderBase.prototype.blockIncrement_ = 10; |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* The minimal extent. The class will ensure that the extent cannot shrink |
| 290 |
* to a value smaller than minExtent. |
| 291 |
* @private |
| 292 |
* @type {number} |
| 293 |
*/ |
| 294 |
goog.ui.SliderBase.prototype.minExtent_ = 0; |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Whether the slider should handle mouse wheel events. |
| 299 |
* @private |
| 300 |
* @type {boolean} |
| 301 |
*/ |
| 302 |
goog.ui.SliderBase.prototype.isHandleMouseWheel_ = true; |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* The time the last mousedown event was received. |
| 307 |
* @private |
| 308 |
* @type {number} |
| 309 |
*/ |
| 310 |
goog.ui.SliderBase.prototype.mouseDownTime_ = 0; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* The delay after mouseDownTime_ during which a click event is ignored. |
| 315 |
* @private |
| 316 |
* @type {number} |
| 317 |
* @const |
| 318 |
*/ |
| 319 |
goog.ui.SliderBase.prototype.MOUSE_DOWN_DELAY_ = 1000; |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* Whether the slider is enabled or not. |
| 324 |
* @private |
| 325 |
* @type {boolean} |
| 326 |
*/ |
| 327 |
goog.ui.SliderBase.prototype.enabled_ = true; |
| 328 |
|
| 329 |
|
| 330 |
/** |
| 331 |
* Whether the slider implements the changes described in http://b/6324964, |
| 332 |
* making it truly RTL. This is a temporary flag to allow clients to transition |
| 333 |
* to the new behavior at their convenience. At some point it will be the |
| 334 |
* default. |
| 335 |
* @type {boolean} |
| 336 |
* @private |
| 337 |
*/ |
| 338 |
goog.ui.SliderBase.prototype.flipForRtl_ = false; |
| 339 |
|
| 340 |
|
| 341 |
/** |
| 342 |
* Enables/disables true RTL behavior. This should be called immediately after |
| 343 |
* construction. This is a temporary flag to allow clients to transition |
| 344 |
* to the new behavior at their convenience. At some point it will be the |
| 345 |
* default. |
| 346 |
* @param {boolean} flipForRtl True if the slider should be flipped for RTL, |
| 347 |
* false otherwise. |
| 348 |
*/ |
| 349 |
goog.ui.SliderBase.prototype.enableFlipForRtl = function(flipForRtl) { |
| 350 |
this.flipForRtl_ = flipForRtl; |
| 351 |
}; |
| 352 |
|
| 353 |
|
| 354 |
// TODO: Make this return a base CSS class (without orientation), in subclasses. |
| 355 |
/** |
| 356 |
* Returns the CSS class applied to the slider element for the given |
| 357 |
* orientation. Subclasses must override this method. |
| 358 |
* @param {goog.ui.SliderBase.Orientation} orient The orientation. |
| 359 |
* @return {string} The CSS class applied to slider elements. |
| 360 |
* @protected |
| 361 |
*/ |
| 362 |
goog.ui.SliderBase.prototype.getCssClass = goog.abstractMethod; |
| 363 |
|
| 364 |
|
| 365 |
/** @override */ |
| 366 |
goog.ui.SliderBase.prototype.createDom = function() { |
| 367 |
goog.ui.SliderBase.superClass_.createDom.call(this); |
| 368 |
var element = this.getDomHelper().createDom( |
| 369 |
goog.dom.TagName.DIV, this.getCssClass(this.orientation_)); |
| 370 |
this.decorateInternal(element); |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
/** |
| 375 |
* Subclasses must implement this method and set the valueThumb and |
| 376 |
* extentThumb to non-null values. They can also set the rangeHighlight |
| 377 |
* element if a range highlight is desired. |
| 378 |
* @type {function() : void} |
| 379 |
* @protected |
| 380 |
*/ |
| 381 |
goog.ui.SliderBase.prototype.createThumbs = goog.abstractMethod; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* CSS class name applied to the slider while its thumbs are being dragged. |
| 386 |
* @type {string} |
| 387 |
* @private |
| 388 |
*/ |
| 389 |
goog.ui.SliderBase.SLIDER_DRAGGING_CSS_CLASS_ = |
| 390 |
goog.getCssName('goog-slider-dragging'); |
| 391 |
|
| 392 |
|
| 393 |
/** |
| 394 |
* CSS class name applied to a thumb while it's being dragged. |
| 395 |
* @type {string} |
| 396 |
* @private |
| 397 |
*/ |
| 398 |
goog.ui.SliderBase.THUMB_DRAGGING_CSS_CLASS_ = |
| 399 |
goog.getCssName('goog-slider-thumb-dragging'); |
| 400 |
|
| 401 |
|
| 402 |
/** |
| 403 |
* CSS class name applied when the slider is disabled. |
| 404 |
* @type {string} |
| 405 |
* @private |
| 406 |
*/ |
| 407 |
goog.ui.SliderBase.DISABLED_CSS_CLASS_ = |
| 408 |
goog.getCssName('goog-slider-disabled'); |
| 409 |
|
| 410 |
|
| 411 |
/** @override */ |
| 412 |
goog.ui.SliderBase.prototype.decorateInternal = function(element) { |
| 413 |
goog.ui.SliderBase.superClass_.decorateInternal.call(this, element); |
| 414 |
goog.asserts.assert(element); |
| 415 |
goog.dom.classlist.add(element, this.getCssClass(this.orientation_)); |
| 416 |
this.createThumbs(); |
| 417 |
this.setAriaRoles(); |
| 418 |
}; |
| 419 |
|
| 420 |
|
| 421 |
/** |
| 422 |
* Called when the DOM for the component is for sure in the document. |
| 423 |
* Subclasses should override this method to set this element's role. |
| 424 |
* @override |
| 425 |
*/ |
| 426 |
goog.ui.SliderBase.prototype.enterDocument = function() { |
| 427 |
goog.ui.SliderBase.superClass_.enterDocument.call(this); |
| 428 |
|
| 429 |
// Attach the events |
| 430 |
this.valueDragger_ = new goog.fx.Dragger(this.valueThumb); |
| 431 |
this.extentDragger_ = new goog.fx.Dragger(this.extentThumb); |
| 432 |
this.valueDragger_.enableRightPositioningForRtl(this.flipForRtl_); |
| 433 |
this.extentDragger_.enableRightPositioningForRtl(this.flipForRtl_); |
| 434 |
|
| 435 |
// The slider is handling the positioning so make the defaultActions empty. |
| 436 |
this.valueDragger_.defaultAction = this.extentDragger_.defaultAction = |
| 437 |
goog.nullFunction; |
| 438 |
this.keyHandler_ = new goog.events.KeyHandler(this.getElement()); |
| 439 |
this.enableEventHandlers_(true); |
| 440 |
|
| 441 |
this.getElement().tabIndex = 0; |
| 442 |
this.updateUi_(); |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
/** |
| 447 |
* Attaches/Detaches the event handlers on the slider. |
| 448 |
* @param {boolean} enable Whether to attach or detach the event handlers. |
| 449 |
* @private |
| 450 |
*/ |
| 451 |
goog.ui.SliderBase.prototype.enableEventHandlers_ = function(enable) { |
| 452 |
if (enable) { |
| 453 |
this.getHandler() |
| 454 |
.listen( |
| 455 |
this.valueDragger_, goog.fx.Dragger.EventType.BEFOREDRAG, |
| 456 |
this.handleBeforeDrag_) |
| 457 |
.listen( |
| 458 |
this.extentDragger_, goog.fx.Dragger.EventType.BEFOREDRAG, |
| 459 |
this.handleBeforeDrag_) |
| 460 |
.listen( |
| 461 |
this.valueDragger_, |
| 462 |
[goog.fx.Dragger.EventType.START, goog.fx.Dragger.EventType.END], |
| 463 |
this.handleThumbDragStartEnd_) |
| 464 |
.listen( |
| 465 |
this.extentDragger_, |
| 466 |
[goog.fx.Dragger.EventType.START, goog.fx.Dragger.EventType.END], |
| 467 |
this.handleThumbDragStartEnd_) |
| 468 |
.listen( |
| 469 |
this.keyHandler_, goog.events.KeyHandler.EventType.KEY, |
| 470 |
this.handleKeyDown_) |
| 471 |
.listen( |
| 472 |
this.getElement(), goog.events.EventType.CLICK, |
| 473 |
this.handleMouseDownAndClick_) |
| 474 |
.listen( |
| 475 |
this.getElement(), goog.events.EventType.MOUSEDOWN, |
| 476 |
this.handleMouseDownAndClick_); |
| 477 |
if (this.isHandleMouseWheel()) { |
| 478 |
this.enableMouseWheelHandling_(true); |
| 479 |
} |
| 480 |
} else { |
| 481 |
this.getHandler() |
| 482 |
.unlisten( |
| 483 |
this.valueDragger_, goog.fx.Dragger.EventType.BEFOREDRAG, |
| 484 |
this.handleBeforeDrag_) |
| 485 |
.unlisten( |
| 486 |
this.extentDragger_, goog.fx.Dragger.EventType.BEFOREDRAG, |
| 487 |
this.handleBeforeDrag_) |
| 488 |
.unlisten( |
| 489 |
this.valueDragger_, |
| 490 |
[goog.fx.Dragger.EventType.START, goog.fx.Dragger.EventType.END], |
| 491 |
this.handleThumbDragStartEnd_) |
| 492 |
.unlisten( |
| 493 |
this.extentDragger_, |
| 494 |
[goog.fx.Dragger.EventType.START, goog.fx.Dragger.EventType.END], |
| 495 |
this.handleThumbDragStartEnd_) |
| 496 |
.unlisten( |
| 497 |
this.keyHandler_, goog.events.KeyHandler.EventType.KEY, |
| 498 |
this.handleKeyDown_) |
| 499 |
.unlisten( |
| 500 |
this.getElement(), goog.events.EventType.CLICK, |
| 501 |
this.handleMouseDownAndClick_) |
| 502 |
.unlisten( |
| 503 |
this.getElement(), goog.events.EventType.MOUSEDOWN, |
| 504 |
this.handleMouseDownAndClick_); |
| 505 |
if (this.isHandleMouseWheel()) { |
| 506 |
this.enableMouseWheelHandling_(false); |
| 507 |
} |
| 508 |
} |
| 509 |
}; |
| 510 |
|
| 511 |
|
| 512 |
/** @override */ |
| 513 |
goog.ui.SliderBase.prototype.exitDocument = function() { |
| 514 |
goog.ui.SliderBase.base(this, 'exitDocument'); |
| 515 |
goog.disposeAll( |
| 516 |
this.valueDragger_, this.extentDragger_, this.keyHandler_, |
| 517 |
this.mouseWheelHandler_); |
| 518 |
}; |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* Handler for the before drag event. We use the event properties to determine |
| 523 |
* the new value. |
| 524 |
* @param {goog.fx.DragEvent} e The drag event used to drag the thumb. |
| 525 |
* @private |
| 526 |
*/ |
| 527 |
goog.ui.SliderBase.prototype.handleBeforeDrag_ = function(e) { |
| 528 |
var thumbToDrag = |
| 529 |
e.dragger == this.valueDragger_ ? this.valueThumb : this.extentThumb; |
| 530 |
var value; |
| 531 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 532 |
var availHeight = this.getElement().clientHeight - thumbToDrag.offsetHeight; |
| 533 |
value = (availHeight - e.top) / availHeight * |
| 534 |
(this.getMaximum() - this.getMinimum()) + |
| 535 |
this.getMinimum(); |
| 536 |
} else { |
| 537 |
var availWidth = this.getElement().clientWidth - thumbToDrag.offsetWidth; |
| 538 |
|
| 539 |
// In LTR mode, the space to the left of the thumb makes up the value. In |
| 540 |
// RTL, we need to flip this given that the space to the right of the thumb |
| 541 |
// is what makes up the value. |
| 542 |
var percentageUsed = e.left / availWidth; |
| 543 |
if (this.flipForRtl_ && this.isRightToLeft()) { |
| 544 |
percentageUsed = 1 - percentageUsed; |
| 545 |
} |
| 546 |
|
| 547 |
value = percentageUsed * (this.getMaximum() - this.getMinimum()) + |
| 548 |
this.getMinimum(); |
| 549 |
} |
| 550 |
// Bind the value within valid range before calling setThumbPosition_. |
| 551 |
// This is necessary because setThumbPosition_ is a no-op for values outside |
| 552 |
// of the legal range. For drag operations, we want the handle to snap to the |
| 553 |
// last valid value instead of remaining at the previous position. |
| 554 |
if (e.dragger == this.valueDragger_) { |
| 555 |
value = Math.min( |
| 556 |
Math.max(value, this.getMinimum()), this.getValue() + this.getExtent()); |
| 557 |
} else { |
| 558 |
value = Math.min(Math.max(value, this.getValue()), this.getMaximum()); |
| 559 |
} |
| 560 |
this.setThumbPosition_(thumbToDrag, value); |
| 561 |
}; |
| 562 |
|
| 563 |
|
| 564 |
/** |
| 565 |
* Handler for the start/end drag event on the thumbs. Adds/removes |
| 566 |
* the "-dragging" CSS classes on the slider and thumb. |
| 567 |
* @param {goog.fx.DragEvent} e The drag event used to drag the thumb. |
| 568 |
* @private |
| 569 |
*/ |
| 570 |
goog.ui.SliderBase.prototype.handleThumbDragStartEnd_ = function(e) { |
| 571 |
var isDragStart = e.type == goog.fx.Dragger.EventType.START; |
| 572 |
goog.dom.classlist.enable( |
| 573 |
goog.asserts.assertElement(this.getElement()), |
| 574 |
goog.ui.SliderBase.SLIDER_DRAGGING_CSS_CLASS_, isDragStart); |
| 575 |
goog.dom.classlist.enable( |
| 576 |
goog.asserts.assertElement(e.target.handle), |
| 577 |
goog.ui.SliderBase.THUMB_DRAGGING_CSS_CLASS_, isDragStart); |
| 578 |
var isValueDragger = e.dragger == this.valueDragger_; |
| 579 |
if (isDragStart) { |
| 580 |
this.dispatchEvent(goog.ui.SliderBase.EventType.DRAG_START); |
| 581 |
this.dispatchEvent( |
| 582 |
isValueDragger ? goog.ui.SliderBase.EventType.DRAG_VALUE_START : |
| 583 |
goog.ui.SliderBase.EventType.DRAG_EXTENT_START); |
| 584 |
} else { |
| 585 |
this.dispatchEvent(goog.ui.SliderBase.EventType.DRAG_END); |
| 586 |
this.dispatchEvent( |
| 587 |
isValueDragger ? goog.ui.SliderBase.EventType.DRAG_VALUE_END : |
| 588 |
goog.ui.SliderBase.EventType.DRAG_EXTENT_END); |
| 589 |
} |
| 590 |
}; |
| 591 |
|
| 592 |
|
| 593 |
/** |
| 594 |
* Event handler for the key down event. This is used to update the value |
| 595 |
* based on the key pressed. |
| 596 |
* @param {goog.events.KeyEvent} e The keyboard event object. |
| 597 |
* @private |
| 598 |
*/ |
| 599 |
goog.ui.SliderBase.prototype.handleKeyDown_ = function(e) { |
| 600 |
var handled = true; |
| 601 |
switch (e.keyCode) { |
| 602 |
case goog.events.KeyCodes.HOME: |
| 603 |
this.animatedSetValue(this.getMinimum()); |
| 604 |
break; |
| 605 |
case goog.events.KeyCodes.END: |
| 606 |
this.animatedSetValue(this.getMaximum()); |
| 607 |
break; |
| 608 |
case goog.events.KeyCodes.PAGE_UP: |
| 609 |
this.moveThumbs(this.getBlockIncrement()); |
| 610 |
break; |
| 611 |
case goog.events.KeyCodes.PAGE_DOWN: |
| 612 |
this.moveThumbs(-this.getBlockIncrement()); |
| 613 |
break; |
| 614 |
case goog.events.KeyCodes.LEFT: |
| 615 |
var sign = this.flipForRtl_ && this.isRightToLeft() ? 1 : -1; |
| 616 |
this.moveThumbs( |
| 617 |
e.shiftKey ? sign * this.getBlockIncrement() : |
| 618 |
sign * this.getUnitIncrement()); |
| 619 |
break; |
| 620 |
case goog.events.KeyCodes.DOWN: |
| 621 |
this.moveThumbs( |
| 622 |
e.shiftKey ? -this.getBlockIncrement() : -this.getUnitIncrement()); |
| 623 |
break; |
| 624 |
case goog.events.KeyCodes.RIGHT: |
| 625 |
var sign = this.flipForRtl_ && this.isRightToLeft() ? -1 : 1; |
| 626 |
this.moveThumbs( |
| 627 |
e.shiftKey ? sign * this.getBlockIncrement() : |
| 628 |
sign * this.getUnitIncrement()); |
| 629 |
break; |
| 630 |
case goog.events.KeyCodes.UP: |
| 631 |
this.moveThumbs( |
| 632 |
e.shiftKey ? this.getBlockIncrement() : this.getUnitIncrement()); |
| 633 |
break; |
| 634 |
|
| 635 |
default: |
| 636 |
handled = false; |
| 637 |
} |
| 638 |
|
| 639 |
if (handled) { |
| 640 |
e.preventDefault(); |
| 641 |
} |
| 642 |
}; |
| 643 |
|
| 644 |
|
| 645 |
/** |
| 646 |
* Handler for the mouse down event and click event. |
| 647 |
* @param {goog.events.Event} e The mouse event object. |
| 648 |
* @private |
| 649 |
*/ |
| 650 |
goog.ui.SliderBase.prototype.handleMouseDownAndClick_ = function(e) { |
| 651 |
if (this.getElement().focus) { |
| 652 |
this.getElement().focus(); |
| 653 |
} |
| 654 |
|
| 655 |
// Known Element. |
| 656 |
var target = /** @type {Element} */ (e.target); |
| 657 |
|
| 658 |
if (!goog.dom.contains(this.valueThumb, target) && |
| 659 |
!goog.dom.contains(this.extentThumb, target)) { |
| 660 |
var isClick = e.type == goog.events.EventType.CLICK; |
| 661 |
if (isClick && goog.now() < this.mouseDownTime_ + this.MOUSE_DOWN_DELAY_) { |
| 662 |
// Ignore a click event that comes a short moment after a mousedown |
| 663 |
// event. This happens for desktop. For devices with both a touch |
| 664 |
// screen and a mouse pad we do not get a mousedown event from the mouse |
| 665 |
// pad and do get a click event. |
| 666 |
return; |
| 667 |
} |
| 668 |
if (!isClick) { |
| 669 |
this.mouseDownTime_ = goog.now(); |
| 670 |
} |
| 671 |
|
| 672 |
if (this.moveToPointEnabled_) { |
| 673 |
// just set the value directly based on the position of the click |
| 674 |
this.animatedSetValue(this.getValueFromMousePosition(e)); |
| 675 |
} else { |
| 676 |
// start a timer that incrementally moves the handle |
| 677 |
this.startBlockIncrementing_(e); |
| 678 |
} |
| 679 |
} |
| 680 |
}; |
| 681 |
|
| 682 |
|
| 683 |
/** |
| 684 |
* Handler for the mouse wheel event. |
| 685 |
* @param {goog.events.MouseWheelEvent} e The mouse wheel event object. |
| 686 |
* @private |
| 687 |
*/ |
| 688 |
goog.ui.SliderBase.prototype.handleMouseWheel_ = function(e) { |
| 689 |
// Just move one unit increment per mouse wheel event |
| 690 |
var direction = e.detail > 0 ? -1 : 1; |
| 691 |
this.moveThumbs(direction * this.getUnitIncrement()); |
| 692 |
e.preventDefault(); |
| 693 |
}; |
| 694 |
|
| 695 |
|
| 696 |
/** |
| 697 |
* Starts the animation that causes the thumb to increment/decrement by the |
| 698 |
* block increment when the user presses down on the background. |
| 699 |
* @param {goog.events.Event} e The mouse event object. |
| 700 |
* @private |
| 701 |
*/ |
| 702 |
goog.ui.SliderBase.prototype.startBlockIncrementing_ = function(e) { |
| 703 |
this.storeMousePos_(e); |
| 704 |
this.thumbToMove_ = this.getClosestThumb_(this.getValueFromMousePosition(e)); |
| 705 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 706 |
this.incrementing_ = this.lastMousePosition_ < this.thumbToMove_.offsetTop; |
| 707 |
} else { |
| 708 |
this.incrementing_ = this.lastMousePosition_ > |
| 709 |
this.getOffsetStart_(this.thumbToMove_) + this.thumbToMove_.offsetWidth; |
| 710 |
} |
| 711 |
|
| 712 |
var doc = goog.dom.getOwnerDocument(this.getElement()); |
| 713 |
this.getHandler() |
| 714 |
.listen( |
| 715 |
doc, goog.events.EventType.MOUSEUP, this.stopBlockIncrementing_, true) |
| 716 |
.listen( |
| 717 |
this.getElement(), goog.events.EventType.MOUSEMOVE, |
| 718 |
this.storeMousePos_); |
| 719 |
|
| 720 |
if (!this.incTimer_) { |
| 721 |
this.incTimer_ = |
| 722 |
new goog.Timer(goog.ui.SliderBase.MOUSE_DOWN_INCREMENT_INTERVAL_); |
| 723 |
this.getHandler().listen( |
| 724 |
this.incTimer_, goog.Timer.TICK, this.handleTimerTick_); |
| 725 |
} |
| 726 |
this.handleTimerTick_(); |
| 727 |
this.incTimer_.start(); |
| 728 |
}; |
| 729 |
|
| 730 |
|
| 731 |
/** |
| 732 |
* Handler for the tick event dispatched by the timer used to update the value |
| 733 |
* in a block increment. This is also called directly from |
| 734 |
* startBlockIncrementing_. |
| 735 |
* @private |
| 736 |
*/ |
| 737 |
goog.ui.SliderBase.prototype.handleTimerTick_ = function() { |
| 738 |
var value; |
| 739 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 740 |
var mouseY = this.lastMousePosition_; |
| 741 |
var thumbY = this.thumbToMove_.offsetTop; |
| 742 |
if (this.incrementing_) { |
| 743 |
if (mouseY < thumbY) { |
| 744 |
value = this.getThumbPosition_(this.thumbToMove_) + |
| 745 |
this.getBlockIncrement(); |
| 746 |
} |
| 747 |
} else { |
| 748 |
var thumbH = this.thumbToMove_.offsetHeight; |
| 749 |
if (mouseY > thumbY + thumbH) { |
| 750 |
value = this.getThumbPosition_(this.thumbToMove_) - |
| 751 |
this.getBlockIncrement(); |
| 752 |
} |
| 753 |
} |
| 754 |
} else { |
| 755 |
var mouseX = this.lastMousePosition_; |
| 756 |
var thumbX = this.getOffsetStart_(this.thumbToMove_); |
| 757 |
if (this.incrementing_) { |
| 758 |
var thumbW = this.thumbToMove_.offsetWidth; |
| 759 |
if (mouseX > thumbX + thumbW) { |
| 760 |
value = this.getThumbPosition_(this.thumbToMove_) + |
| 761 |
this.getBlockIncrement(); |
| 762 |
} |
| 763 |
} else { |
| 764 |
if (mouseX < thumbX) { |
| 765 |
value = this.getThumbPosition_(this.thumbToMove_) - |
| 766 |
this.getBlockIncrement(); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
if (goog.isDef(value)) { // not all code paths sets the value variable |
| 772 |
this.setThumbPosition_(this.thumbToMove_, value); |
| 773 |
} |
| 774 |
}; |
| 775 |
|
| 776 |
|
| 777 |
/** |
| 778 |
* Stops the block incrementing animation and unlistens the necessary |
| 779 |
* event handlers. |
| 780 |
* @private |
| 781 |
*/ |
| 782 |
goog.ui.SliderBase.prototype.stopBlockIncrementing_ = function() { |
| 783 |
if (this.incTimer_) { |
| 784 |
this.incTimer_.stop(); |
| 785 |
} |
| 786 |
|
| 787 |
var doc = goog.dom.getOwnerDocument(this.getElement()); |
| 788 |
this.getHandler() |
| 789 |
.unlisten( |
| 790 |
doc, goog.events.EventType.MOUSEUP, this.stopBlockIncrementing_, true) |
| 791 |
.unlisten( |
| 792 |
this.getElement(), goog.events.EventType.MOUSEMOVE, |
| 793 |
this.storeMousePos_); |
| 794 |
}; |
| 795 |
|
| 796 |
|
| 797 |
/** |
| 798 |
* Returns the relative mouse position to the slider. |
| 799 |
* @param {goog.events.Event} e The mouse event object. |
| 800 |
* @return {number} The relative mouse position to the slider. |
| 801 |
* @private |
| 802 |
*/ |
| 803 |
goog.ui.SliderBase.prototype.getRelativeMousePos_ = function(e) { |
| 804 |
var coord = goog.style.getRelativePosition(e, this.getElement()); |
| 805 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 806 |
return coord.y; |
| 807 |
} else { |
| 808 |
if (this.flipForRtl_ && this.isRightToLeft()) { |
| 809 |
return this.getElement().clientWidth - coord.x; |
| 810 |
} else { |
| 811 |
return coord.x; |
| 812 |
} |
| 813 |
} |
| 814 |
}; |
| 815 |
|
| 816 |
|
| 817 |
/** |
| 818 |
* Stores the current mouse position so that it can be used in the timer. |
| 819 |
* @param {goog.events.Event} e The mouse event object. |
| 820 |
* @private |
| 821 |
*/ |
| 822 |
goog.ui.SliderBase.prototype.storeMousePos_ = function(e) { |
| 823 |
this.lastMousePosition_ = this.getRelativeMousePos_(e); |
| 824 |
}; |
| 825 |
|
| 826 |
|
| 827 |
/** |
| 828 |
* Returns the value to use for the current mouse position |
| 829 |
* @param {goog.events.Event} e The mouse event object. |
| 830 |
* @return {number} The value that this mouse position represents. |
| 831 |
*/ |
| 832 |
goog.ui.SliderBase.prototype.getValueFromMousePosition = function(e) { |
| 833 |
var min = this.getMinimum(); |
| 834 |
var max = this.getMaximum(); |
| 835 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 836 |
var thumbH = this.valueThumb.offsetHeight; |
| 837 |
var availH = this.getElement().clientHeight - thumbH; |
| 838 |
var y = this.getRelativeMousePos_(e) - thumbH / 2; |
| 839 |
return (max - min) * (availH - y) / availH + min; |
| 840 |
} else { |
| 841 |
var thumbW = this.valueThumb.offsetWidth; |
| 842 |
var availW = this.getElement().clientWidth - thumbW; |
| 843 |
var x = this.getRelativeMousePos_(e) - thumbW / 2; |
| 844 |
return (max - min) * x / availW + min; |
| 845 |
} |
| 846 |
}; |
| 847 |
|
| 848 |
|
| 849 |
/** |
| 850 |
* @param {HTMLDivElement} thumb The thumb object. |
| 851 |
* @return {number} The position of the specified thumb. |
| 852 |
* @private |
| 853 |
*/ |
| 854 |
goog.ui.SliderBase.prototype.getThumbPosition_ = function(thumb) { |
| 855 |
if (thumb == this.valueThumb) { |
| 856 |
return this.rangeModel.getValue(); |
| 857 |
} else if (thumb == this.extentThumb) { |
| 858 |
return this.rangeModel.getValue() + this.rangeModel.getExtent(); |
| 859 |
} else { |
| 860 |
throw new Error('Illegal thumb element. Neither minThumb nor maxThumb'); |
| 861 |
} |
| 862 |
}; |
| 863 |
|
| 864 |
|
| 865 |
/** |
| 866 |
* Returns whether a thumb is currently being dragged with the mouse (or via |
| 867 |
* touch). Note that changing the value with keyboard, mouswheel, or via |
| 868 |
* move-to-point click immediately sends a CHANGE event without going through a |
| 869 |
* dragged state. |
| 870 |
* @return {boolean} Whether a dragger is currently being dragged. |
| 871 |
*/ |
| 872 |
goog.ui.SliderBase.prototype.isDragging = function() { |
| 873 |
return this.valueDragger_.isDragging() || this.extentDragger_.isDragging(); |
| 874 |
}; |
| 875 |
|
| 876 |
|
| 877 |
/** |
| 878 |
* Moves the thumbs by the specified delta as follows |
| 879 |
* - as long as both thumbs stay within [min,max], both thumbs are moved |
| 880 |
* - once a thumb reaches or exceeds min (or max, respectively), it stays |
| 881 |
* - at min (or max, respectively). |
| 882 |
* In case both thumbs have reached min (or max), no change event will fire. |
| 883 |
* If the specified delta is smaller than the step size, it will be rounded |
| 884 |
* to the step size. |
| 885 |
* @param {number} delta The delta by which to move the selected range. |
| 886 |
*/ |
| 887 |
goog.ui.SliderBase.prototype.moveThumbs = function(delta) { |
| 888 |
// Assume that a small delta is supposed to be at least a step. |
| 889 |
if (Math.abs(delta) < this.getStep()) { |
| 890 |
delta = goog.math.sign(delta) * this.getStep(); |
| 891 |
} |
| 892 |
var newMinPos = this.getThumbPosition_(this.valueThumb) + delta; |
| 893 |
var newMaxPos = this.getThumbPosition_(this.extentThumb) + delta; |
| 894 |
// correct min / max positions to be within bounds |
| 895 |
newMinPos = goog.math.clamp( |
| 896 |
newMinPos, this.getMinimum(), this.getMaximum() - this.minExtent_); |
| 897 |
newMaxPos = goog.math.clamp( |
| 898 |
newMaxPos, this.getMinimum() + this.minExtent_, this.getMaximum()); |
| 899 |
// Set value and extent atomically |
| 900 |
this.setValueAndExtent(newMinPos, newMaxPos - newMinPos); |
| 901 |
}; |
| 902 |
|
| 903 |
|
| 904 |
/** |
| 905 |
* Sets the position of the given thumb. The set is ignored and no CHANGE event |
| 906 |
* fires if it violates the constraint minimum <= value (valueThumb position) <= |
| 907 |
* value + extent (extentThumb position) <= maximum. |
| 908 |
* |
| 909 |
* Note: To keep things simple, the setThumbPosition_ function does not have the |
| 910 |
* side-effect of "correcting" value or extent to fit the above constraint as it |
| 911 |
* is the case in the underlying range model. Instead, we simply ignore the |
| 912 |
* call. Callers must make these adjustements explicitly if they wish. |
| 913 |
* @param {Element} thumb The thumb whose position to set. |
| 914 |
* @param {number} position The position to move the thumb to. |
| 915 |
* @private |
| 916 |
*/ |
| 917 |
goog.ui.SliderBase.prototype.setThumbPosition_ = function(thumb, position) { |
| 918 |
// Round first so that all computations and checks are consistent. |
| 919 |
var roundedPosition = this.rangeModel.roundToStepWithMin(position); |
| 920 |
var value = |
| 921 |
thumb == this.valueThumb ? roundedPosition : this.rangeModel.getValue(); |
| 922 |
var end = thumb == this.extentThumb ? |
| 923 |
roundedPosition : |
| 924 |
this.rangeModel.getValue() + this.rangeModel.getExtent(); |
| 925 |
if (value >= this.getMinimum() && end >= value + this.minExtent_ && |
| 926 |
this.getMaximum() >= end) { |
| 927 |
this.setValueAndExtent(value, end - value); |
| 928 |
} |
| 929 |
}; |
| 930 |
|
| 931 |
|
| 932 |
/** |
| 933 |
* Sets the value and extent of the underlying range model. We enforce that |
| 934 |
* getMinimum() <= value <= getMaximum() - extent and |
| 935 |
* getMinExtent <= extent <= getMaximum() - getValue() |
| 936 |
* If this is not satisfied for the given extent, the call is ignored and no |
| 937 |
* CHANGE event fires. This is a utility method to allow setting the thumbs |
| 938 |
* simultaneously and ensuring that only one event fires. |
| 939 |
* @param {number} value The value to which to set the value. |
| 940 |
* @param {number} extent The value to which to set the extent. |
| 941 |
*/ |
| 942 |
goog.ui.SliderBase.prototype.setValueAndExtent = function(value, extent) { |
| 943 |
if (this.getMinimum() <= value && value <= this.getMaximum() - extent && |
| 944 |
this.minExtent_ <= extent && extent <= this.getMaximum() - value) { |
| 945 |
if (value == this.getValue() && extent == this.getExtent()) { |
| 946 |
return; |
| 947 |
} |
| 948 |
// because the underlying range model applies adjustements of value |
| 949 |
// and extent to fit within bounds, we need to reset the extent |
| 950 |
// first so these adjustements don't kick in. |
| 951 |
this.rangeModel.setMute(true); |
| 952 |
this.rangeModel.setExtent(0); |
| 953 |
this.rangeModel.setValue(value); |
| 954 |
this.rangeModel.setExtent(extent); |
| 955 |
this.rangeModel.setMute(false); |
| 956 |
this.handleRangeModelChange(null); |
| 957 |
} |
| 958 |
}; |
| 959 |
|
| 960 |
|
| 961 |
/** |
| 962 |
* @return {number} The minimum value. |
| 963 |
*/ |
| 964 |
goog.ui.SliderBase.prototype.getMinimum = function() { |
| 965 |
return this.rangeModel.getMinimum(); |
| 966 |
}; |
| 967 |
|
| 968 |
|
| 969 |
/** |
| 970 |
* Sets the minimum number. |
| 971 |
* @param {number} min The minimum value. |
| 972 |
*/ |
| 973 |
goog.ui.SliderBase.prototype.setMinimum = function(min) { |
| 974 |
this.rangeModel.setMinimum(min); |
| 975 |
}; |
| 976 |
|
| 977 |
|
| 978 |
/** |
| 979 |
* @return {number} The maximum value. |
| 980 |
*/ |
| 981 |
goog.ui.SliderBase.prototype.getMaximum = function() { |
| 982 |
return this.rangeModel.getMaximum(); |
| 983 |
}; |
| 984 |
|
| 985 |
|
| 986 |
/** |
| 987 |
* Sets the maximum number. |
| 988 |
* @param {number} max The maximum value. |
| 989 |
*/ |
| 990 |
goog.ui.SliderBase.prototype.setMaximum = function(max) { |
| 991 |
this.rangeModel.setMaximum(max); |
| 992 |
}; |
| 993 |
|
| 994 |
|
| 995 |
/** |
| 996 |
* @return {HTMLDivElement} The value thumb element. |
| 997 |
*/ |
| 998 |
goog.ui.SliderBase.prototype.getValueThumb = function() { |
| 999 |
return this.valueThumb; |
| 1000 |
}; |
| 1001 |
|
| 1002 |
|
| 1003 |
/** |
| 1004 |
* @return {HTMLDivElement} The extent thumb element. |
| 1005 |
*/ |
| 1006 |
goog.ui.SliderBase.prototype.getExtentThumb = function() { |
| 1007 |
return this.extentThumb; |
| 1008 |
}; |
| 1009 |
|
| 1010 |
|
| 1011 |
/** |
| 1012 |
* @param {number} position The position to get the closest thumb to. |
| 1013 |
* @return {HTMLDivElement} The thumb that is closest to the given position. |
| 1014 |
* @private |
| 1015 |
*/ |
| 1016 |
goog.ui.SliderBase.prototype.getClosestThumb_ = function(position) { |
| 1017 |
if (position <= |
| 1018 |
(this.rangeModel.getValue() + this.rangeModel.getExtent() / 2)) { |
| 1019 |
return this.valueThumb; |
| 1020 |
} else { |
| 1021 |
return this.extentThumb; |
| 1022 |
} |
| 1023 |
}; |
| 1024 |
|
| 1025 |
|
| 1026 |
/** |
| 1027 |
* Call back when the internal range model changes. Sub-classes may override |
| 1028 |
* and re-enter this method to update a11y state. Consider protected. |
| 1029 |
* @param {goog.events.Event} e The event object. |
| 1030 |
* @protected |
| 1031 |
*/ |
| 1032 |
goog.ui.SliderBase.prototype.handleRangeModelChange = function(e) { |
| 1033 |
this.updateUi_(); |
| 1034 |
this.updateAriaStates(); |
| 1035 |
this.dispatchEvent(goog.ui.Component.EventType.CHANGE); |
| 1036 |
}; |
| 1037 |
|
| 1038 |
|
| 1039 |
/** |
| 1040 |
* This is called when we need to update the size of the thumb. This happens |
| 1041 |
* when first created as well as when the value and the orientation changes. |
| 1042 |
* @private |
| 1043 |
*/ |
| 1044 |
goog.ui.SliderBase.prototype.updateUi_ = function() { |
| 1045 |
if (this.valueThumb && !this.isAnimating_) { |
| 1046 |
var minCoord = this.getThumbCoordinateForValue( |
| 1047 |
this.getThumbPosition_(this.valueThumb)); |
| 1048 |
var maxCoord = this.getThumbCoordinateForValue( |
| 1049 |
this.getThumbPosition_(this.extentThumb)); |
| 1050 |
|
| 1051 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 1052 |
this.valueThumb.style.top = minCoord.y + 'px'; |
| 1053 |
this.extentThumb.style.top = maxCoord.y + 'px'; |
| 1054 |
if (this.rangeHighlight) { |
| 1055 |
var highlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1056 |
maxCoord.y, minCoord.y, this.valueThumb.offsetHeight); |
| 1057 |
this.rangeHighlight.style.top = highlightPositioning.offset + 'px'; |
| 1058 |
this.rangeHighlight.style.height = highlightPositioning.size + 'px'; |
| 1059 |
} |
| 1060 |
} else { |
| 1061 |
var pos = (this.flipForRtl_ && this.isRightToLeft()) ? 'right' : 'left'; |
| 1062 |
this.valueThumb.style[pos] = minCoord.x + 'px'; |
| 1063 |
this.extentThumb.style[pos] = maxCoord.x + 'px'; |
| 1064 |
if (this.rangeHighlight) { |
| 1065 |
var highlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1066 |
minCoord.x, maxCoord.x, this.valueThumb.offsetWidth); |
| 1067 |
this.rangeHighlight.style[pos] = highlightPositioning.offset + 'px'; |
| 1068 |
this.rangeHighlight.style.width = highlightPositioning.size + 'px'; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
}; |
| 1073 |
|
| 1074 |
|
| 1075 |
/** |
| 1076 |
* Calculates the start position (offset) and size of the range highlight, e.g. |
| 1077 |
* for a horizontal slider, this will return [left, width] for the highlight. |
| 1078 |
* @param {number} firstThumbPos The position of the first thumb along the |
| 1079 |
* slider axis. |
| 1080 |
* @param {number} secondThumbPos The position of the second thumb along the |
| 1081 |
* slider axis, must be >= firstThumbPos. |
| 1082 |
* @param {number} thumbSize The size of the thumb, along the slider axis. |
| 1083 |
* @return {{offset: number, size: number}} The positioning parameters for the |
| 1084 |
* range highlight. |
| 1085 |
* @private |
| 1086 |
*/ |
| 1087 |
goog.ui.SliderBase.prototype.calculateRangeHighlightPositioning_ = function( |
| 1088 |
firstThumbPos, secondThumbPos, thumbSize) { |
| 1089 |
// Highlight is inset by half the thumb size, from the edges of the thumb. |
| 1090 |
var highlightInset = Math.ceil(thumbSize / 2); |
| 1091 |
var size = secondThumbPos - firstThumbPos + thumbSize - 2 * highlightInset; |
| 1092 |
// Don't return negative size since it causes an error. IE sometimes attempts |
| 1093 |
// to position the thumbs while slider size is 0, resulting in size < 0 here. |
| 1094 |
return {offset: firstThumbPos + highlightInset, size: Math.max(size, 0)}; |
| 1095 |
}; |
| 1096 |
|
| 1097 |
|
| 1098 |
/** |
| 1099 |
* Returns the position to move the handle to for a given value |
| 1100 |
* @param {number} val The value to get the coordinate for. |
| 1101 |
* @return {!goog.math.Coordinate} Coordinate with either x or y set. |
| 1102 |
*/ |
| 1103 |
goog.ui.SliderBase.prototype.getThumbCoordinateForValue = function(val) { |
| 1104 |
var coord = new goog.math.Coordinate; |
| 1105 |
if (this.valueThumb) { |
| 1106 |
var min = this.getMinimum(); |
| 1107 |
var max = this.getMaximum(); |
| 1108 |
|
| 1109 |
// This check ensures the ratio never take NaN value, which is possible when |
| 1110 |
// the slider min & max are same numbers (i.e. 1). |
| 1111 |
var ratio = (val == min && min == max) ? 0 : (val - min) / (max - min); |
| 1112 |
|
| 1113 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 1114 |
var thumbHeight = this.valueThumb.offsetHeight; |
| 1115 |
var h = this.getElement().clientHeight - thumbHeight; |
| 1116 |
var bottom = Math.round(ratio * h); |
| 1117 |
coord.x = this.getOffsetStart_(this.valueThumb); // Keep x the same. |
| 1118 |
coord.y = h - bottom; |
| 1119 |
} else { |
| 1120 |
var w = this.getElement().clientWidth - this.valueThumb.offsetWidth; |
| 1121 |
var left = Math.round(ratio * w); |
| 1122 |
coord.x = left; |
| 1123 |
coord.y = this.valueThumb.offsetTop; // Keep y the same. |
| 1124 |
} |
| 1125 |
} |
| 1126 |
return coord; |
| 1127 |
}; |
| 1128 |
|
| 1129 |
|
| 1130 |
/** |
| 1131 |
* Sets the value and starts animating the handle towards that position. |
| 1132 |
* @param {number} v Value to set and animate to. |
| 1133 |
*/ |
| 1134 |
goog.ui.SliderBase.prototype.animatedSetValue = function(v) { |
| 1135 |
// the value might be out of bounds |
| 1136 |
v = goog.math.clamp(v, this.getMinimum(), this.getMaximum()); |
| 1137 |
|
| 1138 |
if (this.isAnimating_) { |
| 1139 |
this.currentAnimation_.stop(true); |
| 1140 |
this.currentAnimation_.dispose(); |
| 1141 |
} |
| 1142 |
var animations = new goog.fx.AnimationParallelQueue(); |
| 1143 |
var end; |
| 1144 |
|
| 1145 |
var thumb = this.getClosestThumb_(v); |
| 1146 |
var previousValue = this.getValue(); |
| 1147 |
var previousExtent = this.getExtent(); |
| 1148 |
var previousThumbValue = this.getThumbPosition_(thumb); |
| 1149 |
var previousCoord = this.getThumbCoordinateForValue(previousThumbValue); |
| 1150 |
var stepSize = this.getStep(); |
| 1151 |
|
| 1152 |
// If the delta is less than a single step, increase it to a step, else the |
| 1153 |
// range model will reduce it to zero. |
| 1154 |
if (Math.abs(v - previousThumbValue) < stepSize) { |
| 1155 |
var delta = v > previousThumbValue ? stepSize : -stepSize; |
| 1156 |
v = previousThumbValue + delta; |
| 1157 |
|
| 1158 |
// The resulting value may be out of bounds, sanitize. |
| 1159 |
v = goog.math.clamp(v, this.getMinimum(), this.getMaximum()); |
| 1160 |
} |
| 1161 |
|
| 1162 |
this.setThumbPosition_(thumb, v); |
| 1163 |
var coord = this.getThumbCoordinateForValue(this.getThumbPosition_(thumb)); |
| 1164 |
|
| 1165 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 1166 |
end = [this.getOffsetStart_(thumb), coord.y]; |
| 1167 |
} else { |
| 1168 |
end = [coord.x, thumb.offsetTop]; |
| 1169 |
} |
| 1170 |
|
| 1171 |
var slide = new goog.fx.dom.Slide( |
| 1172 |
thumb, [previousCoord.x, previousCoord.y], end, |
| 1173 |
goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1174 |
slide.enableRightPositioningForRtl(this.flipForRtl_); |
| 1175 |
animations.add(slide); |
| 1176 |
if (this.rangeHighlight) { |
| 1177 |
this.addRangeHighlightAnimations_( |
| 1178 |
thumb, previousValue, previousExtent, coord, animations); |
| 1179 |
} |
| 1180 |
|
| 1181 |
// Create additional animations to play if a factory has been set. |
| 1182 |
if (this.additionalAnimations_) { |
| 1183 |
var additionalAnimations = this.additionalAnimations_.createAnimations( |
| 1184 |
previousValue, v, goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1185 |
goog.array.forEach(additionalAnimations, function(animation) { |
| 1186 |
animations.add(animation); |
| 1187 |
}); |
| 1188 |
} |
| 1189 |
|
| 1190 |
this.currentAnimation_ = animations; |
| 1191 |
this.getHandler().listen( |
| 1192 |
animations, goog.fx.Transition.EventType.END, this.endAnimation_); |
| 1193 |
|
| 1194 |
this.isAnimating_ = true; |
| 1195 |
animations.play(false); |
| 1196 |
}; |
| 1197 |
|
| 1198 |
|
| 1199 |
/** |
| 1200 |
* @return {boolean} True if the slider is animating, false otherwise. |
| 1201 |
*/ |
| 1202 |
goog.ui.SliderBase.prototype.isAnimating = function() { |
| 1203 |
return this.isAnimating_; |
| 1204 |
}; |
| 1205 |
|
| 1206 |
|
| 1207 |
/** |
| 1208 |
* Sets the factory that will be used to create additional animations to be |
| 1209 |
* played when animating to a new value. These animations can be for any |
| 1210 |
* element and the animations will be played in addition to the default |
| 1211 |
* animation(s). The animations will also be played in the same parallel queue |
| 1212 |
* ensuring that all animations are played at the same time. |
| 1213 |
* @see #animatedSetValue |
| 1214 |
* |
| 1215 |
* @param {goog.ui.SliderBase.AnimationFactory} factory The animation factory to |
| 1216 |
* use. This will not change the default animations played by the slider. |
| 1217 |
* It will only allow for additional animations. |
| 1218 |
*/ |
| 1219 |
goog.ui.SliderBase.prototype.setAdditionalAnimations = function(factory) { |
| 1220 |
this.additionalAnimations_ = factory; |
| 1221 |
}; |
| 1222 |
|
| 1223 |
|
| 1224 |
/** |
| 1225 |
* Adds animations for the range highlight element to the animation queue. |
| 1226 |
* |
| 1227 |
* @param {Element} thumb The thumb that's moving, must be |
| 1228 |
* either valueThumb or extentThumb. |
| 1229 |
* @param {number} previousValue The previous value of the slider. |
| 1230 |
* @param {number} previousExtent The previous extent of the |
| 1231 |
* slider. |
| 1232 |
* @param {goog.math.Coordinate} newCoord The new pixel coordinate of the |
| 1233 |
* thumb that's moving. |
| 1234 |
* @param {goog.fx.AnimationParallelQueue} animations The animation queue. |
| 1235 |
* @private |
| 1236 |
*/ |
| 1237 |
goog.ui.SliderBase.prototype.addRangeHighlightAnimations_ = function( |
| 1238 |
thumb, previousValue, previousExtent, newCoord, animations) { |
| 1239 |
var previousMinCoord = this.getThumbCoordinateForValue(previousValue); |
| 1240 |
var previousMaxCoord = |
| 1241 |
this.getThumbCoordinateForValue(previousValue + previousExtent); |
| 1242 |
var minCoord = previousMinCoord; |
| 1243 |
var maxCoord = previousMaxCoord; |
| 1244 |
if (thumb == this.valueThumb) { |
| 1245 |
minCoord = newCoord; |
| 1246 |
} else { |
| 1247 |
maxCoord = newCoord; |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (this.orientation_ == goog.ui.SliderBase.Orientation.VERTICAL) { |
| 1251 |
var previousHighlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1252 |
previousMaxCoord.y, previousMinCoord.y, this.valueThumb.offsetHeight); |
| 1253 |
var highlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1254 |
maxCoord.y, minCoord.y, this.valueThumb.offsetHeight); |
| 1255 |
var slide = new goog.fx.dom.Slide( |
| 1256 |
this.rangeHighlight, |
| 1257 |
[ |
| 1258 |
this.getOffsetStart_(this.rangeHighlight), |
| 1259 |
previousHighlightPositioning.offset |
| 1260 |
], |
| 1261 |
[ |
| 1262 |
this.getOffsetStart_(this.rangeHighlight), highlightPositioning.offset |
| 1263 |
], |
| 1264 |
goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1265 |
var resizeHeight = new goog.fx.dom.ResizeHeight( |
| 1266 |
this.rangeHighlight, previousHighlightPositioning.size, |
| 1267 |
highlightPositioning.size, goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1268 |
slide.enableRightPositioningForRtl(this.flipForRtl_); |
| 1269 |
resizeHeight.enableRightPositioningForRtl(this.flipForRtl_); |
| 1270 |
animations.add(slide); |
| 1271 |
animations.add(resizeHeight); |
| 1272 |
} else { |
| 1273 |
var previousHighlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1274 |
previousMinCoord.x, previousMaxCoord.x, this.valueThumb.offsetWidth); |
| 1275 |
var highlightPositioning = this.calculateRangeHighlightPositioning_( |
| 1276 |
minCoord.x, maxCoord.x, this.valueThumb.offsetWidth); |
| 1277 |
var slide = new goog.fx.dom.Slide( |
| 1278 |
this.rangeHighlight, |
| 1279 |
[previousHighlightPositioning.offset, this.rangeHighlight.offsetTop], |
| 1280 |
[highlightPositioning.offset, this.rangeHighlight.offsetTop], |
| 1281 |
goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1282 |
var resizeWidth = new goog.fx.dom.ResizeWidth( |
| 1283 |
this.rangeHighlight, previousHighlightPositioning.size, |
| 1284 |
highlightPositioning.size, goog.ui.SliderBase.ANIMATION_INTERVAL_); |
| 1285 |
slide.enableRightPositioningForRtl(this.flipForRtl_); |
| 1286 |
resizeWidth.enableRightPositioningForRtl(this.flipForRtl_); |
| 1287 |
animations.add(slide); |
| 1288 |
animations.add(resizeWidth); |
| 1289 |
} |
| 1290 |
}; |
| 1291 |
|
| 1292 |
|
| 1293 |
/** |
| 1294 |
* Sets the isAnimating_ field to false once the animation is done. |
| 1295 |
* @param {goog.fx.AnimationEvent} e Event object passed by the animation |
| 1296 |
* object. |
| 1297 |
* @private |
| 1298 |
*/ |
| 1299 |
goog.ui.SliderBase.prototype.endAnimation_ = function(e) { |
| 1300 |
this.isAnimating_ = false; |
| 1301 |
this.dispatchEvent(goog.ui.SliderBase.EventType.ANIMATION_END); |
| 1302 |
}; |
| 1303 |
|
| 1304 |
|
| 1305 |
/** |
| 1306 |
* Changes the orientation. |
| 1307 |
* @param {goog.ui.SliderBase.Orientation} orient The orientation. |
| 1308 |
*/ |
| 1309 |
goog.ui.SliderBase.prototype.setOrientation = function(orient) { |
| 1310 |
if (this.orientation_ != orient) { |
| 1311 |
var oldCss = this.getCssClass(this.orientation_); |
| 1312 |
var newCss = this.getCssClass(orient); |
| 1313 |
this.orientation_ = orient; |
| 1314 |
|
| 1315 |
// Update the DOM |
| 1316 |
if (this.getElement()) { |
| 1317 |
goog.dom.classlist.swap( |
| 1318 |
goog.asserts.assert(this.getElement()), oldCss, newCss); |
| 1319 |
// we need to reset the left and top, plus range highlight |
| 1320 |
var pos = (this.flipForRtl_ && this.isRightToLeft()) ? 'right' : 'left'; |
| 1321 |
this.valueThumb.style[pos] = this.valueThumb.style.top = ''; |
| 1322 |
this.extentThumb.style[pos] = this.extentThumb.style.top = ''; |
| 1323 |
if (this.rangeHighlight) { |
| 1324 |
this.rangeHighlight.style[pos] = this.rangeHighlight.style.top = ''; |
| 1325 |
this.rangeHighlight.style.width = this.rangeHighlight.style.height = ''; |
| 1326 |
} |
| 1327 |
this.updateUi_(); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
}; |
| 1331 |
|
| 1332 |
|
| 1333 |
/** |
| 1334 |
* @return {goog.ui.SliderBase.Orientation} the orientation of the slider. |
| 1335 |
*/ |
| 1336 |
goog.ui.SliderBase.prototype.getOrientation = function() { |
| 1337 |
return this.orientation_; |
| 1338 |
}; |
| 1339 |
|
| 1340 |
|
| 1341 |
/** @override */ |
| 1342 |
goog.ui.SliderBase.prototype.disposeInternal = function() { |
| 1343 |
goog.ui.SliderBase.superClass_.disposeInternal.call(this); |
| 1344 |
if (this.incTimer_) { |
| 1345 |
this.incTimer_.dispose(); |
| 1346 |
} |
| 1347 |
delete this.incTimer_; |
| 1348 |
if (this.currentAnimation_) { |
| 1349 |
this.currentAnimation_.dispose(); |
| 1350 |
} |
| 1351 |
delete this.currentAnimation_; |
| 1352 |
delete this.valueThumb; |
| 1353 |
delete this.extentThumb; |
| 1354 |
if (this.rangeHighlight) { |
| 1355 |
delete this.rangeHighlight; |
| 1356 |
} |
| 1357 |
this.rangeModel.dispose(); |
| 1358 |
delete this.rangeModel; |
| 1359 |
if (this.keyHandler_) { |
| 1360 |
this.keyHandler_.dispose(); |
| 1361 |
delete this.keyHandler_; |
| 1362 |
} |
| 1363 |
if (this.mouseWheelHandler_) { |
| 1364 |
this.mouseWheelHandler_.dispose(); |
| 1365 |
delete this.mouseWheelHandler_; |
| 1366 |
} |
| 1367 |
if (this.valueDragger_) { |
| 1368 |
this.valueDragger_.dispose(); |
| 1369 |
delete this.valueDragger_; |
| 1370 |
} |
| 1371 |
if (this.extentDragger_) { |
| 1372 |
this.extentDragger_.dispose(); |
| 1373 |
delete this.extentDragger_; |
| 1374 |
} |
| 1375 |
}; |
| 1376 |
|
| 1377 |
|
| 1378 |
/** |
| 1379 |
* @return {number} The amount to increment/decrement for page up/down as well |
| 1380 |
* as when holding down the mouse button on the background. |
| 1381 |
*/ |
| 1382 |
goog.ui.SliderBase.prototype.getBlockIncrement = function() { |
| 1383 |
return this.blockIncrement_; |
| 1384 |
}; |
| 1385 |
|
| 1386 |
|
| 1387 |
/** |
| 1388 |
* Sets the amount to increment/decrement for page up/down as well as when |
| 1389 |
* holding down the mouse button on the background. |
| 1390 |
* |
| 1391 |
* @param {number} value The value to set the block increment to. |
| 1392 |
*/ |
| 1393 |
goog.ui.SliderBase.prototype.setBlockIncrement = function(value) { |
| 1394 |
this.blockIncrement_ = value; |
| 1395 |
}; |
| 1396 |
|
| 1397 |
|
| 1398 |
/** |
| 1399 |
* Sets the minimal value that the extent may have. |
| 1400 |
* |
| 1401 |
* @param {number} value The minimal value for the extent. |
| 1402 |
*/ |
| 1403 |
goog.ui.SliderBase.prototype.setMinExtent = function(value) { |
| 1404 |
this.minExtent_ = value; |
| 1405 |
}; |
| 1406 |
|
| 1407 |
|
| 1408 |
/** |
| 1409 |
* The amount to increment/decrement for up, down, left and right arrow keys |
| 1410 |
* and mouse wheel events. |
| 1411 |
* @private |
| 1412 |
* @type {number} |
| 1413 |
*/ |
| 1414 |
goog.ui.SliderBase.prototype.unitIncrement_ = 1; |
| 1415 |
|
| 1416 |
|
| 1417 |
/** |
| 1418 |
* @return {number} The amount to increment/decrement for up, down, left and |
| 1419 |
* right arrow keys and mouse wheel events. |
| 1420 |
*/ |
| 1421 |
goog.ui.SliderBase.prototype.getUnitIncrement = function() { |
| 1422 |
return this.unitIncrement_; |
| 1423 |
}; |
| 1424 |
|
| 1425 |
|
| 1426 |
/** |
| 1427 |
* Sets the amount to increment/decrement for up, down, left and right arrow |
| 1428 |
* keys and mouse wheel events. |
| 1429 |
* @param {number} value The value to set the unit increment to. |
| 1430 |
*/ |
| 1431 |
goog.ui.SliderBase.prototype.setUnitIncrement = function(value) { |
| 1432 |
this.unitIncrement_ = value; |
| 1433 |
}; |
| 1434 |
|
| 1435 |
|
| 1436 |
/** |
| 1437 |
* @return {?number} The step value used to determine how to round the value. |
| 1438 |
*/ |
| 1439 |
goog.ui.SliderBase.prototype.getStep = function() { |
| 1440 |
return this.rangeModel.getStep(); |
| 1441 |
}; |
| 1442 |
|
| 1443 |
|
| 1444 |
/** |
| 1445 |
* Sets the step value. The step value is used to determine how to round the |
| 1446 |
* value. |
| 1447 |
* @param {?number} step The step size. |
| 1448 |
*/ |
| 1449 |
goog.ui.SliderBase.prototype.setStep = function(step) { |
| 1450 |
this.rangeModel.setStep(step); |
| 1451 |
}; |
| 1452 |
|
| 1453 |
|
| 1454 |
/** |
| 1455 |
* @return {boolean} Whether clicking on the backgtround should move directly to |
| 1456 |
* that point. |
| 1457 |
*/ |
| 1458 |
goog.ui.SliderBase.prototype.getMoveToPointEnabled = function() { |
| 1459 |
return this.moveToPointEnabled_; |
| 1460 |
}; |
| 1461 |
|
| 1462 |
|
| 1463 |
/** |
| 1464 |
* Sets whether clicking on the background should move directly to that point. |
| 1465 |
* @param {boolean} val Whether clicking on the background should move directly |
| 1466 |
* to that point. |
| 1467 |
*/ |
| 1468 |
goog.ui.SliderBase.prototype.setMoveToPointEnabled = function(val) { |
| 1469 |
this.moveToPointEnabled_ = val; |
| 1470 |
}; |
| 1471 |
|
| 1472 |
|
| 1473 |
/** |
| 1474 |
* @return {number} The value of the underlying range model. |
| 1475 |
*/ |
| 1476 |
goog.ui.SliderBase.prototype.getValue = function() { |
| 1477 |
return this.rangeModel.getValue(); |
| 1478 |
}; |
| 1479 |
|
| 1480 |
|
| 1481 |
/** |
| 1482 |
* Sets the value of the underlying range model. We enforce that |
| 1483 |
* getMinimum() <= value <= getMaximum() - getExtent() |
| 1484 |
* If this is not satisifed for the given value, the call is ignored and no |
| 1485 |
* CHANGE event fires. |
| 1486 |
* @param {number} value The value. |
| 1487 |
*/ |
| 1488 |
goog.ui.SliderBase.prototype.setValue = function(value) { |
| 1489 |
// Set the position through the thumb method to enforce constraints. |
| 1490 |
this.setThumbPosition_(this.valueThumb, value); |
| 1491 |
}; |
| 1492 |
|
| 1493 |
|
| 1494 |
/** |
| 1495 |
* @return {number} The value of the extent of the underlying range model. |
| 1496 |
*/ |
| 1497 |
goog.ui.SliderBase.prototype.getExtent = function() { |
| 1498 |
return this.rangeModel.getExtent(); |
| 1499 |
}; |
| 1500 |
|
| 1501 |
|
| 1502 |
/** |
| 1503 |
* Sets the extent of the underlying range model. We enforce that |
| 1504 |
* getMinExtent() <= extent <= getMaximum() - getValue() |
| 1505 |
* If this is not satisifed for the given extent, the call is ignored and no |
| 1506 |
* CHANGE event fires. |
| 1507 |
* @param {number} extent The value to which to set the extent. |
| 1508 |
*/ |
| 1509 |
goog.ui.SliderBase.prototype.setExtent = function(extent) { |
| 1510 |
// Set the position through the thumb method to enforce constraints. |
| 1511 |
this.setThumbPosition_( |
| 1512 |
this.extentThumb, (this.rangeModel.getValue() + extent)); |
| 1513 |
}; |
| 1514 |
|
| 1515 |
|
| 1516 |
/** |
| 1517 |
* Change the visibility of the slider. |
| 1518 |
* You must call this if you had set the slider's value when it was invisible. |
| 1519 |
* @param {boolean} visible Whether to show the slider. |
| 1520 |
*/ |
| 1521 |
goog.ui.SliderBase.prototype.setVisible = function(visible) { |
| 1522 |
goog.style.setElementShown(this.getElement(), visible); |
| 1523 |
if (visible) { |
| 1524 |
this.updateUi_(); |
| 1525 |
} |
| 1526 |
}; |
| 1527 |
|
| 1528 |
|
| 1529 |
/** |
| 1530 |
* Set a11y roles and state. |
| 1531 |
* @protected |
| 1532 |
*/ |
| 1533 |
goog.ui.SliderBase.prototype.setAriaRoles = function() { |
| 1534 |
var el = this.getElement(); |
| 1535 |
goog.asserts.assert( |
| 1536 |
el, 'The DOM element for the slider base cannot be null.'); |
| 1537 |
goog.a11y.aria.setRole(el, goog.a11y.aria.Role.SLIDER); |
| 1538 |
this.updateAriaStates(); |
| 1539 |
}; |
| 1540 |
|
| 1541 |
|
| 1542 |
/** |
| 1543 |
* Set a11y roles and state when values change. |
| 1544 |
* @protected |
| 1545 |
*/ |
| 1546 |
goog.ui.SliderBase.prototype.updateAriaStates = function() { |
| 1547 |
var element = this.getElement(); |
| 1548 |
if (element) { |
| 1549 |
goog.a11y.aria.setState( |
| 1550 |
element, goog.a11y.aria.State.VALUEMIN, this.getMinimum()); |
| 1551 |
goog.a11y.aria.setState( |
| 1552 |
element, goog.a11y.aria.State.VALUEMAX, this.getMaximum()); |
| 1553 |
goog.a11y.aria.setState( |
| 1554 |
element, goog.a11y.aria.State.VALUENOW, this.getValue()); |
| 1555 |
// Passing an empty value to setState will restore the default. |
| 1556 |
goog.a11y.aria.setState( |
| 1557 |
element, goog.a11y.aria.State.VALUETEXT, this.getTextValue() || ''); |
| 1558 |
} |
| 1559 |
}; |
| 1560 |
|
| 1561 |
|
| 1562 |
/** |
| 1563 |
* Enables or disables mouse wheel handling for the slider. The mouse wheel |
| 1564 |
* handler enables the user to change the value of slider using a mouse wheel. |
| 1565 |
* |
| 1566 |
* @param {boolean} enable Whether to enable mouse wheel handling. |
| 1567 |
*/ |
| 1568 |
goog.ui.SliderBase.prototype.setHandleMouseWheel = function(enable) { |
| 1569 |
if (this.isInDocument() && enable != this.isHandleMouseWheel()) { |
| 1570 |
this.enableMouseWheelHandling_(enable); |
| 1571 |
} |
| 1572 |
|
| 1573 |
this.isHandleMouseWheel_ = enable; |
| 1574 |
}; |
| 1575 |
|
| 1576 |
|
| 1577 |
/** |
| 1578 |
* @return {boolean} Whether the slider handles mousewheel. |
| 1579 |
*/ |
| 1580 |
goog.ui.SliderBase.prototype.isHandleMouseWheel = function() { |
| 1581 |
return this.isHandleMouseWheel_; |
| 1582 |
}; |
| 1583 |
|
| 1584 |
|
| 1585 |
/** |
| 1586 |
* Enable/Disable mouse wheel handling. |
| 1587 |
* @param {boolean} enable Whether to enable mouse wheel handling. |
| 1588 |
* @private |
| 1589 |
*/ |
| 1590 |
goog.ui.SliderBase.prototype.enableMouseWheelHandling_ = function(enable) { |
| 1591 |
if (enable) { |
| 1592 |
if (!this.mouseWheelHandler_) { |
| 1593 |
this.mouseWheelHandler_ = |
| 1594 |
new goog.events.MouseWheelHandler(this.getElement()); |
| 1595 |
} |
| 1596 |
this.getHandler().listen( |
| 1597 |
this.mouseWheelHandler_, |
| 1598 |
goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 1599 |
this.handleMouseWheel_); |
| 1600 |
} else { |
| 1601 |
this.getHandler().unlisten( |
| 1602 |
this.mouseWheelHandler_, |
| 1603 |
goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 1604 |
this.handleMouseWheel_); |
| 1605 |
} |
| 1606 |
}; |
| 1607 |
|
| 1608 |
|
| 1609 |
/** |
| 1610 |
* Enables or disables the slider. A disabled slider will ignore all |
| 1611 |
* user-initiated events. Also fires goog.ui.Component.EventType.ENABLE/DISABLE |
| 1612 |
* event as appropriate. |
| 1613 |
* @param {boolean} enable Whether to enable the slider or not. |
| 1614 |
*/ |
| 1615 |
goog.ui.SliderBase.prototype.setEnabled = function(enable) { |
| 1616 |
if (this.enabled_ == enable) { |
| 1617 |
return; |
| 1618 |
} |
| 1619 |
|
| 1620 |
var eventType = enable ? goog.ui.Component.EventType.ENABLE : |
| 1621 |
goog.ui.Component.EventType.DISABLE; |
| 1622 |
if (this.dispatchEvent(eventType)) { |
| 1623 |
this.enabled_ = enable; |
| 1624 |
this.enableEventHandlers_(enable); |
| 1625 |
if (!enable) { |
| 1626 |
// Disabling a slider is equivalent to a mouse up event when the block |
| 1627 |
// increment (if happening) should be halted and any possible event |
| 1628 |
// handlers be appropriately unlistened. |
| 1629 |
this.stopBlockIncrementing_(); |
| 1630 |
} |
| 1631 |
goog.dom.classlist.enable( |
| 1632 |
goog.asserts.assert(this.getElement()), |
| 1633 |
goog.ui.SliderBase.DISABLED_CSS_CLASS_, !enable); |
| 1634 |
} |
| 1635 |
}; |
| 1636 |
|
| 1637 |
|
| 1638 |
/** |
| 1639 |
* @return {boolean} Whether the slider is enabled or not. |
| 1640 |
*/ |
| 1641 |
goog.ui.SliderBase.prototype.isEnabled = function() { |
| 1642 |
return this.enabled_; |
| 1643 |
}; |
| 1644 |
|
| 1645 |
|
| 1646 |
/** |
| 1647 |
* @param {Element} element An element for which we want offsetLeft. |
| 1648 |
* @return {number} Returns the element's offsetLeft, accounting for RTL if |
| 1649 |
* flipForRtl_ is true. |
| 1650 |
* @private |
| 1651 |
*/ |
| 1652 |
goog.ui.SliderBase.prototype.getOffsetStart_ = function(element) { |
| 1653 |
return this.flipForRtl_ ? goog.style.bidi.getOffsetStart(element) : |
| 1654 |
element.offsetLeft; |
| 1655 |
}; |
| 1656 |
|
| 1657 |
|
| 1658 |
/** |
| 1659 |
* @return {?string} The text value for the slider's current value, or null if |
| 1660 |
* unavailable. |
| 1661 |
*/ |
| 1662 |
goog.ui.SliderBase.prototype.getTextValue = function() { |
| 1663 |
return this.labelFn_(this.getValue()); |
| 1664 |
}; |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
/** |
| 1669 |
* The factory for creating additional animations to be played when animating to |
| 1670 |
* a new value. |
| 1671 |
* @interface |
| 1672 |
*/ |
| 1673 |
goog.ui.SliderBase.AnimationFactory = function() {}; |
| 1674 |
|
| 1675 |
|
| 1676 |
/** |
| 1677 |
* Creates an additional animation to play when animating to a new value. |
| 1678 |
* |
| 1679 |
* @param {number} previousValue The previous value (before animation). |
| 1680 |
* @param {number} newValue The new value (after animation). |
| 1681 |
* @param {number} interval The animation interval. |
| 1682 |
* @return {!Array<!goog.fx.TransitionBase>} The additional animations to play. |
| 1683 |
*/ |
| 1684 |
goog.ui.SliderBase.AnimationFactory.prototype.createAnimations; |