 Chromium Code Reviews
 Chromium Code Reviews Issue 874283006:
  Add custom Polymer network icon element  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 874283006:
  Add custom Polymer network icon element  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 * @param {string} key The ONC key for the property. May refer to a nested | 43 * @return {!CrOncDataElement} A cr-onc-data element. | 
| 44 * propety, e.g. 'WiFi.Security'. | |
| 45 * @return {*} The value associated with the property. | |
| 46 */ | 44 */ | 
| 47 var getValueFromProperties = function(properties, key) { | 45 var createNetworkState = function(state) { | 
| 48 if (!key) { | 46 var oncData = /** @type {!CrOncDataElement} */( | 
| 49 console.error('Empty key'); | 47 document.createElement('cr-onc-data')); | 
| 50 return undefined; | 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 new 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 the networkState by |key| which may May refer to a | |
| 
michaelpg
2015/02/24 07:24:57
nit... s/May/may
 
stevenjb
2015/02/24 19:28:00
Done.
 | |
| 73 * nested property, e.g. 'WiFi.Security'. If any part of a nested key is | |
| 74 * missing, this will return undefined. | |
| 75 * | |
| 76 * @param {!CrOnc.NetworkConfigType} networkState The network state | |
| 77 * property dictionary. | |
| 78 * @param {string} key The ONC key for the property. | |
| 79 * @return {*} The value associated with the property or undefined if the | |
| 80 * key (any part of it) is not defined. | |
| 
michaelpg
2015/02/24 07:24:57
indent 4 spaces (ie, add 3 spaces)
 
stevenjb
2015/02/24 19:28:00
Done.
 | |
| 81 */ | |
| 82 var getOncProperty = function(networkState, key) { | |
| 83 var dict = /** @type {!Object} */(networkState); | |
| 84 var keys = key.split('.'); | |
| 85 while (keys.length > 1) { | |
| 86 var k = keys.shift(); | |
| 87 dict = dict[k]; | |
| 88 if (!dict || typeof dict != 'object') | |
| 89 return undefined; | |
| 51 } | 90 } | 
| 52 var dot = key.indexOf('.'); | 91 return dict[keys.shift()]; | 
| 53 if (dot > 0) { | |
| 54 var key1 = key.substring(0, dot); | |
| 55 var key2 = key.substring(dot + 1); | |
| 56 var subobject = properties[key1]; | |
| 57 if (subobject) | |
| 58 return getValueFromProperties(subobject, key2); | |
| 59 } | |
| 60 return properties[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 /** | 
| 84 * Creates a cell in network state table. | 115 * Creates a cell with an icon representing the network state. | 
| 85 * | 116 * | 
| 86 * @param {string} value Content in the cell. | 117 * @param {CrOnc.NetworkConfigType} networkState The network state properties. | 
| 87 * @return {!HTMLElement} The created td element that displays the given | 118 * @return {!HTMLTableCellElement} The created td element that displays the | 
| 88 * value. | 119 * icon. | 
| 120 */ | |
| 121 var createStateTableIcon = function(networkState) { | |
| 122 var cell = createTableCellElement(); | |
| 123 cell.className = 'state-table-icon-cell'; | |
| 124 var icon = /** @type {!CrNetworkIconElement} */( | |
| 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 /** | |
| 133 * Creates a cell in the network state table. | |
| 134 * | |
| 135 * @param {*} value Content in the cell. | |
| 136 * @return {!HTMLTableCellElement} The created td element that displays the | |
| 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 } | 
| 121 if (field == 'GUID') | 171 if (field == 'GUID') | 
| 122 value = value.slice(0, 8); | 172 value = value.slice(0, 8); | 
| 123 row.appendChild(createStateTableCell(/** @type {string} */ (value))); | 173 row.appendChild(createStateTableCell(value)); | 
| 124 } | 174 } | 
| 125 return row; | 175 return row; | 
| 126 }; | 176 }; | 
| 127 | 177 | 
| 128 /** | 178 /** | 
| 129 * Creates a table for networks or favorites. | 179 * Creates a table for networks or favorites. | 
| 130 * | 180 * | 
| 131 * @param {string} tablename The name of the table to be created. | 181 * @param {string} tablename The name of the table to be created. | 
| 132 * @param {Array} stateFields The list of fields for the table. | 182 * @param {Array} stateFields The list of fields for the table. | 
| 133 * @param {Array} states An array of network or favorite states. | 183 * @param {Array} states An array of network or favorite states. | 
| 134 */ | 184 */ | 
| 135 var createStateTable = function(tablename, stateFields, states) { | 185 var createStateTable = function(tablename, stateFields, states) { | 
| 136 var table = $(tablename); | 186 var table = $(tablename); | 
| 137 var oldRows = table.querySelectorAll('.state-table-row'); | 187 var oldRows = table.querySelectorAll('.state-table-row'); | 
| 138 for (var i = 0; i < oldRows.length; ++i) | 188 for (var i = 0; i < oldRows.length; ++i) | 
| 139 table.removeChild(oldRows[i]); | 189 table.removeChild(oldRows[i]); | 
| 140 states.forEach(function(state) { | 190 states.forEach(function(state) { | 
| 141 table.appendChild(createStateTableRow(stateFields, state)); | 191 table.appendChild(createStateTableRow( | 
| 192 stateFields, /** @type {!CrOnc.NetworkConfigType} */(state))); | |
| 142 }); | 193 }); | 
| 143 }; | 194 }; | 
| 144 | 195 | 
| 145 /** | 196 /** | 
| 146 * Returns a valid HTMLElement id from |guid|. | 197 * Returns a valid HTMLElement id from |guid|. | 
| 147 * | 198 * | 
| 148 * @param {string} guid A GUID which may start with a digit | 199 * @param {string} guid A GUID which may start with a digit | 
| 
michaelpg
2015/02/24 07:24:57
add trailing '.'
 
stevenjb
2015/02/24 19:28:00
Done.
 | |
| 149 * @return {string} A valid HTMLElement id. | 200 * @return {string} A valid HTMLElement id. | 
| 150 */ | 201 */ | 
| 151 var idFromGuid = function(guid) { | 202 var idFromGuid = function(guid) { | 
| 152 return '_' + guid.replace(/[{}]/g, ''); | 203 return '_' + guid.replace(/[{}]/g, ''); | 
| 153 }; | 204 }; | 
| 154 | 205 | 
| 155 /** | 206 /** | 
| 156 * This callback function is triggered when visible networks are received. | 207 * This callback function is triggered when visible networks are received. | 
| 157 * | 208 * | 
| 158 * @param {Array} states A list of network state information for each | 209 * @param {!Array<!Object>} states A list of network state information for | 
| 159 * visible network. | 210 * each visible network. | 
| 160 */ | 211 */ | 
| 161 var onVisibleNetworksReceived = function(states) { | 212 var onVisibleNetworksReceived = function(states) { | 
| 213 /** @type {CrOnc.NetworkConfigType} */ var defaultState; | |
| 214 if (states.length > 0) | |
| 215 defaultState = /** @type {!CrOnc.NetworkConfigType} */(states[0]); | |
| 216 var icon = /** @type {CrNetworkIconElement} */($('default-network-icon')); | |
| 217 if (defaultState && defaultState.Type != 'VPN') { | |
| 218 $('default-network-text').textContent = | |
| 219 loadTimeData.getStringF('defaultNetworkText', | |
| 220 defaultState.Name, | |
| 221 defaultState.ConnectionState); | |
| 222 icon.networkState = createNetworkState(defaultState); | |
| 223 } else { | |
| 224 $('default-network-text').textContent = | |
| 225 loadTimeData.getString('noNetworkText'); | |
| 226 // Show the disconnected wifi icon if there are no networks. | |
| 227 icon.networkType = CrOnc.Type.WIFI; | |
| 228 } | |
| 229 | |
| 162 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 230 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 
| 163 }; | 231 }; | 
| 164 | 232 | 
| 165 /** | 233 /** | 
| 166 * This callback function is triggered when favorite networks are received. | 234 * This callback function is triggered when favorite networks are received. | 
| 167 * | 235 * | 
| 168 * @param {!Array} states A list of network state information for each | 236 * @param {!Array<!Object>} states A list of network state information for | 
| 169 * favorite network. | 237 * each favorite network. | 
| 170 */ | 238 */ | 
| 171 var onFavoriteNetworksReceived = function(states) { | 239 var onFavoriteNetworksReceived = function(states) { | 
| 172 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); | 240 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); | 
| 173 }; | 241 }; | 
| 174 | 242 | 
| 175 /** | 243 /** | 
| 176 * Toggles the button state and add or remove a row displaying the complete | 244 * Toggles the button state and add or remove a row displaying the complete | 
| 177 * state information for a row. | 245 * state information for a row. | 
| 178 * | 246 * | 
| 179 * @param {!HTMLElement} btn The button that was clicked. | 247 * @param {!HTMLElement} btn The button that was clicked. | 
| 180 * @param {string} guid GUID identifying the network. | 248 * @param {string} guid GUID identifying the network. | 
| 181 */ | 249 */ | 
| 182 var toggleExpandRow = function(btn, guid) { | 250 var toggleExpandRow = function(btn, guid) { | 
| 183 var cell = btn.parentNode; | 251 var cell = btn.parentNode; | 
| 184 var row = /** @type {!HTMLElement} */ (cell.parentNode); | 252 var row = /** @type {!HTMLTableRowElement} */(cell.parentNode); | 
| 185 if (btn.textContent == '-') { | 253 if (btn.textContent == '-') { | 
| 186 btn.textContent = '+'; | 254 btn.textContent = '+'; | 
| 187 row.parentNode.removeChild(row.nextSibling); | 255 row.parentNode.removeChild(row.nextSibling); | 
| 188 } else { | 256 } else { | 
| 189 btn.textContent = '-'; | 257 btn.textContent = '-'; | 
| 190 var expandedRow = createExpandedRow(guid, row); | 258 var expandedRow = createExpandedRow(guid, row); | 
| 191 row.parentNode.insertBefore(expandedRow, row.nextSibling); | 259 row.parentNode.insertBefore(expandedRow, row.nextSibling); | 
| 192 } | 260 } | 
| 193 }; | 261 }; | 
| 194 | 262 | 
| 195 /** | 263 /** | 
| 196 * Creates the expanded row for displaying the complete state as JSON. | 264 * Creates the expanded row for displaying the complete state as JSON. | 
| 197 * | 265 * | 
| 198 * @param {string} guid The GUID identifying the network. | 266 * @param {string} guid The GUID identifying the network. | 
| 199 * @param {!HTMLElement} baseRow The unexpanded row associated with the new | 267 * @param {!HTMLTableRowElement} baseRow The unexpanded row associated with | 
| 200 * row. | 268 * the new row. | 
| 201 * @return {!HTMLElement} The created tr element for the expanded row. | 269 * @return {!HTMLTableRowElement} The created tr element for the expanded row. | 
| 202 */ | 270 */ | 
| 203 var createExpandedRow = function(guid, baseRow) { | 271 var createExpandedRow = function(guid, baseRow) { | 
| 204 var expandedRow = /** @type {!HTMLElement} */ ( | 272 var expandedRow = createTableRowElement(); | 
| 205 document.createElement('tr')); | |
| 206 expandedRow.className = 'state-table-row'; | 273 expandedRow.className = 'state-table-row'; | 
| 207 var emptyCell = document.createElement('td'); | 274 var emptyCell = createTableCellElement(); | 
| 208 emptyCell.style.border = 'none'; | 275 emptyCell.style.border = 'none'; | 
| 209 expandedRow.appendChild(emptyCell); | 276 expandedRow.appendChild(emptyCell); | 
| 210 var detailCell = document.createElement('td'); | 277 var detailCell = createTableCellElement(); | 
| 211 detailCell.id = idFromGuid(guid); | 278 detailCell.id = idFromGuid(guid); | 
| 212 detailCell.className = 'state-table-expanded-cell'; | 279 detailCell.className = 'state-table-expanded-cell'; | 
| 213 detailCell.colSpan = baseRow.childNodes.length - 1; | 280 detailCell.colSpan = baseRow.childNodes.length - 1; | 
| 214 expandedRow.appendChild(detailCell); | 281 expandedRow.appendChild(detailCell); | 
| 215 var showDetail = function(state, error) { | 282 var showDetail = function(state, error) { | 
| 216 if (error && error.message) | 283 if (error && error.message) | 
| 217 detailCell.textContent = error.message; | 284 detailCell.textContent = error.message; | 
| 218 else | 285 else | 
| 219 detailCell.textContent = JSON.stringify(state, null, '\t'); | 286 detailCell.textContent = JSON.stringify(state, null, '\t'); | 
| 220 }; | 287 }; | 
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 document.addEventListener('DOMContentLoaded', function() { | 351 document.addEventListener('DOMContentLoaded', function() { | 
| 285 $('refresh').onclick = requestNetworks; | 352 $('refresh').onclick = requestNetworks; | 
| 286 setRefresh(); | 353 setRefresh(); | 
| 287 requestNetworks(); | 354 requestNetworks(); | 
| 288 }); | 355 }); | 
| 289 | 356 | 
| 290 return { | 357 return { | 
| 291 getShillPropertiesResult: getShillPropertiesResult | 358 getShillPropertiesResult: getShillPropertiesResult | 
| 292 }; | 359 }; | 
| 293 })(); | 360 })(); | 
| OLD | NEW |