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 the IP Config properties for
7 * a network state. TODO(stevenjb): Allow editing of static IP configurations
8 * when 'editable' is true.
14 is: 'network-ip-config',
18 * The current state containing the IP Config properties to display and
20 * @type {?CrOnc.NetworkStateProperties}
25 observer: 'networkStateChanged_'
29 * Whether or not the IP Address can be edited.
30 * TODO(stevenjb): Implement editing.
38 * State of 'Configure IP Addresses Automatically'.
43 observer: 'automaticChanged_'
47 * The currently visible IP Config property dictionary. The 'RoutingPrefix'
48 * property is a human-readable mask instead of a prefix length.
50 * ipv4: !CrOnc.IPConfigUIProperties,
51 * ipv6: !CrOnc.IPConfigUIProperties
60 * Array of properties to pass to the property list.
61 * @type {!Array<string>}
78 * Saved static IP configuration properties when switching to 'automatic'.
79 * @type {?CrOnc.IPConfigUIProperties}
84 * Polymer networkState changed method.
86 networkStateChanged_: function(newValue, oldValue) {
87 if (!this.networkState || !this.ipConfig)
90 if (newValue.GUID != (oldValue && oldValue.GUID))
91 this.savedStaticIp_ = null;
93 // Update the 'automatic' property.
95 CrOnc.getActiveValue(this.networkState, 'IPAddressConfigType');
96 this.automatic = (ipConfigType != CrOnc.IPConfigType.STATIC);
98 // Update the 'ipConfig' property.
99 var ipv4 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV4);
100 var ipv6 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV6);
102 ipv4: this.getIPConfigUIProperties_(ipv4),
103 ipv6: this.getIPConfigUIProperties_(ipv6)
108 * Polymer automatic changed method.
110 automaticChanged_: function() {
111 if (!this.automatic || !this.ipConfig)
113 if (this.automatic || !this.savedStaticIp_) {
114 // Save the static IP configuration when switching to automatic.
115 this.savedStaticIp_ = this.ipConfig.ipv4;
117 this.automatic ? CrOnc.IPConfigType.DHCP : CrOnc.IPConfigType.STATIC;
118 this.fire('ip-change', {
119 field: 'IPAddressConfigType',
123 // Restore the saved static IP configuration.
125 Gateway: this.savedStaticIp_.Gateway,
126 IPAddress: this.savedStaticIp_.IPAddress,
127 RoutingPrefix: this.savedStaticIp_.RoutingPrefix,
128 Type: this.savedStaticIp_.Type
130 this.fire('ip-change', {
131 field: 'StaticIPConfig',
132 value: this.getIPConfigProperties_(ipconfig)
138 * @param {?CrOnc.IPConfigProperties} ipconfig The IP Config properties.
139 * @return {!CrOnc.IPConfigUIProperties} A new IPConfigUIProperties object
140 * with RoutingPrefix expressed as a string mask instead of a prefix
141 * length. Returns an empty object if |ipconfig| is undefined.
144 getIPConfigUIProperties_: function(ipconfig) {
148 for (let key in ipconfig) {
149 let value = ipconfig[key];
150 if (key == 'RoutingPrefix')
151 result.RoutingPrefix = CrOnc.getRoutingPrefixAsNetmask(value);
159 * @param {!CrOnc.IPConfigUIProperties} ipconfig The IP Config UI properties.
160 * @return {!CrOnc.IPConfigProperties} A new IPConfigProperties object with
161 * RoutingPrefix expressed as a a prefix length.
164 getIPConfigProperties_: function(ipconfig) {
166 for (let key in ipconfig) {
167 let value = ipconfig[key];
168 if (key == 'RoutingPrefix')
169 result.RoutingPrefix = CrOnc.getRoutingPrefixAsLength(value);
177 * @param {!CrOnc.IPConfigUIProperties} ipConfig The IP Config properties.
178 * @param {boolean} editable The editable property.
179 * @param {boolean} automatic The automatic property.
180 * @return {Object} An object with the edit type for each editable field.
183 getIPEditFields_: function(ipConfig, editable, automatic) {
184 if (!editable || automatic)
187 'ipv4.IPAddress': 'String',
188 'ipv4.RoutingPrefix': 'String',
189 'ipv4.Gateway': 'String'
194 * Event triggered when the network property list changes.
195 * @param {!{detail: { field: string, value: string}}} event The
196 * network-property-list change event.
199 onIPChange_: function(event) {
202 var field = event.detail.field;
203 var value = event.detail.value;
204 // Note: |field| includes the 'ipv4.' prefix.
205 this.set('ipConfig.' + field, value);
206 this.fire('ip-change', {
207 field: 'StaticIPConfig',
208 value: this.getIPConfigProperties_(this.ipConfig.ipv4)