1 // Copyright (c) 2012 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.
6 * This variable structure is here to document the structure that the template
7 * expects to correctly populate the page.
9 var policyDataFormat
= {
10 // Whether any of the policies in 'policies' have a value.
11 'anyPoliciesSet': true,
24 'deviceFetchInterval': '8min',
25 'deviceId': 'D2AC39A2-3C8FC-E2C0-E45D2DC3782C',
26 'deviceLastFetchTime': '9:50 PM',
27 'devicePolicyDomain': 'google.com',
28 'deviceStatusMessage': 'OK',
29 'displayDeviceStatus': true,
30 'displayStatusSection': true,
31 'displayUserStatus': true,
32 'user': 'simo@google.com',
33 'userFetchInterval': '8min',
34 'userId': 'D2AC39A2-3C8FC-E2C0-E45D2DC3782C',
35 'userLastFetchTime': '9:50 PM',
36 'userStatusMessage': 'OK'
40 cr
.define('policies', function() {
45 cr
.addSingletonGetter(Policy
);
49 * True if none of the received policies are actually set, false otherwise.
52 noActivePolicies_
: false,
55 * The current search term for filtering of the policy table.
62 * Takes the |policyData| argument and populates the page with this data. It
63 * expects an object structure like the policyDataFormat above.
64 * @param {Object} policyData Detailed info about policies.
66 renderTemplate: function(policyData
) {
67 this.noActivePolicies_
= !policyData
.anyPoliciesSet
;
69 if (this.noActivePolicies_
)
70 $('no-policies').hidden
= false;
71 if (policyData
.status
.displayStatusSection
)
72 $('status-section').hidden
= false;
74 // This is the javascript code that processes the template:
75 var input
= new JsEvalContext(policyData
);
76 var output
= $('data-template');
77 jstProcess(input
, output
);
79 var toggles
= document
.querySelectorAll('.policy-set * .toggler');
80 for (var i
= 0; i
< toggles
.length
; i
++) {
81 toggles
[i
].hidden
= true;
82 toggles
[i
].onclick = function() {
83 Policy
.getInstance().toggleCellExpand_(this);
87 var containers
= document
.querySelectorAll('.text-container');
88 for (var i
= 0; i
< containers
.length
; i
++)
89 this.initTextContainer_(containers
[i
]);
93 * Filters the table of policies by name.
94 * @param {string} term The search string.
96 filterTable: function(term
) {
97 this.searchTerm_
= term
.toLowerCase();
98 var table
= $('policy-table');
99 var showUnsent
= $('toggle-unsent-policies').checked
;
100 for (var r
= 1; r
< table
.rows
.length
; r
++) {
101 var row
= table
.rows
[r
];
103 // Don't change visibility of policies that aren't set if the checkbox
105 if (!showUnsent
&& row
.className
== 'policy-unset')
108 var nameCell
= row
.querySelector('.policy-name');
109 var cellContents
= nameCell
.textContent
;
111 !(cellContents
.toLowerCase().indexOf(this.searchTerm_
) >= 0);
116 * Updates the visibility of the policies depending on the state of the
117 * 'toggle-unsent-policies' checkbox.
119 updatePolicyVisibility: function() {
120 if ($('toggle-unsent-policies').checked
)
121 $('policies').style
.display
= '';
122 else if (this.noActivePolicies_
)
123 $('policies').style
.display
= 'none';
125 var tableRows
= document
.getElementsByClassName('policy-unset');
126 for (var i
= 0; i
< tableRows
.length
; i
++)
127 tableRows
[i
].hidden
= !($('toggle-unsent-policies').checked
);
129 // Filter table again in case a search was active.
130 this.filterTable(this.searchTerm_
);
134 * Expands or collapses a table cell that has overflowing text.
135 * @param {Object} toggler The toggler that was clicked on.
138 toggleCellExpand_: function(toggler
) {
139 var textContainer
= toggler
.parentElement
;
140 textContainer
.collapsed
= !textContainer
.collapsed
;
142 if (textContainer
.collapsed
)
143 this.collapseCell_(textContainer
);
145 this.expandCell_(textContainer
);
149 * Collapses all expanded table cells and updates the visibility of the
150 * toggles accordingly. Should be called before the policy information in
151 * the table is updated.
153 collapseExpandedCells: function() {
154 var textContainers
= document
.querySelectorAll('.text-expanded');
155 for (var i
= 0; i
< textContainers
.length
; i
++)
156 this.collapseCell_(textContainers
[i
]);
160 * Expands a table cell so that all the text it contains is visible.
161 * @param {Object} textContainer The cell's div element that contains the
165 expandCell_: function(textContainer
) {
166 textContainer
.classList
.remove('text-collapsed');
167 textContainer
.classList
.add('text-expanded');
168 textContainer
.querySelector('.expand').hidden
= true;
169 textContainer
.querySelector('.collapse').hidden
= false;
173 * Collapses a table cell so that overflowing text is hidden.
174 * @param {Object} textContainer The cell's div element that contains the
178 collapseCell_: function(textContainer
) {
179 textContainer
.classList
.remove('text-expanded');
180 textContainer
.classList
.add('text-collapsed');
181 textContainer
.querySelector('.expand').hidden
= false;
182 textContainer
.querySelector('.collapse').hidden
= true;
186 * Initializes a text container, showing the expand toggle if necessary.
187 * @param {Object} textContainer The text container element.
189 initTextContainer_: function(textContainer
) {
190 textContainer
.collapsed
= true;
191 var textValue
= textContainer
.querySelector('.text-value');
193 // If the text is wider than the text container, the expand toggler should
195 if (textContainer
.offsetWidth
< textValue
.offsetWidth
||
196 textContainer
.offsetHeight
< textValue
.offsetHeight
) {
197 this.collapseCell_(textContainer
);
203 * Asks the C++ PolicyUIHandler to get details about policies and status
204 * information. The PolicyUIHandler should reply to returnData() (below).
206 Policy
.requestData = function() {
207 chrome
.send('requestData');
211 * Called by the C++ PolicyUIHandler when it has the requested data.
212 * @param {Object} policyData The policy information in the format described
213 * by the policyDataFormat.
215 Policy
.returnData = function(policyData
) {
216 var policy
= Policy
.getInstance();
217 policy
.collapseExpandedCells();
218 policy
.renderTemplate(policyData
);
219 policy
.updatePolicyVisibility();
223 * Called by the C++ PolicyUIHandler when a requested policy refresh has
226 Policy
.refreshDone = function() {
227 $('fetch-policies-button').disabled
= false;
231 * Asks the C++ PolicyUIHandler to re-fetch policy information.
233 Policy
.triggerPolicyFetch = function() {
234 chrome
.send('fetchPolicy');
238 * Determines whether a policy should be visible or not.
239 * @param {Object} policy An entry in the 'policies' array given by the above
242 Policy
.shouldDisplayPolicy = function(policy
) {
243 return $('toggle-unsent-policies').checked
|| policy
.set;
247 * Initializes the page and loads the list of policies and the policy
250 Policy
.initialize = function() {
251 Policy
.requestData();
253 // Set HTML event handlers.
254 $('fetch-policies-button').onclick = function(event
) {
255 this.disabled
= true;
256 Policy
.triggerPolicyFetch();
259 $('toggle-unsent-policies').onchange = function(event
) {
260 Policy
.getInstance().updatePolicyVisibility();
263 $('search-field').onsearch = function(event
) {
264 Policy
.getInstance().filterTable(this.value
);
274 var Policy
= policies
.Policy
;
276 // Get data and have it displayed upon loading.
277 document
.addEventListener('DOMContentLoaded', policies
.Policy
.initialize
);