Chromium Code Reviews

Unified 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 + Elim extra div in icon Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Index: ui/webui/resources/cr_elements/cr_network_icon/cr_network_icon.js
diff --git a/ui/webui/resources/cr_elements/cr_network_icon/cr_network_icon.js b/ui/webui/resources/cr_elements/cr_network_icon/cr_network_icon.js
new file mode 100644
index 0000000000000000000000000000000000000000..f970f00dafd3ae9f70da9551e8addeea7f9db49a
--- /dev/null
+++ b/ui/webui/resources/cr_elements/cr_network_icon/cr_network_icon.js
@@ -0,0 +1,178 @@
+// 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.
+
+
Dan Beam 2015/02/20 01:02:43 why \n\n?
stevenjb 2015/02/20 02:22:47 Some places we do \n\n before /**, other places we
michaelpg 2015/02/20 10:00:17 closure (compiler? linter?) complains if you don't
stevenjb 2015/02/23 19:23:08 This doesn't complain: ./tools/gyp/gyp --depth . c
Dan Beam 2015/02/23 19:27:59 the linter runs on `git cl presubmit`, but doesn't
+/**
+ * @fileoverview Polymer element for rendering network icons based on ONC
+ * state properties.
+ */
+
+(function() {
+ /**
+ * @typedef {{
+ * showDisconnected: boolean,
+ * iconType: string,
+ * connected: boolean,
+ * secure: boolean,
+ * strength: number }}
+ */
+ var IconParams;
+
+ /** @const {string} */ var RESOURCE_IMAGE_BASE =
+ 'chrome://resources/cr_elements/cr_network_icon/';
+ /** @const {string} */ var RESOURCE_IMAGE_EXT = '.png';
+
+
+ /**
+ * Gets the icon type from the network type. This allows multiple types
+ * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile).
+ * @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';
+ }
Dan Beam 2015/02/20 01:02:43 no curlies
stevenjb 2015/02/20 02:22:47 Done (although some of us discussed this, and I pr
Dan Beam 2015/02/20 04:01:24 consensus in chrome js is no curlies for consisten
stevenjb 2015/02/23 19:23:08 I'll skip any jokes about "consensus in chrome" :)
Dan Beam 2015/02/23 19:43:02 fwiw, I'm shutting up about style in your new code
+ console.error('Unrecognized network type: ' + networkType);
Dan Beam 2015/02/20 01:02:43 assertNotReached()
stevenjb 2015/02/20 02:22:47 I don't think this should assert. The input to thi
+ return 'ethernet';
+ }
+
+
+ /**
+ * Polymer class definition for 'cr-network-icon'.
+ * @element cr-network-icon
+ */
+ Polymer('cr-network-icon', {
+ publish: {
+ /**
+ * If set, the ONC data properties will be used to display the icon.
+ *
+ * @attribute networkState
+ * @type CrOncDataElement
+ * @default null
+ */
+ networkState: null,
+
+
Dan Beam 2015/02/20 01:02:43 why \n\n?
stevenjb 2015/02/20 02:22:47 I copied this pattern from somewhere, maybe Polyme
michaelpg 2015/02/20 10:00:17 I might've been the one to introduce this, thinkin
stevenjb 2015/02/23 19:23:08 Again, let's fix closure if we need to. If we can'
+ /**
+ * If set, the ONC network type will be used to display the icon.
+ *
+ * @attribute networkType
+ * @type string
Dan Beam 2015/02/20 01:02:43 can this be string|undefined?
Jeremy Klein 2015/02/20 01:23:59 I think it'd be nice to be consistent between null
stevenjb 2015/02/20 02:22:47 I guess the C++ programmer in me associates 'null'
+ * @default undefined
+ */
+ networkType: undefined,
+
+
+ /**
+ * If true, the icon is part of a list of networks and may be displayed
+ * differently, e.g. the disconnected image will never be shown for
+ * list items.
+ *
+ * @attribute isListItem
+ * @type boolean
+ * @default false
+ */
+ isListItem: false
+ },
+
Dan Beam 2015/02/20 01:02:43 assuming this automatically gets called as part of
stevenjb 2015/02/20 02:22:47 We haven't been doing this elsewhere. Jeremy, Mich
Dan Beam 2015/02/20 04:01:23 yes
michaelpg 2015/02/20 10:00:17 meh, I'm kind of indifferent, @override is useful
Jeremy Klein 2015/02/20 18:32:26 +1 to @override for lifecycle callbacks.
stevenjb 2015/02/23 19:23:08 I like /** @override */ for all the reasons we hav
+ attached: function() {
+ var params = /** @type {IconParams} */ {
+ showDisconnected: true,
+ iconType: 'ethernet',
+ secure: false,
+ connected: false,
+ strength: 0
+ };
+ this.setIcon_(params);
+ },
+
+ /**
+ * Updates the icon based on network state when the networkState property
+ * changes.
+ * @param {CrOncDataElement} oldValue Ignored.
Dan Beam 2015/02/20 01:02:42 why is oldValue sent then?
Jeremy Klein 2015/02/20 01:23:59 This is just built into the polymer library as a c
stevenjb 2015/02/20 02:22:47 I don't especially like the idea of overriding foo
michaelpg 2015/02/20 10:00:17 Agreed.
Jeremy Klein 2015/02/20 18:32:26 I'm fine with that, but keep in mind that leaving
stevenjb 2015/02/23 19:23:08 After thinking about the @override question above,
Dan Beam 2015/02/23 19:27:59 difference is that closure compiler actually enfor
+ * @param {CrOncDataElement} newValue The new network state used to set the
+ * icon.
+ */
+ networkStateChanged: function(oldValue, newValue) {
+ var networkState = newValue;
+ var iconType = getIconTypeFromNetworkType(networkState.data.Type);
+ var params = /** @type {IconParams} */ {
+ showDisconnected: !this.isListItem,
+ iconType: iconType,
+ secure: iconType == 'wifi' && networkState.getWiFiSecurity() != 'None',
+ connected: networkState.data.ConnectionState == 'Connected',
+ strength: networkState.getStrength()
Dan Beam 2015/02/20 01:02:42 nit: alpha, add trailing ,
stevenjb 2015/02/20 02:22:47 alpha? I assume all object definitions should inc
Dan Beam 2015/02/20 04:01:24 betize
stevenjb 2015/02/23 19:23:08 Done.
+ };
+ this.setIcon_(params);
+ },
+
+
+ /**
+ * Updates the icon based on network type when the networkType property
+ * changes.
+ * @param {string} oldValue Ignored.
+ * @param {string} newValue The new network type used to set the icon.
+ */
+ networkTypeChanged: function(oldValue, newValue) {
+ var networkType = newValue;
Dan Beam 2015/02/20 01:02:43 opt nit: maybe find a way to explain that newValue
Jeremy Klein 2015/02/20 01:23:59 My suggestion above probably resolves this.
stevenjb 2015/02/20 02:22:47 I'm not sure what 'is a networkType' means exactly
Dan Beam 2015/02/20 04:01:23 the fact that you wrote `var networkType = newValu
stevenjb 2015/02/23 19:23:08 OK, I wasn't familiar with @enum. I created CrOnc.
+ var params = /** @type {IconParams} */ {
+ showDisconnected: true,
+ iconType: getIconTypeFromNetworkType(networkType),
+ connected: false,
+ secure: false,
+ strength: 0
+ };
+ 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.
+ * @private
+ */
+ setIcon_: function(params) {
+ var iconOffset;
+ if (params.iconType == 'wifi' || params.iconType == 'mobile') {
+ if (params.showDisconnected && !params.connected) {
+ iconOffset = '0px';
Dan Beam 2015/02/20 01:02:42 nit: why not use numbers? offset + '%' should work
stevenjb 2015/02/20 02:22:47 You mean e.g. iconOffset = -100, then icon.style.t
+ } else if (params.strength <= 25) {
+ iconOffset = '-100%';
+ } else if (params.strength <= 50) {
+ iconOffset = '-200%';
+ } else if (params.strength <= 75) {
+ iconOffset = '-300%';
+ } else {
+ iconOffset = '-400%';
+ }
Dan Beam 2015/02/20 01:02:43 no curlies
stevenjb 2015/02/20 02:22:47 Done.
+ }
+
+ var icon = this.$.icon;
+ if (params.iconType) {
+ icon.src = RESOURCE_IMAGE_BASE + params.iconType + RESOURCE_IMAGE_EXT;
+ if (iconOffset) {
+ icon.style.height = '500%';
+ icon.style.top = iconOffset;
+ }
+ }
+
+ var badge = this.$.badge;
+ if (params.secure) {
+ badge.style.display = '';
+ badge.src = RESOURCE_IMAGE_BASE + 'secure' + RESOURCE_IMAGE_EXT;
+ } else {
+ badge.style.display = 'none';
Dan Beam 2015/02/20 01:02:43 don't do this. use .hidden = true/false
stevenjb 2015/02/20 02:22:47 Done.
+ }
+ }
+ });
+})();

Powered by Google App Engine