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

76 lines
2.4 KiB
JavaScript
Raw Normal View History

/* global gl, Flash */
/* eslint-disable no-param-reassign, no-underscore-dangle */
((gl) => {
2016-12-07 15:35:26 -05:00
const pageValues = (headers) => {
const values = {};
values.perPage = +headers['X-Per-Page'];
values.page = +headers['X-Page'];
values.total = +headers['X-Total'];
values.totalPages = +headers['X-Total-Pages'];
values.nextPage = +headers['X-Next-Page'];
values.previousPage = +headers['X-Prev-Page'];
return values;
};
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;
2016-10-31 18:10:50 -04:00
}, () => 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 = () => {
removeTimeIntervals();
window.removeEventListener('beforeunload', () => {});
window.removeEventListener('focus', () => {});
window.removeEventListener('blur', () => {});
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-02 13:42:26 -05:00
document.addEventListener('page:fetch', removeAll);
}
};
})(window.gl || (window.gl = {}));