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

Side by Side Diff: ui/webui/resources/cr_elements/network/cr_network_icon.js

Issue 2592383003: MD Settings: Switch cr_network_icon to use svg icons (Closed)
Patch Set: Fix clang Created 3 years, 11 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview Polymer element for rendering network icons based on ONC 6 * @fileoverview Polymer element for rendering network icons based on ONC
7 * state properties. 7 * state properties.
8 */ 8 */
9 9
10 /** 10 Polymer({
11 * @typedef {{ 11 is: 'cr-network-icon',
12 * showBadges: boolean,
13 * showDisconnected: boolean,
14 * strength: number
15 * }}
16 */
17 var NetworkIconParamType;
18 12
19 (function() { 13 properties: {
20 /** @const {string} */ var RESOURCE_IMAGE_BASE = 14 /**
21 'chrome://resources/cr_elements/network/'; 15 * If set, the ONC properties will be used to display the icon. This may
16 * either be the complete set of NetworkProperties or the subset of
17 * NetworkStateProperties.
18 * @type {!CrOnc.NetworkProperties|!CrOnc.NetworkStateProperties|undefined}
19 */
20 networkState: Object,
22 21
23 /** @const {string} */ var RESOURCE_IMAGE_EXT = '.png'; 22 /**
23 * If true, the icon is part of a list of networks and may be displayed
24 * differently, e.g. the disconnected image will never be shown for
25 * list items.
26 */
27 isListItem: {
28 type: Boolean,
29 value: false,
30 },
31 },
24 32
25 /** 33 /**
26 * Gets the icon type from the network type. This allows multiple types 34 * @return {string} The name of the svg icon image to show.
27 * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile). 35 * @private
28 * @param {chrome.networkingPrivate.NetworkType} networkType 36 */
29 * @return {string} The icon type: ethernet, wifi, mobile, or vpn. 37 getIcon_: function() {
30 */ 38 let showDisconnected =
31 function getIconTypeFromNetworkType(networkType) { 39 !this.isListItem && (!this.networkState.ConnectionState ||
32 if (!networkType || networkType == CrOnc.Type.ETHERNET) 40 this.networkState.ConnectionState ==
33 return 'ethernet'; 41 CrOnc.ConnectionState.NOT_CONNECTED);
34 else if (networkType == CrOnc.Type.WI_FI)
35 return 'wifi';
36 else if (networkType == CrOnc.Type.CELLULAR)
37 return 'mobile';
38 else if (networkType == CrOnc.Type.WI_MAX)
39 return 'mobile';
40 else if (networkType == CrOnc.Type.VPN)
41 return 'vpn';
42 42
43 console.error('Unrecognized network type for icon: ' + networkType); 43 switch (this.networkState.Type) {
44 return 'ethernet'; 44 case CrOnc.Type.ETHERNET: {
45 } 45 return 'network:settings-ethernet';
46 }
47 case CrOnc.Type.VPN: {
48 return 'network:vpn-key';
49 }
50 case CrOnc.Type.CELLULAR: {
51 let strength =
52 showDisconnected ? 0 : CrOnc.getSignalStrength(this.networkState);
53 let index = this.strengthToIndex_(strength);
54 return 'network:signal-cellular-' + index.toString(10) + '-bar';
55 }
56 case CrOnc.Type.WI_FI:
57 case CrOnc.Type.WI_MAX: {
58 if (showDisconnected)
59 return 'network:signal-wifi-off';
60 let strength = CrOnc.getSignalStrength(this.networkState);
61 let index = this.strengthToIndex_(strength);
62 return 'network:signal-wifi-' + index.toString(10) + '-bar';
63 }
64 default:
65 assertNotReached();
66 }
67 return '';
68 },
46 69
47 /** 70 /**
48 * Polymer class definition for 'cr-network-icon'. 71 * @param {number} strength The signal strength from [0 - 100].
49 */ 72 * @return {number} An index from 0-4 corresponding to |strength|.
50 Polymer({
51 is: 'cr-network-icon',
52
53 properties: {
54 /**
55 * If set, the ONC properties will be used to display the icon. This may
56 * either be the complete set of NetworkProperties or the subset of
57 * NetworkStateProperties.
58 * @type
59 * {!CrOnc.NetworkProperties|!CrOnc.NetworkStateProperties|undefined}
60 */
61 networkState: {
62 type: Object,
63 observer: 'networkStateChanged_',
64 },
65
66 /**
67 * If set, the ONC network type will be used to display the icon.
68 * @type {?chrome.networkingPrivate.NetworkType}
69 */
70 networkType: {
71 type: String,
72 value: null,
73 observer: 'networkTypeChanged_',
74 },
75
76 /**
77 * If true, the icon is part of a list of networks and may be displayed
78 * differently, e.g. the disconnected image will never be shown for
79 * list items.
80 */
81 isListItem: {
82 type: Boolean,
83 value: false,
84 observer: 'isListItemChanged_',
85 },
86
87 /** The icon type to use for the base image of the icon. */
88 iconType_: {
89 type: String,
90 value: 'ethernet',
91 },
92
93 /** Set to true to show a badge for roaming networks. */
94 roaming_: {
95 type: Boolean,
96 value: false,
97 },
98
99 /** Set to true to show a badge for secure networks. */
100 secure_: {
101 type: Boolean,
102 value: false,
103 },
104
105 /** Set to the name of a technology to show show a badge. */
106 technology_: {
107 type: String,
108 value: '',
109 },
110 },
111
112 /**
113 * Polymer networkState changed method. Updates the icon based on the
114 * network state.
115 * @private
116 */
117 networkStateChanged_: function() {
118 if (!this.networkState)
119 return;
120
121 this.networkType = null;
122 this.iconType_ = getIconTypeFromNetworkType(this.networkState.Type);
123 var strength = CrOnc.getSignalStrength(this.networkState);
124 var params = /** @type {NetworkIconParamType} */ {
125 showBadges: true,
126 showDisconnected: !this.isListItem,
127 strength: strength
128 };
129 this.setIcon_(params);
130 },
131
132 /**
133 * Polymer networkType changed method. Updates the icon based on the type
134 * of network, showing a disconnected icon where appropriate and no badges.
135 * @private
136 */
137 networkTypeChanged_: function() {
138 if (!this.networkType)
139 return;
140
141 this.networkState = undefined;
142 this.iconType_ = getIconTypeFromNetworkType(this.networkType);
143 var params = /** @type {NetworkIconParamType} */ {
144 showBadges: false,
145 showDisconnected: true,
146 strength: 0,
147 };
148 this.setIcon_(params);
149 },
150
151 /**
152 * Polymer isListItem changed method.
153 * @private
154 */
155 isListItemChanged_: function() {
156 if (this.networkState)
157 this.networkStateChanged_();
158 else if (this.networkType)
159 this.networkTypeChanged_();
160 },
161
162 /**
163 * Returns the url for an image based on identifier |id|.
164 * @param {string} id The identifier describing the image.
165 * @return {string} The url to use for the image 'src' property.
166 * @private 73 * @private
167 */ 74 */
168 toImageSrc_: function(id) { 75 strengthToIndex_: function(strength) {
169 return id ? RESOURCE_IMAGE_BASE + id + RESOURCE_IMAGE_EXT : ''; 76 if (strength == 0)
170 }, 77 return 0;
78 return Math.min(Math.trunc((strength - 1) / 25) + 1, 4);
79 },
171 80
172 /** 81 /**
173 * Returns the url for a badge image based on identifier |id|. 82 * @return {boolean}
174 * @param {string} id The identifier describing the badge.
175 * @return {string} The url to use for the badge image 'src' property.
176 * @private 83 * @private
177 */ 84 */
178 toBadgeImageSrc_: function(id) { 85 showTechnology_() {
Dan Beam 2017/01/04 03:10:28 please start a thread about using ES6 object liter
stevenjb 2017/01/04 18:14:04 I have a bad habit of doing this. I don't mean to,
179 return id ? this.toImageSrc_('badge_' + id) : ''; 86 return this.getTechnology_() != '';
180 }, 87 },
181 88
182 /** 89 /**
183 * Sets the icon and badge based on the current state and |strength|. 90 * @return {string}
184 * @param {!NetworkIconParamType} params Set of params describing the icon. 91 * @private
185 * @private 92 */
186 */ 93 getTechnology_() {
187 setIcon_: function(params) { 94 let networkState = this.networkState;
188 var icon = this.$.icon; 95 let type = networkState.Type;
96 if (type == CrOnc.Type.WI_MAX)
97 return 'network:4g';
98 if (type == CrOnc.Type.CELLULAR && networkState.Cellular) {
99 let technology =
100 this.getTechnologyId_(networkState.Cellular.NetworkTechnology);
101 if (technology != '')
102 return 'network:' + technology;
103 }
104 return '';
105 },
189 106
190 var multiLevel = (this.iconType_ == 'wifi' || this.iconType_ == 'mobile'); 107 /**
108 * @param {string|undefined} networkTechnology
109 * @return {string}
110 * @private
111 */
112 getTechnologyId_(networkTechnology) {
113 switch (networkTechnology) {
114 case CrOnc.NetworkTechnology.CDMA1XRTT:
115 return 'badge-1x';
116 case CrOnc.NetworkTechnology.EDGE:
117 return 'badge-edge';
118 case CrOnc.NetworkTechnology.EVDO:
119 return 'badge-evdo';
120 case CrOnc.NetworkTechnology.GPRS:
121 case CrOnc.NetworkTechnology.GSM:
122 return 'badge-gsm';
123 case CrOnc.NetworkTechnology.HSPA:
124 return 'badge-hspa';
125 case CrOnc.NetworkTechnology.HSPA_PLUS:
126 return 'badge-hspa-plus';
127 case CrOnc.NetworkTechnology.LTE:
128 return 'badge-lte';
129 case CrOnc.NetworkTechnology.LTE_ADVANCED:
130 return 'badge-lte-advanced';
131 case CrOnc.NetworkTechnology.UMTS:
132 return 'badge-3g';
133 }
134 return '';
135 },
191 136
192 if (this.networkState && multiLevel) { 137 /**
193 this.setMultiLevelIcon_(params); 138 * @return {boolean}
194 } else { 139 * @private
195 icon.classList.toggle('multi-level', multiLevel); 140 */
196 icon.classList.toggle('level0', multiLevel); 141 showSecure_() {
197 icon.classList.toggle('level1', false); 142 let networkState = this.networkState;
198 icon.classList.toggle('level2', false); 143 if (networkState.Type == CrOnc.Type.WI_FI && networkState.WiFi) {
199 icon.classList.toggle('level3', false); 144 let security = networkState.WiFi.Security;
200 icon.classList.toggle('level4', false); 145 return !!security && security != 'None';
201 } 146 }
202 147 return false;
203 this.setIconBadges_(params); 148 },
204 }, 149 });
205
206 /**
207 * Toggles icon classes based on strength and connecting properties.
208 * |this.networkState| is expected to be specified.
209 * @param {!NetworkIconParamType} params Set of params describing the icon.
210 * @private
211 */
212 setMultiLevelIcon_: function(params) {
213 // Set the strength or connecting properties.
214 var networkState = this.networkState;
215
216 var connectionState = networkState.ConnectionState;
217 var connecting = false;
218 var strength = -1;
219 if (connectionState == CrOnc.ConnectionState.CONNECTING) {
220 strength = 0;
221 connecting = true;
222 } else if (
223 connectionState == CrOnc.ConnectionState.CONNECTED ||
224 !params.showDisconnected) {
225 strength = params.strength || 0;
226 }
227
228 var icon = this.$.icon;
229 icon.classList.toggle('multi-level', true);
230 icon.classList.toggle('connecting', connecting);
231 icon.classList.toggle('level0', strength < 0);
232 icon.classList.toggle('level1', strength >= 0 && strength <= 25);
233 icon.classList.toggle('level2', strength > 25 && strength <= 50);
234 icon.classList.toggle('level3', strength > 50 && strength <= 75);
235 icon.classList.toggle('level4', strength > 75);
236 },
237
238 /**
239 * Sets the icon badge visibility properties: roaming, secure, technology.
240 * @param {!NetworkIconParamType} params Set of params describing the icon.
241 * @private
242 */
243 setIconBadges_: function(params) {
244 var networkState = this.networkState;
245
246 var type = (params.showBadges && networkState) ? networkState.Type : '';
247 if (type == CrOnc.Type.WI_FI) {
248 this.roaming_ = false;
249 var security = networkState.WiFi ? networkState.WiFi.Security : '';
250 this.secure_ = !!security && security != 'None';
251 this.technology_ = '';
252 } else if (type == CrOnc.Type.WI_MAX) {
253 this.roaming_ = false;
254 this.secure_ = false;
255 this.technology_ = '4g';
256 } else if (type == CrOnc.Type.CELLULAR && networkState.Cellular) {
257 this.roaming_ =
258 networkState.Cellular.RoamingState == CrOnc.RoamingState.ROAMING;
259 this.secure_ = false;
260 var oncTechnology = networkState.Cellular.NetworkTechnology;
261 switch (oncTechnology) {
262 case CrOnc.NetworkTechnology.CDMA1XRTT:
263 this.technology_ = '1x';
264 break;
265 case CrOnc.NetworkTechnology.EDGE:
266 this.technology_ = 'edge';
267 break;
268 case CrOnc.NetworkTechnology.EVDO:
269 this.technology_ = 'evdo';
270 break;
271 case CrOnc.NetworkTechnology.GPRS:
272 case CrOnc.NetworkTechnology.GSM:
273 this.technology_ = 'gsm';
274 break;
275 case CrOnc.NetworkTechnology.HSPA:
276 this.technology_ = 'hspa';
277 break;
278 case CrOnc.NetworkTechnology.HSPA_PLUS:
279 this.technology_ = 'hspa_plus';
280 break;
281 case CrOnc.NetworkTechnology.LTE:
282 this.technology_ = 'lte';
283 break;
284 case CrOnc.NetworkTechnology.LTE_ADVANCED:
285 this.technology_ = 'lte_advanced';
286 break;
287 case CrOnc.NetworkTechnology.UMTS:
288 this.technology_ = '3g';
289 break;
290 }
291 } else {
292 this.roaming_ = false;
293 this.secure_ = false;
294 this.technology_ = '';
295 }
296 },
297 });
298 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698