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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/chromeos/set_time.js
diff --git a/chrome/browser/resources/chromeos/set_time.js b/chrome/browser/resources/chromeos/set_time.js
new file mode 100644
index 0000000000000000000000000000000000000000..1f7d53dffe22a5d486b6dbcbe5cf69b039a51bbe
--- /dev/null
+++ b/chrome/browser/resources/chromeos/set_time.js
@@ -0,0 +1,205 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+cr.define('settime', function() {
+ /**
+ * TimeSetter handles a dialog to check and set system time. It can also
+ * include a timezone dropdown if timezoneId is provided.
+ *
+ * TimeSetter uses the system time to populate the controls initially and
+ * update them as the system time or timezone changes, and notifies Chrome
+ * when the user changes the time or timezone.
+ * @constructor
+ */
+ function TimeSetter() {}
+
+ cr.addSingletonGetter(TimeSetter);
+
+ /** @const */ var BODY_PADDING_PX = 20;
+ /** @const */ var LABEL_PADDING_PX = 5;
+
+ TimeSetter.prototype = {
+ /**
+ * Performs initial setup.
+ */
+ initialize: function() {
+ // Store values for reverting inputs when the user's date/time is invalid.
+ this.prevValues_ = {};
+
+ // The build time doesn't include a timezone, so subtract 1 day to get a
+ // safe minimum date.
+ this.minDate_ = new Date(loadTimeData.getValue('buildTime'));
+ this.minDate_.setDate(this.minDate_.getDate() - 1);
+
+ // Set the max date to the min date plus 20 years.
+ this.maxDate_ = new Date(this.minDate_);
+ this.maxDate_.setYear(this.minDate_.getFullYear() + 20);
+
+ // Make sure the ostensible date is within this range.
+ var now = new Date();
+ if (now > this.maxDate_)
+ this.maxDate_ = now;
+ else if (now < this.minDate_)
+ this.minDate_ = now;
+
+ $('date').setAttribute('min', this.toHtmlValues_(this.minDate_).date);
+ $('date').setAttribute('max', this.toHtmlValues_(this.maxDate_).date);
+
+ this.updateTime_();
+
+ // Show the timezone select if we have a timezone ID.
+ var currentTimezoneId = loadTimeData.getValue('currentTimezoneId');
+ if (currentTimezoneId !== '') {
Dan Beam 2014/04/24 21:54:33 if (currentTimezoneId)
michaelpg 2014/04/24 22:42:48 Done.
+ this.setTimezone_(currentTimezoneId);
+ $('timezone-select').addEventListener(
+ 'change', this.onTimezoneChange_.bind(this), false);
+ $('timezone').hidden = false;
+ }
+
+ this.sizeToFit_();
+
+ $('time').addEventListener('blur', this.onTimeBlur_.bind(this), false);
+ $('date').addEventListener('blur', this.onTimeBlur_.bind(this), false);
+
+ $('done').addEventListener('click', this.onSubmit_.bind(this), false);
+ },
+
+ /**
+ * Sets the current timezone.
+ * @param {string} timezoneId The timezone ID to select.
+ * @private
+ */
+ setTimezone_: function(timezoneId) {
+ $('timezone-select').value = timezoneId;
+ this.updateTime_();
+ },
+
+ /**
+ * Updates the date/time controls to the current local time.
+ * Called initially, then called again once a minute.
+ * @private
+ */
+ updateTime_: function() {
+ var now = new Date();
+
+ // Only update time controls if neither is focused or one is invalid.
+ if ((document.activeElement.id != 'date' &&
+ document.activeElement.id != 'time') ||
+ $('date').value === '' || $('time').value === '') {
+ var htmlValues = this.toHtmlValues_(now);
+ this.prevValues_.date = $('date').value = htmlValues.date;
+ this.prevValues_.time = $('time').value = htmlValues.time;
+ }
+
+ window.clearTimeout(this.timeTimeout_);
+
+ // Start timer to update these inputs every minute.
+ var secondsRemaining = 60 - now.getSeconds(); // 0 <= getSeconds() < 60
Dan Beam 2014/04/24 21:54:33 remove commented code
michaelpg 2014/04/24 22:42:48 Done.
+ this.timeTimeout_ = window.setTimeout(this.updateTime_.bind(this),
+ secondsRemaining * 1000);
+ },
+
+ /**
+ * Attempts to set the system time to the time given by the controls.
+ * @private
+ */
+ applyTime_: function() {
+ var date = $('date').valueAsDate;
+ date.setMilliseconds(date.getMilliseconds() + $('time').valueAsNumber);
+
+ // Add timezone offset to get real time.
+ date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
+
+ var seconds = Math.floor(date / 1000);
+ chrome.send('setTimeInSeconds', [seconds]);
+ },
+
+ /**
+ * Handles events for the date/time controls.
+ * @param {Event} e The blur event.
+ * @private
+ */
+ onTimeBlur_: function(e) {
+ if (e.target.validity.valid && e.target.value !== '') {
Dan Beam 2014/04/24 21:54:33 if (e.target.validity.valid && e.target.value)
michaelpg 2014/04/24 22:42:48 Done.
+ // Make this the new fallback time in case of future invalid input.
+ this.prevValues_[e.target.id] = e.target.value;
+ this.applyTime_();
+ } else {
+ // Restore previous value.
+ e.target.value = this.prevValues_[e.target.id];
+ }
+ },
+
+ /**
+ * Handles events for the timezone control.
+ * @param {Event} e The change event.
+ * @private
+ */
+ onTimezoneChange_: function(e) {
+ chrome.send('setTimezone', [e.target.value]);
+ },
+
+ /**
+ * Handles events for the submit button.
+ * @param {Event} e The click event.
+ * @private
+ */
+ onSubmit_: function(e) {
+ chrome.send('DialogClose');
+ },
+
+ /**
+ * Resizes the window if necessary to show the entire contents.
+ * @private
+ */
+ sizeToFit_: function() {
+ // Because of l10n, we should check that the vertical content can fit
+ // within the window.
+ if (window.innerHeight < document.body.scrollHeight) {
+ var newHeight = document.body.scrollHeight +
+ window.outerHeight - window.innerHeight; // title bar
Dan Beam 2014/04/24 21:54:33 chrome webui code (and chrome code in general) doe
michaelpg 2014/04/24 22:42:48 Done.
+ window.resizeTo(window.outerWidth, newHeight);
+ }
+ },
+
+ /**
+ * Builds date and time strings suitable for the values of HTML date and
+ * time elements.
+ * @param {Date} date The date object to represent.
+ * @return {Object} A pair of strings in the following format:
Dan Beam 2014/04/24 21:54:33 @return {{date: string, time: string}} Date is an
michaelpg 2014/04/24 22:42:48 Done.
+ * {
+ * date: 'Full-date string as defined in RFC 3339',
+ * time: 'Time string as "HH:MM"'
+ * }
Dan Beam 2014/04/24 21:54:33 indent off
michaelpg 2014/04/24 22:42:48 Done.
+ * @private
+ */
+ toHtmlValues_: function(date) {
+ // Get the current time and subtract the timezone offset, so the
+ // JSON string is in local time.
+ var localDate = new Date(date);
+ localDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
+ return {
+ date: localDate.toISOString().slice(0, 10),
+ time: localDate.toISOString().slice(11, 16)
+ };
Dan Beam 2014/04/24 21:54:33 nit: return {date: localDate.toISOString().slic
michaelpg 2014/04/24 22:42:48 Done.
+ },
+ };
+
+ TimeSetter.setTimezone = function(timezoneId) {
+ TimeSetter.getInstance().setTimezone_(timezoneId);
+ };
+
+ TimeSetter.updateTime = function() {
+ TimeSetter.getInstance().updateTime_();
+ };
+
+ return {
+ TimeSetter: TimeSetter
+ };
+});
+
+document.addEventListener(
+ 'DOMContentLoaded',
+ settime.TimeSetter.getInstance().initialize.bind(
+ settime.TimeSetter.getInstance()));
Dan Beam 2014/04/24 21:54:33 document.addEventListener('DOMContentLoaded', func
michaelpg 2014/04/24 22:42:48 Done.

Powered by Google App Engine
This is Rietveld 408576698