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

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

Issue 874283006: Add custom Polymer network icon element (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add cr-onc-data, move files, rebase Created 5 years, 10 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 /**
6 * @fileoverview Polymer element for rendering network icons based on ONC
7 * state properties.
8 */
9
10 (function() {
11 /**
12 * @typedef {{
13 * showDisconnected: boolean,
14 * iconType: string,
15 * connected: boolean,
16 * secure: boolean,
17 * strength: number }}
18 * @private
19 */
20 var IconParams_;
michaelpg 2015/02/09 19:38:54 Still think this should neither be @private nor en
Jeremy Klein 2015/02/09 19:45:49 I'm fine with that too, as long as the '_' and '@p
stevenjb 2015/02/10 00:22:04 Done.
21
22 /** @const {string} */ var kResourceImageBase =
23 'chrome://resources/cr_elements/cr_network_icon/';
24 /** @const {string} */ var kResourceImageExt = '.png';
25
26 /**
27 * Gets the icon type from the network type. This allows multiple types
28 * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile).
29 * @param {string} networkType The ONC network type.
30 * @return {string} The icon type: ethernet, wifi, mobile, or vpn.
31 */
32 function getIconTypeFromNetworkType(networkType) {
33 if (networkType == 'Ethernet') {
34 return 'ethernet';
35 } else if (networkType == 'WiFi') {
36 return 'wifi';
37 } else if (networkType == 'Cellular') {
38 return 'mobile';
39 } else if (networkType == 'WiMAX') {
40 return 'mobile';
41 } else if (networkType == 'VPN') {
42 return 'vpn';
43 }
44 console.error('Unrecognized network type: ' + networkType);
45 return 'ethernet';
46 }
47
48 /**
49 * Polymer class definition for 'cr-network-icon'.
50 */
51 Polymer('cr-network-icon', {
52 publish: {
53 /**
54 * If set, the ONC data properties will be used to display the icon.
55 *
56 * @attribute networkState
57 * @type cr-onc-data
Jeremy Klein 2015/02/09 18:16:31 I guess this one is a weird case because it's a Po
michaelpg 2015/02/09 19:38:54 More generally we need to figure out which flavor
Jeremy Klein 2015/02/09 19:45:49 The compiler pass I'm going to write for Polymer({
stevenjb 2015/02/10 00:22:04 I'm going to make this Element for now, we can fix
58 * @default {}
59 */
60 networkState: {},
61
62 /**
63 * If set, the ONC network type will be used to display the icon.
64 *
65 * @attribute networkType
66 * @type string
67 * @default undefined
68 */
69 networkType: undefined,
70
71 /**
72 * If true, the icon is part of a list of networks and may be displayed
73 * differently, e.g. the disconnected image will never be shown for
74 * list items.
75 *
76 * @attribute isListItem
77 * @type boolean
78 * @default false
79 */
80 isListItem: false
81 },
82
83 /**
84 * Updates the icon based on network state when the networkState property
85 * changes.
86 * @param {cr-onc-data} oldValue Ignored.
Jeremy Klein 2015/02/09 18:16:31 See other comment. The correct format would be CrO
stevenjb 2015/02/10 00:22:04 I'm still uncomfortable using a type that isn't se
87 * @param {cr-onc-data} newValue The new network state used to set the icon.
88 */
89 networkStateChanged: function(oldValue, newValue) {
90 var networkState = newValue;
91 var params = /** @type {IconParams} */ {};
Jeremy Klein 2015/02/09 18:16:31 IconParams_
stevenjb 2015/02/10 00:22:04 Acknowledged.
92 params.showDisconnected = !this.isListItem;
93 params.iconType = getIconTypeFromNetworkType(networkState.data.Type);
94 if (params.iconType_ == 'wifi')
95 params.secure = networkState.getWiFiSecurity() != 'None';
96 params.connected = networkState.data.ConnectionState == 'Connected';
97 params.strength = networkState.getStrength();
98
99 this.setIcon_(params);
100 },
101
102 /**
103 * Updates the icon based on network type when the networkType property
104 * changes.
105 * @param {string} oldValue Ignored.
106 * @param {string} newValue The new network type used to set the icon.
107 */
108 networkTypeChanged: function(oldValue, newValue) {
109 var networkType = newValue;
110 var params = /** @type {IconParams} */ {};
Jeremy Klein 2015/02/09 18:16:31 IconParams_
stevenjb 2015/02/10 00:22:04 Acknowledged.
111 params.showDisconnected = true;
112 params.iconType = getIconTypeFromNetworkType(networkType);
113 params.connected = false;
114 params.secure = false;
115 params.strength = 0;
116
117 this.setIcon_(params);
118 },
119
120 /**
121 * Sets the icon and badge based on the current state and |strength|.
122 * @param {!IconParams} params The set of params describing the icon.
Jeremy Klein 2015/02/09 18:16:31 IconParams_
stevenjb 2015/02/10 00:22:04 Acknowledged.
123 * @private
124 */
125 setIcon_: function(params) {
126 var iconOffset;
127 if (params.iconType == 'wifi' || params.iconType == 'mobile') {
128 if (params.showDisconnected && !params.connected) {
129 iconOffset = '0px';
130 } else if (params.strength <= 25) {
131 iconOffset = '-100%';
132 } else if (params.strength <= 50) {
133 iconOffset = '-200%';
134 } else if (params.strength <= 75) {
135 iconOffset = '-300%';
136 } else {
137 iconOffset = '-400%';
138 }
139 }
140
141 var icon = this.$.icon;
142 if (params.iconType) {
143 icon.src = kResourceImageBase + params.iconType + kResourceImageExt;
144 if (iconOffset) {
145 icon.style.height = '500%';
146 icon.style.top = iconOffset;
147 }
148 }
149
150 var badge = this.$.badge;
151 if (params.secure) {
152 badge.style.display = '';
153 badge.src = kResourceImageBase + 'secure' + kResourceImageExt;
154 } else {
155 badge.style.display = 'none';
156 }
157 }
158
159 });
160 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698