Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 localStrings = new LocalStrings(); | 5 //var localStrings = new LocalStrings(); |
|
James Hawkins
2011/09/05 22:10:38
Deadcode.
simo
2011/09/07 12:18:16
Done.
| |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * This variable structure is here to document the structure that the template | 8 * This variable structure is here to document the structure that the template |
| 9 * expects to correctly populate the page. | 9 * expects to correctly populate the page. |
| 10 */ | 10 */ |
| 11 var policyDataformat = { | 11 var policyDataFormat = { |
| 12 'anyPoliciesSet': true, | |
|
James Hawkins
2011/09/05 22:10:38
Document vars.
simo
2011/09/07 12:18:16
Done.
| |
| 13 'isPolicyUpdate': false, | |
| 12 'policies': [ | 14 'policies': [ |
| 13 { | 15 { |
| 14 'level': 'managed', | 16 'level': 'managed', |
| 15 'name': 'AllowXYZ', | 17 'name': 'AllowXYZ', |
| 16 'set': true, | 18 'set': true, |
| 17 'sourceType': 'Device', | 19 'sourceType': 'Device', |
| 18 'status': 'ok', | 20 'status': 'ok', |
| 19 'value': true, | 21 'value': true |
| 20 }, | 22 } |
| 21 ], | 23 ], |
| 22 'anyPoliciesSet': true | 24 'status': { |
| 25 'deviceFetchInterval': '8min', | |
| 26 'deviceId': 'D2AC39A2-3C8FC-E2C0-E45D2DC3782C', | |
| 27 'deviceLastFetchTime': '9:50 PM', | |
| 28 'devicePolicyDomain': 'google.com', | |
| 29 'deviceStatusMessage': 'OK', | |
| 30 'displayStatusSection': true, | |
| 31 'user': 'simo@google.com', | |
| 32 'userFetchInterval': '8min', | |
| 33 'userId': 'D2AC39A2-3C8FC-E2C0-E45D2DC3782C', | |
| 34 'userLastFetchTime': '9:50 PM', | |
| 35 'userStatusMessage': 'OK' | |
| 36 } | |
| 23 }; | 37 }; |
| 24 | 38 |
| 25 cr.define('policies', function() { | 39 cr.define('policies', function() { |
| 26 | 40 |
| 27 function Policy() { | 41 function Policy() { |
| 28 } | 42 } |
| 29 | 43 |
| 30 cr.addSingletonGetter(Policy); | 44 cr.addSingletonGetter(Policy); |
| 31 | 45 |
| 32 Policy.prototype = { | 46 Policy.prototype = { |
| 33 | 47 |
| 34 /** | 48 /** |
| 35 * True if none of the received policies are actually set, false otherwise. | 49 * True if none of the received policies are actually set, false otherwise. |
| 36 * @type {boolean} | 50 * @type {boolean} |
| 37 */ | 51 */ |
| 38 noActivePolicies_: false, | 52 noActivePolicies_: false, |
| 39 | 53 |
| 40 /** | 54 /** |
| 55 * True if the status section is being displayed, false otherwise. | |
| 56 * @type {boolean} | |
| 57 */ | |
| 58 displayStatusSection_: false, | |
|
James Hawkins
2011/09/05 22:10:38
Why not check the visibility of the status on the
simo
2011/09/07 12:18:16
Done. I had some use for this var before and then
| |
| 59 | |
| 60 /** | |
| 41 * The current search term for filtering of the policy table. | 61 * The current search term for filtering of the policy table. |
| 42 * @type {string} | 62 * @type {string} |
| 43 * @private | 63 * @private |
| 44 */ | 64 */ |
| 45 searchTerm_: '', | 65 searchTerm_: '', |
| 46 | 66 |
| 47 /** | 67 /** |
| 48 * Takes the |policyData| input argument which represents data about the | 68 * Takes the |policyData| argument and populates the page with this data. It |
| 49 * policies supported by the device/client and populates the html jstemplate | 69 * expects an object structure like the above. |
|
James Hawkins
2011/09/05 22:10:38
'the above' is ambiguous.
simo
2011/09/07 12:18:16
Done.
| |
| 50 * with that data. It expects an object structure like the above. | |
| 51 * @param {Object} policyData Detailed info about policies | 70 * @param {Object} policyData Detailed info about policies |
|
James Hawkins
2011/09/05 22:10:38
You changed the parameter but not the documentatio
simo
2011/09/07 12:18:16
At some point I distinguished between statusData a
| |
| 52 */ | 71 */ |
| 53 renderTemplate: function(policyData) { | 72 renderTemplate: function(data) { |
| 54 this.noActivePolicies_ = !policyData.anyPoliciesSet; | 73 this.noActivePolicies_ = !data.anyPoliciesSet; |
| 74 this.displayStatusSection_ = data.status.displayStatusSection; | |
| 55 | 75 |
| 56 // This is the javascript code that processes the template: | 76 // This is the javascript code that processes the template: |
| 57 var input = new JsEvalContext(policyData); | 77 var input = new JsEvalContext(data); |
| 58 var output = $('policiesTemplate'); | 78 var output = $('dataTemplate'); |
| 59 jstProcess(input, output); | 79 jstProcess(input, output); |
| 80 this.setToggleEventHandlers_(); | |
| 60 }, | 81 }, |
| 61 | 82 |
| 62 /** | 83 /** |
| 63 * Filters the table of policies by name. | 84 * Filters the table of policies by name. |
| 64 * @param {string} term The search string | 85 * @param {string} term The search string |
| 65 */ | 86 */ |
| 66 filterTable: function(term) { | 87 filterTable: function(term) { |
| 67 this.searchTerm_ = term.toLowerCase(); | 88 this.searchTerm_ = term.toLowerCase(); |
| 68 var table = $('policy-table'); | 89 var table = $('policy-table'); |
| 69 var showUnsent = $('toggle-unsent-policies').checked; | 90 var showUnsent = $('toggle-unsent-policies').checked; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 90 */ | 111 */ |
| 91 updatePolicyVisibility: function() { | 112 updatePolicyVisibility: function() { |
| 92 if ($('toggle-unsent-policies').checked) | 113 if ($('toggle-unsent-policies').checked) |
| 93 $('policies').style.display = ''; | 114 $('policies').style.display = ''; |
| 94 else if (this.noActivePolicies_) | 115 else if (this.noActivePolicies_) |
| 95 $('policies').style.display = 'none'; | 116 $('policies').style.display = 'none'; |
| 96 | 117 |
| 97 var tableRows = document.getElementsByClassName('policy-unset'); | 118 var tableRows = document.getElementsByClassName('policy-unset'); |
| 98 for (var i = 0; i < tableRows.length; i++) { | 119 for (var i = 0; i < tableRows.length; i++) { |
| 99 if ($('toggle-unsent-policies').checked) | 120 if ($('toggle-unsent-policies').checked) |
| 100 tableRows[i].style.visibility = 'visible'; | 121 tableRows[i].style.display = 'table-row'; |
|
James Hawkins
2011/09/05 22:10:38
Using hidden will make this a lot simpler.
simo
2011/09/07 12:18:16
I've changed this but I don't really understand ho
| |
| 101 else | 122 else |
| 102 tableRows[i].style.visibility = 'hidden'; | 123 tableRows[i].style.display = 'none'; |
| 103 } | 124 } |
| 104 | 125 |
| 105 // Filter table again in case a search was active. | 126 // Filter table again in case a search was active. |
| 106 this.filterTable(this.searchTerm_); | 127 this.filterTable(this.searchTerm_); |
| 128 }, | |
| 129 | |
| 130 /** | |
| 131 * Set event handlers for the "Show more"/"Hide" links generated by | |
| 132 * jstemplate. | |
| 133 */ | |
| 134 setToggleEventHandlers_: function() { | |
| 135 var toggles = document.querySelectorAll('.policy-set * .toggler'); | |
| 136 for (var i = 0; i < toggles.length; i++) { | |
| 137 toggles[i].onclick = function() { | |
| 138 Policy.getInstance().toggleCellExpand_(this); | |
| 139 }; | |
| 140 } | |
| 141 }, | |
| 142 | |
| 143 /** | |
| 144 * Expands or collapses a table cell that has overflowing text. | |
| 145 * @param {Object} toggler The toggler that was clicked on | |
| 146 */ | |
| 147 toggleCellExpand_: function(toggler) { | |
| 148 var tableCell = toggler.parentElement; | |
| 149 var textContainer = tableCell.querySelector('.text-container'); | |
| 150 | |
| 151 if (textContainer.collapsed) | |
| 152 this.expandCell_(textContainer); | |
| 153 else | |
| 154 this.collapseCell_(textContainer); | |
| 155 | |
| 156 textContainer.collapsed = !textContainer.collapsed; | |
| 157 var expand = tableCell.querySelector('.expand'); | |
| 158 var collapse = tableCell.querySelector('.collapse'); | |
| 159 expand.style.display = textContainer.collapsed ? '' : 'none'; | |
| 160 collapse.style.display = textContainer.collapsed ? 'none' : ''; | |
| 161 }, | |
| 162 | |
| 163 /** | |
| 164 * Collapses all expanded table cells and updates the visibility of the | |
| 165 * toggles accordingly. Should be called before the policy information in | |
| 166 * the table is updated. | |
| 167 */ | |
| 168 collapseExpandedCells: function() { | |
| 169 var toggles = document.querySelectorAll('.policy-set * .toggler'); | |
| 170 for (var i = 0; i < toggles.length; i++) | |
| 171 toggles[i].style.display = 'none'; | |
| 172 | |
| 173 var textContainers = | |
| 174 document.querySelectorAll('.expanded'); | |
| 175 for (var i = 0; i < textContainers.length; i++) | |
| 176 this.collapseCell_(textContainers[i]); | |
| 177 }, | |
| 178 | |
| 179 /** | |
| 180 * Expands a table cell so that all the text it contains is visible. | |
| 181 * @param {Object} textContainer The cell's div element that contains the | |
| 182 * text | |
| 183 */ | |
| 184 expandCell_: function(textContainer) { | |
| 185 textContainer.classList.remove('collapsed'); | |
| 186 textContainer.classList.add('expanded'); | |
| 187 }, | |
| 188 | |
| 189 /** | |
| 190 * Collapses a table cell so that overflowing text is hidden. | |
| 191 * @param {Object} textContainer The cell's div element that contains the | |
| 192 * text | |
| 193 */ | |
| 194 collapseCell_: function(textContainer) { | |
| 195 textContainer.classList.remove('expanded'); | |
| 196 textContainer.classList.add('collapsed'); | |
| 107 } | 197 } |
| 108 }; | 198 }; |
| 109 | 199 |
| 110 /** | 200 /** |
| 111 * Asks the C++ PolicyUIHandler to get details about policies. The | 201 * Asks the C++ PolicyUIHandler to get details about policies and status |
| 112 * PolicyDOMHandler should reply to returnPolicyData() (below). | 202 * information. The PolicyUIHandler should reply to returnData() (below). |
| 113 */ | 203 */ |
| 114 Policy.requestPolicyData = function() { | 204 Policy.requestData = function() { |
| 115 chrome.send('requestPolicyData'); | 205 chrome.send('requestData'); |
| 116 }; | 206 }; |
| 117 | 207 |
| 118 /** | 208 /** |
| 119 * Called by the C++ PolicyUIHandler when it has the requested policy data. | 209 * Called by the C++ PolicyUIHandler when it has the requested data. |
| 210 * @param {Object} data The data | |
|
James Hawkins
2011/09/05 22:10:38
Period at end of sentence. I'd also elucidate on '
simo
2011/09/07 12:18:16
Done.
| |
| 120 */ | 211 */ |
| 121 Policy.returnPolicyData = function(policyData) { | 212 Policy.returnData = function(data) { |
| 122 Policy.getInstance().renderTemplate(policyData); | 213 if (data.isPolicyUpdate) { |
| 214 Policy.getInstance().collapseExpandedCells(); | |
| 215 Policy.getInstance().renderTemplate(data); | |
| 216 Policy.getInstance().updatePolicyVisibility(); | |
| 217 } else { | |
| 218 Policy.getInstance().renderTemplate(data); | |
| 219 } | |
| 220 }; | |
| 221 | |
| 222 /** | |
| 223 * Asks the C++ PolicyUIHandler to re-fetch policy information. | |
| 224 */ | |
| 225 Policy.triggerPolicyFetch = function() { | |
| 226 chrome.send('fetchPolicy'); | |
| 123 }; | 227 }; |
| 124 | 228 |
| 125 /** | 229 /** |
| 126 * Determines whether a policy should be visible or not. | 230 * Determines whether a policy should be visible or not. |
| 127 * @param {policy} policy information in the format given by above the | 231 * @param {Object} policy An entry in the 'policies' array given by the above |
| 128 * PolicyDataFormat | 232 * PolicyDataFormat |
| 129 */ | 233 */ |
| 130 Policy.shouldDisplayPolicy = function(policy) { | 234 Policy.shouldDisplayPolicy = function(policy) { |
| 131 return $('toggle-unsent-policies').checked || policy.set; | 235 return $('toggle-unsent-policies').checked || policy.set; |
| 132 }; | 236 }; |
| 133 | 237 |
| 134 /** | 238 /** |
| 135 * Initializes the page and loads the list of policies. | 239 * Returns true if the "Show more" toggle should appear in a table cell and |
| 240 * false if not. | |
| 241 * @param {Object} expandToggle The "Show more" DOM element | |
| 242 */ | |
| 243 Policy.shouldShowExpand = function(expandToggle) { | |
| 244 var textContainer = | |
| 245 expandToggle.parentNode.querySelector('.text-container'); | |
| 246 textContainer.collapsed = true; | |
| 247 var cellText = textContainer.querySelector('.cellText'); | |
| 248 | |
| 249 // If the text is wider than the text container, the expand sign should | |
| 250 // appear. | |
| 251 return (textContainer.offsetWidth < cellText.offsetWidth); | |
|
James Hawkins
2011/09/05 22:10:38
Remove parens.
simo
2011/09/07 12:18:16
Done.
| |
| 252 }; | |
| 253 | |
| 254 /** | |
| 255 * Initializes the page and loads the list of policies and the policy | |
| 256 * status data. | |
| 136 */ | 257 */ |
| 137 Policy.initialize = function() { | 258 Policy.initialize = function() { |
| 138 i18nTemplate.process(document, templateData); | 259 i18nTemplate.process(document, templateData); |
| 139 Policy.requestPolicyData(); | 260 Policy.requestData(); |
| 140 | 261 |
| 141 // Set HTML event handlers. | 262 // Set HTML event handlers. |
| 263 $('fetch-policies-button').onclick = function(event) { | |
| 264 Policy.triggerPolicyFetch(); | |
| 265 } | |
| 266 | |
| 142 $('toggle-unsent-policies').onchange = function(event) { | 267 $('toggle-unsent-policies').onchange = function(event) { |
| 143 Policy.getInstance().updatePolicyVisibility(); | 268 Policy.getInstance().updatePolicyVisibility(); |
| 144 }; | 269 }; |
| 145 | 270 |
| 146 $('search-field').onsearch = function(event) { | 271 $('search-field').onsearch = function(event) { |
| 147 Policy.getInstance().filterTable(this.value); | 272 Policy.getInstance().filterTable(this.value); |
| 148 }; | 273 }; |
| 149 }; | 274 }; |
| 150 | 275 |
| 151 // Export | 276 // Export |
| 152 return { | 277 return { |
| 153 Policy: Policy | 278 Policy: Policy |
| 154 }; | 279 }; |
| 155 }); | 280 }); |
| 156 | 281 |
| 157 var Policy = policies.Policy; | 282 var Policy = policies.Policy; |
| 158 | 283 |
| 159 // Get data and have it displayed upon loading. | 284 // Get data and have it displayed upon loading. |
| 160 document.addEventListener('DOMContentLoaded', policies.Policy.initialize); | 285 document.addEventListener('DOMContentLoaded', policies.Policy.initialize); |
| OLD | NEW |