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
10 is
: 'network-apnlist',
14 * The current state for the network matching |guid|.
15 * @type {?CrOnc.NetworkStateProperties}
20 observer
: 'networkStateChanged_'
24 * The CrOnc.APNProperties.AccessPointName value of the selected APN.
32 * Selectable list of APN dictionaries for the UI. Includes an entry
33 * corresponding to |otherApn| (see below).
34 * @type {!Array<!CrOnc.APNProperties>}
38 value: function() { return []; }
42 * The user settable properties for a new ('other') APN. The values for
43 * AccessPointName, Username, and Password will be set to the currently
44 * active APN if it does not match an existing list entry.
45 * @type {!CrOnc.APNProperties}
49 value: function() { return {}; }
53 * Array of property names to pass to the Other APN property list.
54 * @type {!Array<string>}
59 return ['AccessPointName', 'Username', 'Password'];
65 * Array of edit types to pass to the Other APN property list.
71 'AccessPointName': 'String',
80 /** @const */ DefaultAccessPointName
: 'none',
83 * Polymer networkState changed method.
85 networkStateChanged_: function() {
86 if (!this.networkState
|| !this.networkState
.Cellular
)
90 var cellular
= this.networkState
.Cellular
;
91 if (cellular
.APN
&& cellular
.APN
.AccessPointName
)
92 activeApn
= cellular
.APN
;
93 else if (cellular
.LastGoodAPN
&& cellular
.LastGoodAPN
.AccessPointName
)
94 activeApn
= cellular
.LastGoodAPN
;
95 this.setApnSelectList_(activeApn
);
99 * Sets the list of selectable APNs for the UI. Appends an 'Other' entry
100 * (see comments for |otherApn| above).
101 * @param {?CrOnc.APNProperties} activeApn The currently active APN value.
104 setApnSelectList_: function(activeApn
) {
105 var apnList
= this.networkState
.Cellular
.APNList
|| [];
106 var result
= apnList
.slice();
107 var activeApnInList
= result
.some(
108 function(a
) { return a
.AccessPointName
== activeApn
.AccessPointName
; });
111 if (!activeApnInList
&& activeApn
)
112 Object
.assign(otherApn
, activeApn
);
114 Object
.assign(otherApn
, this.otherApn
);
116 // Always use 'Other' for the name of custom APN entries (the name does
118 otherApn
.Name
= 'Other';
119 otherApn
.AccessPointName
=
120 otherApn
.AccessPointName
|| this.DefaultAccessPointName
;
121 this.set('otherApn', otherApn
);
122 result
.push(otherApn
);
124 this.set('apnSelectList', result
);
125 this.set('selectedApn',
126 activeApn
.AccessPointName
|| this.otherApn
.AccessPointName
);
130 * We need to update the select value after the dom-repeat template updates:
131 * 1. Rebuilding the template options resets the select value property.
132 * 2. The template update occurs after any property changed events.
133 * TODO(stevenjb): Remove once we use cr-dropdown-menu which (hopefully)
134 * won't require this.
137 onSelectApnUpdated_: function() {
138 this.$.selectApn
.value
= this.selectedApn
;
142 * Event triggered when the selectApn seleciton changes.
143 * @param {Event} event The select node change event.
146 onSelectApnChange_: function(event
) {
147 var apn
= event
.target
.value
;
148 // Don't send a change event for 'Other' until the 'Save' button is clicked,
149 // unless it has been changed from the default.
150 if (!this.isOtherSelected_(this.networkState
, apn
) ||
151 this.otherApn
.AccessPointName
!= this.DefaultAccessPointName
) {
152 this.sendApnChange_(apn
);
157 * Event triggered when any 'Other' APN network property changes.
158 * @param {!{detail: { field: string, value: string}}} event
161 onOtherApnChange_: function(event
) {
162 this.set('otherApn.' + event
.detail
.field
, event
.detail
.value
);
163 // Don't send a change event for 'Other' until the 'Save' button is clicked.
167 * Event triggered when the Other APN 'Save' button is clicked.
168 * @param {Event} event
171 onSaveOther_: function(event
) {
172 this.sendApnChange_(this.selectedApn
);
176 * Send the apn-change event.
177 * @param {!CrOnc.APNProperties} selectedApn
180 sendApnChange_: function(selectedApn
) {
181 var apnList
= this.networkState
.Cellular
.APNList
|| [];
182 var apn
= this.findApnInList(apnList
, selectedApn
);
183 if (apn
== undefined) {
185 'AccessPointName': this.otherApn
.AccessPointName
,
186 'Username': this.otherApn
.Username
,
187 'Password': this.otherApn
.Password
190 this.fire('apn-change', {field
: 'APN', value
: apn
});
194 * @param {?CrOnc.NetworkStateProperties} networkState
195 * @param {string} selectedApn
196 * @return {boolean} True if the 'other' APN is currently selected.
199 isOtherSelected_: function(networkState
, selectedApn
) {
200 if (!networkState
|| !networkState
.Cellular
)
202 var apnList
= networkState
.Cellular
.APNList
|| [];
203 var apn
= this.findApnInList(apnList
, selectedApn
);
204 return apn
== undefined;
208 * @param {!CrOnc.APNProperties} apn
209 * @return {string} The most descriptive name for the access point.
212 apnDesc_: function(apn
) {
213 return apn
.LocalizedName
|| apn
.Name
|| apn
.AccessPointName
;
217 * @param {!Array<!CrOnc.APNProperties>} apnList
218 * @param {string} accessPointName
219 * @return {CrOnc.APNProperties|undefined} The entry in |apnList| matching
220 * |accessPointName| if it exists, or undefined.
223 findApnInList: function(apnList
, accessPointName
) {
224 for (var a
of apnList
) {
225 if (a
.AccessPointName
== accessPointName
)