OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var NetworkUI = (function() { | 5 var NetworkUI = (function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 // Properties to display in the network state table. Each entry can be either | 8 // Properties to display in the network state table. Each entry can be either |
9 // a single state field or an array of state fields. If more than one is | 9 // a single state field or an array of state fields. If more than one is |
10 // specified then the first non empty value is used. | 10 // specified then the first non empty value is used. |
(...skipping 18 matching lines...) Expand all Loading... | |
29 'GUID', | 29 'GUID', |
30 'service_path', | 30 'service_path', |
31 'Name', | 31 'Name', |
32 'Type', | 32 'Type', |
33 'profile_path', | 33 'profile_path', |
34 'visible', | 34 'visible', |
35 'onc_source' | 35 'onc_source' |
36 ]; | 36 ]; |
37 | 37 |
38 /** | 38 /** |
39 * Returns the property associated with a key (which may reference a | 39 * Returns a cr-onc-data Polymer element to connect to cr-network-icon |
40 * sub-object). | 40 * elements. |
41 * | 41 * |
42 * @param {Object} properties The object containing the network properties. | 42 * @param {CrOnc.NetworkConfigType} networkState The network state properties. |
43 * @return {Element} A cr-onc-data Element. | |
44 */ | |
45 var createNetworkState = function(networkState) { | |
46 var oncData = | |
47 /** @type {Element} */ (document.createElement('cr-onc-data')); | |
michaelpg
2015/02/10 08:11:24
nit: 4 spaces
stevenjb
2015/02/11 00:16:11
Done.
| |
48 oncData.data = networkState; | |
49 return oncData; | |
50 }; | |
51 | |
52 /** | |
53 * Returns the ONC data property for networkState associated with a key. Used | |
54 * to access properties in networkState by name. | |
55 * | |
56 * @param {CrOnc.NetworkConfigType} networkState The network state properties. | |
43 * @param {string} key The ONC key for the property. May refer to a nested | 57 * @param {string} key The ONC key for the property. May refer to a nested |
44 * propety, e.g. 'WiFi.Security'. | 58 * propety, e.g. 'WiFi.Security'. |
45 * @return {*} The value associated with the property. | 59 * @return {*} The value associated with the property. |
46 */ | 60 */ |
47 var getValueFromProperties = function(properties, key) { | 61 var getOncProperty = function(networkState, key) { |
48 if (!key) { | 62 var dict = /** @type {!Object} */ (networkState); |
michaelpg
2015/02/10 08:11:24
no parens needed here
stevenjb
2015/02/11 00:16:11
Hmm, other existing similar examples introduced by
michaelpg
2015/02/11 00:47:28
Parens are needed by closure when using a compound
James Hawkins
2015/02/11 00:48:33
This is a cast, so the parens are required by the
| |
49 console.error('Empty key'); | |
50 return undefined; | |
51 } | |
52 var dot = key.indexOf('.'); | 63 var dot = key.indexOf('.'); |
53 if (dot > 0) { | 64 while (dot > 0) { |
54 var key1 = key.substring(0, dot); | 65 var key1 = key.substring(0, dot); |
55 var key2 = key.substring(dot + 1); | 66 var key2 = key.substring(dot + 1); |
56 var subobject = properties[key1]; | 67 dict = dict[key1]; |
57 if (subobject) | 68 if (!dict) |
58 return getValueFromProperties(subobject, key2); | 69 return undefined; |
70 key = key2; | |
71 dot = key.indexOf('.'); | |
59 } | 72 } |
60 return properties[key]; | 73 return dict[key]; |
61 }; | 74 }; |
62 | 75 |
63 /** | 76 /** |
64 * Creates a cell with a button for expanding a network state table row. | 77 * Creates a cell with a button for expanding a network state table row. |
65 * | 78 * |
66 * @param {string} guid The GUID identifying the network. | 79 * @param {string} guid The GUID identifying the network. |
67 * @return {!HTMLElement} The created td element that displays the given | 80 * @return {!HTMLElement} The created td element that displays the given |
68 * value. | 81 * value. |
69 */ | 82 */ |
70 var createStateTableExpandButton = function(guid) { | 83 var createStateTableExpandButton = function(guid) { |
71 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); | 84 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); |
72 cell.className = 'state-table-expand-button-cell'; | 85 cell.className = 'state-table-expand-button-cell'; |
73 var button = document.createElement('button'); | 86 var button = document.createElement('button'); |
74 button.addEventListener('click', function(event) { | 87 button.addEventListener('click', function(event) { |
75 toggleExpandRow(/** @type {!HTMLElement} */ (event.target), guid); | 88 toggleExpandRow(/** @type {!HTMLElement} */ (event.target), guid); |
76 }); | 89 }); |
77 button.className = 'state-table-expand-button'; | 90 button.className = 'state-table-expand-button'; |
78 button.textContent = '+'; | 91 button.textContent = '+'; |
79 cell.appendChild(button); | 92 cell.appendChild(button); |
80 return cell; | 93 return cell; |
81 }; | 94 }; |
82 | 95 |
83 /** | 96 /** |
97 * Creates a cell with an icon representing the network state. | |
98 * | |
99 * @param {CrOnc.NetworkConfigType} networkState The network state properties. | |
100 * @return {HTMLElement} The created td element that displays the icon. | |
michaelpg
2015/02/10 08:11:24
nit: you could use HTMLTableCellElement
stevenjb
2015/02/11 00:16:11
I copied the example Jeremy did. Jeremy, thoughts?
Jeremy Klein
2015/02/11 00:31:30
Michael's suggestion is more specific so I like it
| |
101 */ | |
102 var createStateTableIcon = function(networkState) { | |
103 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); | |
104 cell.className = 'state-table-icon-cell'; | |
105 var icon = document.createElement('cr-network-icon'); | |
106 icon.isListItem = true; | |
107 icon.networkState = createNetworkState(networkState); | |
108 cell.appendChild(icon); | |
109 return cell; | |
110 }; | |
111 | |
112 /** | |
84 * Creates a cell in network state table. | 113 * Creates a cell in network state table. |
85 * | 114 * |
86 * @param {string} value Content in the cell. | 115 * @param {string} value Content in the cell. |
87 * @return {!HTMLElement} The created td element that displays the given | 116 * @return {!HTMLElement} The created td element that displays the given |
88 * value. | 117 * value. |
89 */ | 118 */ |
90 var createStateTableCell = function(value) { | 119 var createStateTableCell = function(value) { |
91 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); | 120 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); |
92 cell.textContent = value || ''; | 121 cell.textContent = value || ''; |
93 return cell; | 122 return cell; |
94 }; | 123 }; |
95 | 124 |
96 /** | 125 /** |
97 * Creates a row in the network state table. | 126 * Creates a row in the network state table. |
98 * | 127 * |
99 * @param {Array} stateFields The state fields to use for the row. | 128 * @param {Array} stateFields The state fields to use for the row. |
100 * @param {Object} state Property values for the network or favorite. | 129 * @param {CrOnc.NetworkConfigType} networkState The network state properties. |
101 * @return {!HTMLElement} The created tr element that contains the network | 130 * @return {!HTMLElement} The created tr element that contains the network |
michaelpg
2015/02/10 08:11:25
same nit for HTMLTableRowElement
stevenjb
2015/02/11 00:16:11
Acknowledged.
| |
102 * state information. | 131 * state information. |
103 */ | 132 */ |
104 var createStateTableRow = function(stateFields, state) { | 133 var createStateTableRow = function(stateFields, networkState) { |
105 var row = /** @type {!HTMLElement} */ (document.createElement('tr')); | 134 var row = /** @type {!HTMLElement} */ (document.createElement('tr')); |
106 row.className = 'state-table-row'; | 135 row.className = 'state-table-row'; |
107 var guid = state['GUID']; | 136 var guid = networkState.GUID; |
108 row.appendChild(createStateTableExpandButton(guid)); | 137 row.appendChild(createStateTableExpandButton(guid)); |
138 row.appendChild(createStateTableIcon(networkState)); | |
109 for (var i = 0; i < stateFields.length; ++i) { | 139 for (var i = 0; i < stateFields.length; ++i) { |
110 var field = stateFields[i]; | 140 var field = stateFields[i]; |
111 var value = ''; | 141 var value; |
112 if (typeof field == 'string') { | 142 if (typeof field == 'string') { |
113 value = getValueFromProperties(state, field); | 143 value = getOncProperty(networkState, field); |
114 } else { | 144 } else { |
115 for (var j = 0; j < field.length; ++j) { | 145 for (var j = 0; j < field.length; ++j) { |
116 value = getValueFromProperties(state, field[j]); | 146 value = getOncProperty(networkState, field[j]); |
117 if (value) | 147 if (value != undefined) |
118 break; | 148 break; |
119 } | 149 } |
120 } | 150 } |
151 var stringValue = | |
152 (value != undefined) ? /** @type {string} */ (value) : ''; | |
121 if (field == 'GUID') | 153 if (field == 'GUID') |
122 value = value.slice(0, 8); | 154 stringValue = stringValue.slice(0, 8); |
123 row.appendChild(createStateTableCell(/** @type {string} */ (value))); | 155 row.appendChild(createStateTableCell(stringValue)); |
124 } | 156 } |
125 return row; | 157 return row; |
126 }; | 158 }; |
127 | 159 |
128 /** | 160 /** |
129 * Creates a table for networks or favorites. | 161 * Creates a table for networks or favorites. |
130 * | 162 * |
131 * @param {string} tablename The name of the table to be created. | 163 * @param {string} tablename The name of the table to be created. |
132 * @param {Array} stateFields The list of fields for the table. | 164 * @param {Array} stateFields The list of fields for the table. |
133 * @param {Array} states An array of network or favorite states. | 165 * @param {Array} states An array of network or favorite states. |
134 */ | 166 */ |
135 var createStateTable = function(tablename, stateFields, states) { | 167 var createStateTable = function(tablename, stateFields, states) { |
136 var table = $(tablename); | 168 var table = $(tablename); |
137 var oldRows = table.querySelectorAll('.state-table-row'); | 169 var oldRows = table.querySelectorAll('.state-table-row'); |
138 for (var i = 0; i < oldRows.length; ++i) | 170 for (var i = 0; i < oldRows.length; ++i) |
139 table.removeChild(oldRows[i]); | 171 table.removeChild(oldRows[i]); |
140 states.forEach(function(state) { | 172 states.forEach(function(state) { |
141 table.appendChild(createStateTableRow(stateFields, state)); | 173 table.appendChild(createStateTableRow( |
174 stateFields, /** @type {CrOnc.NetworkConfigType} */ (state))); | |
142 }); | 175 }); |
143 }; | 176 }; |
144 | 177 |
145 /** | 178 /** |
146 * This callback function is triggered when visible networks are received. | 179 * This callback function is triggered when visible networks are received. |
147 * | 180 * |
148 * @param {Array} states A list of network state information for each | 181 * @param {Array} states A list of network state information for each |
149 * visible network. | 182 * visible network. |
150 */ | 183 */ |
151 var onVisibleNetworksReceived = function(states) { | 184 var onVisibleNetworksReceived = function(states) { |
185 /** @type {CrOnc.NetworkConfigType} */ var defaultState; | |
186 if (states.length > 0) | |
187 defaultState = /** @type {CrOnc.NetworkConfigType} */ (states[0]); | |
188 if (defaultState && defaultState.Type != 'VPN') { | |
189 $('default-network-text').textContent = | |
190 loadTimeData.getStringF('defaultNetworkText', | |
191 defaultState.Name, | |
192 defaultState.ConnectionState); | |
193 $('default-network-icon').networkState = createNetworkState(defaultState); | |
194 } else { | |
195 $('default-network-text').textContent = | |
196 loadTimeData.getString('noNetworkText'); | |
197 // Show the disconnected wifi icon if there are no networks. | |
198 $('default-network-icon').networkType = 'WiFi'; | |
199 } | |
200 | |
152 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 201 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); |
153 }; | 202 }; |
154 | 203 |
155 /** | 204 /** |
156 * This callback function is triggered when favorite networks are received. | 205 * This callback function is triggered when favorite networks are received. |
157 * | 206 * |
158 * @param {!Array} states A list of network state information for each | 207 * @param {!Array} states A list of network state information for each |
159 * favorite network. | 208 * favorite network. |
160 */ | 209 */ |
161 var onFavoriteNetworksReceived = function(states) { | 210 var onFavoriteNetworksReceived = function(states) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 document.addEventListener('DOMContentLoaded', function() { | 323 document.addEventListener('DOMContentLoaded', function() { |
275 $('refresh').onclick = requestNetworks; | 324 $('refresh').onclick = requestNetworks; |
276 setRefresh(); | 325 setRefresh(); |
277 requestNetworks(); | 326 requestNetworks(); |
278 }); | 327 }); |
279 | 328 |
280 return { | 329 return { |
281 getShillPropertiesResult: getShillPropertiesResult | 330 getShillPropertiesResult: getShillPropertiesResult |
282 }; | 331 }; |
283 })(); | 332 })(); |
OLD | NEW |