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 'source_type': 'Device', | |
18 'status': 'ok', | |
19 'value': 'true', | |
20 }, | |
21 ], | |
22 'any_policies_set': true | |
arv (Not doing code reviews)
2011/08/11 23:30:58
do you have control of the json schema? Can you ch
simo
2011/08/16 17:36:34
Done.
| |
23 } | |
arv (Not doing code reviews)
2011/08/11 23:30:58
missing semicolon
simo
2011/08/16 17:36:34
Done.
| |
24 | |
25 cr.define('cr.ui', 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. | |
arv (Not doing code reviews)
2011/08/11 23:30:58
/**
* ...
*/
simo
2011/08/16 17:36:34
Done.
| |
36 * @type {boolean} | |
37 */ | |
38 no_active_policies_: false, | |
arv (Not doing code reviews)
2011/08/11 23:30:58
no underscores in js names
simo
2011/08/16 17:36:34
Done.
| |
39 | |
40 /** | |
41 * The current search term for filtering of the policy table. | |
42 * @type {string} | |
43 */ | |
44 search_term_: '', | |
45 | |
46 /** | |
47 * Takes the |policyData| input argument which represents data about the | |
48 * policies supported by the device/client and populates the html jstemplate | |
49 * with that data. It expects an object structure like the above. | |
50 * @param {Object} policyData Detailed info about policies | |
51 */ | |
52 renderTemplate: function(policyData) { | |
53 this.no_active_policies_ = !policyData.any_policies_set; | |
54 // This is the javascript code that processes the template: | |
55 var input = new JsEvalContext(policyData); | |
56 var output = $('policiesTemplate'); | |
57 jstProcess(input, output); | |
58 }, | |
59 | |
60 /** | |
61 * Filters the table of policies by name. | |
62 * @param {term} The search string | |
arv (Not doing code reviews)
2011/08/11 23:30:58
@param {TypeName} type The search string.
simo
2011/08/16 17:36:34
Done.
| |
63 */ | |
64 filterTable: function(term) { | |
65 this.search_term_ = term.toLowerCase(); | |
66 var table = $('policy-table'); | |
67 var show_unsent = $('toggle-unsent-policies').checked; | |
arv (Not doing code reviews)
2011/08/11 23:30:58
no underscores
simo
2011/08/16 17:36:34
Done.
| |
68 for (var r = 1; r < table.rows.length; ++r) { | |
arv (Not doing code reviews)
2011/08/11 23:30:58
r++
simo
2011/08/16 17:36:34
Done.
| |
69 var row = table.rows[r]; | |
70 | |
71 // Don't change visibility of policies that aren't set if the checkbox | |
72 // isn't checked. | |
73 if (!show_unsent && row.className == 'policy-unset') | |
74 continue; | |
75 | |
76 var name_cell = row.getElementsByClassName('policy-name')[0]; | |
arv (Not doing code reviews)
2011/08/11 23:30:58
var nameCell = row.querySelector('.policy-name');
simo
2011/08/16 17:36:34
Done.
| |
77 var cell_contents = name_cell.innerHTML.replace(/<[^>]+>/g,""); | |
arv (Not doing code reviews)
2011/08/11 23:30:58
use single quoted strings... but this use of inner
simo
2011/08/16 17:36:34
Done.
| |
78 if (cell_contents.toLowerCase().indexOf(this.search_term_) >= 0) | |
79 row.style.display = 'table-row'; | |
80 else | |
81 row.style.display = 'none'; | |
82 } | |
83 }, | |
84 | |
85 /** | |
86 * Updates the visibility of the policies depending on the state of the | |
87 * 'toggle-unsent-policies' checkbox. | |
88 */ | |
89 updatePolicyVisibility: function() { | |
90 if ($('toggle-unsent-policies').checked) | |
91 $('policies').style.display = ''; | |
arv (Not doing code reviews)
2011/08/11 23:30:58
How about using the 'hidden' dom property instead?
simo
2011/08/16 17:36:34
Done.
| |
92 else if (this.no_active_policies_) | |
93 $('policies').style.display = 'none'; | |
94 | |
95 var table_rows = document.getElementsByClassName('policy-unset'); | |
96 for (var i = 0; i < table_rows.length; ++i) { | |
arv (Not doing code reviews)
2011/08/11 23:30:58
Prefer post increment in JS for consistency
simo
2011/08/16 17:36:34
Done.
| |
97 if ($('toggle-unsent-policies').checked) | |
98 table_rows[i].style.display = 'table-row'; | |
arv (Not doing code reviews)
2011/08/11 23:30:58
here too
simo
2011/08/16 17:36:34
Done.
| |
99 else | |
100 table_rows[i].style.display = 'none'; | |
101 } | |
102 | |
103 // Filter table again in case a search was active. | |
104 this.filterTable(this.search_term_); | |
105 } | |
106 }; | |
107 | |
108 /** | |
109 * Asks the C++ PolicyUIHandler to get details about policies. The | |
110 * PolicyDOMHandler should reply to returnPolicyData() (below). | |
111 */ | |
112 Policy.requestPolicyData = function() { | |
113 chrome.send('requestPolicyData'); | |
114 }; | |
115 | |
116 /** | |
117 * Called by the C++ PolicyUIHandler when it has the requested policy data. | |
118 */ | |
119 Policy.returnPolicyData = function(policyData) { | |
120 Policy.getInstance().renderTemplate(policyData); | |
121 }; | |
122 | |
123 /** | |
124 * Determines whether a policy should be visible or not. | |
125 * @param {policy} policy information in the format given by above the | |
126 * PolicyDataFormat | |
127 */ | |
128 Policy.shouldDisplayPolicy = function(policy) { | |
129 return $('toggle-unsent-policies').checked || policy.set; | |
130 }; | |
131 | |
132 /** | |
133 * Returns true if the given policy was actually set and false otherwise (i.e. | |
134 * it is just one of the policies supported by the client but isn't set to a | |
135 * value). | |
136 * @policy {policy} information in the format given by above PolicyDataFormat | |
137 */ | |
138 Policy.isPolicySet = function(policy) { | |
arv (Not doing code reviews)
2011/08/11 23:30:58
This seems pretty useless. Can you inline this?
simo
2011/08/16 17:36:34
Done.
| |
139 return policy.set; | |
140 }; | |
141 | |
142 /** | |
143 * Initializes the page and loads the list of policies. | |
144 */ | |
145 Policy.initialize = function() { | |
146 i18nTemplate.process(document, templateData); | |
147 Policy.requestPolicyData(); | |
148 | |
149 // Set HTML event handlers. | |
150 $('toggle-unsent-policies').onchange = function (event) { | |
arv (Not doing code reviews)
2011/08/11 23:30:58
... = function(event) {
simo
2011/08/16 17:36:34
Done.
| |
151 Policy.getInstance().updatePolicyVisibility(); | |
152 }; | |
153 | |
154 $('search-field').onsearch = function(event) { | |
155 Policy.getInstance().filterTable(this.value); | |
156 }; | |
157 | |
158 $('search-field').placeholder = | |
159 localStrings.getString('filterPoliciesText'); | |
160 }; | |
161 | |
162 // Export | |
163 return { | |
164 Policy: Policy | |
165 }; | |
166 }); | |
167 | |
168 var Policy = cr.ui.Policy; | |
169 | |
170 // Get data and have it displayed upon loading. | |
171 document.addEventListener('DOMContentLoaded', cr.ui.Policy.initialize); | |
OLD | NEW |