1 // Copyright 2015 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.
5 cr.define('chrome.supervised_user_internals', function() {
8 function initialize() {
9 function submitURL(event) {
10 $('try-url-result').textContent = '';
11 chrome.send('tryURL', [$('try-url-input').value]);
12 event.preventDefault();
15 $('try-url').addEventListener('submit', submitURL);
17 // Make the prototype jscontent element disappear.
18 jstProcess({}, $('filtering-results-container'));
20 chrome.send('registerForEvents');
22 chrome.send('getBasicInfo');
25 function highlightIfChanged(node, oldVal, newVal) {
26 function clearHighlight() {
27 this.removeAttribute('highlighted');
30 var oldStr = oldVal.toString();
31 var newStr = newVal.toString();
32 if (oldStr != '' && oldStr != newStr) {
33 // Note the addListener function does not end up creating duplicate
34 // listeners. There can be only one listener per event at a time.
35 // See https://developer.mozilla.org/en/DOM/element.addEventListener
36 node.addEventListener('webkitAnimationEnd', clearHighlight, false);
37 node.setAttribute('highlighted', '');
41 function receiveBasicInfo(info) {
42 jstProcess(new JsEvalContext(info), $('basic-info'));
44 // Hack: Schedule another refresh after a while.
45 // TODO(treib): Get rid of this once we're properly notified of all
47 setTimeout(function() { chrome.send('getBasicInfo'); }, 5000);
50 function receiveUserSettings(settings) {
51 if (settings === null) {
52 $('user-settings').classList.add('hidden');
56 $('user-settings').classList.remove('hidden');
58 // The user settings are returned as an object, flatten them into a
59 // list of key/value pairs for easier consumption by the HTML template.
60 // This is not done recursively, values are passed as their JSON
62 var kvpairs = Object.keys(settings).map(function(key) {
65 value: JSON.stringify(settings[key], null, 2)
69 jstProcess(new JsEvalContext({settings: kvpairs}), $('user-settings'));
72 function receiveTryURLResult(result) {
73 $('try-url-result').textContent = result;
77 * Helper to determine if an element is scrolled to its bottom limit.
78 * @param {Element} elem element to check
79 * @return {boolean} true if the element is scrolled to the bottom
81 function isScrolledToBottom(elem) {
82 return elem.scrollHeight - elem.scrollTop == elem.clientHeight;
86 * Helper to scroll an element to its bottom limit.
87 * @param {Element} elem element to be scrolled
89 function scrollToBottom(elem) {
90 elem.scrollTop = elem.scrollHeight - elem.clientHeight;
93 /** Container for accumulated filtering results. */
94 var filteringResults = [];
97 * Callback for incoming filtering results.
98 * @param {Object} result The result.
100 function receiveFilteringResult(result) {
101 filteringResults.push(result);
103 var container = $('filtering-results-container');
105 // Scroll to the bottom if we were already at the bottom. Otherwise, leave
106 // the scrollbar alone.
107 var shouldScrollDown = isScrolledToBottom(container);
109 jstProcess(new JsEvalContext({ results: filteringResults }), container);
111 if (shouldScrollDown)
112 scrollToBottom(container);
115 // Return an object with all of the exports.
117 initialize: initialize,
118 highlightIfChanged: highlightIfChanged,
119 receiveBasicInfo: receiveBasicInfo,
120 receiveUserSettings: receiveUserSettings,
121 receiveTryURLResult: receiveTryURLResult,
122 receiveFilteringResult: receiveFilteringResult,
126 document.addEventListener('DOMContentLoaded',
127 chrome.supervised_user_internals.initialize);