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 17 matching lines...) Expand all Loading... | |
28 var FAVORITE_STATE_FIELDS = [ | 28 var FAVORITE_STATE_FIELDS = [ |
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 var CreateNetworkState = function(networkState) { | |
39 var oncData = | |
40 /** @type {cr-onc-data} */ document.createElement('cr-onc-data'); | |
James Hawkins
2015/02/06 21:37:37
/** @type {..} */ (...);
Missing parens.
Jeremy Klein
2015/02/09 18:16:30
This should be either a CrOncDataElement or just H
stevenjb
2015/02/10 00:22:04
I don't really understand (3), and I'm not sure wh
Jeremy Klein
2015/02/10 00:31:09
That's fine, but keep in mind that all properties
stevenjb
2015/02/10 03:21:14
If we have a solution I am happy to implement it,
| |
41 oncData.data = networkState; | |
42 return oncData; | |
43 }; | |
44 | |
38 /** | 45 /** |
39 * Returns the property associated with a key (which may reference a | 46 * Returns the property associated with a key (which may reference a |
40 * sub-object). | 47 * sub-object). |
41 * | 48 * |
42 * @param {Object} properties The object containing the network properties. | 49 * @param {Object} properties The object containing the network properties. |
43 * @param {string} key The ONC key for the property. May refer to a nested | 50 * @param {string} key The ONC key for the property. May refer to a nested |
44 * propety, e.g. 'WiFi.Security'. | 51 * propety, e.g. 'WiFi.Security'. |
45 * @return {*} The value associated with the property. | 52 * @return {*} The value associated with the property. |
46 */ | 53 */ |
47 var getValueFromProperties = function(properties, key) { | 54 var getValueFromProperties = function(properties, key) { |
48 if (!key) { | 55 if (!key) { |
49 console.error('Empty key'); | 56 console.error('Empty key'); |
50 return undefined; | 57 return undefined; |
51 } | 58 } |
52 var dot = key.indexOf('.'); | 59 var dot = key.indexOf('.'); |
53 if (dot > 0) { | 60 if (dot > 0) { |
54 var key1 = key.substring(0, dot); | 61 var key1 = key.substring(0, dot); |
55 var key2 = key.substring(dot + 1); | 62 var key2 = key.substring(dot + 1); |
56 var subobject = properties[key1]; | 63 var subobject = properties[key1]; |
57 if (subobject) | 64 if (subobject) |
58 return getValueFromProperties(subobject, key2); | 65 return getValueFromProperties(subobject, key2); |
59 } | 66 } |
60 return properties[key]; | 67 return properties[key]; |
61 }; | 68 }; |
62 | 69 |
63 /** | 70 /** |
64 * Creates a cell with a button for expanding a network state table row. | 71 * Creates a cell with a button for expanding a network state table row. |
65 * | 72 * |
66 * @param {string} guid The GUID identifying the network. | 73 * @param {string} guid The GUID identifying the network. |
67 * @return {DOMElement} The created td element that displays the given value. | 74 * @return {HTMLElement} The created td element that displays the given value. |
68 */ | 75 */ |
69 var createStateTableExpandButton = function(guid) { | 76 var createStateTableExpandButton = function(guid) { |
70 var cell = document.createElement('td'); | 77 var cell = document.createElement('td'); |
71 cell.className = 'state-table-expand-button-cell'; | 78 cell.className = 'state-table-expand-button-cell'; |
72 var button = document.createElement('button'); | 79 var button = document.createElement('button'); |
73 button.addEventListener('click', function(event) { | 80 button.addEventListener('click', function(event) { |
74 toggleExpandRow(event.target, guid); | 81 toggleExpandRow(event.target, guid); |
75 }); | 82 }); |
76 button.className = 'state-table-expand-button'; | 83 button.className = 'state-table-expand-button'; |
77 button.textContent = '+'; | 84 button.textContent = '+'; |
78 cell.appendChild(button); | 85 cell.appendChild(button); |
79 return cell; | 86 return cell; |
80 }; | 87 }; |
81 | 88 |
82 /** | 89 /** |
90 * Creates a cell with an icon representing the network state. | |
91 * | |
92 * @param {Object} state Property values for the network. | |
James Hawkins
2015/02/06 21:37:36
Can we provide a well-named typedef for state inst
stevenjb
2015/02/10 00:22:04
After discussing this offline, I think we can cast
| |
93 * @return {HTMLElement} The created td element that displays the icon. | |
94 */ | |
95 var createStateTableIcon = function(state) { | |
96 var cell = document.createElement('td'); | |
97 cell.className = 'state-table-icon-cell'; | |
98 var icon = document.createElement('cr-network-icon'); | |
99 icon.isListItem = true; | |
100 icon.networkState = CreateNetworkState(state); | |
101 cell.appendChild(icon); | |
102 return cell; | |
103 }; | |
104 | |
105 /** | |
83 * Creates a cell in network state table. | 106 * Creates a cell in network state table. |
84 * | 107 * |
85 * @param {string} value Content in the cell. | 108 * @param {string} value Content in the cell. |
86 * @return {DOMElement} The created td element that displays the given value. | 109 * @return {HTMLElement} The created td element that displays the given value. |
87 */ | 110 */ |
88 var createStateTableCell = function(value) { | 111 var createStateTableCell = function(value) { |
89 var cell = document.createElement('td'); | 112 var cell = document.createElement('td'); |
90 cell.textContent = value || ''; | 113 cell.textContent = value || ''; |
91 return cell; | 114 return cell; |
92 }; | 115 }; |
93 | 116 |
94 /** | 117 /** |
95 * Creates a row in the network state table. | 118 * Creates a row in the network state table. |
96 * | 119 * |
97 * @param {Array} stateFields The state fields to use for the row. | 120 * @param {Array} stateFields The state fields to use for the row. |
98 * @param {Object} state Property values for the network or favorite. | 121 * @param {Object} state Property values for the network or favorite. |
99 * @return {DOMElement} The created tr element that contains the network | 122 * @return {HTMLElement} The created tr element that contains the network |
100 * state information. | 123 * state information. |
101 */ | 124 */ |
102 var createStateTableRow = function(stateFields, state) { | 125 var createStateTableRow = function(stateFields, state) { |
103 var row = document.createElement('tr'); | 126 var row = document.createElement('tr'); |
104 row.className = 'state-table-row'; | 127 row.className = 'state-table-row'; |
105 var guid = state.GUID; | 128 var guid = state.GUID; |
106 row.appendChild(createStateTableExpandButton(guid)); | 129 row.appendChild(createStateTableExpandButton(guid)); |
130 row.appendChild(createStateTableIcon(state)); | |
107 for (var i = 0; i < stateFields.length; ++i) { | 131 for (var i = 0; i < stateFields.length; ++i) { |
108 var field = stateFields[i]; | 132 var field = stateFields[i]; |
109 var value = ''; | 133 var value = ''; |
110 if (typeof field == 'string') { | 134 if (typeof field == 'string') { |
111 value = getValueFromProperties(state, field); | 135 value = getValueFromProperties(state, field); |
112 } else { | 136 } else { |
113 for (var j = 0; j < field.length; ++j) { | 137 for (var j = 0; j < field.length; ++j) { |
114 value = getValueFromProperties(state, field[j]); | 138 value = getValueFromProperties(state, field[j]); |
115 if (value) | 139 if (value) |
116 break; | 140 break; |
(...skipping 23 matching lines...) Expand all Loading... | |
140 }); | 164 }); |
141 }; | 165 }; |
142 | 166 |
143 /** | 167 /** |
144 * This callback function is triggered when visible networks are received. | 168 * This callback function is triggered when visible networks are received. |
145 * | 169 * |
146 * @param {Array} data A list of network state information for each | 170 * @param {Array} data A list of network state information for each |
147 * visible network. | 171 * visible network. |
148 */ | 172 */ |
149 var onVisibleNetworksReceived = function(states) { | 173 var onVisibleNetworksReceived = function(states) { |
174 var defaultState; | |
175 if (states.length > 0) | |
176 defaultState = states[0]; | |
177 if (defaultState && defaultState['Type'] != 'VPN') { | |
178 $('default-network-text').textContent = | |
179 loadTimeData.getStringF('defaultNetworkText', | |
180 defaultState['Name'], | |
181 defaultState['ConnectionState']); | |
182 $('default-network-icon').networkState = CreateNetworkState(defaultState); | |
183 } else { | |
184 $('default-network-text').textContent = | |
185 loadTimeData.getString('noNetworkText'); | |
186 // Show the disconnected wifi icon if there are no networks. | |
187 $('default-network-icon').networkType = 'WiFi'; | |
188 } | |
189 | |
150 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); | 190 createStateTable('network-state-table', NETWORK_STATE_FIELDS, states); |
151 }; | 191 }; |
152 | 192 |
153 /** | 193 /** |
154 * This callback function is triggered when favorite networks are received. | 194 * This callback function is triggered when favorite networks are received. |
155 * | 195 * |
156 * @param {Object} data A list of network state information for each | 196 * @param {Object} data A list of network state information for each |
157 * favorite network. | 197 * favorite network. |
158 */ | 198 */ |
159 var onFavoriteNetworksReceived = function(states) { | 199 var onFavoriteNetworksReceived = function(states) { |
160 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); | 200 createStateTable('favorite-state-table', FAVORITE_STATE_FIELDS, states); |
161 }; | 201 }; |
162 | 202 |
163 /** | 203 /** |
164 * Toggles the button state and add or remove a row displaying the complete | 204 * Toggles the button state and add or remove a row displaying the complete |
165 * state information for a row. | 205 * state information for a row. |
166 * | 206 * |
167 * @param {DOMElement} btn The button that was clicked. | 207 * @param {HTMLElement} btn The button that was clicked. |
168 * @param {string} guid GUID identifying the network. | 208 * @param {string} guid GUID identifying the network. |
169 */ | 209 */ |
170 var toggleExpandRow = function(btn, guid) { | 210 var toggleExpandRow = function(btn, guid) { |
171 var cell = btn.parentNode; | 211 var cell = btn.parentNode; |
172 var row = cell.parentNode; | 212 var row = cell.parentNode; |
173 if (btn.textContent == '-') { | 213 if (btn.textContent == '-') { |
174 btn.textContent = '+'; | 214 btn.textContent = '+'; |
175 row.parentNode.removeChild(row.nextSibling); | 215 row.parentNode.removeChild(row.nextSibling); |
176 } else { | 216 } else { |
177 btn.textContent = '-'; | 217 btn.textContent = '-'; |
178 var expandedRow = createExpandedRow(guid, row); | 218 var expandedRow = createExpandedRow(guid, row); |
179 row.parentNode.insertBefore(expandedRow, row.nextSibling); | 219 row.parentNode.insertBefore(expandedRow, row.nextSibling); |
180 } | 220 } |
181 }; | 221 }; |
182 | 222 |
183 /** | 223 /** |
184 * Creates the expanded row for displaying the complete state as JSON. | 224 * Creates the expanded row for displaying the complete state as JSON. |
185 * | 225 * |
186 * @param {Object} state Property values for the network or favorite. | 226 * @param {Object} state Property values for the network or favorite. |
187 * @param {DOMElement} baseRow The unexpanded row associated with the new row. | 227 * @param {HTMLElement} baseRow The unexpanded row above the new row. |
188 * @return {DOMElement} The created tr element for the expanded row. | 228 * @return {HTMLElement} The created tr element for the expanded row. |
189 */ | 229 */ |
190 var createExpandedRow = function(guid, baseRow) { | 230 var createExpandedRow = function(guid, baseRow) { |
191 var expandedRow = document.createElement('tr'); | 231 var expandedRow = document.createElement('tr'); |
192 expandedRow.className = 'state-table-row'; | 232 expandedRow.className = 'state-table-row'; |
193 var emptyCell = document.createElement('td'); | 233 var emptyCell = document.createElement('td'); |
194 emptyCell.style.border = 'none'; | 234 emptyCell.style.border = 'none'; |
195 expandedRow.appendChild(emptyCell); | 235 expandedRow.appendChild(emptyCell); |
196 var detailCell = document.createElement('td'); | 236 var detailCell = document.createElement('td'); |
197 detailCell.id = guid; | 237 detailCell.id = guid; |
198 detailCell.className = 'state-table-expanded-cell'; | 238 detailCell.className = 'state-table-expanded-cell'; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 document.addEventListener('DOMContentLoaded', function() { | 310 document.addEventListener('DOMContentLoaded', function() { |
271 $('refresh').onclick = requestNetworks; | 311 $('refresh').onclick = requestNetworks; |
272 setRefresh(); | 312 setRefresh(); |
273 requestNetworks(); | 313 requestNetworks(); |
274 }); | 314 }); |
275 | 315 |
276 return { | 316 return { |
277 getShillPropertiesResult: getShillPropertiesResult | 317 getShillPropertiesResult: getShillPropertiesResult |
278 }; | 318 }; |
279 })(); | 319 })(); |
OLD | NEW |