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 * |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 /** @type {!Array<number>} Values corresponding to each tick. */ | 24 /** @type {!Array<number>} Values corresponding to each tick. */ |
25 tickValues: Array, | 25 tickValues: Array, |
26 | 26 |
27 disabled: { | 27 disabled: { |
28 type: Boolean, | 28 type: Boolean, |
29 value: false, | 29 value: false, |
30 reflectToAttribute: true, | 30 reflectToAttribute: true, |
31 }, | 31 }, |
32 | 32 |
33 snaps: { | 33 labelMin: String, |
34 type: Boolean, | |
35 value: false, | |
36 }, | |
37 | 34 |
38 maxMarkers: Number, | 35 labelMax: String, |
39 }, | 36 }, |
40 | 37 |
41 observers: [ | 38 observers: [ |
42 'valueChanged_(value, tickValues.*)', | 39 'valueChanged_(value, tickValues.*)', |
43 ], | 40 ], |
44 | 41 |
45 /** | 42 /** |
46 * Sets the |value| property to the value corresponding to the knob position | 43 * Sets the |value| property to the value corresponding to the knob position |
47 * after a user action. | 44 * after a user action. |
48 * @private | 45 * @private |
49 */ | 46 */ |
50 onSliderChange_: function() { | 47 onSliderChanged_: function() { |
51 this.value = this.tickValues[this.$.slider.immediateValue]; | 48 if (this.tickValues && this.tickValues.length > 0) |
| 49 this.value = this.tickValues[this.$.slider.immediateValue]; |
52 }, | 50 }, |
53 | 51 |
54 /** | 52 /** |
55 * Updates the knob position when |value| changes. If the knob is still being | 53 * Updates the knob position when |value| changes. If the knob is still being |
56 * dragged, this instead forces |value| back to the current position. | 54 * dragged, this instead forces |value| back to the current position. |
57 * @private | 55 * @private |
58 */ | 56 */ |
59 valueChanged_: function() { | 57 valueChanged_: function() { |
60 // First update the slider settings if |tickValues| was set. | 58 // First update the slider settings if |tickValues| was set. |
61 this.$.slider.max = this.tickValues.length - 1; | 59 let numTicks = Math.max(1, this.tickValues.length); |
| 60 this.$.slider.max = numTicks - 1; |
| 61 // Limit the number of ticks to 10 to keep the slider from looking too busy. |
| 62 /** @const */ var MAX_TICKS = 10; |
| 63 this.$.slider.snaps = numTicks < MAX_TICKS; |
| 64 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; |
62 | 65 |
63 if (this.$.slider.dragging && | 66 if (this.$.slider.dragging && this.tickValues.length > 0 && |
64 this.value != this.tickValues[this.$.slider.immediateValue]) { | 67 this.value != this.tickValues[this.$.slider.immediateValue]) { |
65 // The value changed outside cr-slider but we're still holding the knob, | 68 // The value changed outside cr-slider but we're still holding the knob, |
66 // so set the value back to where the knob was. | 69 // so set the value back to where the knob was. |
67 // Async so we don't confuse Polymer's data binding. | 70 // Async so we don't confuse Polymer's data binding. |
68 this.async(function() { | 71 this.async(function() { |
69 this.value = this.tickValues[this.$.slider.immediateValue]; | 72 this.value = this.tickValues[this.$.slider.immediateValue]; |
70 }); | 73 }); |
71 return; | 74 return; |
72 } | 75 } |
73 | 76 |
74 // Convert from the public |value| to the slider index (where the knob | 77 // Convert from the public |value| to the slider index (where the knob |
75 // should be positioned on the slider). | 78 // should be positioned on the slider). |
76 var sliderIndex = this.tickValues.indexOf(this.value); | 79 var sliderIndex = |
| 80 this.tickValues.length > 0 ? this.tickValues.indexOf(this.value) : 0; |
77 if (sliderIndex == -1) { | 81 if (sliderIndex == -1) { |
78 // No exact match. | 82 // No exact match. |
79 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); | 83 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); |
80 } | 84 } |
81 this.$.slider.value = sliderIndex; | 85 this.$.slider.value = sliderIndex; |
82 }, | 86 }, |
83 | 87 |
84 /** | 88 /** |
85 * Returns the index of the item in |arr| closest to |value|. | 89 * Returns the index of the item in |arr| closest to |value|. |
86 * @param {!Array<number>} arr | 90 * @param {!Array<number>} arr |
87 * @param {number} value | 91 * @param {number} value |
88 * @return {number} | 92 * @return {number} |
89 * @private | 93 * @private |
90 */ | 94 */ |
91 findNearestIndex_: function(arr, value) { | 95 findNearestIndex_: function(arr, value) { |
92 var closestIndex; | 96 var closestIndex; |
93 var minDifference = Number.MAX_VALUE; | 97 var minDifference = Number.MAX_VALUE; |
94 for (var i = 0; i < arr.length; i++) { | 98 for (var i = 0; i < arr.length; i++) { |
95 var difference = Math.abs(arr[i] - value); | 99 var difference = Math.abs(arr[i] - value); |
96 if (difference < minDifference) { | 100 if (difference < minDifference) { |
97 closestIndex = i; | 101 closestIndex = i; |
98 minDifference = difference; | 102 minDifference = difference; |
99 } | 103 } |
100 } | 104 } |
101 | 105 |
102 assert(typeof closestIndex != 'undefined'); | 106 assert(typeof closestIndex != 'undefined'); |
103 return closestIndex; | 107 return closestIndex; |
104 }, | 108 }, |
105 }); | 109 }); |
OLD | NEW |