OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 (function() { | 5 (function() { |
6 | 6 |
7 /** | 7 /** |
8 * @fileoverview | 8 * @fileoverview |
9 * night-light-slider is used to set the custom automatic schedule of the | 9 * night-light-slider is used to set the custom automatic schedule of the |
10 * Night Light feature, so that users can set their desired start and end | 10 * Night Light feature, so that users can set their desired start and end |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 'right': 'onRightKey_', | 49 'right': 'onRightKey_', |
50 }, | 50 }, |
51 | 51 |
52 /** | 52 /** |
53 * The object currently being dragged. Either the start or end knobs. | 53 * The object currently being dragged. Either the start or end knobs. |
54 * @type {?Object} | 54 * @type {?Object} |
55 * @private | 55 * @private |
56 */ | 56 */ |
57 dragObject_: null, | 57 dragObject_: null, |
58 | 58 |
59 /** | |
60 * The styles of slider legends determining their absolute left position in | |
61 * both LTR and RTL locales. | |
62 * @type {Array} | |
63 * @private | |
64 */ | |
65 legendsStyles_: [ | |
66 // The beginning 6:00 PM. | |
67 {ltr: 'left: 0%;', rtl: 'left: 100%'}, | |
68 // 12:00 AM. | |
69 {ltr: 'left: 25%;', rtl: 'left: 75%'}, | |
70 // 6:00 AM. | |
71 {ltr: 'left: 50%;', rtl: 'left: 50%'}, | |
72 // 12:00 PM. | |
73 {ltr: 'left: 75%;', rtl: 'left: 25%'}, | |
74 // The ending 6:00 PM. | |
75 {ltr: 'left: 100%;', rtl: 'left: 0%'}, | |
76 ], | |
77 | |
59 /** @override */ | 78 /** @override */ |
60 ready: function() { | 79 ready: function() { |
61 // Build the legend markers. | 80 // Build the legend markers. |
62 var markersContainer = this.$.markersContainer; | 81 var markersContainer = this.$.markersContainer; |
63 var width = markersContainer.offsetWidth; | 82 var width = markersContainer.offsetWidth; |
64 for (var i = 0; i <= HOURS_PER_DAY; ++i) { | 83 for (var i = 0; i <= HOURS_PER_DAY; ++i) { |
65 var marker = document.createElement('div'); | 84 var marker = document.createElement('div'); |
66 marker.className = 'markers'; | 85 marker.className = 'markers'; |
67 markersContainer.appendChild(marker); | 86 markersContainer.appendChild(marker); |
68 marker.style.left = (i * 100 / HOURS_PER_DAY) + '%'; | 87 marker.style.left = (i * 100 / HOURS_PER_DAY) + '%'; |
69 } | 88 } |
70 | 89 |
71 this.async(function() { | 90 this.async(function() { |
72 // Read the initial prefs values and refresh the slider. | 91 // Read the initial prefs values and refresh the slider. |
73 this.customTimesChanged_(); | 92 this.customTimesChanged_(); |
74 }); | 93 }); |
75 }, | 94 }, |
76 | 95 |
77 /** | 96 /** |
97 * @return {boolean} True if window is in a right-to-left locale. | |
98 * @private | |
99 */ | |
100 isRTL_: function() { | |
101 return window.getComputedStyle(document.firstElementChild).direction === | |
102 'rtl'; | |
103 }, | |
104 | |
105 /** | |
106 * Gets the style of legend div determining its absolute left position. | |
107 * @param {number} index The index of the div in its parent #legendContainer. | |
108 * @return {string} the CSS style of the legend div. | |
109 * @private | |
110 */ | |
111 getLegendStyle_: function(index) { | |
112 var style = this.legendsStyles_[index]; | |
113 return this.isRTL_() ? style.rtl : style.ltr; | |
stevenjb
2017/06/22 20:23:42
Any particular reason not to pass 0-100 to this an
afakhry
2017/06/22 21:01:16
No reason, other than I didn't want to do the calc
| |
114 }, | |
115 | |
116 /** | |
78 * Expands or un-expands the knob being dragged along with its corresponding | 117 * Expands or un-expands the knob being dragged along with its corresponding |
79 * label bubble. | 118 * label bubble. |
80 * @param {boolean} expand True to expand, and false to un-expand. | 119 * @param {boolean} expand True to expand, and false to un-expand. |
81 * @private | 120 * @private |
82 */ | 121 */ |
83 setExpanded_: function(expand) { | 122 setExpanded_: function(expand) { |
84 var knob = this.$.startKnob; | 123 var knob = this.$.startKnob; |
85 var label = this.$.startLabel; | 124 var label = this.$.startLabel; |
86 if (this.dragObject_ == this.$.endKnob) { | 125 if (this.dragObject_ == this.$.endKnob) { |
87 knob = this.$.endKnob; | 126 knob = this.$.endKnob; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 doKnobTracking_: function(event) { | 202 doKnobTracking_: function(event) { |
164 var deltaRatio = Math.abs(event.detail.ddx) / this.$.sliderBar.offsetWidth; | 203 var deltaRatio = Math.abs(event.detail.ddx) / this.$.sliderBar.offsetWidth; |
165 var deltaMinutes = Math.floor(deltaRatio * TOTAL_MINUTES_PER_DAY); | 204 var deltaMinutes = Math.floor(deltaRatio * TOTAL_MINUTES_PER_DAY); |
166 if (deltaMinutes <= 0) | 205 if (deltaMinutes <= 0) |
167 return; | 206 return; |
168 | 207 |
169 var knobPref = this.dragObject_ == this.$.startKnob ? | 208 var knobPref = this.dragObject_ == this.$.startKnob ? |
170 'ash.night_light.custom_start_time' : | 209 'ash.night_light.custom_start_time' : |
171 'ash.night_light.custom_end_time'; | 210 'ash.night_light.custom_end_time'; |
172 | 211 |
173 if (event.detail.ddx > 0) { | 212 var ddx = this.isRTL_() ? (-1 * event.detail.ddx) : event.detail.ddx; |
stevenjb
2017/06/22 20:23:43
nit: this.isRTL_() ? event.detail.ddx * -1 : event
afakhry
2017/06/22 21:01:16
Done.
| |
213 if (ddx > 0) { | |
174 // Increment the knob's pref by the amount of deltaMinutes. | 214 // Increment the knob's pref by the amount of deltaMinutes. |
175 this.incrementPref_(knobPref, deltaMinutes); | 215 this.incrementPref_(knobPref, deltaMinutes); |
176 } else { | 216 } else { |
177 // Decrement the knob's pref by the amount of deltaMinutes. | 217 // Decrement the knob's pref by the amount of deltaMinutes. |
178 this.decrementPref_(knobPref, deltaMinutes); | 218 this.decrementPref_(knobPref, deltaMinutes); |
179 } | 219 } |
180 }, | 220 }, |
181 | 221 |
182 /** | 222 /** |
183 * Ends the dragging. | 223 * Ends the dragging. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 | 304 |
265 if (ratio == 0) { | 305 if (ratio == 0) { |
266 // If the ratio is 0, then there are two possibilities: | 306 // If the ratio is 0, then there are two possibilities: |
267 // - The knob time is 6:00 PM on the left side of the slider. | 307 // - The knob time is 6:00 PM on the left side of the slider. |
268 // - The knob time is 6:00 PM on the right side of the slider. | 308 // - The knob time is 6:00 PM on the right side of the slider. |
269 // We need to check the current knob offset ratio to determine which case | 309 // We need to check the current knob offset ratio to determine which case |
270 // it is. | 310 // it is. |
271 var currentKnobRatio = this.getKnobRatio_(knob); | 311 var currentKnobRatio = this.getKnobRatio_(knob); |
272 ratio = currentKnobRatio > 0.5 ? 1.0 : 0.0; | 312 ratio = currentKnobRatio > 0.5 ? 1.0 : 0.0; |
273 } | 313 } |
274 | 314 ratio = this.isRTL_() ? (1.0 - ratio) : ratio; |
275 knob.style.left = (ratio * this.$.sliderBar.offsetWidth) + 'px'; | 315 knob.style.left = (ratio * this.$.sliderBar.offsetWidth) + 'px'; |
276 }, | 316 }, |
277 | 317 |
278 /** | 318 /** |
279 * Refreshes elements of the slider other than the knobs (the label bubbles, | 319 * Refreshes elements of the slider other than the knobs (the label bubbles, |
280 * and the progress bar). | 320 * and the progress bar). |
281 * @private | 321 * @private |
282 */ | 322 */ |
283 refresh_: function() { | 323 refresh_: function() { |
284 var endKnob = this.$.endKnob; | 324 var endKnob = this.$.endKnob; |
285 var startKnob = this.$.startKnob; | 325 var startKnob = this.$.startKnob; |
286 var startProgress = this.$.startProgress; | 326 var startProgress = this.$.startProgress; |
287 var endProgress = this.$.endProgress; | 327 var endProgress = this.$.endProgress; |
288 var startLabel = this.$.startLabel; | 328 var startLabel = this.$.startLabel; |
289 var endLabel = this.$.endLabel; | 329 var endLabel = this.$.endLabel; |
290 | 330 |
291 // The label bubbles have the same left coordinates as their corresponding | 331 // The label bubbles have the same left coordinates as their corresponding |
292 // knobs. | 332 // knobs. |
293 startLabel.style.left = startKnob.style.left; | 333 startLabel.style.left = startKnob.style.left; |
294 endLabel.style.left = endKnob.style.left; | 334 endLabel.style.left = endKnob.style.left; |
295 | 335 |
336 if (this.isRTL_()) { | |
337 // In RTL locales, the relative positions of the knobs are flipped for the | |
338 // purpose of calculating the styles of the progress bars below. | |
339 startKnob = endKnob; | |
340 endKnob = this.$.startKnob; | |
341 startProgress = endProgress; | |
342 endProgress = this.$.startProgress; | |
343 } | |
stevenjb
2017/06/22 20:23:42
nit: I think this would be more clear as:
var rtl
afakhry
2017/06/22 21:01:16
Done.
| |
344 | |
296 // The end progress bar starts from either the start knob or the start of | 345 // The end progress bar starts from either the start knob or the start of |
297 // the slider (whichever is to its left) and ends at the end knob. | 346 // the slider (whichever is to its left) and ends at the end knob. |
298 var endProgressLeft = startKnob.offsetLeft >= endKnob.offsetLeft ? | 347 var endProgressLeft = startKnob.offsetLeft >= endKnob.offsetLeft ? |
299 '0px' : | 348 '0px' : |
300 startKnob.style.left; | 349 startKnob.style.left; |
301 endProgress.style.left = endProgressLeft; | 350 endProgress.style.left = endProgressLeft; |
302 endProgress.style.width = | 351 endProgress.style.width = |
303 (parseFloat(endKnob.style.left) - parseFloat(endProgressLeft)) + 'px'; | 352 (parseFloat(endKnob.style.left) - parseFloat(endProgressLeft)) + 'px'; |
304 | 353 |
305 // The start progress bar starts at the start knob, and ends at either the | 354 // The start progress bar starts at the start knob, and ends at either the |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 /** | 485 /** |
437 * Handles the 'left' key event. | 486 * Handles the 'left' key event. |
438 * @private | 487 * @private |
439 */ | 488 */ |
440 onLeftKey_: function(e) { | 489 onLeftKey_: function(e) { |
441 e.preventDefault(); | 490 e.preventDefault(); |
442 var knobPref = this.getFocusedKnobPrefPathIfAny_(); | 491 var knobPref = this.getFocusedKnobPrefPathIfAny_(); |
443 if (!knobPref) | 492 if (!knobPref) |
444 return; | 493 return; |
445 | 494 |
446 this.decrementPref_(knobPref, 1); | 495 if (this.isRTL_()) |
496 this.incrementPref_(knobPref, 1); | |
497 else | |
498 this.decrementPref_(knobPref, 1); | |
447 }, | 499 }, |
448 | 500 |
449 /** | 501 /** |
450 * Handles the 'right' key event. | 502 * Handles the 'right' key event. |
451 * @private | 503 * @private |
452 */ | 504 */ |
453 onRightKey_: function(e) { | 505 onRightKey_: function(e) { |
454 e.preventDefault(); | 506 e.preventDefault(); |
455 var knobPref = this.getFocusedKnobPrefPathIfAny_(); | 507 var knobPref = this.getFocusedKnobPrefPathIfAny_(); |
456 if (!knobPref) | 508 if (!knobPref) |
457 return; | 509 return; |
458 | 510 |
459 this.incrementPref_(knobPref, 1); | 511 if (this.isRTL_()) |
512 this.decrementPref_(knobPref, 1); | |
513 else | |
514 this.incrementPref_(knobPref, 1); | |
460 }, | 515 }, |
461 | 516 |
462 /** | 517 /** |
463 * @return {boolean} Whether either of the two knobs is focused. | 518 * @return {boolean} Whether either of the two knobs is focused. |
464 * @private | 519 * @private |
465 */ | 520 */ |
466 isEitherKnobFocused_: function() { | 521 isEitherKnobFocused_: function() { |
467 var activeElement = this.shadowRoot.activeElement; | 522 var activeElement = this.shadowRoot.activeElement; |
468 return activeElement == this.$.startKnob || activeElement == this.$.endKnob; | 523 return activeElement == this.$.startKnob || activeElement == this.$.endKnob; |
469 }, | 524 }, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 _focusedChanged: function(receivedFocusFromKeyboard) { | 571 _focusedChanged: function(receivedFocusFromKeyboard) { |
517 // Overrides the _focusedChanged() from the PaperInkyFocusBehavior so that | 572 // Overrides the _focusedChanged() from the PaperInkyFocusBehavior so that |
518 // it does nothing. This function is called only once for the entire | 573 // it does nothing. This function is called only once for the entire |
519 // night-light-slider element even when focus is moved between the two | 574 // night-light-slider element even when focus is moved between the two |
520 // knobs. This doesn't allow us to decide on which knob the ripple will be | 575 // knobs. This doesn't allow us to decide on which knob the ripple will be |
521 // created. Hence we handle focus and blur explicitly above. | 576 // created. Hence we handle focus and blur explicitly above. |
522 } | 577 } |
523 }); | 578 }); |
524 | 579 |
525 })(); | 580 })(); |
OLD | NEW |