summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/internet_page/network_summary_item.js
blob: f363bf44bd85784533fc6aa4f175eb8224ca0709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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 displaying the network state for a specific
 * type and a list of networks for that type.
 */

/** @typedef {chrome.networkingPrivate.DeviceStateProperties} */
var DeviceStateProperties;

Polymer({
  is: 'network-summary-item',

  properties: {
    /**
     * True if the list is expanded.
     */
    expanded: {
      type: Boolean,
      value: false,
      observer: 'expandedChanged_'
    },

    /**
     * The maximum height in pixels for the list of networks.
     */
    maxHeight: {
      type: Number,
      value: 200
    },

    /**
     * True if this item should be hidden. We need this computed property so
     * that it can default to true, hiding this element, since no changed event
     * will be fired for deviceState if it is undefined (in NetworkSummary).
     */
    isHidden: {
      type: Boolean,
      value: true,
      computed: 'noDeviceState_(deviceState)'
    },

    /**
     * Device state for the network type.
     * @type {DeviceStateProperties|undefined}
     */
    deviceState: {
      type: Object,
      observer: 'deviceStateChanged_'
    },

    /**
     * Network state for the active network.
     * @type {!CrOnc.NetworkStateProperties|undefined}
     */
    networkState: {
      type: Object,
    },

    /**
     * List of all network state data for the network type.
     * @type {!Array<!CrOnc.NetworkStateProperties>}
     */
    networkStateList: {
      type: Array,
      value: function() { return []; },
      observer: 'networkStateListChanged_'
    }
  },

  /**
   * Polymer expanded changed method.
   */
  expandedChanged_: function() {
    var type = this.deviceState ? this.deviceState.Type : '';
    this.fire('expanded', {expanded: this.expanded, type: type});
  },

  /**
   * Polymer deviceState changed method.
   */
  deviceStateChanged_: function() {
    this.updateSelectable_();
    if (this.expanded && !this.deviceIsEnabled_(this.deviceState))
      this.expanded = false;
  },

  /**
   * Polymer networkStateList changed method.
   */
  networkStateListChanged_: function() {
    this.updateSelectable_();
  },

  /**
   * @param {DeviceStateProperties} deviceState
   * @return {boolean} True if the device state is not set.
   * @private
   */
  noDeviceState_: function(deviceState) {
    return !deviceState;
  },

  /**
   * @param {DeviceStateProperties} deviceState
   * @param {boolean} expanded The expanded state.
   * @return {boolean} Whether or not the scanning spinner should be shown.
   * @private
   */
  showScanning_: function(deviceState, expanded) {
    return !!expanded && !!deviceState.Scanning;
  },

  /**
   * @param {DeviceStateProperties|undefined} deviceState
   * @return {boolean} Whether or not the device state is enabled.
   * @private
   */
  deviceIsEnabled_: function(deviceState) {
    return !!deviceState && deviceState.State == 'Enabled';
  },

  /**
   * @param {DeviceStateProperties} deviceState
   * @return {string} The class value for the device enabled button.
   * @private
   */
  getDeviceEnabledButtonClass_: function(deviceState) {
    var visible = deviceState && deviceState.Type != CrOnc.Type.ETHERNET &&
                  deviceState.Type != CrOnc.Type.VPN;
    return visible ? '' : 'invisible';
  },

  /**
   * @param {DeviceStateProperties} deviceState
   * @param {!Array<!CrOnc.NetworkStateProperties>} networkList
   * @return {string} The class value for the expand button.
   * @private
   */
  getExpandButtonClass_: function(deviceState, networkList) {
    var visible = this.expandIsVisible_(deviceState, networkList);
    return visible ? '' : 'invisible';
  },

  /**
   * @param {DeviceStateProperties|undefined} deviceState
   * @param {!Array<!CrOnc.NetworkStateProperties>} networkList
   * @return {boolean} Whether or not to show the UI to expand the list.
   * @private
   */
  expandIsVisible_: function(deviceState, networkList) {
    if (!this.deviceIsEnabled_(deviceState))
      return false;
    var minLength = (this.deviceState.Type == CrOnc.Type.WI_FI) ? 1 : 2;
    return networkList.length >= minLength;
  },

  /**
   * @param {!CrOnc.NetworkStateProperties} state
   * @param {boolean} expanded The expanded state.
   * @return {boolean} True if the 'Known networks' button should be shown.
   * @private
   */
  showKnownNetworks_: function(state, expanded) {
    return !!expanded && !!state && state.Type == CrOnc.Type.WI_FI;
  },

  /**
   * Event triggered when the details div is tapped.
   * @param {Event} event The enable button event.
   * @private
   */
  onDetailsTap_: function(event) {
    if ((event.target.id == 'expandListButton') ||
        (this.deviceState && !this.deviceIsEnabled_(this.deviceState))) {
      // Already handled or disabled, do nothing.
      return;
    }
    if (this.expandIsVisible_(this.deviceState, this.networkStateList)) {
      // Expandable, toggle expand.
      this.expanded = !this.expanded;
      return;
    }
    // Not expandable, fire 'selected' with |networkState|.
    this.fire('selected', this.networkState);
  },

  /**
   * Event triggered when the known networks button is tapped.
   * @private
   */
  onKnownNetworksTap_: function() {
    this.fire('show-known-networks', {type: CrOnc.Type.WI_FI});
  },

  /**
   * Event triggered when the enable button is toggled.
   * @param {!Event} event
   * @private
   */
  onDeviceEnabledTap_: function(event) {
    var deviceIsEnabled = this.deviceIsEnabled_(this.deviceState);
    var type = this.deviceState ? this.deviceState.Type : '';
    this.fire('device-enabled-toggled',
              {enabled: !deviceIsEnabled, type: type});
    // Make sure this does not propagate to onDetailsTap_.
    event.stopPropagation();
  },

  /**
   * Called whenever the 'selectable' state might change.
   * @private
   */
  updateSelectable_: function() {
    var selectable = this.deviceIsEnabled_(this.deviceState);
    this.$.details.classList.toggle('selectable', selectable);
  },
});