2017-05-10 04:00:50 -04:00
|
|
|
<script>
|
2017-03-17 13:30:32 -04:00
|
|
|
const PAGINATION_UI_BUTTON_LIMIT = 4;
|
|
|
|
const UI_LIMIT = 6;
|
|
|
|
const SPREAD = '...';
|
|
|
|
const PREV = 'Prev';
|
|
|
|
const NEXT = 'Next';
|
2017-03-28 01:27:55 -04:00
|
|
|
const FIRST = '« First';
|
|
|
|
const LAST = 'Last »';
|
2017-03-17 13:30:32 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
/**
|
|
|
|
This function will take the information given by the pagination component
|
|
|
|
|
|
|
|
Here is an example `change` method:
|
|
|
|
|
|
|
|
change(pagenum) {
|
|
|
|
gl.utils.visitUrl(`?page=${pagenum}`);
|
2016-12-15 12:28:17 -05:00
|
|
|
},
|
2017-03-17 13:30:32 -04:00
|
|
|
*/
|
|
|
|
change: {
|
|
|
|
type: Function,
|
|
|
|
required: true,
|
|
|
|
},
|
2016-12-15 12:32:44 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
/**
|
|
|
|
pageInfo will come from the headers of the API call
|
|
|
|
in the `.then` clause of the VueResource API call
|
|
|
|
there should be a function that contructs the pageInfo for this component
|
|
|
|
|
|
|
|
This is an example:
|
|
|
|
|
|
|
|
const pageInfo = 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'],
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
pageInfo: {
|
|
|
|
type: Object,
|
|
|
|
required: true,
|
2016-12-15 12:28:17 -05:00
|
|
|
},
|
2017-03-17 13:30:32 -04:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
changePage(e) {
|
2017-07-07 09:27:01 -04:00
|
|
|
if (e.target.parentElement.classList.contains('disabled')) return;
|
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
const text = e.target.innerText;
|
|
|
|
const { totalPages, nextPage, previousPage } = this.pageInfo;
|
|
|
|
|
|
|
|
switch (text) {
|
|
|
|
case SPREAD:
|
|
|
|
break;
|
|
|
|
case LAST:
|
|
|
|
this.change(totalPages);
|
|
|
|
break;
|
|
|
|
case NEXT:
|
|
|
|
this.change(nextPage);
|
|
|
|
break;
|
|
|
|
case PREV:
|
|
|
|
this.change(previousPage);
|
|
|
|
break;
|
|
|
|
case FIRST:
|
|
|
|
this.change(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.change(+text);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-08 19:37:00 -05:00
|
|
|
},
|
2017-03-17 13:30:32 -04:00
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
prev() {
|
|
|
|
return this.pageInfo.previousPage;
|
|
|
|
},
|
|
|
|
next() {
|
|
|
|
return this.pageInfo.nextPage;
|
|
|
|
},
|
|
|
|
getItems() {
|
|
|
|
const total = this.pageInfo.totalPages;
|
|
|
|
const page = this.pageInfo.page;
|
|
|
|
const items = [];
|
2016-11-08 14:23:29 -05:00
|
|
|
|
2017-07-07 17:23:13 -04:00
|
|
|
if (page > 1) {
|
|
|
|
items.push({ title: FIRST, first: true });
|
|
|
|
}
|
2016-11-08 16:57:16 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
if (page > 1) {
|
|
|
|
items.push({ title: PREV, prev: true });
|
|
|
|
} else {
|
|
|
|
items.push({ title: PREV, disabled: true, prev: true });
|
|
|
|
}
|
2016-11-08 14:23:29 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
if (page > UI_LIMIT) items.push({ title: SPREAD, separator: true });
|
2016-11-08 14:23:29 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
const start = Math.max(page - PAGINATION_UI_BUTTON_LIMIT, 1);
|
|
|
|
const end = Math.min(page + PAGINATION_UI_BUTTON_LIMIT, total);
|
2016-11-08 16:23:16 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
for (let i = start; i <= end; i += 1) {
|
|
|
|
const isActive = i === page;
|
|
|
|
items.push({ title: i, active: isActive, page: true });
|
|
|
|
}
|
2016-11-08 16:23:16 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
if (total - page > PAGINATION_UI_BUTTON_LIMIT) {
|
|
|
|
items.push({ title: SPREAD, separator: true, page: true });
|
|
|
|
}
|
2016-11-08 16:57:16 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
if (page === total) {
|
|
|
|
items.push({ title: NEXT, disabled: true, next: true });
|
|
|
|
} else if (total - page >= 1) {
|
|
|
|
items.push({ title: NEXT, next: true });
|
|
|
|
}
|
2016-11-08 16:57:16 -05:00
|
|
|
|
2017-07-07 17:23:13 -04:00
|
|
|
if (total - page >= 1) {
|
|
|
|
items.push({ title: LAST, last: true });
|
|
|
|
}
|
2016-11-08 14:23:29 -05:00
|
|
|
|
2017-03-17 13:30:32 -04:00
|
|
|
return items;
|
2016-11-03 00:41:51 -04:00
|
|
|
},
|
2017-03-17 13:30:32 -04:00
|
|
|
},
|
|
|
|
};
|
2017-05-10 04:00:50 -04:00
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div class="gl-pagination">
|
|
|
|
<ul class="pagination clearfix">
|
|
|
|
<li
|
|
|
|
v-for="item in getItems"
|
|
|
|
:class="{
|
|
|
|
page: item.page,
|
2017-07-07 17:23:13 -04:00
|
|
|
'js-previous-button': item.prev,
|
|
|
|
'js-next-button': item.next,
|
|
|
|
'js-last-button': item.last,
|
|
|
|
'js-first-button': item.first,
|
2017-05-10 04:00:50 -04:00
|
|
|
separator: item.separator,
|
|
|
|
active: item.active,
|
|
|
|
disabled: item.disabled
|
|
|
|
}">
|
2017-07-07 14:11:58 -04:00
|
|
|
<a @click.prevent="changePage($event)">{{item.title}}</a>
|
2017-05-10 04:00:50 -04:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</template>
|