Chromium Code Reviews| Index: ui/webui/resources/js/cr/polymer_elements/cr_network_icon/cr-network-icon.js |
| diff --git a/ui/webui/resources/js/cr/polymer_elements/cr_network_icon/cr-network-icon.js b/ui/webui/resources/js/cr/polymer_elements/cr_network_icon/cr-network-icon.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2769368063162c697ef0073d9dbd7c311b5c5576 |
| --- /dev/null |
| +++ b/ui/webui/resources/js/cr/polymer_elements/cr_network_icon/cr-network-icon.js |
| @@ -0,0 +1,166 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview Polymer element for rendering network icons based on ONC |
| + * state properties. |
| + */ |
| + |
| +(function() { |
| + /** |
| + * @typedef({ |
|
michaelpg
2015/02/05 06:45:52
I think this should be @typedef {{
("the outer br
stevenjb
2015/02/05 22:41:34
Done.
|
| + * showDisconnected: boolean, |
| + * iconType: string, |
| + * connected: boolean, |
| + * secure: boolean |
| + */ |
| + var IconParams_; |
|
michaelpg
2015/02/05 06:45:52
why the underscore? Or if this isn't really a vari
stevenjb
2015/02/05 22:41:33
The _ is to indicate that it's private. Added the
Dan Beam
2015/02/05 22:52:35
fwiw, the style guide only asks for _ after privat
|
| + |
| + /** @const */ var kResourceImageBase = |
|
Jeremy Klein
2015/02/05 05:05:30
nit: @const {string} here and below.
michaelpg
2015/02/05 06:45:52
Jeremy, source for that syntax?
From https://deve
Jeremy Klein
2015/02/05 07:30:36
This was added in a compiler release Februrary 201
stevenjb
2015/02/05 22:41:34
Done.
Tyler Breisacher (Chromium)
2015/02/23 20:50:51
Two drive by comments from the compiler team's POV
|
| + 'chrome://resources/images/polymer_elements/cr_network_icon/'; |
| + /** @const */ var kResourceImageExt = '.png'; |
| + |
| + /** |
| + * Gets the network strength from the |networkState| object if available. |
| + * @param {Object} networkState The ONC network state object provided by |
|
Jeremy Klein
2015/02/05 07:30:35
nit: "!Object"
stevenjb
2015/02/05 22:41:34
Done.
|
| + * chrome.networkingPrivate.getState(). |
| + * @return {integer} The strength value, [0-100]. This is always 0 for non |
|
Jeremy Klein
2015/02/05 05:05:30
This should be "number" instead of "integer"
stevenjb
2015/02/05 22:41:34
Done.
|
| + * wireless networks. |
| + */ |
| + function getStrengthFromState(networkState) { |
|
Jeremy Klein
2015/02/05 05:05:30
The signal strength here can probably be a compute
Jeremy Klein
2015/02/05 05:05:30
I feel like these functions can just be inside the
|
| + var type = networkState['Type']; |
| + var strength = 0; |
| + if (type == 'WiFi') { |
| + var wifi = networkState['WiFi']; |
| + if (wifi) |
| + strength = wifi['SignalStrength'] || 0; |
| + } else if (type == 'Cellular') { |
| + var cellular = networkState['Cellular']; |
| + if (cellular) |
| + strength = cellular['SignalStrength'] || 0; |
| + } else if (type == 'WiMAX') { |
| + var wimax = networkState['WiMAX']; |
| + if (wimax) |
| + strength = wimax['SignalStrength'] || 0; |
| + } |
| + return strength; |
|
michaelpg
2015/02/05 06:45:52
This is kind of a lot :-\
Do we need to explicitly
|
| + }; |
| + |
| + /** |
| + * Gets the icon type from the networkState object. This allows multiple |
| + * types (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile). |
|
michaelpg
2015/02/05 06:45:52
"from the |networkType| object"
stevenjb
2015/02/05 22:41:33
Done.
|
| + * @param {string} networkType The ONC network type. |
| + * @return {string} The icon type: ethernet, wifi, mobile, or vpn. |
| + */ |
| + function getIconTypeFromNetworkType(networkType) { |
| + if (networkType == 'Ethernet') { |
| + return 'ethernet'; |
| + } else if (networkType == 'WiFi') { |
| + return 'wifi'; |
| + } else if (networkType == 'Cellular') { |
| + return 'mobile'; |
| + } else if (networkType == 'WiMAX') { |
| + return 'mobile'; |
| + } else if (networkType == 'VPN') { |
| + return 'vpn'; |
| + } |
| + console.log('Unrecognized network type: ' + networkType); |
| + return 'ethernet'; |
| + } |
| + |
| + /** |
| + * Polymer class definition for 'cr-network-icon'. |
| + */ |
| + Polymer('cr-network-icon', { |
| + publish: { |
| + /** |
| + * If true, the icon is part of a list of networks and will not show |
| + * the disconnected image. |
|
michaelpg
2015/02/05 06:45:52
I don't understand this comment. Could the attribu
stevenjb
2015/02/05 22:41:34
The attribute is intended to describe the context,
|
| + * |
| + * @attribute listItem |
| + * @type boolean |
| + * @default false |
|
michaelpg
2015/02/05 06:45:52
So, @attribute and @default aren't used by closure
Jeremy Klein
2015/02/05 07:30:36
I wonder if it might be worth trying to get the do
stevenjb
2015/02/05 22:41:33
So, I just copy/pasted this from paper-button.html
|
| + */ |
| + listItem: false |
| + }, |
| + |
| + /** |
| + * Updates the icon based on |networkState|. Should be called whenever the |
| + * state changes. |
| + * @param {Object} networkState The ONC network state object provided by |
|
Jeremy Klein
2015/02/05 07:30:36
Is there any stronger type we can use for the ONC
stevenjb
2015/02/05 22:41:34
I am going to re-factor this part and create a sep
|
| + * chrome.networkingPrivate.getState(). |
|
michaelpg
2015/02/05 06:45:52
nit: 4 space indent (5 spaces total)
stevenjb
2015/02/05 22:41:34
Done.
|
| + */ |
| + setNetworkState: function(networkState) { |
|
Jeremy Klein
2015/02/05 05:05:30
It seems like the network state and network type c
stevenjb
2015/02/05 22:41:34
I think that may be possible with the re-factor, w
|
| + var params = /** @type {IconParams} */ {}; |
| + params.showDisconnected = !this.$.listItem; |
|
michaelpg
2015/02/05 06:45:52
Does this work?
The members of this.$ should be t
stevenjb
2015/02/05 22:41:33
Done.
|
| + params.iconType = getIconTypeFromNetworkType(networkState['Type']); |
| + |
| + if (params.iconType_ == 'wifi') { |
| + var wifi = networkState['WiFi']; |
| + if (wifi && wifi['Security'] && wifi['Security'] != 'None') |
| + params.secure = true; |
| + } |
| + params.connected = networkState['ConnectionState'] == 'Connected'; |
| + |
| + params.strength = getStrengthFromState(networkState); |
| + this.setIcon_(params); |
| + }, |
| + |
| + /** |
| + * Provides the ONC network type to display a disconnected icon for the |
| + * specified type. |
|
michaelpg
2015/02/05 06:45:52
4+1=5 spaces
stevenjb
2015/02/05 22:41:34
Done.
|
| + * @param {string} type The ONC network type. |
| + */ |
| + setNetworkType: function(networkType) { |
| + var params = /** @type {IconParams} */ {}; |
| + params.showDisconnected = true; |
| + params.iconType = getIconTypeFromNetworkType(networkType); |
| + params.connected = false; |
| + params.secure = false; |
| + params.strenght = 0; |
|
michaelpg
2015/02/05 06:45:52
s/strenght/strength/
& strength isn't listed in t
|
| + |
| + this.setIcon_(params); |
| + }, |
| + |
| + /** |
| + * Sets the icon and badge based on the current state and |strength|. |
| + * @param {IconParams} params The set of params describing the icon. |
|
Jeremy Klein
2015/02/05 07:30:35
nit: "!IconParams" to ensure non-null.
stevenjb
2015/02/05 22:41:33
Done.
|
| + * @private |
| + */ |
| + setIcon_: function(params) { |
| + var iconOffset; |
| + if (params.iconType == 'wifi' || params.iconType == 'mobile') { |
| + if (params.showDisconnected && !params.connected) { |
|
michaelpg
2015/02/05 06:45:52
nit: remove unnecessary braces for single-line if
stevenjb
2015/02/05 22:41:34
I chrome both are valid. For single if statements
michaelpg
2015/02/06 18:52:38
I'll leave that up to dbeam@.
James Hawkins
2015/02/06 18:58:45
The style rule from [1] is to leave the braces off
|
| + iconOffset = '0px'; |
| + } else if (params.strength <= 25) { |
| + iconOffset = '-100%'; |
| + } else if (params.strength <= 50) { |
| + iconOffset = '-200%'; |
| + } else if (params.strength <= 75) { |
| + iconOffset = '-300%'; |
| + } else { |
| + iconOffset = '-400%'; |
| + } |
| + } |
| + |
| + var icon = this.$.icon; |
| + if (params.iconType) { |
| + icon.src = kResourceImageBase + params.iconType + kResourceImageExt; |
| + if (iconOffset) { |
| + icon.style.height = '500%'; |
| + icon.style.top = iconOffset; |
| + } |
| + } |
| + |
| + var badge = this.$.badge; |
| + if (params.secure) { |
| + badge.style.display = undefined; |
|
michaelpg
2015/02/05 06:45:52
nit(?): badge.style.display = '';
stevenjb
2015/02/05 22:41:34
So, undefined clearly sets the behavior to whateve
michaelpg
2015/02/06 18:52:38
Actually this is not a nit, it seems to be a bug:
|
| + badge.src = kResourceImageBase + 'secure' + kResourceImageExt; |
| + } else { |
| + badge.style.display = 'none'; |
| + } |
| + } |
| + |
| + }); |
| +})(); |