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.
11 is: 'network-ip-config',
15 * The current state containing the IP Config properties to display and
17 * @type {?CrOnc.NetworkStateProperties}
22 observer: 'networkStateChanged_'
26 * Whether or not the IP Address can be edited.
27 * TODO(stevenjb): Implement editing.
35 * State of 'Configure IP Addresses Automatically'.
40 observer: 'automaticChanged_'
44 * The currently visible IP Config property dictionary. The 'RoutingPrefix'
45 * property is a human-readable mask instead of a prefix length.
47 * ipv4: !CrOnc.IPConfigUIProperties,
48 * ipv6: !CrOnc.IPConfigUIProperties
53 value: function() { return {ipv4: {}, ipv6: {}}; }
57 * Array of properties to pass to the property list.
58 * @type {!Array<string>}
75 * Saved static IP configuration properties when switching to 'automatic'.
76 * @type {?CrOnc.IPConfigUIProperties}
81 * Polymer networkState changed method.
83 networkStateChanged_: function(newValue, oldValue) {
84 if (this.networkState === undefined || this.ipConfig === undefined)
87 if (newValue.GUID != (oldValue && oldValue.GUID))
88 this.savedStaticIp_ = null;
90 // Update the 'automatic' property.
92 CrOnc.getActiveValue(this.networkState, 'IPAddressConfigType');
93 this.automatic = (ipConfigType != CrOnc.IPConfigType.STATIC);
95 // Update the 'ipConfig' property.
96 var ipv4 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV4);
97 var ipv6 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV6);
99 ipv4: this.getIPConfigUIProperties_(ipv4),
100 ipv6: this.getIPConfigUIProperties_(ipv6)
105 * Polymer automatic changed method.
107 automaticChanged_: function() {
108 if (this.automatic === undefined || this.ipConfig === undefined)
110 if (this.automatic || !this.savedStaticIp_) {
111 // Save the static IP configuration when switching to automatic.
112 this.savedStaticIp_ = this.ipConfig.ipv4;
113 this.fire('ip-change', {
114 field: 'IPAddressConfigType',
115 value: this.automatic ? 'DHCP' : 'Static'
118 // Restore the saved static IP configuration.
120 Gateway: this.savedStaticIp_.Gateway,
121 IPAddress: this.savedStaticIp_.IPAddress,
122 RoutingPrefix: this.savedStaticIp_.RoutingPrefix,
123 Type: this.savedStaticIp_.Type
125 this.fire('ip-change', {
126 field: 'StaticIPConfig',
127 value: this.getIPConfigProperties_(ipconfig)
133 * @param {?CrOnc.IPConfigProperties} ipconfig The IP Config properties.
134 * @return {!CrOnc.IPConfigUIProperties} A new IPConfigUIProperties object
135 * with RoutingPrefix expressed as a string mask instead of a prefix
136 * length. Returns an empty object if |ipconfig| is undefined.
139 getIPConfigUIProperties_: function(ipconfig) {
143 for (var key in ipconfig) {
144 var value = ipconfig[key];
145 if (key == 'RoutingPrefix')
146 result.RoutingPrefix = CrOnc.getRoutingPrefixAsNetmask(value);
154 * @param {!CrOnc.IPConfigUIProperties} ipconfig The IP Config UI properties.
155 * @return {!CrOnc.IPConfigProperties} A new IPConfigProperties object with
156 * RoutingPrefix expressed as a a prefix length.
159 getIPConfigProperties_: function(ipconfig) {
161 for (var key in ipconfig) {
162 var value = ipconfig[key];
163 if (key == 'RoutingPrefix')
164 result.RoutingPrefix = CrOnc.getRoutingPrefixAsLength(value);
172 * @param {!CrOnc.IPConfigUIProperties} ipConfig The IP Config properties.
173 * @param {boolean} editable The editable property.
174 * @param {boolean} automatic The automatic property.
175 * @return {Object} An object with the edit type for each editable field.
178 getIPEditFields_: function(ipConfig, editable, automatic) {
179 if (!editable || automatic)
182 'ipv4.IPAddress': 'String',
183 'ipv4.RoutingPrefix': 'String',
184 'ipv4.Gateway': 'String'
189 * Event triggered when the network property list changes.
190 * @param {!{detail: { field: string, value: string}}} event The
191 * network-property-list change event.
194 onIPChange_: function(event) {
195 var field = event.detail.field;
196 var value = event.detail.value;
197 // Note: |field| includes the 'ipv4.' prefix.
198 this.set('ipConfig.' + field, value);
199 this.fire('ip-change', {
200 field: 'StaticIPConfig',
201 value: this.getIPConfigProperties_(this.ipConfig.ipv4)