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