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
.exportPath('options');
8 * Copied from ash/system/chromeos/power/power_status.h.
11 options
.PowerStatusDeviceType
= {
19 * type: options.PowerStatusDeviceType,
25 cr
.define('options', function() {
26 var Page
= cr
.ui
.pageManager
.Page
;
27 var PageManager
= cr
.ui
.pageManager
.PageManager
;
30 * Encapsulated handling of the power overlay.
32 * @extends {cr.ui.pageManager.Page}
34 function PowerOverlay() {
35 Page
.call(this, 'power-overlay',
36 loadTimeData
.getString('powerOverlayTabTitle'),
40 cr
.addSingletonGetter(PowerOverlay
);
42 PowerOverlay
.prototype = {
43 __proto__
: Page
.prototype,
46 initializePage: function() {
47 Page
.prototype.initializePage
.call(this);
49 $('power-confirm').onclick
=
50 PageManager
.closeOverlay
.bind(PageManager
);
51 $('power-source-dropdown').onchange
=
52 this.powerSourceChanged_
.bind(this);
56 didShowPage: function() {
57 chrome
.send('updatePowerStatus');
61 * @param {string} status
64 setBatteryStatusText_: function(status
) {
65 $('battery-status-value').textContent
= status
;
69 * @param {Array<options.PowerSource>} sources External power sources.
70 * @param {string} selectedId The ID of the currently used power source.
71 * @param {boolean} isUsbCharger Whether the currently used power source
72 * is a USB (low-powered) charger.
73 * @param {boolean} isCalculating Whether the power info is still
77 setPowerSources_: function(sources
, selectedId
, isUsbCharger
,
79 if (this.lastPowerSource_
!= selectedId
) {
80 this.lastPowerSource_
= selectedId
;
81 if (selectedId
&& !isUsbCharger
) {
82 // It can take a while to detect a USB charger, but triggering a
83 // power status update makes the determination faster.
84 setTimeout(chrome
.send
.bind(null, 'updatePowerStatus'), 1000);
88 var chargerRow
= $('power-source-charger');
89 var chargingDescRow
= $('charging-description');
91 $('power-sources').hidden
= chargerRow
.hidden
=
92 chargingDescRow
.hidden
= true;
94 // If no power sources are available, only show the battery status text.
95 if (sources
.length
== 0)
98 // If we're still calculating battery time and seem to have an AC
99 // adapter, the charger information may be wrong.
100 if (isCalculating
&& selectedId
&& !isUsbCharger
) {
101 $('power-source-charger-type').textContent
=
102 loadTimeData
.getString('calculatingPower');
103 chargerRow
.hidden
= false;
107 // Check if a dedicated charger is being used.
108 var usingDedicatedCharger
= false;
110 usingDedicatedCharger
= sources
.some(function(source
) {
111 return source
.id
== selectedId
&&
112 source
.type
== options
.PowerStatusDeviceType
.DEDICATED_CHARGER
;
116 if (usingDedicatedCharger
) {
117 // Show charger information.
118 $('power-source-charger-type').textContent
= loadTimeData
.getString(
119 isUsbCharger
? 'powerSourceLowPowerCharger' :
120 'powerSourceAcAdapter');
121 chargerRow
.hidden
= false;
122 chargingDescRow
.hidden
= isUsbCharger
;
124 this.showPowerSourceList_(sources
, selectedId
);
129 * Populates and shows the dropdown of available power sources.
130 * @param {Array<options.PowerSource>} sources External power sources.
131 * @param {string} selectedId The ID of the currently used power source.
132 * The empty string indicates no external power source is in use
133 * (running on battery).
136 showPowerSourceList_: function(sources
, selectedId
) {
137 // Clear the dropdown.
138 var dropdown
= $('power-source-dropdown');
139 dropdown
.innerHTML
= '';
141 // Add a battery option.
144 description
: loadTimeData
.getString('powerSourceBattery'),
147 // Build the power source list.
148 sources
.forEach(function(source
) {
149 var option
= document
.createElement('option');
150 option
.value
= source
.id
;
151 option
.textContent
= source
.description
;
152 option
.selected
= source
.id
== selectedId
;
153 dropdown
.appendChild(option
);
156 // Show the power source list.
157 $('power-sources').hidden
= false;
161 powerSourceChanged_: function() {
162 chrome
.send('setPowerSource', [$('power-source-dropdown').value
]);
166 cr
.makePublic(PowerOverlay
, [
167 'setBatteryStatusText',
173 PowerOverlay
: PowerOverlay