OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * settings-slider wraps a paper-slider. It maps the slider's values from a | 7 * settings-slider wraps a paper-slider. It maps the slider's values from a |
8 * linear UI range to a range of real values. When |value| does not map exactly | 8 * linear UI range to a range of real values. When |value| does not map exactly |
9 * to a tick mark, it interpolates to the nearest tick. | 9 * to a tick mark, it interpolates to the nearest tick. |
10 * | 10 * |
11 * Unlike paper-slider, there is no distinction between value and | 11 * Unlike paper-slider, there is no distinction between value and |
12 * immediateValue; when either changes, the |value| property is updated. | 12 * immediateValue; when either changes, the |value| property is updated. |
13 */ | 13 */ |
14 Polymer({ | 14 Polymer({ |
15 is: 'settings-slider', | 15 is: 'settings-slider', |
16 | 16 |
17 behaviors: [CrPolicyPrefBehavior], | 17 behaviors: [CrPolicyPrefBehavior], |
18 | 18 |
19 properties: { | 19 properties: { |
20 /** @type {!chrome.settingsPrivate.PrefObject} */ | 20 /** @type {!chrome.settingsPrivate.PrefObject} */ |
21 pref: Object, | 21 pref: Object, |
22 | 22 |
23 /** @type {!Array<number>} Values corresponding to each tick. */ | 23 /** @type {!Array<number>} Values corresponding to each tick. */ |
24 tickValues: {type: Array, value: []}, | 24 tickValues: {type: Array, value: []}, |
25 | 25 |
26 /** | |
27 * |scale| is applied to the slider value if |tickValues| is empty, | |
michaelpg
2017/05/15 19:53:18
the pref value is being scaled, not the slider val
stevenjb
2017/05/15 21:45:09
No, it's the other way around, but I will clarify
michaelpg
2017/05/15 22:14:32
it's still confusing -- I think we're reading it i
stevenjb
2017/05/15 22:24:00
Dude, the code says:
slider.value = pref.value *
michaelpg
2017/05/15 22:55:05
What term is multiplied by scale in this equation?
| |
28 * otherwise it is ignored. Can be used to set fractional pref values | |
29 * (paper-slider will not set fractional values). | |
30 */ | |
31 scale: Number, | |
32 | |
26 min: Number, | 33 min: Number, |
27 | 34 |
28 max: Number, | 35 max: Number, |
29 | 36 |
30 labelMin: String, | 37 labelMin: String, |
31 | 38 |
32 labelMax: String, | 39 labelMax: String, |
33 | 40 |
34 disabled: Boolean, | 41 disabled: Boolean, |
35 | 42 |
(...skipping 14 matching lines...) Expand all Loading... | |
50 * @private | 57 * @private |
51 */ | 58 */ |
52 onSliderChanged_: function() { | 59 onSliderChanged_: function() { |
53 var sliderValue = isNaN(this.$.slider.immediateValue) | 60 var sliderValue = isNaN(this.$.slider.immediateValue) |
54 ? this.$.slider.value | 61 ? this.$.slider.value |
55 : this.$.slider.immediateValue; | 62 : this.$.slider.immediateValue; |
56 | 63 |
57 var newValue; | 64 var newValue; |
58 if (this.tickValues && this.tickValues.length > 0) | 65 if (this.tickValues && this.tickValues.length > 0) |
59 newValue = this.tickValues[sliderValue]; | 66 newValue = this.tickValues[sliderValue]; |
67 else if (this.scale) | |
68 newValue = sliderValue / this.scale; | |
afakhry
2017/05/12 19:12:13
Is it possible to give |scale| a default value of
stevenjb
2017/05/12 20:14:13
It is, and I considered that, but this seemed slig
afakhry
2017/05/12 23:56:11
I'm fine with either way.
michaelpg
2017/05/15 19:53:18
Either way, I'd suggest an assert so it breaks if
stevenjb
2017/05/15 21:45:09
Done (assert placed in valueChanged where it is ea
| |
60 else | 69 else |
61 newValue = sliderValue; | 70 newValue = sliderValue; |
62 | 71 |
63 this.set('pref.value', newValue); | 72 this.set('pref.value', newValue); |
64 }, | 73 }, |
65 | 74 |
66 /** @private */ | 75 /** @private */ |
67 computeDisableSlider_: function() { | 76 computeDisableSlider_: function() { |
68 return this.disabled || this.isPrefEnforced(); | 77 return this.disabled || this.isPrefEnforced(); |
69 }, | 78 }, |
70 | 79 |
71 /** | 80 /** |
72 * Updates the knob position when |pref.value| changes. If the knob is still | 81 * Updates the knob position when |pref.value| changes. If the knob is still |
73 * being dragged, this instead forces |pref.value| back to the current | 82 * being dragged, this instead forces |pref.value| back to the current |
74 * position. | 83 * position. |
75 * @private | 84 * @private |
76 */ | 85 */ |
77 valueChanged_: function() { | 86 valueChanged_: function() { |
78 // If |tickValues| is empty, simply set current value to the slider. | 87 // If |tickValues| is empty, simply set current value to the slider. |
79 if (this.tickValues.length == 0) { | 88 if (this.tickValues.length == 0) { |
80 this.$.slider.value = this.pref.value; | 89 if (this.scale) { |
michaelpg
2017/05/15 19:53:18
in the same vein as afakhry's chomment, this may b
stevenjb
2017/05/15 21:45:09
Set default scale to 1.
| |
90 this.$.slider.value = | |
91 /** @type {number} */ (this.pref.value) * this.scale; | |
92 } else { | |
93 this.$.slider.value = this.pref.value; | |
94 } | |
81 return; | 95 return; |
82 } | 96 } |
83 | 97 |
84 // First update the slider settings if |tickValues| was set. | 98 // First update the slider settings if |tickValues| was set. |
85 var numTicks = Math.max(1, this.tickValues.length); | 99 var numTicks = Math.max(1, this.tickValues.length); |
86 this.$.slider.max = numTicks - 1; | 100 this.$.slider.max = numTicks - 1; |
87 // Limit the number of ticks to 10 to keep the slider from looking too busy. | 101 // Limit the number of ticks to 10 to keep the slider from looking too busy. |
88 /** @const */ var MAX_TICKS = 10; | 102 /** @const */ var MAX_TICKS = 10; |
89 this.$.slider.snaps = numTicks < MAX_TICKS; | 103 this.$.slider.snaps = numTicks < MAX_TICKS; |
90 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; | 104 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 if (difference < minDifference) { | 144 if (difference < minDifference) { |
131 closestIndex = i; | 145 closestIndex = i; |
132 minDifference = difference; | 146 minDifference = difference; |
133 } | 147 } |
134 } | 148 } |
135 | 149 |
136 assert(typeof closestIndex != 'undefined'); | 150 assert(typeof closestIndex != 'undefined'); |
137 return closestIndex; | 151 return closestIndex; |
138 }, | 152 }, |
139 }); | 153 }); |
OLD | NEW |