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 * |
| 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 /** @type {!chrome.settingsPrivate.PrefObject} */ |
|
Dan Beam
2017/03/11 01:08:27
why are you making this less generic / using setti
Dan Beam
2017/03/11 01:30:51
talked with dschuyler@ offline
let's just move <c
dschuyler
2017/03/11 01:30:55
This will move into the settings/controls director
| |
| 19 value: { | 21 pref: Object, |
| 20 type: Number, | |
| 21 notify: true, | |
| 22 }, | |
| 23 | 22 |
| 24 /** @type {!Array<number>} Values corresponding to each tick. */ | 23 /** @type {!Array<number>} Values corresponding to each tick. */ |
| 25 tickValues: {type: Array, value: []}, | 24 tickValues: {type: Array, value: []}, |
| 26 | 25 |
| 27 disabled: { | |
| 28 type: Boolean, | |
| 29 value: false, | |
| 30 reflectToAttribute: true, | |
| 31 }, | |
| 32 | |
| 33 min: Number, | 26 min: Number, |
| 34 | 27 |
| 35 max: Number, | 28 max: Number, |
| 36 | 29 |
| 37 labelMin: String, | 30 labelMin: String, |
| 38 | 31 |
| 39 labelMax: String, | 32 labelMax: String, |
| 33 | |
| 34 /** @private */ | |
| 35 disableSlider_: { | |
| 36 computed: 'computeDisableSlider_(pref.*)', | |
| 37 type: Boolean, | |
| 38 }, | |
| 40 }, | 39 }, |
| 41 | 40 |
| 42 observers: [ | 41 observers: [ |
| 43 'valueChanged_(value, tickValues.*)', | 42 'valueChanged_(pref.*, tickValues.*)', |
| 44 ], | 43 ], |
| 45 | 44 |
| 46 /** | 45 /** |
| 47 * Sets the |value| property to the value corresponding to the knob position | 46 * Sets the |pref.value| property to the value corresponding to the knob |
| 48 * after a user action. | 47 * position after a user action. |
| 49 * @private | 48 * @private |
| 50 */ | 49 */ |
| 51 onSliderChanged_: function() { | 50 onSliderChanged_: function() { |
| 51 var newValue; | |
| 52 if (this.tickValues && this.tickValues.length > 0) | 52 if (this.tickValues && this.tickValues.length > 0) |
| 53 this.value = this.tickValues[this.$.slider.immediateValue]; | 53 newValue = this.tickValues[this.$.slider.immediateValue]; |
| 54 else | 54 else |
| 55 this.value = this.$.slider.immediateValue; | 55 newValue = this.$.slider.immediateValue; |
| 56 | |
| 57 this.set('pref.value', newValue); | |
| 58 }, | |
| 59 | |
| 60 /** @private */ | |
| 61 computeDisableSlider_: function() { | |
| 62 return this.hasAttribute('disabled') || this.isPrefEnforced(); | |
| 56 }, | 63 }, |
| 57 | 64 |
| 58 /** | 65 /** |
| 59 * Updates the knob position when |value| changes. If the knob is still being | 66 * Updates the knob position when |pref.value| changes. If the knob is still |
| 60 * dragged, this instead forces |value| back to the current position. | 67 * being dragged, this instead forces |pref.value| back to the current |
| 68 * position. | |
| 61 * @private | 69 * @private |
| 62 */ | 70 */ |
| 63 valueChanged_: function() { | 71 valueChanged_: function() { |
| 64 // If |tickValues| is empty, simply set current value to the slider. | 72 // If |tickValues| is empty, simply set current value to the slider. |
| 65 if (this.tickValues.length == 0) { | 73 if (this.tickValues.length == 0) { |
| 66 this.$.slider.value = this.value; | 74 this.$.slider.value = this.pref.value; |
| 67 return; | 75 return; |
| 68 } | 76 } |
| 69 | 77 |
| 70 // First update the slider settings if |tickValues| was set. | 78 // First update the slider settings if |tickValues| was set. |
| 71 var numTicks = Math.max(1, this.tickValues.length); | 79 var numTicks = Math.max(1, this.tickValues.length); |
| 72 this.$.slider.max = numTicks - 1; | 80 this.$.slider.max = numTicks - 1; |
| 73 // Limit the number of ticks to 10 to keep the slider from looking too busy. | 81 // Limit the number of ticks to 10 to keep the slider from looking too busy. |
| 74 /** @const */ var MAX_TICKS = 10; | 82 /** @const */ var MAX_TICKS = 10; |
| 75 this.$.slider.snaps = numTicks < MAX_TICKS; | 83 this.$.slider.snaps = numTicks < MAX_TICKS; |
| 76 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; | 84 this.$.slider.maxMarkers = numTicks < MAX_TICKS ? numTicks : 0; |
| 77 | 85 |
| 78 if (this.$.slider.dragging && this.tickValues.length > 0 && | 86 if (this.$.slider.dragging && this.tickValues.length > 0 && |
| 79 this.value != this.tickValues[this.$.slider.immediateValue]) { | 87 this.pref.value != this.tickValues[this.$.slider.immediateValue]) { |
| 80 // The value changed outside cr-slider but we're still holding the knob, | 88 // The value changed outside cr-slider but we're still holding the knob, |
| 81 // so set the value back to where the knob was. | 89 // so set the value back to where the knob was. |
| 82 // Async so we don't confuse Polymer's data binding. | 90 // Async so we don't confuse Polymer's data binding. |
| 83 this.async(function() { | 91 this.async(function() { |
| 84 this.value = this.tickValues[this.$.slider.immediateValue]; | 92 var newValue = this.tickValues[this.$.slider.immediateValue]; |
| 93 this.set('pref.value', newValue); | |
| 85 }); | 94 }); |
| 86 return; | 95 return; |
| 87 } | 96 } |
| 88 | 97 |
| 89 // Convert from the public |value| to the slider index (where the knob | 98 // Convert from the public |value| to the slider index (where the knob |
| 90 // should be positioned on the slider). | 99 // should be positioned on the slider). |
| 91 var sliderIndex = | 100 var sliderIndex = this.tickValues.length > 0 ? |
| 92 this.tickValues.length > 0 ? this.tickValues.indexOf(this.value) : 0; | 101 this.tickValues.indexOf(/** @type {number} */ (this.pref.value)) : |
| 102 0; | |
| 93 if (sliderIndex == -1) { | 103 if (sliderIndex == -1) { |
| 94 // No exact match. | 104 // No exact match. |
| 95 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); | 105 sliderIndex = this.findNearestIndex_( |
| 106 this.tickValues, | |
| 107 /** @type {number} */ (this.pref.value)); | |
| 96 } | 108 } |
| 97 this.$.slider.value = sliderIndex; | 109 this.$.slider.value = sliderIndex; |
| 98 }, | 110 }, |
| 99 | 111 |
| 100 /** | 112 /** |
| 101 * Returns the index of the item in |arr| closest to |value|. | 113 * Returns the index of the item in |arr| closest to |value|. |
| 102 * @param {!Array<number>} arr | 114 * @param {!Array<number>} arr |
| 103 * @param {number} value | 115 * @param {number} value |
| 104 * @return {number} | 116 * @return {number} |
| 105 * @private | 117 * @private |
| 106 */ | 118 */ |
| 107 findNearestIndex_: function(arr, value) { | 119 findNearestIndex_: function(arr, value) { |
| 108 var closestIndex; | 120 var closestIndex; |
| 109 var minDifference = Number.MAX_VALUE; | 121 var minDifference = Number.MAX_VALUE; |
| 110 for (var i = 0; i < arr.length; i++) { | 122 for (var i = 0; i < arr.length; i++) { |
| 111 var difference = Math.abs(arr[i] - value); | 123 var difference = Math.abs(arr[i] - value); |
| 112 if (difference < minDifference) { | 124 if (difference < minDifference) { |
| 113 closestIndex = i; | 125 closestIndex = i; |
| 114 minDifference = difference; | 126 minDifference = difference; |
| 115 } | 127 } |
| 116 } | 128 } |
| 117 | 129 |
| 118 assert(typeof closestIndex != 'undefined'); | 130 assert(typeof closestIndex != 'undefined'); |
| 119 return closestIndex; | 131 return closestIndex; |
| 120 }, | 132 }, |
| 121 }); | 133 }); |
| OLD | NEW |