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

Side by Side Diff: chrome/browser/resources/chromeos/emulator/device_emulator.js

Issue 1205753002: Create chrome://device-emulator and add the ability to get a battery percentage and send an updated… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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 2015 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('device_emulator', function() {
6 'use strict';
7
8 /**
9 * Updates the UI with the battery status.
10 * @param {number} percent Battery percentage (out of 100).
11 */
12 function setBatteryInfo(percent) {
13 var slider = $('battery-percent-slider');
14 var text = $('battery-percent-text');
15
16 slider.value = percent;
17 text.value = percent.toString();
michaelpg 2015/06/26 17:45:59 just use slider.valueAsNumber/text.valueAsNumber
rfrappier 2015/06/26 18:58:03 Done.
18 }
19
20 /**
21 * Event listener fired when the battery percent slider is moved and the mouse
22 * is released. Updates the ChromeOS UI.
michaelpg 2015/06/26 17:46:00 uber nit: Chrome OS is two words! :-P
rfrappier 2015/06/26 18:58:03 Done.
23 * @param {Event} event Contains information about the event which was fired.
24 */
25 function onBatterySliderChange(event) {
26 var slider = event.target;
27 chrome.send('updateBatteryInfo', [slider.valueAsNumer]);
28 }
29
30 /**
31 * Event listener fired when the battery percent slider is moved. Updates
32 * the battery slider's associated text input.
33 * @param {Event} event Contains information about the event which was fired.
34 */
35 function onBatterySliderInput(event) {
36 var slider = event.target;
37 var text = $('battery-percent-text');
38
39 text.value = slider.value;
40 }
41
42 /**
43 * Event listener fired when a percentage is entered in the battery
44 * percentage text input. Updates the slider and ChromeOS UI.
45 * @param {Event} event Contains information about the event which was fired.
46 */
47 function onBatteryTextChange(event) {
48 var text = event.target;
49 var slider = $('battery-percent-slider');
50 var percent = text.valueAsNumber;
51
52 if (isNaN(percent)) {
53 percent = 0;
54 text.valueAsNumber = 0;
55 }
56
57 slider.value = percent;
58
59 chrome.send('updateBatteryInfo', [percent]);
60 }
61
62 function initialize() {
63 chrome.send('requestBatteryInfo');
64
65 wireEvents();
66 initializeControls();
67 }
68
69 /**
70 * Initializes any form controls as necessary.
71 */
72 function initializeControls() {
73 // Initialize the Power Source select box
74 var select = $('power-source-select');
75 var disconnectedOptionValue = loadTimeData.getString('DISCONNECTED');
michaelpg 2015/06/26 17:46:00 use camelCase for localized string keys: 'disconne
rfrappier 2015/06/26 18:58:03 Done.
76 var usbPowerOptionValue = loadTimeData.getString('USB_POWER');
77 var acPowerOptionValue = loadTimeData.getString('AC_POWER');
78
79 select.appendChild(createOptionForSelect(acPowerOptionValue,
80 'AC Power (Main/Line Power Connected)'));
81 select.appendChild(createOptionForSelect(usbPowerOptionValue,
82 'USB Power'));
83 select.appendChild(createOptionForSelect(disconnectedOptionValue,
84 'Disconnected (No external power source)'));
85
86 select.value = disconnectedOptionValue;
87 }
88
89 /**
90 * A helper function to create and return an <option> node
91 * to be added to a select box.
92 * @param {string} value Will be the <option>'s value attribute.
93 * @param {string} text Will be the <option>'s innerHTML attribute.
94 */
95 function createOptionForSelect(value, text) {
96 var opt = document.createElement('option');
97 opt.value = value;
98 opt.innerHTML = text;
99
100 return opt;
101 }
102
103 /**
104 * Sets up all event listeners for the page.
105 */
106 function wireEvents() {
107 var slider = $('battery-percent-slider');
108 var text = $('battery-percent-text');
109
110 slider.addEventListener('change', onBatterySliderChange);
111 slider.addEventListener('input', onBatterySliderInput);
112 text.addEventListener('input', onBatteryTextChange);
michaelpg 2015/06/26 17:45:59 name should match event ('input' is probably fine)
rfrappier 2015/06/26 18:58:03 Done.
113 }
114
115 // Return an object with all of the exports.
116 return {
117 initialize: initialize,
118 setBatteryInfo: setBatteryInfo,
119 };
120 });
121
122 document.addEventListener('DOMContentLoaded', device_emulator.initialize);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698