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
3 // found in the LICENSE file.
5 cr
.define('settime', function() {
7 * TimeSetter handles a dialog to check and set system time. It can also
8 * include a timezone dropdown if timezoneId is provided.
10 * TimeSetter uses the system time to populate the controls initially and
11 * update them as the system time or timezone changes, and notifies Chrome
12 * when the user changes the time or timezone.
15 function TimeSetter() {}
17 cr
.addSingletonGetter(TimeSetter
);
19 /** @const */ var BODY_PADDING_PX
= 20;
20 /** @const */ var LABEL_PADDING_PX
= 5;
22 TimeSetter
.prototype = {
24 * Performs initial setup.
26 initialize: function() {
27 // Store values for reverting inputs when the user's date/time is invalid.
28 this.prevValues_
= {};
30 // The build time doesn't include a timezone, so subtract 1 day to get a
32 this.minDate_
= new Date(loadTimeData
.getValue('buildTime'));
33 this.minDate_
.setDate(this.minDate_
.getDate() - 1);
35 // Set the max date to the min date plus 20 years.
36 this.maxDate_
= new Date(this.minDate_
);
37 this.maxDate_
.setYear(this.minDate_
.getFullYear() + 20);
39 // Make sure the ostensible date is within this range.
41 if (now
> this.maxDate_
)
43 else if (now
< this.minDate_
)
46 $('date').setAttribute('min', this.toHtmlValues_(this.minDate_
).date
);
47 $('date').setAttribute('max', this.toHtmlValues_(this.maxDate_
).date
);
51 // Show the timezone select if we have a timezone ID.
52 var currentTimezoneId
= loadTimeData
.getValue('currentTimezoneId');
53 if (currentTimezoneId
) {
54 this.setTimezone_(currentTimezoneId
);
55 $('timezone-select').addEventListener(
56 'change', this.onTimezoneChange_
.bind(this), false);
57 $('timezone').hidden
= false;
62 $('time').addEventListener('blur', this.onTimeBlur_
.bind(this), false);
63 $('date').addEventListener('blur', this.onTimeBlur_
.bind(this), false);
65 $('set-time').addEventListener(
66 'submit', this.onSubmit_
.bind(this), false);
70 * Sets the current timezone.
71 * @param {string} timezoneId The timezone ID to select.
74 setTimezone_: function(timezoneId
) {
75 $('timezone-select').value
= timezoneId
;
80 * Updates the date/time controls to the current local time.
81 * Called initially, then called again once a minute.
84 updateTime_: function() {
87 // Only update time controls if neither is focused.
88 if (document
.activeElement
.id
!= 'date' &&
89 document
.activeElement
.id
!= 'time') {
90 var htmlValues
= this.toHtmlValues_(now
);
91 this.prevValues_
.date
= $('date').value
= htmlValues
.date
;
92 this.prevValues_
.time
= $('time').value
= htmlValues
.time
;
95 window
.clearTimeout(this.timeTimeout_
);
97 // Start timer to update these inputs every minute.
98 var secondsRemaining
= 60 - now
.getSeconds();
99 this.timeTimeout_
= window
.setTimeout(this.updateTime_
.bind(this),
100 secondsRemaining
* 1000);
104 * Sets the system time from the UI.
107 applyTime_: function() {
108 var date
= $('date').valueAsDate
;
109 date
.setMilliseconds(date
.getMilliseconds() + $('time').valueAsNumber
);
111 // Add timezone offset to get real time.
112 date
.setMinutes(date
.getMinutes() + date
.getTimezoneOffset());
114 var seconds
= Math
.floor(date
/ 1000);
115 chrome
.send('setTimeInSeconds', [seconds
]);
119 * Called when focus is lost on date/time controls.
120 * @param {Event} e The blur event.
123 onTimeBlur_: function(e
) {
124 if (e
.target
.validity
.valid
&& e
.target
.value
) {
125 // Make this the new fallback time in case of future invalid input.
126 this.prevValues_
[e
.target
.id
] = e
.target
.value
;
129 // Restore previous value.
130 e
.target
.value
= this.prevValues_
[e
.target
.id
];
135 * @param {Event} e The change event.
138 onTimezoneChange_: function(e
) {
139 chrome
.send('setTimezone', [e
.currentTarget
.value
]);
143 * Closes the dialog window.
144 * @param {Event} e The submit event.
147 onSubmit_: function(e
) {
149 chrome
.send('dialogClose');
153 * Resizes the window if necessary to show the entire contents.
156 sizeToFit_: function() {
157 // Because of l10n, we should check that the vertical content can fit
158 // within the window.
159 if (window
.innerHeight
< document
.body
.scrollHeight
) {
160 // Resize window to fit scrollHeight and the title bar.
161 var newHeight
= document
.body
.scrollHeight
+
162 window
.outerHeight
- window
.innerHeight
;
163 window
.resizeTo(window
.outerWidth
, newHeight
);
168 * Builds date and time strings suitable for the values of HTML date and
170 * @param {Date} date The date object to represent.
171 * @return {{date: string, time: string}} Date is an RFC 3339 formatted date
172 * and time is an HH:MM formatted time.
175 toHtmlValues_: function(date
) {
176 // Get the current time and subtract the timezone offset, so the
177 // JSON string is in local time.
178 var localDate
= new Date(date
);
179 localDate
.setMinutes(date
.getMinutes() - date
.getTimezoneOffset());
180 return {date
: localDate
.toISOString().slice(0, 10),
181 time
: localDate
.toISOString().slice(11, 16)};
185 TimeSetter
.setTimezone = function(timezoneId
) {
186 TimeSetter
.getInstance().setTimezone_(timezoneId
);
189 TimeSetter
.updateTime = function() {
190 TimeSetter
.getInstance().updateTime_();
194 TimeSetter
: TimeSetter
198 document
.addEventListener('DOMContentLoaded', function() {
199 settime
.TimeSetter
.getInstance().initialize();