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 19 matching lines...) Expand all Loading... |
30 * The start knob time as a string to be shown on the start label bubble. | 30 * The start knob time as a string to be shown on the start label bubble. |
31 * @private | 31 * @private |
32 */ | 32 */ |
33 startTime_: String, | 33 startTime_: String, |
34 | 34 |
35 /** | 35 /** |
36 * The end knob time as a string to be shown on the end label bubble. | 36 * The end knob time as a string to be shown on the end label bubble. |
37 * @private | 37 * @private |
38 */ | 38 */ |
39 endTime_: String, | 39 endTime_: String, |
| 40 |
| 41 /** |
| 42 * Whether the window is in RTL locales. |
| 43 * @private |
| 44 */ |
| 45 isRTL_: Boolean, |
40 }, | 46 }, |
41 | 47 |
42 observers: [ | 48 observers: [ |
43 'customTimesChanged_(prefs.ash.night_light.custom_start_time.*, ' + | 49 'customTimesChanged_(prefs.ash.night_light.custom_start_time.*, ' + |
44 'prefs.ash.night_light.custom_end_time.*)', | 50 'prefs.ash.night_light.custom_end_time.*)', |
45 ], | 51 ], |
46 | 52 |
47 keyBindings: { | 53 keyBindings: { |
48 'left': 'onLeftKey_', | 54 'left': 'onLeftKey_', |
49 'right': 'onRightKey_', | 55 'right': 'onRightKey_', |
50 }, | 56 }, |
51 | 57 |
52 /** | 58 /** |
53 * The object currently being dragged. Either the start or end knobs. | 59 * The object currently being dragged. Either the start or end knobs. |
54 * @type {?Object} | 60 * @type {?Object} |
55 * @private | 61 * @private |
56 */ | 62 */ |
57 dragObject_: null, | 63 dragObject_: null, |
58 | 64 |
59 /** @override */ | 65 /** @override */ |
| 66 attached: function() { |
| 67 this.isRTL_ = window.getComputedStyle(this).direction == 'rtl'; |
| 68 }, |
| 69 |
| 70 /** @override */ |
60 ready: function() { | 71 ready: function() { |
61 // Build the legend markers. | 72 // Build the legend markers. |
62 var markersContainer = this.$.markersContainer; | 73 var markersContainer = this.$.markersContainer; |
63 var width = markersContainer.offsetWidth; | 74 var width = markersContainer.offsetWidth; |
64 for (var i = 0; i <= HOURS_PER_DAY; ++i) { | 75 for (var i = 0; i <= HOURS_PER_DAY; ++i) { |
65 var marker = document.createElement('div'); | 76 var marker = document.createElement('div'); |
66 marker.className = 'markers'; | 77 marker.className = 'markers'; |
67 markersContainer.appendChild(marker); | 78 markersContainer.appendChild(marker); |
68 marker.style.left = (i * 100 / HOURS_PER_DAY) + '%'; | 79 marker.style.left = (i * 100 / HOURS_PER_DAY) + '%'; |
69 } | 80 } |
70 | 81 |
71 this.async(function() { | 82 this.async(function() { |
72 // Read the initial prefs values and refresh the slider. | 83 // Read the initial prefs values and refresh the slider. |
73 this.customTimesChanged_(); | 84 this.customTimesChanged_(); |
74 }); | 85 }); |
75 }, | 86 }, |
76 | 87 |
77 /** | 88 /** |
| 89 * Gets the style of legend div determining its absolute left position. |
| 90 * @param {number} percent The value of the div's left as a percent (0 - 100). |
| 91 * @param {boolean} isRTL whether window is in RTL locale. |
| 92 * @return {string} The CSS style of the legend div. |
| 93 * @private |
| 94 */ |
| 95 getLegendStyle_: function(percent, isRTL) { |
| 96 percent = isRTL ? 100 - percent : percent; |
| 97 return 'left: ' + percent + '%'; |
| 98 }, |
| 99 |
| 100 /** |
78 * Expands or un-expands the knob being dragged along with its corresponding | 101 * Expands or un-expands the knob being dragged along with its corresponding |
79 * label bubble. | 102 * label bubble. |
80 * @param {boolean} expand True to expand, and false to un-expand. | 103 * @param {boolean} expand True to expand, and false to un-expand. |
81 * @private | 104 * @private |
82 */ | 105 */ |
83 setExpanded_: function(expand) { | 106 setExpanded_: function(expand) { |
84 var knob = this.$.startKnob; | 107 var knob = this.$.startKnob; |
85 var label = this.$.startLabel; | 108 var label = this.$.startLabel; |
86 if (this.dragObject_ == this.$.endKnob) { | 109 if (this.dragObject_ == this.$.endKnob) { |
87 knob = this.$.endKnob; | 110 knob = this.$.endKnob; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 doKnobTracking_: function(event) { | 186 doKnobTracking_: function(event) { |
164 var deltaRatio = Math.abs(event.detail.ddx) / this.$.sliderBar.offsetWidth; | 187 var deltaRatio = Math.abs(event.detail.ddx) / this.$.sliderBar.offsetWidth; |
165 var deltaMinutes = Math.floor(deltaRatio * TOTAL_MINUTES_PER_DAY); | 188 var deltaMinutes = Math.floor(deltaRatio * TOTAL_MINUTES_PER_DAY); |
166 if (deltaMinutes <= 0) | 189 if (deltaMinutes <= 0) |
167 return; | 190 return; |
168 | 191 |
169 var knobPref = this.dragObject_ == this.$.startKnob ? | 192 var knobPref = this.dragObject_ == this.$.startKnob ? |
170 'ash.night_light.custom_start_time' : | 193 'ash.night_light.custom_start_time' : |
171 'ash.night_light.custom_end_time'; | 194 'ash.night_light.custom_end_time'; |
172 | 195 |
173 if (event.detail.ddx > 0) { | 196 var ddx = this.isRTL_ ? event.detail.ddx * -1 : event.detail.ddx; |
| 197 if (ddx > 0) { |
174 // Increment the knob's pref by the amount of deltaMinutes. | 198 // Increment the knob's pref by the amount of deltaMinutes. |
175 this.incrementPref_(knobPref, deltaMinutes); | 199 this.incrementPref_(knobPref, deltaMinutes); |
176 } else { | 200 } else { |
177 // Decrement the knob's pref by the amount of deltaMinutes. | 201 // Decrement the knob's pref by the amount of deltaMinutes. |
178 this.decrementPref_(knobPref, deltaMinutes); | 202 this.decrementPref_(knobPref, deltaMinutes); |
179 } | 203 } |
180 }, | 204 }, |
181 | 205 |
182 /** | 206 /** |
183 * Ends the dragging. | 207 * Ends the dragging. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 288 |
265 if (ratio == 0) { | 289 if (ratio == 0) { |
266 // If the ratio is 0, then there are two possibilities: | 290 // 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. | 291 // - 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. | 292 // - 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 | 293 // We need to check the current knob offset ratio to determine which case |
270 // it is. | 294 // it is. |
271 var currentKnobRatio = this.getKnobRatio_(knob); | 295 var currentKnobRatio = this.getKnobRatio_(knob); |
272 ratio = currentKnobRatio > 0.5 ? 1.0 : 0.0; | 296 ratio = currentKnobRatio > 0.5 ? 1.0 : 0.0; |
273 } | 297 } |
274 | 298 ratio = this.isRTL_ ? (1.0 - ratio) : ratio; |
275 knob.style.left = (ratio * this.$.sliderBar.offsetWidth) + 'px'; | 299 knob.style.left = (ratio * this.$.sliderBar.offsetWidth) + 'px'; |
276 }, | 300 }, |
277 | 301 |
278 /** | 302 /** |
279 * Refreshes elements of the slider other than the knobs (the label bubbles, | 303 * Refreshes elements of the slider other than the knobs (the label bubbles, |
280 * and the progress bar). | 304 * and the progress bar). |
281 * @private | 305 * @private |
282 */ | 306 */ |
283 refresh_: function() { | 307 refresh_: function() { |
284 var endKnob = this.$.endKnob; | |
285 var startKnob = this.$.startKnob; | |
286 var startProgress = this.$.startProgress; | |
287 var endProgress = this.$.endProgress; | |
288 var startLabel = this.$.startLabel; | |
289 var endLabel = this.$.endLabel; | |
290 | |
291 // The label bubbles have the same left coordinates as their corresponding | 308 // The label bubbles have the same left coordinates as their corresponding |
292 // knobs. | 309 // knobs. |
293 startLabel.style.left = startKnob.style.left; | 310 this.$.startLabel.style.left = this.$.startKnob.style.left; |
294 endLabel.style.left = endKnob.style.left; | 311 this.$.endLabel.style.left = this.$.endKnob.style.left; |
| 312 |
| 313 // In RTL locales, the relative positions of the knobs are flipped for the |
| 314 // purpose of calculating the styles of the progress bars below. |
| 315 var rtl = this.isRTL_; |
| 316 var endKnob = rtl ? this.$.startKnob : this.$.endKnob; |
| 317 var startKnob = rtl ? this.$.endKnob : this.$.startKnob; |
| 318 var startProgress = rtl ? this.$.endProgress : this.$.startProgress; |
| 319 var endProgress = rtl ? this.$.startProgress : this.$.endProgress; |
295 | 320 |
296 // The end progress bar starts from either the start knob or the start of | 321 // 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. | 322 // the slider (whichever is to its left) and ends at the end knob. |
298 var endProgressLeft = startKnob.offsetLeft >= endKnob.offsetLeft ? | 323 var endProgressLeft = startKnob.offsetLeft >= endKnob.offsetLeft ? |
299 '0px' : | 324 '0px' : |
300 startKnob.style.left; | 325 startKnob.style.left; |
301 endProgress.style.left = endProgressLeft; | 326 endProgress.style.left = endProgressLeft; |
302 endProgress.style.width = | 327 endProgress.style.width = |
303 (parseFloat(endKnob.style.left) - parseFloat(endProgressLeft)) + 'px'; | 328 (parseFloat(endKnob.style.left) - parseFloat(endProgressLeft)) + 'px'; |
304 | 329 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 /** | 461 /** |
437 * Handles the 'left' key event. | 462 * Handles the 'left' key event. |
438 * @private | 463 * @private |
439 */ | 464 */ |
440 onLeftKey_: function(e) { | 465 onLeftKey_: function(e) { |
441 e.preventDefault(); | 466 e.preventDefault(); |
442 var knobPref = this.getFocusedKnobPrefPathIfAny_(); | 467 var knobPref = this.getFocusedKnobPrefPathIfAny_(); |
443 if (!knobPref) | 468 if (!knobPref) |
444 return; | 469 return; |
445 | 470 |
446 this.decrementPref_(knobPref, 1); | 471 if (this.isRTL_) |
| 472 this.incrementPref_(knobPref, 1); |
| 473 else |
| 474 this.decrementPref_(knobPref, 1); |
447 }, | 475 }, |
448 | 476 |
449 /** | 477 /** |
450 * Handles the 'right' key event. | 478 * Handles the 'right' key event. |
451 * @private | 479 * @private |
452 */ | 480 */ |
453 onRightKey_: function(e) { | 481 onRightKey_: function(e) { |
454 e.preventDefault(); | 482 e.preventDefault(); |
455 var knobPref = this.getFocusedKnobPrefPathIfAny_(); | 483 var knobPref = this.getFocusedKnobPrefPathIfAny_(); |
456 if (!knobPref) | 484 if (!knobPref) |
457 return; | 485 return; |
458 | 486 |
459 this.incrementPref_(knobPref, 1); | 487 if (this.isRTL_) |
| 488 this.decrementPref_(knobPref, 1); |
| 489 else |
| 490 this.incrementPref_(knobPref, 1); |
460 }, | 491 }, |
461 | 492 |
462 /** | 493 /** |
463 * @return {boolean} Whether either of the two knobs is focused. | 494 * @return {boolean} Whether either of the two knobs is focused. |
464 * @private | 495 * @private |
465 */ | 496 */ |
466 isEitherKnobFocused_: function() { | 497 isEitherKnobFocused_: function() { |
467 var activeElement = this.shadowRoot.activeElement; | 498 var activeElement = this.shadowRoot.activeElement; |
468 return activeElement == this.$.startKnob || activeElement == this.$.endKnob; | 499 return activeElement == this.$.startKnob || activeElement == this.$.endKnob; |
469 }, | 500 }, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 _focusedChanged: function(receivedFocusFromKeyboard) { | 547 _focusedChanged: function(receivedFocusFromKeyboard) { |
517 // Overrides the _focusedChanged() from the PaperInkyFocusBehavior so that | 548 // Overrides the _focusedChanged() from the PaperInkyFocusBehavior so that |
518 // it does nothing. This function is called only once for the entire | 549 // 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 | 550 // 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 | 551 // 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. | 552 // created. Hence we handle focus and blur explicitly above. |
522 } | 553 } |
523 }); | 554 }); |
524 | 555 |
525 })(); | 556 })(); |
OLD | NEW |