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 * 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. | |
michaelpg
2015/02/20 10:00:17
s/net/new/
stevenjb
2015/02/23 19:23:07
Done.
| |
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('.'); |
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; |
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. |
michaelpg
2015/02/20 10:00:17
"in the"
stevenjb
2015/02/23 19:23:08
Done.
| |
85 * | 135 * |
86 * @param {string} value Content in the cell. | 136 * @param {string} 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) |
Dan Beam
2015/02/20 01:02:42
why != undefined?
stevenjb
2015/02/20 02:22:47
'' or 0 are valid values, but getOncProperty retur
| |
118 break; | 169 break; |
119 } | 170 } |
120 } | 171 } |
172 var stringValue = | |
Dan Beam
2015/02/20 01:02:42
value = value || '';
OR
var stringValue = val
stevenjb
2015/02/20 02:22:47
I think I am incorrectly forcing this to a string;
| |
173 (value != undefined) ? /** @type {string} */ (value) : ''; | |
121 if (field == 'GUID') | 174 if (field == 'GUID') |
122 value = value.slice(0, 8); | 175 stringValue = stringValue.slice(0, 8); |
123 row.appendChild(createStateTableCell(/** @type {string} */ (value))); | 176 row.appendChild(createStateTableCell(stringValue)); |
124 } | 177 } |
125 return row; | 178 return row; |
126 }; | 179 }; |
127 | 180 |
128 /** | 181 /** |
129 * Creates a table for networks or favorites. | 182 * Creates a table for networks or favorites. |
130 * | 183 * |
131 * @param {string} tablename The name of the table to be created. | 184 * @param {string} tablename The name of the table to be created. |
132 * @param {Array} stateFields The list of fields for the table. | 185 * @param {Array} stateFields The list of fields for the table. |
133 * @param {Array} states An array of network or favorite states. | 186 * @param {Array} states An array of network or favorite states. |
134 */ | 187 */ |
135 var createStateTable = function(tablename, stateFields, states) { | 188 var createStateTable = function(tablename, stateFields, states) { |
136 var table = $(tablename); | 189 var table = $(tablename); |
137 var oldRows = table.querySelectorAll('.state-table-row'); | 190 var oldRows = table.querySelectorAll('.state-table-row'); |
138 for (var i = 0; i < oldRows.length; ++i) | 191 for (var i = 0; i < oldRows.length; ++i) |
139 table.removeChild(oldRows[i]); | 192 table.removeChild(oldRows[i]); |
140 states.forEach(function(state) { | 193 states.forEach(function(state) { |
141 table.appendChild(createStateTableRow(stateFields, state)); | 194 table.appendChild(createStateTableRow( |
195 stateFields, /** @type {!CrOnc.NetworkConfigType} */ (state))); | |
Dan Beam
2015/02/20 01:02:42
nit: no space between cast, i.e.
/** cast */(ob
stevenjb
2015/02/20 02:22:47
Done throughout.
| |
142 }); | 196 }); |
143 }; | 197 }; |
144 | 198 |
145 /** | 199 /** |
146 * This callback function is triggered when visible networks are received. | 200 * This callback function is triggered when visible networks are received. |
147 * | 201 * |
148 * @param {Array} states A list of network state information for each | 202 * @param {!Array<!Object>} states A list of network state information for |
149 * visible network. | 203 * each visible network. |
150 */ | 204 */ |
151 var onVisibleNetworksReceived = function(states) { | 205 var onVisibleNetworksReceived = function(states) { |
206 /** @type {CrOnc.NetworkConfigType} */ var defaultState; | |
207 if (states.length > 0) | |
208 defaultState = /** @type {!CrOnc.NetworkConfigType} */ (states[0]); | |
Dan Beam
2015/02/20 01:02:42
wrong indent
stevenjb
2015/02/20 02:22:47
Done.
| |
209 var icon = /** @type {CrNetworkIconElement} */ ($('default-network-icon')); | |
210 if (defaultState && defaultState.Type != 'VPN') { | |
211 $('default-network-text').textContent = | |
212 loadTimeData.getStringF('defaultNetworkText', | |
213 defaultState.Name, | |
214 defaultState.ConnectionState); | |
215 icon.networkState = createNetworkState(defaultState); | |
216 } else { | |
217 $('default-network-text').textContent = | |
218 loadTimeData.getString('noNetworkText'); | |
219 // Show the disconnected wifi icon if there are no networks. | |
220 icon.networkType = 'WiFi'; | |
221 } | |
222 | |
152 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 223 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); |
153 }; | 224 }; |
154 | 225 |
155 /** | 226 /** |
156 * This callback function is triggered when favorite networks are received. | 227 * This callback function is triggered when favorite networks are received. |
157 * | 228 * |
158 * @param {!Array} states A list of network state information for each | 229 * @param {!Array<!Object>} states A list of network state information for |
159 * favorite network. | 230 * each favorite network. |
160 */ | 231 */ |
161 var onFavoriteNetworksReceived = function(states) { | 232 var onFavoriteNetworksReceived = function(states) { |
162 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); | 233 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); |
163 }; | 234 }; |
164 | 235 |
165 /** | 236 /** |
166 * Toggles the button state and add or remove a row displaying the complete | 237 * Toggles the button state and add or remove a row displaying the complete |
167 * state information for a row. | 238 * state information for a row. |
168 * | 239 * |
169 * @param {!HTMLElement} btn The button that was clicked. | 240 * @param {!HTMLElement} btn The button that was clicked. |
170 * @param {string} guid GUID identifying the network. | 241 * @param {string} guid GUID identifying the network. |
171 */ | 242 */ |
172 var toggleExpandRow = function(btn, guid) { | 243 var toggleExpandRow = function(btn, guid) { |
173 var cell = btn.parentNode; | 244 var cell = btn.parentNode; |
174 var row = /** @type {!HTMLElement} */ (cell.parentNode); | 245 var row = /** @type {!HTMLTableRowElement} */ (cell.parentNode); |
175 if (btn.textContent == '-') { | 246 if (btn.textContent == '-') { |
176 btn.textContent = '+'; | 247 btn.textContent = '+'; |
177 row.parentNode.removeChild(row.nextSibling); | 248 row.parentNode.removeChild(row.nextSibling); |
178 } else { | 249 } else { |
179 btn.textContent = '-'; | 250 btn.textContent = '-'; |
180 var expandedRow = createExpandedRow(guid, row); | 251 var expandedRow = createExpandedRow(guid, row); |
181 row.parentNode.insertBefore(expandedRow, row.nextSibling); | 252 row.parentNode.insertBefore(expandedRow, row.nextSibling); |
182 } | 253 } |
183 }; | 254 }; |
184 | 255 |
185 /** | 256 /** |
186 * Creates the expanded row for displaying the complete state as JSON. | 257 * Creates the expanded row for displaying the complete state as JSON. |
187 * | 258 * |
188 * @param {string} guid The GUID identifying the network. | 259 * @param {string} guid The GUID identifying the network. |
189 * @param {!HTMLElement} baseRow The unexpanded row associated with the new | 260 * @param {!HTMLTableRowElement} baseRow The unexpanded row associated with |
190 * row. | 261 * the new row. |
191 * @return {!HTMLElement} The created tr element for the expanded row. | 262 * @return {!HTMLTableRowElement} The created tr element for the expanded row. |
192 */ | 263 */ |
193 var createExpandedRow = function(guid, baseRow) { | 264 var createExpandedRow = function(guid, baseRow) { |
194 var expandedRow = /** @type {!HTMLElement} */ ( | 265 var expandedRow = createTableRowElement(); |
195 document.createElement('tr')); | |
196 expandedRow.className = 'state-table-row'; | 266 expandedRow.className = 'state-table-row'; |
197 var emptyCell = document.createElement('td'); | 267 var emptyCell = createTableCellElement(); |
198 emptyCell.style.border = 'none'; | 268 emptyCell.style.border = 'none'; |
199 expandedRow.appendChild(emptyCell); | 269 expandedRow.appendChild(emptyCell); |
200 var detailCell = document.createElement('td'); | 270 var detailCell = createTableCellElement(); |
201 detailCell.id = guid; | 271 detailCell.id = guid; |
202 detailCell.className = 'state-table-expanded-cell'; | 272 detailCell.className = 'state-table-expanded-cell'; |
203 detailCell.colSpan = baseRow.childNodes.length - 1; | 273 detailCell.colSpan = baseRow.childNodes.length - 1; |
204 expandedRow.appendChild(detailCell); | 274 expandedRow.appendChild(detailCell); |
205 var showDetail = function(state, error) { | 275 var showDetail = function(state, error) { |
206 if (error && error.message) | 276 if (error && error.message) |
207 detailCell.textContent = error.message; | 277 detailCell.textContent = error.message; |
208 else | 278 else |
209 detailCell.textContent = JSON.stringify(state, null, '\t'); | 279 detailCell.textContent = JSON.stringify(state, null, '\t'); |
210 }; | 280 }; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 document.addEventListener('DOMContentLoaded', function() { | 344 document.addEventListener('DOMContentLoaded', function() { |
275 $('refresh').onclick = requestNetworks; | 345 $('refresh').onclick = requestNetworks; |
276 setRefresh(); | 346 setRefresh(); |
277 requestNetworks(); | 347 requestNetworks(); |
278 }); | 348 }); |
279 | 349 |
280 return { | 350 return { |
281 getShillPropertiesResult: getShillPropertiesResult | 351 getShillPropertiesResult: getShillPropertiesResult |
282 }; | 352 }; |
283 })(); | 353 })(); |
OLD | NEW |