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.
9 is: 'network-nameservers',
13 * The current state containing the IP Config properties to display and
15 * @type {?CrOnc.NetworkStateProperties}
20 observer: 'networkStateChanged_'
24 * Whether or not the nameservers can be edited.
32 * Array of nameserver addresses stored as strings.
33 * @type {!Array<string>}
37 value: function() { return []; }
41 * The selected nameserver type.
49 * Array of nameserver types.
51 nameserverTypeNames_: {
53 value: ['automatic', 'google', 'custom'],
58 /** @const */ GoogleNameservers: ['8.8.4.4', '8.8.8.8'],
61 * Saved nameservers when switching to 'automatic'.
62 * @type {!Array<string>}
64 savedNameservers_: [],
67 * Polymer networkState changed method.
69 networkStateChanged_: function(newValue, oldValue) {
70 if (!this.networkState)
73 if (!oldValue || newValue.GUID != oldValue.GUID)
74 this.savedNameservers_ = [];
76 // Update the 'nameservers' property.
78 var ipv4 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV4);
79 if (ipv4 && ipv4.NameServers)
80 nameservers = ipv4.NameServers;
82 // Update the 'nameserversType' property.
84 CrOnc.getActiveValue(this.networkState, 'NameServersConfigType');
86 if (configType == 'Static') {
87 if (nameservers.join(',') == this.GoogleNameservers.join(','))
94 this.nameserversType = type;
95 this.$$('#type').selectedIndex = this.getSelectedIndex_(type);
97 this.nameservers = nameservers;
101 * @param {string} nameserversType The nameservers type.
102 * @return {number} The selected index for |nameserversType|.
105 getSelectedIndex_: function(nameserversType) {
106 var idx = this.nameserverTypeNames_.indexOf(nameserversType);
109 console.error('Unexpected type: ' + nameserversType);
114 * @param {string} nameserversType The nameservers type.
115 * @return {string} The description for |nameserversType|.
118 nameserverTypeDesc_: function(nameserversType) {
119 // TODO(stevenjb): Translate.
120 if (nameserversType == 'custom')
121 return 'Custom name servers';
122 if (nameserversType == 'google')
123 return 'Google name servers';
124 return 'Automatic name servers';
128 * @param {boolean} editable The editable state.
129 * @param {string} nameserversType The nameservers type.
130 * @return {boolean} True if the nameservers are editable.
133 canEdit_: function(editable, nameserversType) {
134 return editable && nameserversType == 'custom';
138 * Event triggered when the selected type changes. Updates nameservers and
139 * sends the change value if necessary.
140 * @param {Event} event The select node change event.
143 onTypeChange_: function(event) {
144 if (this.nameserversType == 'custom')
145 this.savedNameservers_ = this.nameservers;
146 var type = this.nameserverTypeNames_[event.target.selectedIndex];
147 this.nameserversType = type;
148 if (type == 'custom') {
149 if (this.savedNameservers_.length == 0)
150 return; // Don't change nameservers until onValueChange_().
151 // Restore the saved nameservers and send them.
152 this.nameservers = this.savedNameservers_;
154 this.sendNameServers_();
158 * Event triggered when a nameserver value changes.
161 onValueChange_: function() {
162 if (this.nameserversType != 'custom') {
163 // If a user inputs Google nameservers in the custom nameservers fields,
164 // |nameserversType| will change to 'google' so don't send the values.
167 this.sendNameServers_();
171 * Sends the current nameservers type (for automatic) or value.
174 sendNameServers_: function() {
175 var type = this.nameserversType;
178 if (type == 'custom') {
180 for (var i = 0; i < 4; ++i) {
181 var id = 'nameserver' + i;
182 var nameserver = this.$$('#' + id).value;
184 nameservers.push(nameserver);
186 this.fire('nameservers-change', {
187 field: 'NameServers',
190 } else if (type == 'google') {
191 nameservers = this.GoogleNameservers;
192 this.fire('nameservers-change', {
193 field: 'NameServers',
198 this.fire('nameservers-change', {
199 field: 'NameServersConfigType',