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

     1  /**
     2   * @fileoverview Defines behavior of the opening popup page.
     3   */
     4  (function() {
     5  
     6  // List of divs that hold information about tabs. There will be one for each
     7  // dashboard.
     8  let dashboardDivList = [];
     9  // List of divs that hold information about tests. There will be one for each
    10  // tab
    11  let tabDivList = [];
    12  // The object to use to get information from updater.
    13  let updater = chrome.extension.getBackgroundPage().Updater;
    14  
    15  document.addEventListener('DOMContentLoaded', function() {
    16    // Element to contain any warning text, such as if there are no selected
    17    // dashboards
    18    let warning = document.getElementById('warning');
    19  
    20    // Element representing the Expand All button. Used to expand all dashboards
    21    // and tabs to make their alerts visble.
    22    let expandAllButton = document.getElementById('expand');
    23    expandAllButton.addEventListener('click', expandAll);
    24    // Element representing the Collapse All button. Used to collapse all
    25    // dashboards and tabs to hide their alerts.
    26    let collapseAllButton = document.getElementById('collapse');
    27    collapseAllButton.addEventListener('click', collapseAll);
    28    // Element representing the Expand All Dashboards button. Used to expand all
    29    // dashboards but leaves tabs collapsed such that tabs are visible but tests
    30    // are not.
    31    let expandDashboardsButton = document.getElementById('expand-dashboards');
    32    expandDashboardsButton.addEventListener('click', expandDashboards);
    33    // Element representing the Collapse All Dashboards button. Used to collapse
    34    // all dashboards but leaves tabs in their current state such that when the
    35    // dashboard is re-expanded the tabs will maintain their state.
    36    let collapseDashboardsButton = document.getElementById('collapse-dashboards');
    37    collapseDashboardsButton.addEventListener('click', collapseDashboards);
    38  
    39    // List of Alerts gathered by updater.js. There will be one alert object for
    40    // each dashboard.
    41    let alerts = updater.getAlerts();
    42    if (alerts.length == 0) {
    43      warning.textContent =
    44          'You don\'t have any dashboards or tabs selected. Open Select Dashboards under Settings to choose dashboards to follow';
    45    }
    46    // Element representing the div to put alert information in.
    47    let alertsDiv = document.getElementById('alerts');
    48    for (let i = 0; i < alerts.length; i++) {
    49      addDashboard(alerts[i], alertsDiv);
    50    }
    51  
    52    // Element representing the button that takes the user to the settings page.
    53    let settings = document.getElementById('settings');
    54    settings.addEventListener('click', settingsOnClick);
    55  });
    56  
    57  /**
    58   * Switches the current page to settings.html.
    59   */
    60  function settingsOnClick() {
    61    document.location = 'settings.html';
    62  }
    63  
    64  /**
    65   * Adds information about a selected dashboard, its tabs, and their tests.
    66   * @param {Alert} alert: An Alert object for a dashboard.
    67   * @param {Element} alertsDiv The div to put the information in.
    68   */
    69  function addDashboard(alert, alertsDiv) {
    70    console.log(alert);
    71    // Element to hold the dashboard name.
    72    let dashboardHeader = addHeader(alert.dashboard, 'dashboard', alertsDiv);
    73    // Element representing the div to put all information (tabs and tests)
    74    // about this dashboard in.
    75    let dashboardDiv = addHolder(
    76        alertsDiv, alert.dashboard, 'dashboard-div', dashboardHeader,
    77        dashboardDivList);
    78  
    79    for (tabName in alert.tabs) {
    80      // The DashboardTabSummary for the given tab
    81      let tabSummary = alert.tabs[tabName];
    82      // The number of failing tests in the current tab.
    83      let numTests = tabSummary.tests.length;
    84  
    85      let tabText =
    86          tabName + ' has ' + numTests + ' failing tests: ' + tabSummary.alert;
    87      // Element to hold the tab name and tab-level alerts.
    88      let tabHeader = addHeader(tabText, 'tab', dashboardDiv);
    89      if (numTests > 0) {
    90        // Element representing the div to put all test information about this
    91        // tab in.
    92        let tabDiv = addHolder(
    93            dashboardDiv, alert.dashboard + '-' + tabName, 'tab-div', tabHeader,
    94            tabDivList);
    95        addTestList(tabDiv, tabSummary.tests);
    96      }
    97      addAlertColors(tabSummary.alert, numTests, tabHeader, dashboardHeader);
    98    }
    99  }
   100  
   101  function addTestList(tabDiv, testList) {
   102    // The unordered list element to hold all test information for this tab.
   103    let list = document.createElement('ul');
   104    tabDiv.appendChild(list);
   105    list.classList.add('tab_ul');
   106  
   107    testList.forEach(function(testSummary) {
   108      // The list item element that displays information about this test.
   109      let test = document.createElement('li');
   110      test.textContent = 'Test ' + testSummary.display_name + ' has failed ' +
   111          testSummary.fail_count + ' times.';
   112      list.appendChild(test);
   113      test.classList.add('test-li');
   114    });
   115  }
   116  
   117  function addHeader(text, className, div) {
   118    let header = document.createElement('p');
   119    header.classList.add(className);
   120    header.textContent = text;
   121    div.appendChild(header);
   122    return header;
   123  }
   124  
   125  function addHolder(container, id, className, header, list) {
   126    let div = document.createElement('div');
   127    container.appendChild(div);
   128    div.id = id + '-div';
   129    div.classList.add(className);
   130    div.classList.add('collapsed');
   131    header.addEventListener('click', function() {
   132      div.classList.toggle('collapsed');
   133    });
   134    list.push(div);
   135    return div;
   136  }
   137  
   138  function addAlertColors(alert, numTests, tabHeader, dashboardHeader) {
   139    if (alert == 'Warning: Test results are stale.') {
   140      tabHeader.classList.add('stale');
   141    } else if (numTests > 0) {
   142      tabHeader.classList.add('failing');
   143    }
   144    if (alert == 'Warning: Test results are stale.' || numTests > 0) {
   145      dashboardHeader.classList.add('failing');
   146    }
   147  }
   148  
   149  /**
   150   * Expand all dashboards and their tabs to make alerts visible.
   151   */
   152  function expandAll() {
   153    expandDashboards();
   154    tabDivList.forEach(function(div) {
   155      div.classList.remove('collapsed');
   156    });
   157  }
   158  
   159  /**
   160   * Collapse all dashboards and tabs to hide tabs and alerts.
   161   */
   162  function collapseAll() {
   163    collapseDashboards();
   164    tabDivList.forEach(function(div) {
   165      div.classList.add('collapsed');
   166    });
   167  }
   168  
   169  /**
   170   * Expand all dashboards to make tabs visible but not alerts.
   171   */
   172  function expandDashboards() {
   173    dashboardDivList.forEach(function(div) {
   174      div.classList.remove('collapsed');
   175    });
   176  }
   177  
   178  /**
   179   * Collapse all dashboards to hide information but leave tabs in their current
   180   * state when the dashboard is re-opened.
   181   */
   182  function collapseDashboards() {
   183    dashboardDivList.forEach(function(div) {
   184      div.classList.add('collapsed');
   185    });
   186  }
   187  })();