Commit 64b66c9a authored by Regis's avatar Regis

use API values for pagination

parent c9b63924
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
/* eslint-disable no-param-reassign, no-plusplus */ /* eslint-disable no-param-reassign, no-plusplus */
((gl) => { ((gl) => {
const PAGINATION_SIZE = 30;
const PAGINATION_UI_BUTTON_LIMIT = 4; const PAGINATION_UI_BUTTON_LIMIT = 4;
const SPREAD = '...'; const SPREAD = '...';
const PREV = 'Prev'; const PREV = 'Prev';
...@@ -13,24 +12,26 @@ ...@@ -13,24 +12,26 @@
gl.VueGlPagination = Vue.extend({ gl.VueGlPagination = Vue.extend({
props: [ props: [
'changepage', 'changepage',
'count', 'pageInfo',
'pagenum',
], ],
computed: { computed: {
last() { prev() {
return Math.ceil(+this.count / PAGINATION_SIZE); return this.pageInfo.previousPage;
},
next() {
return this.pageInfo.nextPage;
}, },
getItems() { getItems() {
const total = +this.last; const total = this.pageInfo.totalPages;
const page = +this.pagenum; const page = this.pageInfo.page;
const items = []; const items = [];
if (page > 1) items.push({ title: FIRST, where: 1 }); if (page > 1) items.push({ title: FIRST });
if (page > 1) { if (page > 1) {
items.push({ title: PREV, where: page - 1 }); items.push({ title: PREV });
} else { } else {
items.push({ title: PREV, where: page - 1, disabled: true }); items.push({ title: PREV, disabled: true });
} }
if (page > 6) items.push({ title: SPREAD, separator: true }); if (page > 6) items.push({ title: SPREAD, separator: true });
...@@ -40,7 +41,7 @@ ...@@ -40,7 +41,7 @@
for (let i = start; i <= end; i++) { for (let i = start; i <= end; i++) {
const isActive = i === page; const isActive = i === page;
items.push({ title: i, active: isActive, where: i }); items.push({ title: i, active: isActive });
} }
if (total - page > PAGINATION_UI_BUTTON_LIMIT) { if (total - page > PAGINATION_UI_BUTTON_LIMIT) {
...@@ -48,12 +49,12 @@ ...@@ -48,12 +49,12 @@
} }
if (page === total) { if (page === total) {
items.push({ title: NEXT, where: page + 1, disabled: true }); items.push({ title: NEXT, disabled: true });
} else if (total - page >= 1) { } else if (total - page >= 1) {
items.push({ title: NEXT, where: page + 1 }); items.push({ title: NEXT });
} }
if (total - page >= 1) items.push({ title: LAST, where: total }); if (total - page >= 1) items.push({ title: LAST });
return items; return items;
}, },
...@@ -69,7 +70,7 @@ ...@@ -69,7 +70,7 @@
}' }'
> >
<span <span
@click="changepage($event, item.where)" @click="changepage($event)"
> >
{{item.title}} {{item.title}}
</span> </span>
......
...@@ -35,8 +35,8 @@ ...@@ -35,8 +35,8 @@
pipelines: [], pipelines: [],
timeLoopInterval: '', timeLoopInterval: '',
intervalId: '', intervalId: '',
pagenum: 1,
apiScope: 'all', apiScope: 'all',
pageInfo: {},
count: { count: {
all: 0, all: 0,
running_or_pending: 0, running_or_pending: 0,
...@@ -64,13 +64,14 @@ ...@@ -64,13 +64,14 @@
); );
}, },
methods: { methods: {
changepage(e, last) { changepage(e) {
const text = e.target.innerText; const text = e.target.innerText;
const { totalPages, nextPage, previousPage } = this.pageInfo;
if (text === SPREAD) return; if (text === SPREAD) return;
if (/^-?[\d.]+(?:e-?\d+)?$/.test(text)) this.pagenum = +text; if (/^-?[\d.]+(?:e-?\d+)?$/.test(text)) this.pagenum = +text;
if (text === LAST) this.pagenum = last; if (text === LAST) this.pagenum = totalPages;
if (text === NEXT) this.pagenum = +this.pagenum + 1; if (text === NEXT) this.pagenum = nextPage;
if (text === PREV) this.pagenum = +this.pagenum - 1; if (text === PREV) this.pagenum = previousPage;
if (text === FIRST) this.pagenum = 1; if (text === FIRST) this.pagenum = 1;
window.history.pushState({}, null, `?p=${this.pagenum}`); window.history.pushState({}, null, `?p=${this.pagenum}`);
...@@ -141,10 +142,11 @@ ...@@ -141,10 +142,11 @@
<i class="fa fa-spinner fa-spin"></i> <i class="fa fa-spinner fa-spin"></i>
</div> </div>
<gl-pagination <gl-pagination
v-if='count.all > 30' v-if='pageInfo.total > 30'
:pagenum='pagenum' :pagenum='pagenum'
:changepage='changepage' :changepage='changepage'
:count='count.all' :count='count.all'
:pageInfo='pageInfo'
> >
</gl-pagination> </gl-pagination>
</div> </div>
......
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
/* eslint-disable no-param-reassign, no-underscore-dangle */ /* eslint-disable no-param-reassign, no-underscore-dangle */
((gl) => { ((gl) => {
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 { gl.PipelineStore = class {
fetchDataLoop(Vue, pageNum, url, apiScope) { fetchDataLoop(Vue, pageNum, url, apiScope) {
const updatePipelineNums = (count) => { const updatePipelineNums = (count) => {
...@@ -14,9 +25,13 @@ ...@@ -14,9 +25,13 @@
const goFetch = () => const goFetch = () =>
this.$http.get(`${url}?scope=${apiScope}&page=${pageNum}`) this.$http.get(`${url}?scope=${apiScope}&page=${pageNum}`)
.then((response) => { .then((response) => {
const pageInfo = pageValues(response.headers);
Vue.set(this, 'pageInfo', pageInfo);
const res = JSON.parse(response.body); const res = JSON.parse(response.body);
Vue.set(this, 'pipelines', res.pipelines); Vue.set(this, 'pipelines', res.pipelines);
Vue.set(this, 'count', res.count); Vue.set(this, 'count', res.count);
updatePipelineNums(this.count); updatePipelineNums(this.count);
this.pageRequest = false; this.pageRequest = false;
}, () => new Flash( }, () => new Flash(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment