1// Copyright 2013 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.
4
5'use strict';
6
7/**
8 * Takes the |componentsData| input argument which represents data about the
9 * currently installed components and populates the html jstemplate with
10 * that data. It expects an object structure like the above.
11 * @param {Object} componentsData Detailed info about installed components.
12 *      Same expected format as returnComponentsData().
13 */
14function renderTemplate(componentsData) {
15  // This is the javascript code that processes the template:
16  var input = new JsEvalContext(componentsData);
17  var output = $('component-template').cloneNode(true);
18  $('component-placeholder').innerHTML = '';
19  $('component-placeholder').appendChild(output);
20  jstProcess(input, output);
21  output.removeAttribute('hidden');
22}
23
24/**
25 * Asks the C++ ComponentsDOMHandler to get details about the installed
26 * components.
27 * The ComponentsDOMHandler should reply to returnComponentsData() (below).
28 */
29function requestComponentsData() {
30  chrome.send('requestComponentsData');
31}
32
33/**
34 * Called by the WebUI to re-populate the page with data representing the
35 * current state of installed components.
36 * @param {Object} componentsData Detailed info about installed components. The
37 *     template expects each component's format to match the following
38 *     structure to correctly populate the page:
39 *   {
40 *     components: [
41 *       {
42 *          name: 'Component1',
43 *          version: '1.2.3',
44 *       },
45 *       {
46 *          name: 'Component2',
47 *          version: '4.5.6',
48 *       },
49 *     ]
50 *   }
51 */
52function returnComponentsData(componentsData) {
53  var bodyContainer = $('body-container');
54  var body = document.body;
55
56  bodyContainer.style.visibility = 'hidden';
57  body.className = '';
58
59  renderTemplate(componentsData);
60
61  // Add handlers to dynamically created HTML elements.
62  var links = document.getElementsByClassName('button-check-update');
63  for (var i = 0; i < links.length; i++) {
64    links[i].onclick = function(e) {
65      handleCheckUpdate(this);
66      e.preventDefault();
67    };
68  }
69
70  if (cr.isChromeOS) {
71    // Disable some controls for Guest in ChromeOS.
72    uiAccountTweaks.UIAccountTweaks.applyGuestSessionVisibility(document);
73
74    // Disable some controls for Public session in ChromeOS.
75    uiAccountTweaks.UIAccountTweaks.applyPublicSessionVisibility(document);
76  }
77
78  bodyContainer.style.visibility = 'visible';
79  body.className = 'show-tmi-mode-initial';
80}
81
82/**
83 * This event function is called from component UI indicating changed state
84 * of component updater service.
85 * @param {Object} eventArgs Contains event and component ID. Component ID is
86 * optional.
87 */
88function onComponentEvent(eventArgs) {
89  if (eventArgs['id']) {
90    var id = eventArgs['id'];
91    $('status-' + id).textContent = eventArgs['event'];
92  }
93  if (eventArgs['version']) {
94    $('version-' + id).textContent = eventArgs['version'];
95  }
96}
97
98/**
99 * Handles an 'enable' or 'disable' button getting clicked.
100 * @param {HTMLElement} node The HTML element representing the component
101 *     being checked for update.
102 */
103function handleCheckUpdate(node) {
104  $('status-' + String(node.id)).textContent =
105      loadTimeData.getString('checkingLabel');
106
107  // Tell the C++ ComponentssDOMHandler to check for update.
108  chrome.send('checkUpdate', [String(node.id)]);
109}
110
111// Get data and have it displayed upon loading.
112document.addEventListener('DOMContentLoaded', requestComponentsData);
113