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.
6 * @fileoverview Polymer element for displaying the network state for a specific
7 * type and a list of networks for that type.
10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */
11 var DeviceStateProperties;
14 is: 'network-summary-item',
18 * True if the list is expanded.
23 observer: 'expandedChanged_'
27 * The maximum height in pixels for the list of networks.
35 * True if this item should be hidden. We need this computed property so
36 * that it can default to true, hiding this element, since no changed event
37 * will be fired for deviceState if it is undefined (in NetworkSummary).
42 computed: 'noDeviceState_(deviceState)'
46 * Device state for the network type.
47 * @type {?DeviceStateProperties}
52 observer: 'deviceStateChanged_'
56 * Network state for the active network.
57 * @type {?CrOnc.NetworkStateProperties}
65 * List of all network state data for the network type.
66 * @type {!Array<!CrOnc.NetworkStateProperties>}
70 value: function() { return []; },
71 observer: 'networkStateListChanged_'
76 * Polymer expanded changed method.
78 expandedChanged_: function() {
79 var type = this.deviceState ? this.deviceState.Type : '';
80 this.fire('expanded', {expanded: this.expanded, type: type});
84 * Polymer deviceState changed method.
86 deviceStateChanged_: function() {
87 this.updateSelectable_();
88 if (!this.deviceIsEnabled_(this.deviceState))
89 this.expanded = false;
93 * Polymer networkStateList changed method.
95 networkStateListChanged_: function() {
96 this.updateSelectable_();
100 * @param {?DeviceStateProperties} deviceState The state of a device.
101 * @return {boolean} True if the device state is not set.
104 noDeviceState_: function(deviceState) {
109 * @param {?DeviceStateProperties} deviceState The state of a device.
110 * @param {boolean} expanded The expanded state.
111 * @return {boolean} Whether or not the scanning spinner should be shown.
114 showScanning_: function(deviceState, expanded) {
115 return !!expanded && !!deviceState.Scanning;
119 * @param {?DeviceStateProperties} deviceState The state of a device.
120 * @return {boolean} Whether or not the device state is enabled.
123 deviceIsEnabled_: function(deviceState) {
124 return !!deviceState && deviceState.State == 'Enabled';
128 * @param {?DeviceStateProperties} deviceState The device state.
129 * @return {string} The class value for the device enabled button.
132 getDeviceEnabledButtonClass_: function(deviceState) {
133 var visible = deviceState && deviceState.Type != CrOnc.Type.ETHERNET &&
134 deviceState.Type != CrOnc.Type.VPN;
135 return visible ? '' : 'invisible';
139 * @param {?DeviceStateProperties} deviceState The device state.
140 * @param {!Array<!CrOnc.NetworkStateProperties>} networkList
141 * @return {string} The class value for the expand button.
144 getExpandButtonClass_: function(deviceState, networkList) {
145 var visible = this.expandIsVisible_(deviceState, networkList);
146 return visible ? '' : 'invisible';
150 * @param {?DeviceStateProperties} deviceState The device state.
151 * @param {!Array<!CrOnc.NetworkStateProperties>} networkList
152 * @return {boolean} Whether or not to show the UI to expand the list.
155 expandIsVisible_: function(deviceState, networkList) {
156 if (!this.deviceIsEnabled_(deviceState))
158 var minLength = (this.deviceState.Type == CrOnc.Type.WI_FI) ? 1 : 2;
159 return networkList.length >= minLength;
163 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
164 * @param {boolean} expanded The expanded state.
165 * @return {boolean} True if the 'Known networks' button should be shown.
168 showKnownNetworks_: function(state, expanded) {
169 return !!expanded && !!state && state.Type == CrOnc.Type.WI_FI;
173 * Event triggered when the details div is clicked.
174 * @param {Event} event The enable button event.
177 onDetailsClicked_: function(event) {
178 if ((event.target.id == 'expandListButton') ||
179 (this.deviceState && !this.deviceIsEnabled_(this.deviceState))) {
180 // Already handled or disabled, do nothing.
183 if (this.expandIsVisible_(this.deviceState, this.networkStateList)) {
184 // Expandable, toggle expand.
185 this.expanded = !this.expanded;
188 // Not expandable, fire 'selected' with |networkState|.
189 this.fire('selected', this.networkState);
193 * Event triggered when the known networks button is clicked.
196 onKnownNetworksClicked_: function() {
197 this.fire('show-known-networks', {type: CrOnc.Type.WI_FI});
201 * Event triggered when a network-list-item is the network list is selected.
202 * @param {!{detail: !CrOnc.NetworkStateProperties}} event
205 onListItemSelected_: function(event) {
206 var state = event.detail;
207 this.fire('selected', state);
211 * Event triggered when the enable button is toggled.
212 * @param {!Object} event The enable button event.
215 onDeviceEnabledToggled_: function(event) {
216 var deviceIsEnabled = this.deviceIsEnabled_(this.deviceState);
217 var type = this.deviceState ? this.deviceState.Type : '';
218 this.fire('device-enabled-toggled',
219 {enabled: !deviceIsEnabled, type: type});
220 // Make sure this does not propagate to onDetailsClicked_.
221 event.stopPropagation();
225 * Called whenever the 'selectable' state might change.
228 updateSelectable_: function() {
229 var selectable = this.deviceIsEnabled_(this.deviceState);
230 this.$.details.classList.toggle('selectable', selectable);