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

Side by Side Diff: chrome/browser/resources/chromeos/switch_access/options.js

Issue 2760453002: Creating options page and storing preferences. (Closed)
Patch Set: Fixed closure compiler error Created 3 years, 9 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 /**
6 * Alias for document.getElementById.
7 *
8 * @param {string} id
9 * @return {Element}
10 */
11 let $ = function(id) {
12 let el = document.getElementById(id);
David Tseng 2017/03/24 18:53:11 nit: return document.getElementById(id);
elichtenberg 2017/03/25 00:02:19 Done.
13 return el;
14 };
15
16 /**
17 * Class to manage the options page.
18 *
19 * @constructor
20 */
21 let SwitchAccessOptions = function() {
22 let background = chrome.extension.getBackgroundPage();
23
24 /**
25 * True if the page has already been initialized, meaning that initial values
26 * of all pref elements have been set.
27 *
28 * @private {boolean}
29 */
30 this.inited_ = false;
31
32 /**
33 * User preferences.
34 *
35 * @private
36 */
37 this.switchAccessPrefs_ = background.switchAccess.switchAccessPrefs;
38
39 document.addEventListener('DOMContentLoaded', this.init_.bind(this));
David Tseng 2017/03/24 18:53:11 Perhaps do this in the top level scope (see last c
elichtenberg 2017/03/25 00:02:19 Done.
40 document.addEventListener('change', this.handleInputChange_.bind(this));
41 background.document.addEventListener(
42 'prefsUpdate', this.handlePrefsUpdate_.bind(this));
43 };
44
45 SwitchAccessOptions.prototype = {
46 /**
47 * Initialize the options page by setting all elements representing a user
48 * preference to show the correct value.
49 *
50 * @private
51 */
52 init_: function() {
53 let prefs = this.switchAccessPrefs_.getPrefs();
54 $('enableAutoScan').checked = prefs['enableAutoScan'];
55 $('autoScanTime').value = prefs['autoScanTime'];
56 this.inited_ = true;
57 },
58
59 /**
60 * Handle a change by the user to an element representing a user preference.
61 *
62 * @param {!Event} event
63 * @private
64 */
65 handleInputChange_: function(event) {
66 switch (event.target.id) {
67 case 'enableAutoScan':
68 this.switchAccessPrefs_.setPref(event.target.id, event.target.checked);
69 break;
70 case 'autoScanTime':
71 this.switchAccessPrefs_.setPref(event.target.id, event.target.value);
72 break;
73 }
74 },
75
76 /**
77 * Handle a change in user preferences.
78 *
79 * @param {!Event} event
80 * @private
81 */
82 handlePrefsUpdate_: function(event) {
83 // Check that this.init_ has been called, since it sets values for all pref
84 // elements anyways.
85 if (!this.inited_)
86 return;
87
88 let updatedPrefs = event.detail;
89 for (let key of Object.keys(updatedPrefs)) {
90 switch (key) {
91 case 'enableAutoScan':
92 $(key).checked = updatedPrefs[key];
93 break;
94 case 'autoScanTime':
95 $(key).value = updatedPrefs[key];
96 break;
97 }
98 }
99 }
100 };
101
102 new SwitchAccessOptions();
David Tseng 2017/03/24 18:53:11 Would it make sense to do this when the page conte
elichtenberg 2017/03/25 00:02:19 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698