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