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