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

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: Rebase + use CrOnc.NetworkConfigType 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 /**
michaelpg 2015/02/10 08:11:25 If we're following jhawkin's suggested convention,
stevenjb 2015/02/11 00:16:12 Done.
stevenjb 2015/02/11 00:16:12 Done.
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 */
19 var IconParams;
20
21 /** @const {string} */ var kResourceImageBase =
michaelpg 2015/02/10 08:11:25 RESOURCE_IMAGE_BASE, RESOURCE_IMAGE_EXT 1. https:
stevenjb 2015/02/11 00:16:12 Done.
22 'chrome://resources/cr_elements/cr_network_icon/';
23 /** @const {string} */ var kResourceImageExt = '.png';
24
25 /**
michaelpg 2015/02/10 08:11:25 These aren't technically top-level blocks so we do
stevenjb 2015/02/11 00:16:12 I went ahead and added them, since the only reason
26 * Gets the icon type from the network type. This allows multiple types
27 * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile).
28 * @param {string} networkType The ONC network type.
29 * @return {string} The icon type: ethernet, wifi, mobile, or vpn.
30 */
31 function getIconTypeFromNetworkType(networkType) {
32 if (networkType == 'Ethernet') {
33 return 'ethernet';
34 } else if (networkType == 'WiFi') {
35 return 'wifi';
36 } else if (networkType == 'Cellular') {
37 return 'mobile';
38 } else if (networkType == 'WiMAX') {
39 return 'mobile';
40 } else if (networkType == 'VPN') {
41 return 'vpn';
42 }
43 console.error('Unrecognized network type: ' + networkType);
44 return 'ethernet';
45 }
46
47 /**
48 * Polymer class definition for 'cr-network-icon'.
michaelpg 2015/02/10 08:11:25 @element cr-network-icon
stevenjb 2015/02/11 00:16:12 Done.
49 */
50 Polymer('cr-network-icon', {
51 publish: {
52 /**
53 * If set, the ONC data properties will be used to display the icon.
54 *
55 * @attribute networkState
56 * @type Element
57 * @default {}
58 */
59 networkState: {},
michaelpg 2015/02/10 08:11:25 I would encourage you to set this to null and set
stevenjb 2015/02/11 00:16:11 Done. I also added a created() method here that in
60
61 /**
62 * If set, the ONC network type will be used to display the icon.
63 *
64 * @attribute networkType
65 * @type string
66 * @default undefined
67 */
68 networkType: undefined,
69
70 /**
71 * If true, the icon is part of a list of networks and may be displayed
72 * differently, e.g. the disconnected image will never be shown for
73 * list items.
74 *
75 * @attribute isListItem
76 * @type boolean
77 * @default false
78 */
79 isListItem: false
80 },
81
82 /**
83 * Updates the icon based on network state when the networkState property
84 * changes.
85 * @param {Element} oldValue Ignored.
86 * @param {Element} newValue The new network state used to set the icon.
87 */
88 networkStateChanged: function(oldValue, newValue) {
89 var networkState = newValue;
90 var params = /** @type {IconParams} */ {};
91 params.showDisconnected = !this.isListItem;
92 params.iconType = getIconTypeFromNetworkType(networkState.data.Type);
93 if (params.iconType_ == 'wifi')
94 params.secure = networkState.getWiFiSecurity() != 'None';
95 params.connected = networkState.data.ConnectionState == 'Connected';
96 params.strength = networkState.getStrength();
97
98 this.setIcon_(params);
99 },
100
101 /**
102 * Updates the icon based on network type when the networkType property
103 * changes.
104 * @param {string} oldValue Ignored.
105 * @param {string} newValue The new network type used to set the icon.
106 */
107 networkTypeChanged: function(oldValue, newValue) {
108 var networkType = newValue;
109 var params = /** @type {IconParams} */ {};
110 params.showDisconnected = true;
111 params.iconType = getIconTypeFromNetworkType(networkType);
112 params.connected = false;
113 params.secure = false;
114 params.strength = 0;
115
116 this.setIcon_(params);
117 },
118
119 /**
120 * Sets the icon and badge based on the current state and |strength|.
121 * @param {!IconParams} params The set of params describing the icon.
122 * @private
123 */
124 setIcon_: function(params) {
125 var iconOffset;
126 if (params.iconType == 'wifi' || params.iconType == 'mobile') {
127 if (params.showDisconnected && !params.connected) {
128 iconOffset = '0px';
129 } else if (params.strength <= 25) {
130 iconOffset = '-100%';
131 } else if (params.strength <= 50) {
132 iconOffset = '-200%';
133 } else if (params.strength <= 75) {
134 iconOffset = '-300%';
135 } else {
136 iconOffset = '-400%';
137 }
138 }
139
140 var icon = this.$.icon;
141 if (params.iconType) {
142 icon.src = kResourceImageBase + params.iconType + kResourceImageExt;
143 if (iconOffset) {
144 icon.style.height = '500%';
145 icon.style.top = iconOffset;
146 }
147 }
148
149 var badge = this.$.badge;
150 if (params.secure) {
151 badge.style.display = '';
152 badge.src = kResourceImageBase + 'secure' + kResourceImageExt;
153 } else {
154 badge.style.display = 'none';
155 }
156 }
157
michaelpg 2015/02/10 08:11:25 nit: remove extra space?
stevenjb 2015/02/11 00:16:12 Done.
158 });
159 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698