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 * cr-slider wraps a paper-slider. It maps the slider's values from a linear UI | 7 * cr-slider wraps a paper-slider. It maps the slider's values from a linear UI |
8 * range to a range of real values. When |value| does not map exactly to a | 8 * range to a range of real values. When |value| does not map exactly to a |
9 * tick mark, it interpolates to the nearest tick. | 9 * 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: 'cr-slider', | 15 is: 'cr-slider', |
16 | 16 |
17 behaviors: [CrPolicyPrefBehavior], | |
18 | |
17 properties: { | 19 properties: { |
18 /** The value the slider represents and controls. */ | 20 pref: Object, |
19 value: { | |
20 type: Number, | |
21 notify: true, | |
22 }, | |
23 | 21 |
24 /** @type {!Array<number>} Values corresponding to each tick. */ | 22 /** @type {!Array<number>} Values corresponding to each tick. */ |
25 tickValues: {type: Array, value: []}, | 23 tickValues: {type: Array, value: []}, |
26 | 24 |
27 disabled: { | |
28 type: Boolean, | |
29 value: false, | |
30 reflectToAttribute: true, | |
31 }, | |
32 | |
33 min: Number, | 25 min: Number, |
34 | 26 |
35 max: Number, | 27 max: Number, |
36 | 28 |
37 labelMin: String, | 29 labelMin: String, |
38 | 30 |
39 labelMax: String, | 31 labelMax: String, |
40 }, | 32 }, |
41 | 33 |
42 observers: [ | 34 observers: [ |
43 'valueChanged_(value, tickValues.*)', | 35 'valueChanged_(pref.*, tickValues.*)', |
44 ], | 36 ], |
45 | 37 |
46 /** | 38 /** |
47 * Sets the |value| property to the value corresponding to the knob position | 39 * Sets the |pref.value| property to the value corresponding to the knob |
48 * after a user action. | 40 * position after a user action. |
49 * @private | 41 * @private |
50 */ | 42 */ |
51 onSliderChanged_: function() { | 43 onSliderChanged_: function() { |
44 var newValue; | |
52 if (this.tickValues && this.tickValues.length > 0) | 45 if (this.tickValues && this.tickValues.length > 0) |
53 this.value = this.tickValues[this.$.slider.immediateValue]; | 46 newValue = this.tickValues[this.$.slider.immediateValue]; |
54 else | 47 else |
55 this.value = this.$.slider.immediateValue; | 48 newValue = this.$.slider.immediateValue; |
49 | |
50 this.set('pref.value', newValue); | |
51 }, | |
52 | |
53 /** @private */ | |
54 shouldDisableSlider_: function() { | |
55 return this.disabled || this.isPrefEnforced(); | |
stevenjb
2017/03/07 23:11:48
Did you test this? I thought we needed to to use g
dschuyler
2017/03/08 21:50:11
or hasAttribute for boolean attributes.
| |
56 }, | 56 }, |
57 | 57 |
58 /** | 58 /** |
59 * Updates the knob position when |value| changes. If the knob is still being | 59 * Updates the knob position when |pref.value| changes. If the knob is still |
60 * dragged, this instead forces |value| back to the current position. | 60 * being dragged, this instead forces |pref.value| back to the current |
61 * position. | |
61 * @private | 62 * @private |
62 */ | 63 */ |
63 valueChanged_: function() { | 64 valueChanged_: function() { |
64 // If |tickValues| is empty, simply set current value to the slider. | 65 // If |tickValues| is empty, simply set current value to the slider. |
65 if (this.tickValues.length == 0) { | 66 if (this.tickValues.length == 0) { |
66 this.$.slider.value = this.value; | 67 this.$.slider.value = this.pref.value; |
67 return; | 68 return; |
68 } | 69 } |
69 | 70 |
70 // First update the slider settings if |tickValues| was set. | 71 // First update the slider settings if |tickValues| was set. |
71 var numTicks = Math.max(1, this.tickValues.length); | 72 var numTicks = Math.max(1, this.tickValues.length); |
72 this.$.slider.max = numTicks - 1; | 73 this.$.slider.max = numTicks - 1; |
73 // Limit the number of ticks to 10 to keep the slider from looking too busy. | 74 // Limit the number of ticks to 10 to keep the slider from looking too busy. |
74 /** @const */ var MAX_TICKS = 10; | 75 /** @const */ var MAX_TICKS = 10; |
75 this.$.slider.snaps = numTicks < MAX_TICKS; | 76 this.$.slider.snaps = numTicks < MAX_TICKS; |
76 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; | 77 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; |
77 | 78 |
78 if (this.$.slider.dragging && this.tickValues.length > 0 && | 79 if (this.$.slider.dragging && this.tickValues.length > 0 && |
79 this.value != this.tickValues[this.$.slider.immediateValue]) { | 80 this.pref.value != this.tickValues[this.$.slider.immediateValue]) { |
80 // The value changed outside cr-slider but we're still holding the knob, | 81 // The value changed outside cr-slider but we're still holding the knob, |
81 // so set the value back to where the knob was. | 82 // so set the value back to where the knob was. |
82 // Async so we don't confuse Polymer's data binding. | 83 // Async so we don't confuse Polymer's data binding. |
83 this.async(function() { | 84 this.async(function() { |
84 this.value = this.tickValues[this.$.slider.immediateValue]; | 85 var newValue = this.tickValues[this.$.slider.immediateValue]; |
86 this.set('pref.value', newValue); | |
85 }); | 87 }); |
86 return; | 88 return; |
87 } | 89 } |
88 | 90 |
89 // Convert from the public |value| to the slider index (where the knob | 91 // Convert from the public |value| to the slider index (where the knob |
90 // should be positioned on the slider). | 92 // should be positioned on the slider). |
91 var sliderIndex = | 93 var sliderIndex = this.tickValues.length > 0 ? |
92 this.tickValues.length > 0 ? this.tickValues.indexOf(this.value) : 0; | 94 this.tickValues.indexOf(this.pref.value) : |
95 0; | |
93 if (sliderIndex == -1) { | 96 if (sliderIndex == -1) { |
94 // No exact match. | 97 // No exact match. |
95 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); | 98 sliderIndex = this.findNearestIndex_(this.tickValues, this.pref.value); |
96 } | 99 } |
97 this.$.slider.value = sliderIndex; | 100 this.$.slider.value = sliderIndex; |
98 }, | 101 }, |
99 | 102 |
100 /** | 103 /** |
101 * Returns the index of the item in |arr| closest to |value|. | 104 * Returns the index of the item in |arr| closest to |value|. |
102 * @param {!Array<number>} arr | 105 * @param {!Array<number>} arr |
103 * @param {number} value | 106 * @param {number} value |
104 * @return {number} | 107 * @return {number} |
105 * @private | 108 * @private |
106 */ | 109 */ |
107 findNearestIndex_: function(arr, value) { | 110 findNearestIndex_: function(arr, value) { |
108 var closestIndex; | 111 var closestIndex; |
109 var minDifference = Number.MAX_VALUE; | 112 var minDifference = Number.MAX_VALUE; |
110 for (var i = 0; i < arr.length; i++) { | 113 for (var i = 0; i < arr.length; i++) { |
111 var difference = Math.abs(arr[i] - value); | 114 var difference = Math.abs(arr[i] - value); |
112 if (difference < minDifference) { | 115 if (difference < minDifference) { |
113 closestIndex = i; | 116 closestIndex = i; |
114 minDifference = difference; | 117 minDifference = difference; |
115 } | 118 } |
116 } | 119 } |
117 | 120 |
118 assert(typeof closestIndex != 'undefined'); | 121 assert(typeof closestIndex != 'undefined'); |
119 return closestIndex; | 122 return closestIndex; |
120 }, | 123 }, |
121 }); | 124 }); |
OLD | NEW |