Home | History | Annotate | Download | only in resources
      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  */
     14 function renderTemplate(componentsData) {
     15   // This is the javascript code that processes the template:
     16   var input = new JsEvalContext(componentsData);
     17   var output = $('componentTemplate');
     18   jstProcess(input, output);
     19 }
     20 
     21 /**
     22  * Asks the C++ ComponentsDOMHandler to get details about the installed
     23  * components.
     24  * The ComponentsDOMHandler should reply to returnComponentsData() (below).
     25  */
     26 function requestComponentsData() {
     27   chrome.send('requestComponentsData');
     28 }
     29 
     30 /**
     31  * Called by the WebUI to re-populate the page with data representing the
     32  * current state of installed components.
     33  * @param {Object} componentsData Detailed info about installed components. The
     34  *     template expects each component's format to match the following
     35  *     structure to correctly populate the page:
     36  *   {
     37  *     components: [
     38  *       {
     39  *          name: 'Component1',
     40  *          version: '1.2.3',
     41  *       },
     42  *       {
     43  *          name: 'Component2',
     44  *          version: '4.5.6',
     45  *       },
     46  *     ]
     47  *   }
     48  */
     49 function returnComponentsData(componentsData) {
     50   var bodyContainer = $('body-container');
     51   var body = document.body;
     52 
     53   bodyContainer.style.visibility = 'hidden';
     54   body.className = '';
     55 
     56   renderTemplate(componentsData);
     57 
     58   // Add handlers to dynamically created HTML elements.
     59   var links = document.getElementsByClassName('button-check-update');
     60   for (var i = 0; i < links.length; i++) {
     61     links[i].onclick = function(e) {
     62       handleCheckUpdate(this);
     63       e.preventDefault();
     64     };
     65   }
     66 
     67   // Disable some controls for Guest in ChromeOS.
     68   if (cr.isChromeOS)
     69     uiAccountTweaks.UIAccountTweaks.applyGuestModeVisibility(document);
     70 
     71   bodyContainer.style.visibility = 'visible';
     72   body.className = 'show-tmi-mode-initial';
     73 }
     74 
     75 /**
     76  * Handles an 'enable' or 'disable' button getting clicked.
     77  * @param {HTMLElement} node The HTML element representing the component
     78  *     being checked for update.
     79  */
     80 function handleCheckUpdate(node) {
     81   node.disabled = true;
     82   // Tell the C++ ComponentssDOMHandler to check for update.
     83   chrome.send('checkUpdate', [String(node.id)]);
     84 }
     85 
     86 // Get data and have it displayed upon loading.
     87 document.addEventListener('DOMContentLoaded', requestComponentsData);
     88 
     89 // Add handlers to static HTML elements.
     90 $('button-check-update').onclick = handleCheckUpdate;
     91