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 'Source' | 35 'Source' |
36 ]; | 36 ]; |
37 | 37 |
38 /** | 38 /** |
39 * Returns the property associated with a key (which may reference a | 39 * Creates and returns a typed cr-onc-data Polymer element for connecting to |
40 * sub-object). | 40 * cr-network-icon elements. Sets the data property of the element to |state|. |
41 * | 41 * |
42 * @param {Object} properties The object containing the network properties. | 42 * @param {!CrOnc.NetworkConfigType} state The network state properties. |
43 * @return {!CrOncDataElement} A cr-onc-data element. | |
44 */ | |
45 var createNetworkState = function(state) { | |
46 var oncData = /** @type {!CrOncDataElement} */( | |
47 document.createElement('cr-onc-data')); | |
48 oncData.data = state; | |
49 return oncData; | |
50 }; | |
51 | |
52 /** | |
53 * Creates and returns a typed HTMLTableCellElement. | |
54 * | |
55 * @return {!HTMLTableCellElement} A new td element. | |
56 */ | |
57 var createTableCellElement = function() { | |
58 return /** @type {!HTMLTableCellElement} */(document.createElement('td')); | |
59 }; | |
60 | |
61 /** | |
62 * Creates and returns a typed HTMLTableRowElement. | |
63 * | |
64 * @return {!HTMLTableRowElement} A net tr element. | |
65 */ | |
66 var createTableRowElement = function() { | |
67 return /** @type {!HTMLTableRowElement} */(document.createElement('tr')); | |
68 }; | |
69 | |
70 /** | |
71 * Returns the ONC data property for networkState associated with a key. Used | |
72 * to access properties in networkState by name. | |
73 * | |
74 * @param {!CrOnc.NetworkConfigType} networkState The network state | |
75 * property dictionary. | |
43 * @param {string} key The ONC key for the property. May refer to a nested | 76 * @param {string} key The ONC key for the property. May refer to a nested |
44 * propety, e.g. 'WiFi.Security'. | 77 * property, e.g. 'WiFi.Security'. |
45 * @return {*} The value associated with the property. | 78 * @return {*} The value associated with the property. |
46 */ | 79 */ |
47 var getValueFromProperties = function(properties, key) { | 80 var getOncProperty = function(networkState, key) { |
48 if (!key) { | 81 var dict = /** @type {!Object} */(networkState); |
49 console.error('Empty key'); | |
50 return undefined; | |
51 } | |
52 var dot = key.indexOf('.'); | 82 var dot = key.indexOf('.'); |
Dan Beam
2015/02/21 01:07:57
i think this method would be simpler if you just u
stevenjb
2015/02/23 21:31:57
Done.
| |
53 if (dot > 0) { | 83 while (dot > 0) { |
54 var key1 = key.substring(0, dot); | 84 var key1 = key.substring(0, dot); |
55 var key2 = key.substring(dot + 1); | 85 var key2 = key.substring(dot + 1); |
56 var subobject = properties[key1]; | 86 dict = dict[key1]; |
57 if (subobject) | 87 if (!dict) |
58 return getValueFromProperties(subobject, key2); | 88 return undefined; |
Dan Beam
2015/02/21 01:07:57
is this valid if we call getOncProperty('a.b.doesn
stevenjb
2015/02/23 21:31:57
Both should return undefined. Preserved that behav
| |
89 key = key2; | |
90 dot = key.indexOf('.'); | |
59 } | 91 } |
60 return properties[key]; | 92 return dict[key]; |
61 }; | 93 }; |
62 | 94 |
63 /** | 95 /** |
64 * Creates a cell with a button for expanding a network state table row. | 96 * Creates a cell with a button for expanding a network state table row. |
65 * | 97 * |
66 * @param {string} guid The GUID identifying the network. | 98 * @param {string} guid The GUID identifying the network. |
67 * @return {!HTMLElement} The created td element that displays the given | 99 * @return {!HTMLTableCellElement} The created td element that displays the |
68 * value. | 100 * given value. |
69 */ | 101 */ |
70 var createStateTableExpandButton = function(guid) { | 102 var createStateTableExpandButton = function(guid) { |
71 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); | 103 var cell = createTableCellElement(); |
72 cell.className = 'state-table-expand-button-cell'; | 104 cell.className = 'state-table-expand-button-cell'; |
73 var button = document.createElement('button'); | 105 var button = document.createElement('button'); |
74 button.addEventListener('click', function(event) { | 106 button.addEventListener('click', function(event) { |
75 toggleExpandRow(/** @type {!HTMLElement} */ (event.target), guid); | 107 toggleExpandRow(/** @type {!HTMLElement} */(event.target), guid); |
76 }); | 108 }); |
77 button.className = 'state-table-expand-button'; | 109 button.className = 'state-table-expand-button'; |
78 button.textContent = '+'; | 110 button.textContent = '+'; |
79 cell.appendChild(button); | 111 cell.appendChild(button); |
80 return cell; | 112 return cell; |
81 }; | 113 }; |
82 | 114 |
83 /** | 115 /** |
116 * Creates a cell with an icon representing the network state. | |
117 * | |
118 * @param {CrOnc.NetworkConfigType} networkState The network state properties. | |
119 * @return {!HTMLTableCellElement} The created td element that displays the | |
120 * icon. | |
121 */ | |
122 var createStateTableIcon = function(networkState) { | |
123 var cell = createTableCellElement(); | |
124 cell.className = 'state-table-icon-cell'; | |
125 var icon = /** @type {!CrNetworkIconElement} */( | |
126 document.createElement('cr-network-icon')); | |
127 icon.isListItem = true; | |
128 icon.networkState = createNetworkState(networkState); | |
129 cell.appendChild(icon); | |
130 return cell; | |
131 }; | |
132 | |
133 /** | |
84 * Creates a cell in network state table. | 134 * Creates a cell in network state table. |
85 * | 135 * |
86 * @param {string} value Content in the cell. | 136 * @param {*} value Content in the cell. |
87 * @return {!HTMLElement} The created td element that displays the given | 137 * @return {!HTMLTableCellElement} The created td element that displays the |
88 * value. | 138 * given value. |
89 */ | 139 */ |
90 var createStateTableCell = function(value) { | 140 var createStateTableCell = function(value) { |
91 var cell = /** @type {!HTMLElement} */ (document.createElement('td')); | 141 var cell = createTableCellElement(); |
92 cell.textContent = value || ''; | 142 cell.textContent = value || ''; |
93 return cell; | 143 return cell; |
94 }; | 144 }; |
95 | 145 |
96 /** | 146 /** |
97 * Creates a row in the network state table. | 147 * Creates a row in the network state table. |
98 * | 148 * |
99 * @param {Array} stateFields The state fields to use for the row. | 149 * @param {Array} stateFields The state fields to use for the row. |
100 * @param {Object} state Property values for the network or favorite. | 150 * @param {CrOnc.NetworkConfigType} networkState The network state properties. |
101 * @return {!HTMLElement} The created tr element that contains the network | 151 * @return {!HTMLTableRowElement} The created tr element that contains the |
102 * state information. | 152 * network state information. |
103 */ | 153 */ |
104 var createStateTableRow = function(stateFields, state) { | 154 var createStateTableRow = function(stateFields, networkState) { |
105 var row = /** @type {!HTMLElement} */ (document.createElement('tr')); | 155 var row = createTableRowElement(); |
106 row.className = 'state-table-row'; | 156 row.className = 'state-table-row'; |
107 var guid = state['GUID']; | 157 var guid = networkState.GUID; |
108 row.appendChild(createStateTableExpandButton(guid)); | 158 row.appendChild(createStateTableExpandButton(guid)); |
159 row.appendChild(createStateTableIcon(networkState)); | |
109 for (var i = 0; i < stateFields.length; ++i) { | 160 for (var i = 0; i < stateFields.length; ++i) { |
110 var field = stateFields[i]; | 161 var field = stateFields[i]; |
111 var value = ''; | 162 var value; |
112 if (typeof field == 'string') { | 163 if (typeof field == 'string') { |
113 value = getValueFromProperties(state, field); | 164 value = getOncProperty(networkState, field); |
114 } else { | 165 } else { |
115 for (var j = 0; j < field.length; ++j) { | 166 for (var j = 0; j < field.length; ++j) { |
116 value = getValueFromProperties(state, field[j]); | 167 value = getOncProperty(networkState, field[j]); |
117 if (value) | 168 if (value != undefined) |
118 break; | 169 break; |
119 } | 170 } |
120 } | 171 } |
121 if (field == 'GUID') | 172 if (field == 'GUID') |
122 value = value.slice(0, 8); | 173 value = value.slice(0, 8); |
123 row.appendChild(createStateTableCell(/** @type {string} */ (value))); | 174 row.appendChild(createStateTableCell(value)); |
124 } | 175 } |
125 return row; | 176 return row; |
126 }; | 177 }; |
127 | 178 |
128 /** | 179 /** |
129 * Creates a table for networks or favorites. | 180 * Creates a table for networks or favorites. |
130 * | 181 * |
131 * @param {string} tablename The name of the table to be created. | 182 * @param {string} tablename The name of the table to be created. |
132 * @param {Array} stateFields The list of fields for the table. | 183 * @param {Array} stateFields The list of fields for the table. |
133 * @param {Array} states An array of network or favorite states. | 184 * @param {Array} states An array of network or favorite states. |
134 */ | 185 */ |
135 var createStateTable = function(tablename, stateFields, states) { | 186 var createStateTable = function(tablename, stateFields, states) { |
136 var table = $(tablename); | 187 var table = $(tablename); |
137 var oldRows = table.querySelectorAll('.state-table-row'); | 188 var oldRows = table.querySelectorAll('.state-table-row'); |
138 for (var i = 0; i < oldRows.length; ++i) | 189 for (var i = 0; i < oldRows.length; ++i) |
139 table.removeChild(oldRows[i]); | 190 table.removeChild(oldRows[i]); |
140 states.forEach(function(state) { | 191 states.forEach(function(state) { |
141 table.appendChild(createStateTableRow(stateFields, state)); | 192 table.appendChild(createStateTableRow( |
193 stateFields, /** @type {!CrOnc.NetworkConfigType} */(state))); | |
142 }); | 194 }); |
143 }; | 195 }; |
144 | 196 |
145 /** | 197 /** |
146 * Returns a valid HTMLElement id from |guid|. | 198 * Returns a valid HTMLElement id from |guid|. |
147 * | 199 * |
148 * @param {string} guid A GUID which may start with a digit | 200 * @param {string} guid A GUID which may start with a digit |
149 * @return {string} A valid HTMLElement id. | 201 * @return {string} A valid HTMLElement id. |
150 */ | 202 */ |
151 var idFromGuid = function(guid) { | 203 var idFromGuid = function(guid) { |
152 return '_' + guid.replace(/[{}]/g, ''); | 204 return '_' + guid.replace(/[{}]/g, ''); |
153 }; | 205 }; |
154 | 206 |
155 /** | 207 /** |
156 * This callback function is triggered when visible networks are received. | 208 * This callback function is triggered when visible networks are received. |
157 * | 209 * |
158 * @param {Array} states A list of network state information for each | 210 * @param {!Array<!Object>} states A list of network state information for |
159 * visible network. | 211 * each visible network. |
160 */ | 212 */ |
161 var onVisibleNetworksReceived = function(states) { | 213 var onVisibleNetworksReceived = function(states) { |
214 /** @type {CrOnc.NetworkConfigType} */ var defaultState; | |
215 if (states.length > 0) | |
216 defaultState = /** @type {!CrOnc.NetworkConfigType} */(states[0]); | |
217 var icon = /** @type {CrNetworkIconElement} */($('default-network-icon')); | |
218 if (defaultState && defaultState.Type != 'VPN') { | |
219 $('default-network-text').textContent = | |
220 loadTimeData.getStringF('defaultNetworkText', | |
221 defaultState.Name, | |
222 defaultState.ConnectionState); | |
223 icon.networkState = createNetworkState(defaultState); | |
224 } else { | |
225 $('default-network-text').textContent = | |
226 loadTimeData.getString('noNetworkText'); | |
227 // Show the disconnected wifi icon if there are no networks. | |
228 icon.networkType = 'WiFi'; | |
229 } | |
230 | |
162 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 231 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); |
163 }; | 232 }; |
164 | 233 |
165 /** | 234 /** |
166 * This callback function is triggered when favorite networks are received. | 235 * This callback function is triggered when favorite networks are received. |
167 * | 236 * |
168 * @param {!Array} states A list of network state information for each | 237 * @param {!Array<!Object>} states A list of network state information for |
169 * favorite network. | 238 * each favorite network. |
170 */ | 239 */ |
171 var onFavoriteNetworksReceived = function(states) { | 240 var onFavoriteNetworksReceived = function(states) { |
172 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); | 241 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); |
173 }; | 242 }; |
174 | 243 |
175 /** | 244 /** |
176 * Toggles the button state and add or remove a row displaying the complete | 245 * Toggles the button state and add or remove a row displaying the complete |
177 * state information for a row. | 246 * state information for a row. |
178 * | 247 * |
179 * @param {!HTMLElement} btn The button that was clicked. | 248 * @param {!HTMLElement} btn The button that was clicked. |
180 * @param {string} guid GUID identifying the network. | 249 * @param {string} guid GUID identifying the network. |
181 */ | 250 */ |
182 var toggleExpandRow = function(btn, guid) { | 251 var toggleExpandRow = function(btn, guid) { |
183 var cell = btn.parentNode; | 252 var cell = btn.parentNode; |
184 var row = /** @type {!HTMLElement} */ (cell.parentNode); | 253 var row = /** @type {!HTMLTableRowElement} */(cell.parentNode); |
185 if (btn.textContent == '-') { | 254 if (btn.textContent == '-') { |
186 btn.textContent = '+'; | 255 btn.textContent = '+'; |
187 row.parentNode.removeChild(row.nextSibling); | 256 row.parentNode.removeChild(row.nextSibling); |
188 } else { | 257 } else { |
189 btn.textContent = '-'; | 258 btn.textContent = '-'; |
190 var expandedRow = createExpandedRow(guid, row); | 259 var expandedRow = createExpandedRow(guid, row); |
191 row.parentNode.insertBefore(expandedRow, row.nextSibling); | 260 row.parentNode.insertBefore(expandedRow, row.nextSibling); |
192 } | 261 } |
193 }; | 262 }; |
194 | 263 |
195 /** | 264 /** |
196 * Creates the expanded row for displaying the complete state as JSON. | 265 * Creates the expanded row for displaying the complete state as JSON. |
197 * | 266 * |
198 * @param {string} guid The GUID identifying the network. | 267 * @param {string} guid The GUID identifying the network. |
199 * @param {!HTMLElement} baseRow The unexpanded row associated with the new | 268 * @param {!HTMLTableRowElement} baseRow The unexpanded row associated with |
200 * row. | 269 * the new row. |
201 * @return {!HTMLElement} The created tr element for the expanded row. | 270 * @return {!HTMLTableRowElement} The created tr element for the expanded row. |
202 */ | 271 */ |
203 var createExpandedRow = function(guid, baseRow) { | 272 var createExpandedRow = function(guid, baseRow) { |
204 var expandedRow = /** @type {!HTMLElement} */ ( | 273 var expandedRow = createTableRowElement(); |
205 document.createElement('tr')); | |
206 expandedRow.className = 'state-table-row'; | 274 expandedRow.className = 'state-table-row'; |
207 var emptyCell = document.createElement('td'); | 275 var emptyCell = createTableCellElement(); |
208 emptyCell.style.border = 'none'; | 276 emptyCell.style.border = 'none'; |
209 expandedRow.appendChild(emptyCell); | 277 expandedRow.appendChild(emptyCell); |
210 var detailCell = document.createElement('td'); | 278 var detailCell = createTableCellElement(); |
211 detailCell.id = idFromGuid(guid); | 279 detailCell.id = idFromGuid(guid); |
212 detailCell.className = 'state-table-expanded-cell'; | 280 detailCell.className = 'state-table-expanded-cell'; |
213 detailCell.colSpan = baseRow.childNodes.length - 1; | 281 detailCell.colSpan = baseRow.childNodes.length - 1; |
214 expandedRow.appendChild(detailCell); | 282 expandedRow.appendChild(detailCell); |
215 var showDetail = function(state, error) { | 283 var showDetail = function(state, error) { |
216 if (error && error.message) | 284 if (error && error.message) |
217 detailCell.textContent = error.message; | 285 detailCell.textContent = error.message; |
218 else | 286 else |
219 detailCell.textContent = JSON.stringify(state, null, '\t'); | 287 detailCell.textContent = JSON.stringify(state, null, '\t'); |
220 }; | 288 }; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 document.addEventListener('DOMContentLoaded', function() { | 352 document.addEventListener('DOMContentLoaded', function() { |
285 $('refresh').onclick = requestNetworks; | 353 $('refresh').onclick = requestNetworks; |
286 setRefresh(); | 354 setRefresh(); |
287 requestNetworks(); | 355 requestNetworks(); |
288 }); | 356 }); |
289 | 357 |
290 return { | 358 return { |
291 getShillPropertiesResult: getShillPropertiesResult | 359 getShillPropertiesResult: getShillPropertiesResult |
292 }; | 360 }; |
293 })(); | 361 })(); |
OLD | NEW |