gitlab-org--gitlab-foss/app/assets/javascripts/vue_pagination/index.js.es6

83 lines
2.0 KiB
JavaScript
Raw Normal View History

/* global Vue, gl */
2016-11-08 21:57:16 +00:00
/* eslint-disable no-param-reassign, no-plusplus */
((gl) => {
2016-11-18 22:26:49 +00:00
const PAGINATION_UI_BUTTON_LIMIT = 4;
const SPREAD = '...';
const PREV = 'Prev';
const NEXT = 'Next';
const FIRST = '<< First';
const LAST = 'Last >>';
gl.VueGlPagination = Vue.extend({
props: [
'changepage',
2016-12-07 20:35:26 +00:00
'pageInfo',
],
computed: {
2016-12-07 20:35:26 +00:00
prev() {
return this.pageInfo.previousPage;
},
next() {
return this.pageInfo.nextPage;
2016-11-08 22:03:25 +00:00
},
2016-11-08 19:23:29 +00:00
getItems() {
2016-12-07 20:35:26 +00:00
const total = this.pageInfo.totalPages;
const page = this.pageInfo.page;
2016-11-08 19:23:29 +00:00
const items = [];
2016-12-07 20:35:26 +00:00
if (page > 1) items.push({ title: FIRST });
2016-11-08 21:57:16 +00:00
if (page > 1) {
2016-12-07 20:35:26 +00:00
items.push({ title: PREV });
2016-11-08 21:57:16 +00:00
} else {
2016-12-07 20:35:26 +00:00
items.push({ title: PREV, disabled: true });
2016-11-08 21:57:16 +00:00
}
2016-11-08 19:23:29 +00:00
2016-11-18 22:26:49 +00:00
if (page > 6) items.push({ title: SPREAD, separator: true });
2016-11-08 19:23:29 +00:00
2016-11-18 22:26:49 +00:00
const start = Math.max(page - PAGINATION_UI_BUTTON_LIMIT, 1);
const end = Math.min(page + PAGINATION_UI_BUTTON_LIMIT, total);
2016-11-08 21:23:16 +00:00
2016-11-08 21:57:16 +00:00
for (let i = start; i <= end; i++) {
const isActive = i === page;
2016-12-07 20:35:26 +00:00
items.push({ title: i, active: isActive });
2016-11-08 21:57:16 +00:00
}
2016-11-08 21:23:16 +00:00
2016-11-18 22:26:49 +00:00
if (total - page > PAGINATION_UI_BUTTON_LIMIT) {
items.push({ title: SPREAD, separator: true });
}
2016-11-08 21:57:16 +00:00
if (page === total) {
2016-12-07 20:35:26 +00:00
items.push({ title: NEXT, disabled: true });
2016-11-08 21:57:16 +00:00
} else if (total - page >= 1) {
2016-12-07 20:35:26 +00:00
items.push({ title: NEXT });
2016-11-08 21:57:16 +00:00
}
2016-12-07 20:35:26 +00:00
if (total - page >= 1) items.push({ title: LAST });
2016-11-08 19:23:29 +00:00
return items;
},
},
template: `
<div class="gl-pagination">
2016-11-08 21:57:16 +00:00
<ul class="pagination clearfix" v-for='item in getItems'>
2016-11-08 21:23:16 +00:00
<li
2016-11-08 21:57:16 +00:00
:class='{
separator: item.separator,
active: item.active,
disabled: item.disabled
}'
2016-11-08 21:23:16 +00:00
>
2016-11-08 21:57:16 +00:00
<span
2016-12-07 20:35:26 +00:00
@click="changepage($event)"
2016-11-08 21:57:16 +00:00
>
{{item.title}}
</span>
2016-11-08 21:23:16 +00:00
</li>
</ul>
</div>
`,
});
})(window.gl || (window.gl = {}));