summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/supervised_user_internals.js
blob: c3a5666af6dc4d0b5f22c3624e1c34ab02972042 (plain)
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
// Copyright 2015 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.

cr.define('chrome.supervised_user_internals', function() {
  'use strict';

  function initialize() {
    function submitURL(event) {
      $('try-url-result').textContent = '';
      chrome.send('tryURL', [$('try-url-input').value]);
      event.preventDefault();
    }

    $('try-url').addEventListener('submit', submitURL);

    // Make the prototype jscontent element disappear.
    jstProcess({}, $('filtering-results-container'));

    chrome.send('registerForEvents');

    chrome.send('getBasicInfo');
  }

  function highlightIfChanged(node, oldVal, newVal) {
    function clearHighlight() {
      this.removeAttribute('highlighted');
    }

    var oldStr = oldVal.toString();
    var newStr = newVal.toString();
    if (oldStr != '' && oldStr != newStr) {
      // Note the addListener function does not end up creating duplicate
      // listeners.  There can be only one listener per event at a time.
      // See https://developer.mozilla.org/en/DOM/element.addEventListener
      node.addEventListener('webkitAnimationEnd', clearHighlight, false);
      node.setAttribute('highlighted', '');
    }
  }

  function receiveBasicInfo(info) {
    jstProcess(new JsEvalContext(info), $('basic-info'));

    // Hack: Schedule another refresh after a while.
    // TODO(treib): Get rid of this once we're properly notified of all
    // relevant changes.
    setTimeout(function() { chrome.send('getBasicInfo'); }, 5000);
  }

  function receiveUserSettings(settings) {
    if (settings === null) {
      $('user-settings').classList.add('hidden');
      return;
    }

    $('user-settings').classList.remove('hidden');

    // The user settings are returned as an object, flatten them into a
    // list of key/value pairs for easier consumption by the HTML template.
    // This is not done recursively, values are passed as their JSON
    // representation.
    var kvpairs = Object.keys(settings).map(function(key) {
      return {
        key: key,
        value: JSON.stringify(settings[key], null, 2)
      };
    });

    jstProcess(new JsEvalContext({settings: kvpairs}), $('user-settings'));
  }

  function receiveTryURLResult(result) {
    $('try-url-result').textContent = result;
  }

  /**
   * Helper to determine if an element is scrolled to its bottom limit.
   * @param {Element} elem element to check
   * @return {boolean} true if the element is scrolled to the bottom
   */
  function isScrolledToBottom(elem) {
    return elem.scrollHeight - elem.scrollTop == elem.clientHeight;
  }

  /**
   * Helper to scroll an element to its bottom limit.
   * @param {Element} elem element to be scrolled
   */
  function scrollToBottom(elem) {
    elem.scrollTop = elem.scrollHeight - elem.clientHeight;
  }

  /** Container for accumulated filtering results. */
  var filteringResults = [];

  /**
   * Callback for incoming filtering results.
   * @param {Object} result The result.
   */
  function receiveFilteringResult(result) {
    filteringResults.push(result);

    var container = $('filtering-results-container');

    // Scroll to the bottom if we were already at the bottom.  Otherwise, leave
    // the scrollbar alone.
    var shouldScrollDown = isScrolledToBottom(container);

    jstProcess(new JsEvalContext({ results: filteringResults }), container);

    if (shouldScrollDown)
      scrollToBottom(container);
  }

  // Return an object with all of the exports.
  return {
    initialize: initialize,
    highlightIfChanged: highlightIfChanged,
    receiveBasicInfo: receiveBasicInfo,
    receiveUserSettings: receiveUserSettings,
    receiveTryURLResult: receiveTryURLResult,
    receiveFilteringResult: receiveFilteringResult,
  };
});

document.addEventListener('DOMContentLoaded',
                          chrome.supervised_user_internals.initialize);