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 and modifying a list of cellular
13 is
: 'network-apnlist',
17 * The current state for the network matching |guid|.
18 * @type {?CrOnc.NetworkStateProperties}
23 observer
: 'networkStateChanged_'
27 * The CrOnc.APNProperties.AccessPointName value of the selected APN.
35 * Selectable list of APN dictionaries for the UI. Includes an entry
36 * corresponding to |otherApn| (see below).
37 * @type {!Array<!CrOnc.APNProperties>}
41 value: function() { return []; }
45 * The user settable properties for a new ('other') APN. The values for
46 * AccessPointName, Username, and Password will be set to the currently
47 * active APN if it does not match an existing list entry.
48 * @type {?CrOnc.APNProperties}
56 * Array of property names to pass to the Other APN property list.
57 * @type {!Array<string>}
62 return ['AccessPointName', 'Username', 'Password'];
68 * Array of edit types to pass to the Other APN property list.
74 'AccessPointName': 'String',
83 /** @const */ DefaultAccessPointName
: 'none',
86 * Polymer networkState changed method.
88 networkStateChanged_: function() {
89 if (!this.networkState
|| !this.networkState
.Cellular
)
93 var cellular
= this.networkState
.Cellular
;
94 if (cellular
.APN
&& cellular
.APN
.AccessPointName
)
95 activeApn
= cellular
.APN
;
96 else if (cellular
.LastGoodAPN
&& cellular
.LastGoodAPN
.AccessPointName
)
97 activeApn
= cellular
.LastGoodAPN
;
98 this.setApnSelectList_(activeApn
);
102 * Sets the list of selectable APNs for the UI. Appends an 'Other' entry
103 * (see comments for |otherApn| above).
104 * @param {?CrOnc.APNProperties} activeApn The currently active APN value.
107 setApnSelectList_: function(activeApn
) {
108 // Copy the list of APNs from this.networkState.
109 var result
= this.getApnList_().slice();
111 // Test whether |activeApn| is in the current APN list in this.networkState.
112 var activeApnInList
= activeApn
&& result
.some(
113 function(a
) { return a
.AccessPointName
== activeApn
.AccessPointName
; });
115 // If |activeApn| is specified and not in the list, use the active
116 // properties for 'other'. Otherwise use any existing 'other' properties.
117 var otherApnProperties
=
118 (activeApn
&& !activeApnInList
) ? activeApn
: this.otherApn
;
119 var otherApn
= this.createApnObject_(otherApnProperties
);
121 // Always use 'Other' for the name of custom APN entries (the name does
123 otherApn
.Name
= 'Other';
125 // If no 'active' or 'other' AccessPointName was provided, use the default.
126 otherApn
.AccessPointName
=
127 otherApn
.AccessPointName
|| this.DefaultAccessPointName
;
129 // Save the 'other' properties.
130 this.otherApn
= otherApn
;
132 // Append 'other' to the end of the list of APNs.
133 result
.push(otherApn
);
135 this.set('apnSelectList', result
);
138 (activeApn
&& activeApn
.AccessPointName
) || otherApn
.AccessPointName
);
142 * @param {?CrOnc.APNProperties=} apnProperties
143 * @return {!CrOnc.APNProperties} A new APN object with properties from
144 * |apnProperties| if provided.
147 createApnObject_: function(apnProperties
) {
148 var newApn
= {AccessPointName
: ''};
150 Object
.assign(newApn
, apnProperties
);
155 * @return {!Array<!CrOnc.APNProperties>} The list of APN properties in
156 * |networkState| or an empty list if the property is not set.
159 getApnList_: function() {
160 var apnList
= /** @type {Array<!CrOnc.APNProperties>|undefined} */(
161 CrOnc
.getActiveValue(this.networkState
, 'Cellular.APNList'));
162 return apnList
|| [];
166 * We need to update the select value after the dom-repeat template updates:
167 * 1. Rebuilding the template options resets the select value property.
168 * 2. The template update occurs after any property changed events.
169 * TODO(stevenjb): Remove once we use cr-dropdown-menu which (hopefully)
170 * won't require this.
173 onSelectApnUpdated_: function() {
174 this.$.selectApn
.value
= this.selectedApn
;
178 * Event triggered when the selectApn selection changes.
179 * @param {Event} event The select node change event.
182 onSelectApnChange_: function(event
) {
183 var selectedApn
= event
.target
.value
;
184 // When selecting 'Other', don't set a change event unless a valid
185 // non-default value has been set for Other.
186 if (this.isOtherSelected_(this.networkState
, selectedApn
) &&
187 (!this.otherApn
|| !this.otherApn
.AccessPointName
||
188 this.otherApn
.AccessPointName
== this.DefaultAccessPointName
)) {
191 this.sendApnChange_(selectedApn
);
195 * Event triggered when any 'Other' APN network property changes.
196 * @param {!{detail: {field: string, value: string}}} event
199 onOtherApnChange_: function(event
) {
200 this.set('otherApn.' + event
.detail
.field
, event
.detail
.value
);
201 // Don't send a change event for 'Other' until the 'Save' button is clicked.
205 * Event triggered when the Other APN 'Save' button is clicked.
206 * @param {Event} event
209 onSaveOther_: function(event
) {
210 this.sendApnChange_(this.selectedApn
);
214 * Send the apn-change event.
215 * @param {string} selectedApn
218 sendApnChange_: function(selectedApn
) {
219 var apnList
= this.getApnList_();
220 var apn
= this.findApnInList(apnList
, selectedApn
);
221 if (apn
== undefined) {
222 apn
= this.createApnObject_();
224 apn
.AccessPointName
= this.otherApn
.AccessPointName
;
225 apn
.Username
= this.otherApn
.Username
;
226 apn
.Password
= this.otherApn
.Password
;
229 this.fire('apn-change', {field
: 'APN', value
: apn
});
233 * @param {?CrOnc.NetworkStateProperties} networkState
234 * @param {string} selectedApn
235 * @return {boolean} True if the 'other' APN is currently selected.
238 isOtherSelected_: function(networkState
, selectedApn
) {
239 if (!networkState
|| !networkState
.Cellular
)
241 var apnList
= this.getApnList_();
242 var apn
= this.findApnInList(apnList
, selectedApn
);
243 return apn
== undefined;
247 * @param {!CrOnc.APNProperties} apn
248 * @return {string} The most descriptive name for the access point.
251 apnDesc_: function(apn
) {
252 return apn
.LocalizedName
|| apn
.Name
|| apn
.AccessPointName
;
256 * @param {!Array<!CrOnc.APNProperties>} apnList
257 * @param {string} accessPointName
258 * @return {CrOnc.APNProperties|undefined} The entry in |apnList| matching
259 * |accessPointName| if it exists, or undefined.
262 findApnInList: function(apnList
, accessPointName
) {
263 for (let a
of apnList
) {
264 if (a
.AccessPointName
== accessPointName
)