Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / resources / options / chromeos / power_overlay.js
blobfe2051bd3b6907b2a7eef44bf286f02a47549dba
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');
7 /**
8 * Copied from ash/system/chromeos/power/power_status.h.
9 * @enum {number}
11 options.PowerStatusDeviceType = {
12 DEDICATED_CHARGER: 0,
13 DUAL_ROLE_USB: 1,
16 /**
17 * @typedef {{
18 * id: string,
19 * type: options.PowerStatusDeviceType,
20 * description: string
21 * }}
23 options.PowerSource;
25 cr.define('options', function() {
26 var Page = cr.ui.pageManager.Page;
27 var PageManager = cr.ui.pageManager.PageManager;
29 /**
30 * Encapsulated handling of the power overlay.
31 * @constructor
32 * @extends {cr.ui.pageManager.Page}
34 function PowerOverlay() {
35 Page.call(this, 'power-overlay',
36 loadTimeData.getString('powerOverlayTabTitle'),
37 'power-overlay');
40 cr.addSingletonGetter(PowerOverlay);
42 PowerOverlay.prototype = {
43 __proto__: Page.prototype,
45 /** @override */
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);
55 /** @override */
56 didShowPage: function() {
57 chrome.send('updatePowerStatus');
60 /**
61 * @param {string} status
62 * @private
64 setBatteryStatusText_: function(status) {
65 $('battery-status-value').textContent = status;
68 /**
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
74 * being calculated.
75 * @private
77 setPowerSources_: function(sources, selectedId, isUsbCharger,
78 isCalculating) {
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)
96 return;
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;
104 return;
107 // Check if a dedicated charger is being used.
108 var usingDedicatedCharger = false;
109 if (selectedId) {
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;
123 } else {
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).
134 * @private
136 showPowerSourceList_: function(sources, selectedId) {
137 // Clear the dropdown.
138 var dropdown = $('power-source-dropdown');
139 dropdown.innerHTML = '';
141 // Add a battery option.
142 sources.unshift({
143 id: '',
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;
160 /** @private */
161 powerSourceChanged_: function() {
162 chrome.send('setPowerSource', [$('power-source-dropdown').value]);
166 cr.makePublic(PowerOverlay, [
167 'setBatteryStatusText',
168 'setPowerSources',
171 // Export
172 return {
173 PowerOverlay: PowerOverlay