summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/mini_pipeline_graph_dropdown.js.es6
blob: 90b3366f14b83a223b48ee3cc02e4bc3aeee8979 (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
/* eslint-disable no-new */
/* global Flash */

/**
 * In each pipelines table we have a mini pipeline graph for each pipeline.
 *
 * When we click in a pipeline stage, we need to make an API call to get the
 * builds list to render in a dropdown.
 *
 * The container should be the table element.
 *
 * The stage icon clicked needs to have the following HTML structure:
 * <div>
 *   <button class="dropdown js-builds-dropdown-button"></button>
 *   <div class="js-builds-dropdown-container"></div>
 * </div>
 */
(() => {
  class MiniPipelineGraph {
    constructor(opts = {}) {
      this.container = opts.container || '';
      this.dropdownListSelector = '.js-builds-dropdown-container';
      this.getBuildsList = this.getBuildsList.bind(this);

      this.bindEvents();
    }

    /**
     * Adds and removes the event listener.
     */
    bindEvents() {
      const dropdownButtonSelector = 'button.js-builds-dropdown-button';

      $(this.container).off('click', dropdownButtonSelector, this.getBuildsList)
        .on('click', dropdownButtonSelector, this.getBuildsList);
    }

    /**
     * For the clicked stage, renders the given data in the dropdown list.
     *
     * @param  {HTMLElement} stageContainer
     * @param  {Object} data
     */
    renderBuildsList(stageContainer, data) {
      const dropdownContainer = stageContainer.parentElement.querySelector(
        `${this.dropdownListSelector} .js-builds-dropdown-list`,
      );

      dropdownContainer.innerHTML = data;
    }

    /**
     * For the clicked stage, gets the list of builds.
     *
     * @param  {Object} e
     * @return {Promise}
     */
    getBuildsList(e) {
      const button = e.currentTarget;
      const endpoint = button.dataset.stageEndpoint;

      return $.ajax({
        dataType: 'json',
        type: 'GET',
        url: endpoint,
        beforeSend: () => {
          this.renderBuildsList(button, '');
          this.toggleLoading(button);
        },
        success: (data) => {
          this.toggleLoading(button);
          this.renderBuildsList(button, data.html);
        },
        error: () => {
          this.toggleLoading(button);
          new Flash('An error occurred while fetching the builds.', 'alert');
        },
      });
    }

    /**
     * Toggles the visibility of the loading icon.
     *
     * @param  {HTMLElement} stageContainer
     * @return {type}
     */
    toggleLoading(stageContainer) {
      stageContainer.parentElement.querySelector(
        `${this.dropdownListSelector} .js-builds-dropdown-loading`,
      ).classList.toggle('hidden');
    }
  }

  window.gl = window.gl || {};
  window.gl.MiniPipelineGraph = MiniPipelineGraph;
})();