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
;
114 this.automatic
? CrOnc
.IPConfigType
.DHCP
: CrOnc
.IPConfigType
.STATIC
;
115 this.fire('ip-change', {
116 field
: 'IPAddressConfigType',
120 // Restore the saved static IP configuration.
122 Gateway
: this.savedStaticIp_
.Gateway
,
123 IPAddress
: this.savedStaticIp_
.IPAddress
,
124 RoutingPrefix
: this.savedStaticIp_
.RoutingPrefix
,
125 Type
: this.savedStaticIp_
.Type
127 this.fire('ip-change', {
128 field
: 'StaticIPConfig',
129 value
: this.getIPConfigProperties_(ipconfig
)
135 * @param {?CrOnc.IPConfigProperties} ipconfig The IP Config properties.
136 * @return {!CrOnc.IPConfigUIProperties} A new IPConfigUIProperties object
137 * with RoutingPrefix expressed as a string mask instead of a prefix
138 * length. Returns an empty object if |ipconfig| is undefined.
141 getIPConfigUIProperties_: function(ipconfig
) {
145 for (var key
in ipconfig
) {
146 var value
= ipconfig
[key
];
147 if (key
== 'RoutingPrefix')
148 result
.RoutingPrefix
= CrOnc
.getRoutingPrefixAsNetmask(value
);
156 * @param {!CrOnc.IPConfigUIProperties} ipconfig The IP Config UI properties.
157 * @return {!CrOnc.IPConfigProperties} A new IPConfigProperties object with
158 * RoutingPrefix expressed as a a prefix length.
161 getIPConfigProperties_: function(ipconfig
) {
163 for (var key
in ipconfig
) {
164 var value
= ipconfig
[key
];
165 if (key
== 'RoutingPrefix')
166 result
.RoutingPrefix
= CrOnc
.getRoutingPrefixAsLength(value
);
174 * @param {!CrOnc.IPConfigUIProperties} ipConfig The IP Config properties.
175 * @param {boolean} editable The editable property.
176 * @param {boolean} automatic The automatic property.
177 * @return {Object} An object with the edit type for each editable field.
180 getIPEditFields_: function(ipConfig
, editable
, automatic
) {
181 if (!editable
|| automatic
)
184 'ipv4.IPAddress': 'String',
185 'ipv4.RoutingPrefix': 'String',
186 'ipv4.Gateway': 'String'
191 * Event triggered when the network property list changes.
192 * @param {!{detail: { field: string, value: string}}} event The
193 * network-property-list change event.
196 onIPChange_: function(event
) {
197 var field
= event
.detail
.field
;
198 var value
= event
.detail
.value
;
199 // Note: |field| includes the 'ipv4.' prefix.
200 this.set('ipConfig.' + field
, value
);
201 this.fire('ip-change', {
202 field
: 'StaticIPConfig',
203 value
: this.getIPConfigProperties_(this.ipConfig
.ipv4
)