| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 cr.define('settime', function() { | 5 cr.define('settime', function() { |
| 6 /** | 6 /** |
| 7 * TimeSetter handles a dialog to check and set system time. It can also | 7 * TimeSetter handles a dialog to check and set system time. It can also |
| 8 * include a timezone dropdown if timezoneId is provided. | 8 * include a timezone dropdown if timezoneId is provided. |
| 9 * | 9 * |
| 10 * TimeSetter uses the system time to populate the controls initially and | 10 * TimeSetter uses the system time to populate the controls initially and |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 $('date').setAttribute('min', this.toHtmlValues_(this.minDate_).date); | 46 $('date').setAttribute('min', this.toHtmlValues_(this.minDate_).date); |
| 47 $('date').setAttribute('max', this.toHtmlValues_(this.maxDate_).date); | 47 $('date').setAttribute('max', this.toHtmlValues_(this.maxDate_).date); |
| 48 | 48 |
| 49 this.updateTime_(); | 49 this.updateTime_(); |
| 50 | 50 |
| 51 // Show the timezone select if we have a timezone ID. | 51 // Show the timezone select if we have a timezone ID. |
| 52 var currentTimezoneId = loadTimeData.getValue('currentTimezoneId'); | 52 var currentTimezoneId = loadTimeData.getValue('currentTimezoneId'); |
| 53 if (currentTimezoneId) { | 53 if (currentTimezoneId) { |
| 54 this.setTimezone_(currentTimezoneId); | 54 this.setTimezone_(currentTimezoneId); |
| 55 $('timezone-select').addEventListener( | 55 $('timezone-select') |
| 56 'change', this.onTimezoneChange_.bind(this), false); | 56 .addEventListener( |
| 57 'change', this.onTimezoneChange_.bind(this), false); |
| 57 $('timezone').hidden = false; | 58 $('timezone').hidden = false; |
| 58 } | 59 } |
| 59 | 60 |
| 60 this.sizeToFit_(); | 61 this.sizeToFit_(); |
| 61 | 62 |
| 62 $('time').addEventListener('blur', this.onTimeBlur_.bind(this), false); | 63 $('time').addEventListener('blur', this.onTimeBlur_.bind(this), false); |
| 63 $('date').addEventListener('blur', this.onTimeBlur_.bind(this), false); | 64 $('date').addEventListener('blur', this.onTimeBlur_.bind(this), false); |
| 64 | 65 |
| 65 $('set-time').addEventListener( | 66 $('set-time') |
| 66 'submit', this.onSubmit_.bind(this), false); | 67 .addEventListener('submit', this.onSubmit_.bind(this), false); |
| 67 }, | 68 }, |
| 68 | 69 |
| 69 /** | 70 /** |
| 70 * Sets the current timezone. | 71 * Sets the current timezone. |
| 71 * @param {string} timezoneId The timezone ID to select. | 72 * @param {string} timezoneId The timezone ID to select. |
| 72 * @private | 73 * @private |
| 73 */ | 74 */ |
| 74 setTimezone_: function(timezoneId) { | 75 setTimezone_: function(timezoneId) { |
| 75 $('timezone-select').value = timezoneId; | 76 $('timezone-select').value = timezoneId; |
| 76 this.updateTime_(); | 77 this.updateTime_(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 89 document.activeElement.id != 'time') { | 90 document.activeElement.id != 'time') { |
| 90 var htmlValues = this.toHtmlValues_(now); | 91 var htmlValues = this.toHtmlValues_(now); |
| 91 this.prevValues_.date = $('date').value = htmlValues.date; | 92 this.prevValues_.date = $('date').value = htmlValues.date; |
| 92 this.prevValues_.time = $('time').value = htmlValues.time; | 93 this.prevValues_.time = $('time').value = htmlValues.time; |
| 93 } | 94 } |
| 94 | 95 |
| 95 window.clearTimeout(this.timeTimeout_); | 96 window.clearTimeout(this.timeTimeout_); |
| 96 | 97 |
| 97 // Start timer to update these inputs every minute. | 98 // Start timer to update these inputs every minute. |
| 98 var secondsRemaining = 60 - now.getSeconds(); | 99 var secondsRemaining = 60 - now.getSeconds(); |
| 99 this.timeTimeout_ = window.setTimeout(this.updateTime_.bind(this), | 100 this.timeTimeout_ = window.setTimeout( |
| 100 secondsRemaining * 1000); | 101 this.updateTime_.bind(this), secondsRemaining * 1000); |
| 101 }, | 102 }, |
| 102 | 103 |
| 103 /** | 104 /** |
| 104 * Sets the system time from the UI. | 105 * Sets the system time from the UI. |
| 105 * @private | 106 * @private |
| 106 */ | 107 */ |
| 107 applyTime_: function() { | 108 applyTime_: function() { |
| 108 var date = $('date').valueAsDate; | 109 var date = $('date').valueAsDate; |
| 109 date.setMilliseconds(date.getMilliseconds() + $('time').valueAsNumber); | 110 date.setMilliseconds(date.getMilliseconds() + $('time').valueAsNumber); |
| 110 | 111 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 | 152 |
| 152 /** | 153 /** |
| 153 * Resizes the window if necessary to show the entire contents. | 154 * Resizes the window if necessary to show the entire contents. |
| 154 * @private | 155 * @private |
| 155 */ | 156 */ |
| 156 sizeToFit_: function() { | 157 sizeToFit_: function() { |
| 157 // Because of l10n, we should check that the vertical content can fit | 158 // Because of l10n, we should check that the vertical content can fit |
| 158 // within the window. | 159 // within the window. |
| 159 if (window.innerHeight < document.body.scrollHeight) { | 160 if (window.innerHeight < document.body.scrollHeight) { |
| 160 // Resize window to fit scrollHeight and the title bar. | 161 // Resize window to fit scrollHeight and the title bar. |
| 161 var newHeight = document.body.scrollHeight + | 162 var newHeight = document.body.scrollHeight + window.outerHeight - |
| 162 window.outerHeight - window.innerHeight; | 163 window.innerHeight; |
| 163 window.resizeTo(window.outerWidth, newHeight); | 164 window.resizeTo(window.outerWidth, newHeight); |
| 164 } | 165 } |
| 165 }, | 166 }, |
| 166 | 167 |
| 167 /** | 168 /** |
| 168 * Builds date and time strings suitable for the values of HTML date and | 169 * Builds date and time strings suitable for the values of HTML date and |
| 169 * time elements. | 170 * time elements. |
| 170 * @param {Date} date The date object to represent. | 171 * @param {Date} date The date object to represent. |
| 171 * @return {{date: string, time: string}} Date is an RFC 3339 formatted date | 172 * @return {{date: string, time: string}} Date is an RFC 3339 formatted date |
| 172 * and time is an HH:MM formatted time. | 173 * and time is an HH:MM formatted time. |
| 173 * @private | 174 * @private |
| 174 */ | 175 */ |
| 175 toHtmlValues_: function(date) { | 176 toHtmlValues_: function(date) { |
| 176 // Get the current time and subtract the timezone offset, so the | 177 // Get the current time and subtract the timezone offset, so the |
| 177 // JSON string is in local time. | 178 // JSON string is in local time. |
| 178 var localDate = new Date(date); | 179 var localDate = new Date(date); |
| 179 localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset()); | 180 localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset()); |
| 180 return {date: localDate.toISOString().slice(0, 10), | 181 return { |
| 181 time: localDate.toISOString().slice(11, 16)}; | 182 date: localDate.toISOString().slice(0, 10), |
| 183 time: localDate.toISOString().slice(11, 16) |
| 184 }; |
| 182 }, | 185 }, |
| 183 }; | 186 }; |
| 184 | 187 |
| 185 TimeSetter.setTimezone = function(timezoneId) { | 188 TimeSetter.setTimezone = function(timezoneId) { |
| 186 TimeSetter.getInstance().setTimezone_(timezoneId); | 189 TimeSetter.getInstance().setTimezone_(timezoneId); |
| 187 }; | 190 }; |
| 188 | 191 |
| 189 TimeSetter.updateTime = function() { | 192 TimeSetter.updateTime = function() { |
| 190 TimeSetter.getInstance().updateTime_(); | 193 TimeSetter.getInstance().updateTime_(); |
| 191 }; | 194 }; |
| 192 | 195 |
| 193 return { | 196 return {TimeSetter: TimeSetter}; |
| 194 TimeSetter: TimeSetter | |
| 195 }; | |
| 196 }); | 197 }); |
| 197 | 198 |
| 198 document.addEventListener('DOMContentLoaded', function() { | 199 document.addEventListener('DOMContentLoaded', function() { |
| 199 settime.TimeSetter.getInstance().initialize(); | 200 settime.TimeSetter.getInstance().initialize(); |
| 200 }); | 201 }); |
| OLD | NEW |