Chromium Code Reviews| 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 this.$.slider.snaps = numTicks < 10; | |
| 62 this.$.slider.maxMarkers = numTicks < 10 ? numTicks : 0; | |
| 62 | 63 |
| 63 if (this.$.slider.dragging && | 64 if (this.$.slider.dragging && this.tickValues.length > 0 && |
|
dschuyler
2016/12/22 02:36:35
I'm not seeing why the this.tickValues.length > 0
stevenjb
2016/12/22 02:55:25
It can happen briefly in display.js:157 when the l
| |
| 64 this.value != this.tickValues[this.$.slider.immediateValue]) { | 65 this.value != this.tickValues[this.$.slider.immediateValue]) { |
| 65 // The value changed outside cr-slider but we're still holding the knob, | 66 // The value changed outside cr-slider but we're still holding the knob, |
| 66 // so set the value back to where the knob was. | 67 // so set the value back to where the knob was. |
| 67 // Async so we don't confuse Polymer's data binding. | 68 // Async so we don't confuse Polymer's data binding. |
| 68 this.async(function() { | 69 this.async(function() { |
| 69 this.value = this.tickValues[this.$.slider.immediateValue]; | 70 this.value = this.tickValues[this.$.slider.immediateValue]; |
| 70 }); | 71 }); |
| 71 return; | 72 return; |
| 72 } | 73 } |
| 73 | 74 |
| 75 // If |tickValues| is set explicitly before |value| is set, this might | |
| 76 // get called with an undefined value. | |
|
dschuyler
2016/12/22 02:36:35
Can the observer on line 39 be altered to account
stevenjb
2016/12/22 02:55:25
I'm not entirely sure why this gets called with va
michaelpg
2016/12/22 07:31:32
i thought multi-property observers are called when
stevenjb
2016/12/22 20:42:18
Yeah, you're right, this really shouldn't be happe
| |
| 77 if (this.value === undefined) | |
| 78 return; | |
| 79 | |
| 74 // Convert from the public |value| to the slider index (where the knob | 80 // Convert from the public |value| to the slider index (where the knob |
| 75 // should be positioned on the slider). | 81 // should be positioned on the slider). |
| 76 var sliderIndex = this.tickValues.indexOf(this.value); | 82 var sliderIndex = |
| 83 this.tickValues.length > 0 ? this.tickValues.indexOf(this.value) : 0; | |
| 77 if (sliderIndex == -1) { | 84 if (sliderIndex == -1) { |
| 78 // No exact match. | 85 // No exact match. |
| 79 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); | 86 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); |
| 80 } | 87 } |
| 81 this.$.slider.value = sliderIndex; | 88 this.$.slider.value = sliderIndex; |
| 82 }, | 89 }, |
| 83 | 90 |
| 84 /** | 91 /** |
| 85 * Returns the index of the item in |arr| closest to |value|. | 92 * Returns the index of the item in |arr| closest to |value|. |
| 86 * @param {!Array<number>} arr | 93 * @param {!Array<number>} arr |
| 87 * @param {number} value | 94 * @param {number} value |
| 88 * @return {number} | 95 * @return {number} |
| 89 * @private | 96 * @private |
| 90 */ | 97 */ |
| 91 findNearestIndex_: function(arr, value) { | 98 findNearestIndex_: function(arr, value) { |
| 92 var closestIndex; | 99 var closestIndex; |
| 93 var minDifference = Number.MAX_VALUE; | 100 var minDifference = Number.MAX_VALUE; |
| 94 for (var i = 0; i < arr.length; i++) { | 101 for (var i = 0; i < arr.length; i++) { |
| 95 var difference = Math.abs(arr[i] - value); | 102 var difference = Math.abs(arr[i] - value); |
| 96 if (difference < minDifference) { | 103 if (difference < minDifference) { |
| 97 closestIndex = i; | 104 closestIndex = i; |
| 98 minDifference = difference; | 105 minDifference = difference; |
| 99 } | 106 } |
| 100 } | 107 } |
| 101 | 108 |
| 102 assert(typeof closestIndex != 'undefined'); | 109 assert(typeof closestIndex != 'undefined'); |
| 103 return closestIndex; | 110 return closestIndex; |
| 104 }, | 111 }, |
| 105 }); | 112 }); |
| OLD | NEW |