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 document = this.container_.ownerDocument; | |
yoshiki
2013/08/28 08:58:44
Could you change the name of 'document' to some ot
falken
2013/08/29 09:35:44
Done.
| |
26 | |
27 this.container_.classList.add('slider'); | |
28 | |
29 this.input_ = document.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_ = document.createElement('div'); | |
40 this.bar_.className = 'bar'; | |
41 this.container_.appendChild(this.bar_); | |
42 | |
43 this.filled_ = document.createElement('div'); | |
44 this.filled_.className = 'filled'; | |
45 this.bar_.appendChild(this.filled_); | |
46 | |
47 var leftCap = document.createElement('div'); | |
48 leftCap.className = 'cap left'; | |
49 this.bar_.appendChild(leftCap); | |
50 | |
51 var rightCap = document.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. | |
yoshiki
2013/08/28 08:58:44
Add "@private" anotation.
falken
2013/08/29 09:35:44
Done.
| |
83 */ | |
84 Slider.prototype.updateFilledWidth_ = function() { | |
85 var proportion = (this.input_.value - this.input_.min) / | |
86 (this.input_.max - this.input_.min); | |
87 this.filled_.style.width = proportion * 100 + '%'; | |
88 }; | |
89 | |
90 /** | |
91 * Called when the slider's value changes. | |
yoshiki
2013/08/28 08:58:44
ditto
falken
2013/08/29 09:35:44
Done.
| |
92 */ | |
93 Slider.prototype.onInputChange_ = function() { | |
94 this.updateFilledWidth_(); | |
95 if (this.onChange_) | |
96 this.onChange_(this.input_.value); | |
97 }; | |
98 | |
OLD | NEW |