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 // The user settings are returned as an object, flatten them into a
52 // list of key/value pairs for easier consumption by the HTML template.
53 // This is not done recursively, values are passed as their JSON
55 var kvpairs = Object.keys(settings).map(function(key) {
58 value: JSON.stringify(settings[key], null, 2)
62 jstProcess(new JsEvalContext({settings: kvpairs}), $('user-settings'));
65 function receiveTryURLResult(result) {
66 $('try-url-result').textContent = result;
70 * Helper to determine if an element is scrolled to its bottom limit.
71 * @param {Element} elem element to check
72 * @return {boolean} true if the element is scrolled to the bottom
74 function isScrolledToBottom(elem) {
75 return elem.scrollHeight - elem.scrollTop == elem.clientHeight;
79 * Helper to scroll an element to its bottom limit.
80 * @param {Element} elem element to be scrolled
82 function scrollToBottom(elem) {
83 elem.scrollTop = elem.scrollHeight - elem.clientHeight;
86 /** Container for accumulated filtering results. */
87 var filteringResults = [];
90 * Callback for incoming filtering results.
91 * @param {Object} result The result.
93 function receiveFilteringResult(result) {
94 filteringResults.push(result);
96 var container = $('filtering-results-container');
98 // Scroll to the bottom if we were already at the bottom. Otherwise, leave
99 // the scrollbar alone.
100 var shouldScrollDown = isScrolledToBottom(container);
102 jstProcess(new JsEvalContext({ results: filteringResults }), container);
104 if (shouldScrollDown)
105 scrollToBottom(container);
108 // Return an object with all of the exports.
110 initialize: initialize,
111 highlightIfChanged: highlightIfChanged,
112 receiveBasicInfo: receiveBasicInfo,
113 receiveUserSettings: receiveUserSettings,
114 receiveTryURLResult: receiveTryURLResult,
115 receiveFilteringResult: receiveFilteringResult,
119 document.addEventListener('DOMContentLoaded',
120 chrome.supervised_user_internals.initialize);