2016-12-20 08:25:58 -05:00
|
|
|
/* eslint-disable no-new */
|
2016-12-19 12:47:45 -05:00
|
|
|
/* 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:
|
2017-01-12 14:19:34 -05:00
|
|
|
* <div class="dropdown">
|
|
|
|
* <button class="dropdown js-builds-dropdown-button" data-toggle="dropdown"></button>
|
|
|
|
* <div class="js-builds-dropdown-container dropdown-menu"></div>
|
2016-12-19 12:47:45 -05:00
|
|
|
* </div>
|
|
|
|
*/
|
2017-03-09 08:03:08 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
export default class MiniPipelineGraph {
|
|
|
|
constructor(opts = {}) {
|
|
|
|
this.container = opts.container || '';
|
|
|
|
this.dropdownListSelector = '.js-builds-dropdown-container';
|
|
|
|
this.getBuildsList = this.getBuildsList.bind(this);
|
|
|
|
}
|
2016-12-19 12:47:45 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
/**
|
|
|
|
* Adds the event listener when the dropdown is opened.
|
|
|
|
* All dropdown events are fired at the .dropdown-menu's parent element.
|
|
|
|
*/
|
|
|
|
bindEvents() {
|
2017-04-26 12:37:54 -04:00
|
|
|
$(document)
|
|
|
|
.off('shown.bs.dropdown', this.container)
|
|
|
|
.on('shown.bs.dropdown', this.container, this.getBuildsList);
|
2017-03-09 13:26:15 -05:00
|
|
|
}
|
2017-03-09 08:03:08 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
/**
|
|
|
|
* When the user right clicks or cmd/ctrl + click in the job name
|
|
|
|
* the dropdown should not be closed and the link should open in another tab,
|
|
|
|
* so we stop propagation of the click event inside the dropdown.
|
|
|
|
*
|
|
|
|
* Since this component is rendered multiple times per page we need to guarantee we only
|
|
|
|
* target the click event of this component.
|
|
|
|
*/
|
|
|
|
stopDropdownClickPropagation() {
|
2017-03-09 13:51:52 -05:00
|
|
|
$(document).on(
|
|
|
|
'click',
|
|
|
|
`${this.container} .js-builds-dropdown-list a.mini-pipeline-graph-dropdown-item`,
|
|
|
|
(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
},
|
|
|
|
);
|
2017-03-09 13:26:15 -05:00
|
|
|
}
|
2016-12-19 12:47:45 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
/**
|
|
|
|
* 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`,
|
|
|
|
);
|
2016-12-19 12:47:45 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
dropdownContainer.innerHTML = data;
|
|
|
|
}
|
2016-12-19 12:47:45 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
/**
|
|
|
|
* For the clicked stage, gets the list of builds.
|
|
|
|
*
|
|
|
|
* All dropdown events have a relatedTarget property,
|
|
|
|
* whose value is the toggling anchor element.
|
|
|
|
*
|
|
|
|
* @param {Object} e bootstrap dropdown event
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
getBuildsList(e) {
|
|
|
|
const button = e.relatedTarget;
|
|
|
|
const endpoint = button.dataset.stageEndpoint;
|
2016-12-19 12:58:46 -05:00
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
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);
|
|
|
|
this.stopDropdownClickPropagation();
|
|
|
|
},
|
|
|
|
error: () => {
|
|
|
|
this.toggleLoading(button);
|
2017-04-26 12:37:54 -04:00
|
|
|
if ($(button).parent().hasClass('open')) {
|
|
|
|
$(button).dropdown('toggle');
|
|
|
|
}
|
2017-03-09 13:26:15 -05:00
|
|
|
new Flash('An error occurred while fetching the builds.', 'alert');
|
|
|
|
},
|
|
|
|
});
|
2016-12-19 12:47:45 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:26:15 -05:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
}
|