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

Side by Side Diff: chrome/browser/resources/chromeos/set_time.js

Issue 247663003: Date and Time dialog for when the clock isn't synced. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Set Time Dialog Created 6 years, 8 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 cr.define('setTime', function() {
Dan Beam 2014/04/23 17:51:45 nit: "namespaces" are all lowercase, e.g. settime
michaelpg 2014/04/24 01:32:23 Done.
6 /**
7 * TimeSetter class
Dan Beam 2014/04/23 17:51:45 ^ better description of what this does or how it w
michaelpg 2014/04/24 01:32:23 Done.
8 * @class
Dan Beam 2014/04/23 17:51:45 s/@class/@constructor
michaelpg 2014/04/24 01:32:23 Done.
9 */
10
Dan Beam 2014/04/23 17:51:45 remove blank line
michaelpg 2014/04/24 01:32:23 Done.
11 function TimeSetter() {}
12
13 cr.addSingletonGetter(TimeSetter);
14
15 var BODY_PADDING_PX = 20;
Dan Beam 2014/04/23 17:51:45 /** @const */ var BODY_PADDING_PX (as well as on L
michaelpg 2014/04/24 01:32:23 Done.
16 var LABEL_PADDING_PX = 5;
17
18 TimeSetter.prototype = {
19 /**
20 * Performs initial setup.
21 */
22 initialize: function() {
23 // Store values for reverting inputs when the user's date/time is invalid.
24 this.prevValues_ = {};
25
26 // The build time doesn't include a timezone, so subtract 1 day to get a
27 // safe minimum date.
28 this.minDate_ = new Date(loadTimeData.getValue('buildTime'));
29 this.minDate_.setDate(this.minDate_.getDate() - 1);
30
31 // Set the max date to the min date plus 20 years.
32 this.maxDate_ = new Date(this.minDate_);
33 this.maxDate_.setYear(this.minDate_.getFullYear() + 20);
Dan Beam 2014/04/23 17:51:45 what happens at 2018-01-19 03:14:07 UTC?
michaelpg 2014/04/24 01:32:23 tlsdate accepts times up to 20 years after the bui
Dan Beam 2014/04/24 21:32:23 i assume you mean the year changes to 1970 because
michaelpg 2014/04/24 22:42:47 No, Date doesn't overflow. The message will be pas
michaelpg 2014/04/25 20:05:14 I've suggested a hard cutoff to drewry@. He liked
34
35 // Make sure the ostensible date is within this range.
36 var now = new Date(Date.now());
Dan Beam 2014/04/23 17:51:45 this is the same as var now = new Date();
michaelpg 2014/04/24 01:32:23 sadly, I know this...
37 if (now > this.maxDate_)
38 this.maxDate_ = now;
39 else if (now < this.minDate_)
40 this.minDate_ = now;
41
42 $('date').setAttribute('min', this.htmlDate_(this.minDate_));
43 $('date').setAttribute('max', this.htmlDate_(this.maxDate_));
44
45 this.updateTime_();
46
47 // Show the timezone select if we have a timezone ID.
48 var currentTimezoneId = loadTimeData.getValue('currentTimezoneId');
49 if (currentTimezoneId !== '') {
50 this.setTimezone_(currentTimezoneId);
51 $('timezone-select').addEventListener(
52 'change', this.onTimezoneChange_.bind(this), false);
53 $('timezone').hidden = false;
54 }
55
56 this.sizeToFit_();
57
58 $('time').addEventListener('blur', this.onTimeBlur_.bind(this), false);
59 $('date').addEventListener('blur', this.onTimeBlur_.bind(this), false);
60
61 $('done').addEventListener('click', this.onSubmit_.bind(this), false);
62 },
63
64 /**
65 * Sets the current timezone.
66 * @param {string} timezoneId The timezone ID to select.
Dan Beam 2014/04/23 17:51:45 @private
michaelpg 2014/04/24 01:32:23 Done.
67 */
68 setTimezone_: function(timezoneId) {
69 $('timezone-select').value = timezoneId;
70 this.updateTime_();
71 },
72
73 /**
74 * Updates the date/time controls to the current local time.
75 * Called initially, then called again once a minute.
76 * @private
77 */
78 updateTime_: function() {
79 var now = new Date();
80
81 // Only update time controls if neither is focused.
82 if (document.activeElement.id != 'date' &&
Dan Beam 2014/04/23 17:51:45 can |document.activeElement| ever be null?
michaelpg 2014/04/24 01:32:23 According to the w3c, if there is no focused eleme
83 document.activeElement.id != 'time') {
84 $('time').value = this.htmlTime_(now);
85 this.prevValues_['time'] = $('time').value;
86
87 $('date').value = this.htmlDate_(now);
88 this.prevValues_['date'] = $('date').value;
Dan Beam 2014/04/23 17:51:45 this.prevValues_.time = $('time').value = this.htm
michaelpg 2014/04/24 01:32:23 Done.
89 }
90
91 window.clearTimeout(this.timeTimeout_);
92
93 // Start timer to update these inputs every minute.
94 var secondsRemaining = 60 - now.getSeconds(); // 0 <= getSeconds() < 60
95 this.timeTimeout_ = window.setTimeout(this.updateTime_.bind(this),
96 secondsRemaining * 1000);
Dan Beam 2014/04/23 17:51:45 use setInterval instead
michaelpg 2014/04/24 01:32:23 no. I want this to be called right when the second
97 },
98
99 /**
100 * Attempts to set the system time to the time given by the controls.
101 * @private
102 */
103 applyTime_: function() {
104 var date = $('date').valueAsDate;
Dan Beam 2014/04/23 17:51:45 these valueAs*() getters should probably be functi
michaelpg 2014/04/24 01:32:23 it's a build-it attribute of the date/time element
105 date.setMilliseconds(date.getMilliseconds() + $('time').valueAsNumber);
106
107 // Add timezone offset to get real time.
108 date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
109
110 var seconds = Math.floor(date.valueOf() / 1000);
Dan Beam 2014/04/23 17:51:45 i don't think you need .valueOf() here, just divid
michaelpg 2014/04/24 01:32:23 Done.
111 chrome.send('setTimeInSeconds', [seconds]);
112 },
113
114 /**
115 * Handles events for the date/time controls.
Dan Beam 2014/04/23 17:51:45 @param {Event} e The blur event.
michaelpg 2014/04/24 01:32:23 Done.
116 * @private
117 */
118 onTimeBlur_: function(e) {
119 if (e.type != 'blur')
120 return;
Dan Beam 2014/04/23 17:51:45 I'm confused, why we would ever hit this?
michaelpg 2014/04/24 01:32:23 We wouldn't, this code is vestigial.
Dan Beam 2014/04/24 21:32:23 don't write vestigal code ;)
121 if (e.target.validity.valid) {
122 // Make this the new fallback time in case of future invalid input.
123 this.prevValues_[e.target.id] = e.target.value;
124 this.applyTime_();
125 } else {
126 // Restore previous value.
127 e.target.value = this.prevValues_[e.target.id];
128 }
129 },
130
131 /**
132 * Handles events for the timezone control.
Dan Beam 2014/04/23 17:51:45 needs a @param
michaelpg 2014/04/24 01:32:23 Done.
133 * @private
134 */
135 onTimezoneChange_: function(e) {
136 chrome.send('setTimezone', [e.target.value]);
Dan Beam 2014/04/23 17:51:45 nit: e.currentTarget
michaelpg 2014/04/24 01:32:23 Not sure I follow. is that to make sure we get the
Dan Beam 2014/04/24 21:32:23 this is to make your code impervious to event dele
michaelpg 2014/04/24 22:42:47 Done.
137 },
138
139 /**
140 * Handles events for the submit button.
Dan Beam 2014/04/23 17:51:45 needs a @param
michaelpg 2014/04/24 01:32:23 Done.
141 * @private
142 */
143 onSubmit_: function(e) {
144 chrome.send('DialogClose');
Dan Beam 2014/04/23 17:51:45 s/DialogClose/dialogClose/
michaelpg 2014/04/24 01:32:23 I know, but no. https://code.google.com/p/chromiu
Dan Beam 2014/04/24 21:32:23 rename it
michaelpg 2014/04/24 22:42:47 OK. Since that'll require changing 10 more files a
Dan Beam 2014/04/25 00:05:48 https://chromiumcodereview.appspot.com/251623003/
145 },
146
147 /**
148 * Resizes the window if necessary to show the entire contents.
149 * @private
150 */
151 sizeToFit_: function() {
152 var newWidth = window.outerWidth;
153 var newHeight = window.outerHeight;
154
155 // Because of l10n, we should check if the window can fit the
156 // timezone select.
157 var timezoneWidth = $('timezone-select').offsetWidth;
158 var labelWidth = $('timezone-label').offsetWidth;
159
160 var minWidth = 2 * BODY_PADDING_PX + labelWidth +
161 LABEL_PADDING_PX + timezoneWidth;
162 if (window.innerWidth < minWidth) {
163 // Apply the width to the body now so the next calculation is correct.
164 document.body.style.width = (minWidth - 2 * BODY_PADDING_PX) + 'px';
165 newWidth = minWidth;
166 }
167
168 // After resizing horizontally, make sure the page will fit vertically.
169 var minHeight = document.body.scrollHeight;
170 if (window.innerHeight < minHeight) {
171 newHeight = minHeight +
172 window.outerHeight - window.innerHeight; // title bar
173 }
174
175 if (newWidth != window.outerWidth || newHeight != window.outerHeight) {
176 // Move before resize for a less jumpy transition.
177 window.moveTo(window.screen.width / 2 - newWidth / 2,
178 window.screen.height / 2 - newHeight / 2);
179 window.resizeTo(newWidth, newHeight);
180 }
181 },
182
183 /**
184 * Builds a time string as "HH:MM".
185 * @param {Date} date The time to represent.
Dan Beam 2014/04/23 17:51:45 @return {string} A date suitable for representing
michaelpg 2014/04/24 01:32:23 Done.
186 * @private
187 */
188 htmlTime_: function(date) {
189 // Get the current time and subtract the timezone offset, so the
190 // JSON string is in local time.
191 var localDate = new Date(date);
192 localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
193 return localDate.toJSON().slice(11, 16);
Dan Beam 2014/04/23 17:51:45 s/toJSON/toISOString/
michaelpg 2014/04/24 01:32:23 Done.
194 },
195
196 /**
197 * Builds a full-date string as defined in RFC 3339.
198 * @param {Date} date The date to represent.
Dan Beam 2014/04/23 17:51:45 needs @return
michaelpg 2014/04/24 01:32:23 Done.
199 * @private
200 */
201 htmlDate_: function(date) {
Dan Beam 2014/04/23 17:51:45 seems like htmlDate_ and htmlTime_ share some code
michaelpg 2014/04/24 01:32:23 Done.
202 // Get the current time and subtract the timezone offset, so the
203 // JSON string is in local time.
204 var localDate = new Date(date);
205 localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
206 return localDate.toJSON().slice(0, 10);
Dan Beam 2014/04/23 17:51:45 toISOString
michaelpg 2014/04/24 01:32:23 Done.
207 }
208 };
209
210 // Forward public APIs to private implementations.
211 [
212 'setTimezone',
213 ].forEach(function(name) {
214 TimeSetter[name] = function() {
215 var instance = TimeSetter.getInstance();
216 return instance[name + '_'].apply(instance, arguments);
217 };
218 });
Dan Beam 2014/04/23 17:51:45 meh, wait until there's more than 1 of these, IMO,
michaelpg 2014/04/24 01:32:23 Done.
219
220 return {
221 TimeSetter: TimeSetter
222 };
223 });
224
225 document.addEventListener(
226 'DOMContentLoaded',
227 setTime.TimeSetter.getInstance().initialize.bind(
228 setTime.TimeSetter.getInstance()));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698