OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var localStrings = new LocalStrings(); |
| 6 |
| 7 /** |
| 8 * This variable structure is here to document the structure that the template |
| 9 * expects to correctly populate the page. |
| 10 */ |
| 11 var policyDataformat = { |
| 12 'policies': [ |
| 13 { |
| 14 'level': 'managed', |
| 15 'name': 'AllowXYZ', |
| 16 'set': true, |
| 17 'sourceType': 'Device', |
| 18 'status': 'ok', |
| 19 'value': true, |
| 20 }, |
| 21 ], |
| 22 'anyPoliciesSet': true |
| 23 }; |
| 24 |
| 25 cr.define('policies', function() { |
| 26 |
| 27 function Policy() { |
| 28 } |
| 29 |
| 30 cr.addSingletonGetter(Policy); |
| 31 |
| 32 Policy.prototype = { |
| 33 |
| 34 /** |
| 35 * True if none of the received policies are actually set, false otherwise. |
| 36 * @type {boolean} |
| 37 */ |
| 38 noActivePolicies_: false, |
| 39 |
| 40 /** |
| 41 * The current search term for filtering of the policy table. |
| 42 * @type {string} |
| 43 * @private |
| 44 */ |
| 45 searchTerm_: '', |
| 46 |
| 47 /** |
| 48 * Takes the |policyData| input argument which represents data about the |
| 49 * policies supported by the device/client and populates the html jstemplate |
| 50 * with that data. It expects an object structure like the above. |
| 51 * @param {Object} policyData Detailed info about policies |
| 52 */ |
| 53 renderTemplate: function(policyData) { |
| 54 this.noActivePolicies_ = !policyData.anyPoliciesSet; |
| 55 |
| 56 // This is the javascript code that processes the template: |
| 57 var input = new JsEvalContext(policyData); |
| 58 var output = $('policiesTemplate'); |
| 59 jstProcess(input, output); |
| 60 }, |
| 61 |
| 62 /** |
| 63 * Filters the table of policies by name. |
| 64 * @param {string} term The search string |
| 65 */ |
| 66 filterTable: function(term) { |
| 67 this.searchTerm_ = term.toLowerCase(); |
| 68 var table = $('policy-table'); |
| 69 var showUnsent = $('toggle-unsent-policies').checked; |
| 70 for (var r = 1; r < table.rows.length; r++) { |
| 71 var row = table.rows[r]; |
| 72 |
| 73 // Don't change visibility of policies that aren't set if the checkbox |
| 74 // isn't checked. |
| 75 if (!showUnsent && row.className == 'policy-unset') |
| 76 continue; |
| 77 |
| 78 var nameCell = row.querySelector('.policy-name'); |
| 79 var cellContents = nameCell.textContent; |
| 80 if (cellContents.toLowerCase().indexOf(this.searchTerm_) >= 0) |
| 81 row.style.display = 'table-row'; |
| 82 else |
| 83 row.style.display = 'none'; |
| 84 } |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Updates the visibility of the policies depending on the state of the |
| 89 * 'toggle-unsent-policies' checkbox. |
| 90 */ |
| 91 updatePolicyVisibility: function() { |
| 92 if ($('toggle-unsent-policies').checked) |
| 93 $('policies').style.display = ''; |
| 94 else if (this.noActivePolicies_) |
| 95 $('policies').style.display = 'none'; |
| 96 |
| 97 var tableRows = document.getElementsByClassName('policy-unset'); |
| 98 for (var i = 0; i < tableRows.length; i++) { |
| 99 if ($('toggle-unsent-policies').checked) |
| 100 tableRows[i].style.visibility = 'visible'; |
| 101 else |
| 102 tableRows[i].style.visibility = 'hidden'; |
| 103 } |
| 104 |
| 105 // Filter table again in case a search was active. |
| 106 this.filterTable(this.searchTerm_); |
| 107 } |
| 108 }; |
| 109 |
| 110 /** |
| 111 * Asks the C++ PolicyUIHandler to get details about policies. The |
| 112 * PolicyDOMHandler should reply to returnPolicyData() (below). |
| 113 */ |
| 114 Policy.requestPolicyData = function() { |
| 115 chrome.send('requestPolicyData'); |
| 116 }; |
| 117 |
| 118 /** |
| 119 * Called by the C++ PolicyUIHandler when it has the requested policy data. |
| 120 */ |
| 121 Policy.returnPolicyData = function(policyData) { |
| 122 Policy.getInstance().renderTemplate(policyData); |
| 123 }; |
| 124 |
| 125 /** |
| 126 * Determines whether a policy should be visible or not. |
| 127 * @param {policy} policy information in the format given by above the |
| 128 * PolicyDataFormat |
| 129 */ |
| 130 Policy.shouldDisplayPolicy = function(policy) { |
| 131 return $('toggle-unsent-policies').checked || policy.set; |
| 132 }; |
| 133 |
| 134 /** |
| 135 * Initializes the page and loads the list of policies. |
| 136 */ |
| 137 Policy.initialize = function() { |
| 138 i18nTemplate.process(document, templateData); |
| 139 Policy.requestPolicyData(); |
| 140 |
| 141 // Set HTML event handlers. |
| 142 $('toggle-unsent-policies').onchange = function(event) { |
| 143 Policy.getInstance().updatePolicyVisibility(); |
| 144 }; |
| 145 |
| 146 $('search-field').onsearch = function(event) { |
| 147 Policy.getInstance().filterTable(this.value); |
| 148 }; |
| 149 }; |
| 150 |
| 151 // Export |
| 152 return { |
| 153 Policy: Policy |
| 154 }; |
| 155 }); |
| 156 |
| 157 var Policy = policies.Policy; |
| 158 |
| 159 // Get data and have it displayed upon loading. |
| 160 document.addEventListener('DOMContentLoaded', policies.Policy.initialize); |
OLD | NEW |