OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 /** |
| 8 * @fileoverview A Slider control. Based on Chromium's MediaControls.Slider. |
| 9 */ |
| 10 |
| 11 /** |
| 12 * Creates a slider control. |
| 13 * |
| 14 * @param {HTMLElement} container The containing div element. |
| 15 * @param {number} value Initial value |
| 16 * @param {number} min Minimum value |
| 17 * @param {number} max Maximum value |
| 18 * @param {?function(number)=} opt_onChange Value change handler |
| 19 * @constructor |
| 20 */ |
| 21 function Slider(container, value, min, max, opt_onChange) { |
| 22 this.container_ = container; |
| 23 this.onChange_ = opt_onChange; |
| 24 |
| 25 var containerDocument = this.container_.ownerDocument; |
| 26 |
| 27 this.container_.classList.add('slider'); |
| 28 |
| 29 this.input_ = containerDocument.createElement('input'); |
| 30 this.input_.type = 'range'; |
| 31 this.input_.min = min; |
| 32 this.input_.max = max; |
| 33 this.input_.value = value; |
| 34 this.container_.appendChild(this.input_); |
| 35 |
| 36 this.input_.addEventListener( |
| 37 'change', this.onInputChange_.bind(this)); |
| 38 |
| 39 this.bar_ = containerDocument.createElement('div'); |
| 40 this.bar_.className = 'bar'; |
| 41 this.container_.appendChild(this.bar_); |
| 42 |
| 43 this.filled_ = containerDocument.createElement('div'); |
| 44 this.filled_.className = 'filled'; |
| 45 this.bar_.appendChild(this.filled_); |
| 46 |
| 47 var leftCap = containerDocument.createElement('div'); |
| 48 leftCap.className = 'cap left'; |
| 49 this.bar_.appendChild(leftCap); |
| 50 |
| 51 var rightCap = containerDocument.createElement('div'); |
| 52 rightCap.className = 'cap right'; |
| 53 this.bar_.appendChild(rightCap); |
| 54 |
| 55 this.updateFilledWidth_(); |
| 56 }; |
| 57 |
| 58 /** |
| 59 * @return {number} The value of the input control. |
| 60 */ |
| 61 Slider.prototype.getValue = function() { |
| 62 return this.input_.value; |
| 63 }; |
| 64 |
| 65 /** |
| 66 * @param{number} value The value to set the input control to. |
| 67 */ |
| 68 Slider.prototype.setValue = function(value) { |
| 69 this.input_.value = value; |
| 70 this.updateFilledWidth_(); |
| 71 }; |
| 72 |
| 73 /** |
| 74 * @return {HTMLInputElement} The underlying input control. |
| 75 */ |
| 76 Slider.prototype.getInput = function() { |
| 77 return this.input_; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Updates the filled portion of the slider to reflect the slider's current |
| 82 * value. |
| 83 * @private |
| 84 */ |
| 85 Slider.prototype.updateFilledWidth_ = function() { |
| 86 var proportion = (this.input_.value - this.input_.min) / |
| 87 (this.input_.max - this.input_.min); |
| 88 this.filled_.style.width = proportion * 100 + '%'; |
| 89 }; |
| 90 |
| 91 /** |
| 92 * Called when the slider's value changes. |
| 93 * @private |
| 94 */ |
| 95 Slider.prototype.onInputChange_ = function() { |
| 96 this.updateFilledWidth_(); |
| 97 if (this.onChange_) |
| 98 this.onChange_(this.input_.value); |
| 99 }; |
| 100 |
OLD | NEW |