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 * A scale factor used to support fractional pref values since paper-slider |
| 28 * only supports integers. This is not compatible with |tickValues|, |
| 29 * i.e. if |scale| is not 1 then |tickValues| must be empty. |
| 30 */ |
| 31 scale: { |
| 32 type: Number, |
| 33 value: 1, |
| 34 }, |
| 35 |
26 min: Number, | 36 min: Number, |
27 | 37 |
28 max: Number, | 38 max: Number, |
29 | 39 |
30 labelMin: String, | 40 labelMin: String, |
31 | 41 |
32 labelMax: String, | 42 labelMax: String, |
33 | 43 |
34 disabled: Boolean, | 44 disabled: Boolean, |
35 | 45 |
(...skipping 15 matching lines...) Expand all Loading... |
51 */ | 61 */ |
52 onSliderChanged_: function() { | 62 onSliderChanged_: function() { |
53 var sliderValue = isNaN(this.$.slider.immediateValue) | 63 var sliderValue = isNaN(this.$.slider.immediateValue) |
54 ? this.$.slider.value | 64 ? this.$.slider.value |
55 : this.$.slider.immediateValue; | 65 : this.$.slider.immediateValue; |
56 | 66 |
57 var newValue; | 67 var newValue; |
58 if (this.tickValues && this.tickValues.length > 0) | 68 if (this.tickValues && this.tickValues.length > 0) |
59 newValue = this.tickValues[sliderValue]; | 69 newValue = this.tickValues[sliderValue]; |
60 else | 70 else |
61 newValue = sliderValue; | 71 newValue = sliderValue / this.scale; |
62 | 72 |
63 this.set('pref.value', newValue); | 73 this.set('pref.value', newValue); |
64 }, | 74 }, |
65 | 75 |
66 /** @private */ | 76 /** @private */ |
67 computeDisableSlider_: function() { | 77 computeDisableSlider_: function() { |
68 return this.disabled || this.isPrefEnforced(); | 78 return this.disabled || this.isPrefEnforced(); |
69 }, | 79 }, |
70 | 80 |
71 /** | 81 /** |
72 * Updates the knob position when |pref.value| changes. If the knob is still | 82 * 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 | 83 * being dragged, this instead forces |pref.value| back to the current |
74 * position. | 84 * position. |
75 * @private | 85 * @private |
76 */ | 86 */ |
77 valueChanged_: function() { | 87 valueChanged_: function() { |
78 // If |tickValues| is empty, simply set current value to the slider. | 88 // If |tickValues| is empty, simply set current value to the slider. |
79 if (this.tickValues.length == 0) { | 89 if (this.tickValues.length == 0) { |
80 this.$.slider.value = this.pref.value; | 90 this.$.slider.value = |
| 91 /** @type {number} */ (this.pref.value) * this.scale; |
81 return; | 92 return; |
82 } | 93 } |
| 94 assert(this.scale == 1); |
83 | 95 |
84 // First update the slider settings if |tickValues| was set. | 96 // First update the slider settings if |tickValues| was set. |
85 var numTicks = Math.max(1, this.tickValues.length); | 97 var numTicks = Math.max(1, this.tickValues.length); |
86 this.$.slider.max = numTicks - 1; | 98 this.$.slider.max = numTicks - 1; |
87 // Limit the number of ticks to 10 to keep the slider from looking too busy. | 99 // Limit the number of ticks to 10 to keep the slider from looking too busy. |
88 /** @const */ var MAX_TICKS = 10; | 100 /** @const */ var MAX_TICKS = 10; |
89 this.$.slider.snaps = numTicks < MAX_TICKS; | 101 this.$.slider.snaps = numTicks < MAX_TICKS; |
90 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; | 102 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; |
91 | 103 |
92 if (this.$.slider.dragging && this.tickValues.length > 0 && | 104 if (this.$.slider.dragging && this.tickValues.length > 0 && |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if (difference < minDifference) { | 142 if (difference < minDifference) { |
131 closestIndex = i; | 143 closestIndex = i; |
132 minDifference = difference; | 144 minDifference = difference; |
133 } | 145 } |
134 } | 146 } |
135 | 147 |
136 assert(typeof closestIndex != 'undefined'); | 148 assert(typeof closestIndex != 'undefined'); |
137 return closestIndex; | 149 return closestIndex; |
138 }, | 150 }, |
139 }); | 151 }); |
OLD | NEW |