Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: ui/webui/resources/cr_elements/cr_slider/cr_slider.js

Issue 2587913007: MD Settings: cr-slider: Make display consistent and clean up. (Closed)
Patch Set: Feedback Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/webui/resources/cr_elements/cr_slider/cr_slider.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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;
dschuyler 2016/12/22 22:09:45 Please make a comment about 10 being an aesthetic
dschuyler 2016/12/22 22:17:34 Or maybe make a var like (which wouldn't need a co
stevenjb 2016/12/23 00:18:38 So many choices :) Going with a slightly konstant
62 this.$.slider.maxMarkers = numTicks < 10 ? numTicks : 0;
62 63
63 if (this.$.slider.dragging && 64 if (this.$.slider.dragging && this.tickValues.length > 0 &&
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
74 // Convert from the public |value| to the slider index (where the knob 75 // Convert from the public |value| to the slider index (where the knob
75 // should be positioned on the slider). 76 // should be positioned on the slider).
76 var sliderIndex = this.tickValues.indexOf(this.value); 77 var sliderIndex =
78 this.tickValues.length > 0 ? this.tickValues.indexOf(this.value) : 0;
77 if (sliderIndex == -1) { 79 if (sliderIndex == -1) {
78 // No exact match. 80 // No exact match.
79 sliderIndex = this.findNearestIndex_(this.tickValues, this.value); 81 sliderIndex = this.findNearestIndex_(this.tickValues, this.value);
80 } 82 }
81 this.$.slider.value = sliderIndex; 83 this.$.slider.value = sliderIndex;
82 }, 84 },
83 85
84 /** 86 /**
85 * Returns the index of the item in |arr| closest to |value|. 87 * Returns the index of the item in |arr| closest to |value|.
86 * @param {!Array<number>} arr 88 * @param {!Array<number>} arr
87 * @param {number} value 89 * @param {number} value
88 * @return {number} 90 * @return {number}
89 * @private 91 * @private
90 */ 92 */
91 findNearestIndex_: function(arr, value) { 93 findNearestIndex_: function(arr, value) {
92 var closestIndex; 94 var closestIndex;
93 var minDifference = Number.MAX_VALUE; 95 var minDifference = Number.MAX_VALUE;
94 for (var i = 0; i < arr.length; i++) { 96 for (var i = 0; i < arr.length; i++) {
95 var difference = Math.abs(arr[i] - value); 97 var difference = Math.abs(arr[i] - value);
96 if (difference < minDifference) { 98 if (difference < minDifference) {
97 closestIndex = i; 99 closestIndex = i;
98 minDifference = difference; 100 minDifference = difference;
99 } 101 }
100 } 102 }
101 103
102 assert(typeof closestIndex != 'undefined'); 104 assert(typeof closestIndex != 'undefined');
103 return closestIndex; 105 return closestIndex;
104 }, 106 },
105 }); 107 });
OLDNEW
« no previous file with comments | « ui/webui/resources/cr_elements/cr_slider/cr_slider.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698