gitlab-org--gitlab-foss/app/assets/javascripts/vue_pipelines_index/store.js.es6

78 lines
2.4 KiB
JavaScript
Raw Normal View History

/* global gl, Flash */
/* eslint-disable no-param-reassign, no-underscore-dangle */
((gl) => {
2016-12-16 17:50:28 -05:00
const pageValues = headers => ({
perPage: +headers['X-Per-Page'],
page: +headers['X-Page'],
total: +headers['X-Total'],
totalPages: +headers['X-Total-Pages'],
nextPage: +headers['X-Next-Page'],
previousPage: +headers['X-Prev-Page'],
});
2016-12-07 15:35:26 -05:00
gl.PipelineStore = class {
fetchDataLoop(Vue, pageNum, url, apiScope) {
const updatePipelineNums = (count) => {
const { all } = count;
const running = count.running_or_pending;
document.querySelector('.js-totalbuilds-count').innerHTML = all;
document.querySelector('.js-running-count').innerHTML = running;
};
const goFetch = () =>
this.$http.get(`${url}?scope=${apiScope}&page=${pageNum}`)
.then((response) => {
2016-12-07 15:35:26 -05:00
const pageInfo = pageValues(response.headers);
Vue.set(this, 'pageInfo', pageInfo);
const res = JSON.parse(response.body);
2016-11-10 12:16:27 -05:00
Vue.set(this, 'pipelines', res.pipelines);
Vue.set(this, 'count', res.count);
2016-12-07 15:35:26 -05:00
updatePipelineNums(this.count);
2016-11-11 00:21:09 -05:00
this.pageRequest = false;
}, () => {
this.pageRequest = false;
return new Flash('Something went wrong on our end.');
});
2016-11-10 15:17:33 -05:00
goFetch();
const startTimeLoops = () => {
this.timeLoopInterval = setInterval(() => {
this.$children
.filter(e => e.$options._componentTag === 'time-ago')
.forEach(e => e.changeTime());
}, 1000);
};
startTimeLoops();
const removeTimeIntervals = () => {
clearInterval(this.timeLoopInterval);
};
const startIntervalLoops = () => {
startTimeLoops();
};
2016-12-02 13:42:26 -05:00
const removeAll = () => {
window.removeEventListener('beforeunload', removeTimeIntervals);
window.removeEventListener('focus', startIntervalLoops);
window.removeEventListener('blur', removeTimeIntervals);
2016-12-15 12:04:26 -05:00
// turbolinks event handler
document.removeEventListener('page:fetch', () => {});
};
window.addEventListener('beforeunload', removeTimeIntervals);
2016-12-02 13:42:26 -05:00
window.addEventListener('focus', startIntervalLoops);
window.addEventListener('blur', removeTimeIntervals);
2016-12-15 12:04:26 -05:00
// turbolinks event handler
2016-12-02 13:42:26 -05:00
document.addEventListener('page:fetch', removeAll);
}
};
})(window.gl || (window.gl = {}));