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 network nameserver options.
12 is: 'network-nameservers',
16 * The current state containing the IP Config properties to display and
18 * @type {?CrOnc.NetworkStateProperties}
23 observer: 'networkStateChanged_'
27 * Whether or not the nameservers can be edited.
35 * Array of nameserver addresses stored as strings.
36 * @type {!Array<string>}
40 value: function() { return []; }
44 * The selected nameserver type.
52 * Array of nameserver types.
54 nameserverTypeNames_: {
56 value: ['automatic', 'google', 'custom'],
61 /** @const */ GoogleNameservers: ['8.8.4.4', '8.8.8.8'],
64 * Saved nameservers when switching to 'automatic'.
65 * @type {!Array<string>}
67 savedNameservers_: [],
70 * Polymer networkState changed method.
72 networkStateChanged_: function(newValue, oldValue) {
73 if (!this.networkState)
76 if (!oldValue || newValue.GUID != oldValue.GUID)
77 this.savedNameservers_ = [];
79 // Update the 'nameservers' property.
81 var ipv4 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV4);
82 if (ipv4 && ipv4.NameServers)
83 nameservers = ipv4.NameServers;
85 // Update the 'nameserversType' property.
87 CrOnc.getActiveValue(this.networkState, 'NameServersConfigType');
89 if (configType == CrOnc.IPConfigType.STATIC) {
90 if (nameservers.join(',') == this.GoogleNameservers.join(','))
97 this.nameserversType = type;
98 this.$$('#type').selectedIndex = this.getSelectedIndex_(type);
100 this.nameservers = nameservers;
104 * @param {string} nameserversType The nameservers type.
105 * @return {number} The selected index for |nameserversType|.
108 getSelectedIndex_: function(nameserversType) {
109 var idx = this.nameserverTypeNames_.indexOf(nameserversType);
112 console.error('Unexpected type: ' + nameserversType);
117 * @param {string} nameserversType The nameservers type.
118 * @return {string} The description for |nameserversType|.
121 nameserverTypeDesc_: function(nameserversType) {
122 // TODO(stevenjb): Translate.
123 if (nameserversType == 'custom')
124 return 'Custom name servers';
125 if (nameserversType == 'google')
126 return 'Google name servers';
127 return 'Automatic name servers';
131 * @param {boolean} editable The editable state.
132 * @param {string} nameserversType The nameservers type.
133 * @return {boolean} True if the nameservers are editable.
136 canEdit_: function(editable, nameserversType) {
137 return editable && nameserversType == 'custom';
141 * Event triggered when the selected type changes. Updates nameservers and
142 * sends the change value if necessary.
143 * @param {Event} event The select node change event.
146 onTypeChange_: function(event) {
147 if (this.nameserversType == 'custom')
148 this.savedNameservers_ = this.nameservers;
149 var type = this.nameserverTypeNames_[event.target.selectedIndex];
150 this.nameserversType = type;
151 if (type == 'custom') {
152 if (this.savedNameservers_.length == 0)
153 return; // Don't change nameservers until onValueChange_().
154 // Restore the saved nameservers and send them.
155 this.nameservers = this.savedNameservers_;
157 this.sendNameServers_();
161 * Event triggered when a nameserver value changes.
164 onValueChange_: function() {
165 if (this.nameserversType != 'custom') {
166 // If a user inputs Google nameservers in the custom nameservers fields,
167 // |nameserversType| will change to 'google' so don't send the values.
170 this.sendNameServers_();
174 * Sends the current nameservers type (for automatic) or value.
177 sendNameServers_: function() {
178 var type = this.nameserversType;
181 if (type == 'custom') {
183 for (let i = 0; i < 4; ++i) {
184 let id = 'nameserver' + i;
185 let nameserver = this.$$('#' + id).value;
187 nameservers.push(nameserver);
189 this.fire('nameservers-change', {
190 field: 'NameServers',
193 } else if (type == 'google') {
194 nameservers = this.GoogleNameservers;
195 this.fire('nameservers-change', {
196 field: 'NameServers',
201 this.fire('nameservers-change', {
202 field: 'NameServersConfigType',
203 value: CrOnc.IPConfigType.DHCP