github.com/jenkins-x/test-infra@v0.0.7/testgrid/extension/testgrid_alerter/js/settings.js (about)

     1  /**
     2   * @fileoverview Defines behavior of the settings page.
     3   */
     4  
     5  (function() {
     6  
     7  // Mapping from dashboard name to list of checkboxes (one for each tab).
     8  let tabCheckLists = {};
     9  // List of checkboxes (one for each dashboard).
    10  let dashboardCheckboxes = [];
    11  // The object to use to get information from updater.
    12  let updater = chrome.extension.getBackgroundPage().Updater;
    13  
    14  document.addEventListener('DOMContentLoaded', function() {
    15    /* extension navigation buttons */
    16    // Element representing the button to get back to the alerts page without
    17    // saving.
    18    let back = document.getElementById('back');
    19    back.addEventListener('click', backOnClick);
    20    // Element representing the button to save settings and return to the alerts
    21    // page.
    22    let save = document.getElementById('save');
    23    save.addEventListener('click', saveOnClick);
    24  
    25    // List of information requested from updater.js:
    26    // [dashboards, selectedDashboards, hasPermission]
    27    let info = updater.openSettings();
    28  
    29    /* add dashboards */
    30    // Element holding the Select Dashboards section header
    31    let selectDashboards = document.getElementById('select-dashboards');
    32    // Element representing the div that will contain the list of dashboards and
    33    // tabs.
    34    let dashboardsDiv = document.getElementById('dashboards-div');
    35    selectDashboards.addEventListener('click', function() {
    36      dashboardsDiv.classList.toggle('collapsed');
    37    });
    38  
    39    // Mapping from dashboard name to list of tab names for all dashboards.
    40    let dashboards = info[0];
    41    // Mapping from dashboard name to list of tab names for selected dashboards
    42    // and tabs.
    43    let selectedDashboards = info[1];
    44    // A boolean indicating whether the user is able to request data from
    45    // TestGrid. If not, the extension displays an error message instead of data.
    46    let hasPermission = info[2];
    47    if (hasPermission) {
    48      // Element representing the Expand All button in the Select Dashboards
    49      // section. Used to expand all dashboards to show all tabs.
    50      let expandButton = createButton('Expand All', dashboardsDiv);
    51      // Element representing the Collapse All button in the Select Dashboards
    52      // section. Used to collapse all dashboards to hide all tabs.
    53      let collapseButton = createButton('Collapse All', dashboardsDiv);
    54      // Array containing a list of divs for each dashboard and a list of
    55      // dictionaries for each dashboard containing the checkboxes for their tabs.
    56      let lists = addDashboards(dashboards, selectedDashboards, dashboardsDiv);
    57      expandButton.addEventListener('click', expandAll.bind(null, lists[0]));
    58      collapseButton.addEventListener('click', collapseAll.bind(null, lists[0]));
    59      dashboardCheckboxes = lists[1];
    60    } else {
    61      // Element to display error message if the user cannot access TestGrid.
    62      let error = document.createElement('p');
    63      error.textContent = 'You do not have permission to access TestGrid';
    64      dashboardsDiv.appendChild(error);
    65      console.log(dashboards.length);
    66    }
    67  
    68  });
    69  
    70  /**
    71   * Creates a button with the given text and adds it to the given div.
    72   * @param {string} text displayed on the button
    73   * @param {Element} div to add the button to
    74   * @return {Element} the button
    75   */
    76  function createButton(text, div) {
    77    let button = document.createElement('button');
    78    button.type = 'button';
    79    button.textContent = text;
    80    div.appendChild(button);
    81    return button;
    82  }
    83  
    84  /**
    85   * Switches the current page to popup.html.
    86   */
    87  function backOnClick() {
    88    document.location = 'popup.html';
    89  }
    90  
    91  /**
    92   * Send the currently selected settings information to updater.py and switches
    93   * the current page to popup.html.
    94   */
    95  function saveOnClick() {
    96    // Mapping from dashboard name to list of tab names for selected dashboards
    97    // and tabs.
    98    let selectedDashboards = {};
    99    dashboardCheckboxes.forEach(function(tabCheckboxes) {
   100      // List of the names of selected tabs in this dashboard.
   101      let tabs = [];
   102      tabCheckboxes.tabs.forEach(function(checkbox) {
   103        if (checkbox.checked) {
   104          tabs.push(checkbox.id);
   105        }
   106      });
   107      if (tabs.length > 0) {
   108        selectedDashboards[tabCheckboxes.dashboard] = tabs;
   109      }
   110    });
   111    updater.saveSettings(selectedDashboards);
   112    document.location = 'popup.html';
   113  }
   114  
   115  /**
   116   * Adds dashboards and their tabs to the Select Dashboards section. Selected
   117   * tabs and dashboards with selected tabs in them start checked.
   118   * @param {Array} dashboards List of Dashboard objects.
   119   * @param {Object} selectedDashboards dictionary from dashboard name to list of
   120   *   tab names for selected dashboards and tabs.
   121   * @param {Element} dashboardsDiv The div to add the dashboards and tabs to.
   122   * @return {Array} list of divs for each dashboard, list of dictionaries for
   123   *   each dashboard containing the checkboxes for their tabs
   124   */
   125  function addDashboards(dashboards, selectedDashboards, dashboardsDiv) {
   126    // List of divs containing tabs, one for each dashboard
   127    let divList = [];
   128    let checkboxes = [];
   129    for (dashboardName in dashboards) {
   130      // Array containing the checkbox and text elements for the dashboard
   131      let dashboard = addLine(dashboardName, dashboardsDiv);
   132      dashboard[0].addEventListener(
   133          'click', toggleDashboard.bind(null, dashboardName));
   134      // Object containing the dashboard name and a list of tab checkboxes for
   135      // this dashboard.
   136      let boxes = {'dashboard': dashboardName, 'tabs': []};
   137      // Element representing the div that tabs in this dashboard are listed in.
   138      let tabs = document.createElement('div');
   139      tabs.id = dashboardName + '-div';
   140      tabs.classList.add('dashboard-div');
   141      tabs.classList.add('collapsed');
   142      dashboardsDiv.appendChild(tabs);
   143      divList.push(tabs);
   144      dashboard[1].addEventListener('click', function() {
   145        tabs.classList.toggle('collapsed');
   146      });
   147      dashboards[dashboardName].forEach(function(name) {
   148        // Array containing the checkbox and text elements for the tab
   149        let tab = addLine(name, tabs);
   150        boxes.tabs.push(tab[0]);
   151        if (dashboardName in selectedDashboards &&
   152            selectedDashboards[dashboardName].includes(name)) {
   153          tab[0].checked = true;
   154        }
   155      });
   156      if (dashboardName in selectedDashboards) {
   157        dashboard[0].checked = true;
   158      }
   159      checkboxes.push(boxes);
   160      tabCheckLists[dashboardName] = boxes.tabs;
   161    }
   162    return [divList, checkboxes];
   163  }
   164  
   165  /**
   166   * Adds a line with a checkbox and a label for the given name.
   167   *
   168   * The id of the checkbox is set to name.
   169   *
   170   * @param {string} name The name of the tab or dashboard being indicated.
   171   * @param {Element} div The div to add the line to.
   172   * @return {Array} An array containing the checkbox and the paragraph objects.
   173   */
   174  function addLine(name, div) {
   175    // Div to hold the checkbox and text element.
   176    let line = document.createElement('div');
   177    div.appendChild(line);
   178    let box = document.createElement('input');
   179    box.id = name;
   180    box.type = 'checkbox';
   181    box.classList.add('line');
   182    line.appendChild(box);
   183    let text = document.createElement('p');
   184    text.textContent = name;
   185    text.classList.add('line');
   186    line.appendChild(text);
   187    return [box, text];
   188  }
   189  
   190  /**
   191   * Expands every div contained in divList.
   192   *
   193   * @param {Array} divList A list of div elements.
   194   */
   195  function expandAll(divList) {
   196    divList.forEach(function(div) {
   197      div.classList.remove('collapsed');
   198    });
   199  }
   200  
   201  /**
   202   * Collapses every div contained in divList.
   203   *
   204   * @param {Array} divList A list of div elements.
   205   */
   206  function collapseAll(divList) {
   207    divList.forEach(function(div) {
   208      div.classList.add('collapsed');
   209    });
   210  }
   211  
   212  /**
   213   * Sets every tab checkbox for the dashboard with the given dashboard name to
   214   * the same value as the dashboard's checkbox.
   215   *
   216   * @param {string} dashboardName The name of the dashboard whose tab's
   217   *   checkboxes should be toggled
   218   */
   219  function toggleDashboard(dashboardName) {
   220    let set = document.getElementById(dashboardName).checked;
   221    tabCheckLists[dashboardName].forEach(function(tab) {
   222      tab.checked = set;
   223    });
   224  }
   225  
   226  })();