source: src/main/resources/static/js/pagination.js@ a64f926

Last change on this file since a64f926 was 7888b17, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

Added post grouping

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[276a8b6]1let url = window.location.href.toString().split(window.location.host)[1].split("?")[0];
2
3let pages = parseInt(document.getElementById("helper").getAttribute("data-pages"));
4let currentPage = parseInt(findGetParameter("page"));
5let sort = findGetParameter("sort");
6let order = findGetParameter("order");
[7888b17]7let group = findGetParameter("groupBy")
[276a8b6]8
9let sortByInput = document.getElementById("sortBy");
10sortByInput.value = sort;
11
12let orderInput = document.getElementById("order");
13orderInput.value = order;
14
[7888b17]15let groupByInput = document.getElementById("groupBy") === null ? document.createElement("input") : document.getElementById("groupBy");
16groupByInput.value = group;
17
[276a8b6]18sortByInput.addEventListener("change", function () {
[7888b17]19 window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
[276a8b6]20})
21
22orderInput.addEventListener("change", function() {
[7888b17]23 window.location.href = url + "?page=" + currentPage + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
24})
25
26groupByInput.addEventListener("change", function () {
27 window.location.href = url + "?page=" + 1 + "&sort=" + sortByInput.value + "&order=" + orderInput.value + "&groupBy=" + groupByInput.value;
[276a8b6]28})
29
30document.getElementById('pagination').innerHTML = createPagination(pages, currentPage);
31
32function createPagination(pages, page) {
33 let str = '<ul class="pagination justify-content-center">';
34 let active;
35 let pagesBefore = page - 1;
36 let pagesAfter = page + 1;
37
38 if (page > 1) {
[7888b17]39 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-1)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">Previous</a></li>';
[276a8b6]40 }
41
42 if (pages < 6) {
43 for (let p = 1; p <= pages; p++) {
44 active = page == p ? "active" : "";
[7888b17]45 str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+ p +'</a></li>';
[276a8b6]46 }
47 }
48
49 else {
50 if (page > 2) {
[7888b17]51 str += '<li class="page-item"><a class="page-link" href="'+url+'?page=1&sort='+sort+'&order='+order+'&groupBy='+group+'">1</a></li>';
[276a8b6]52 if (page > 3) {
[7888b17]53 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page-2)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">...</a></li>';
[276a8b6]54 }
55 }
56
57 if (page === 1) {
58 pagesAfter += 2;
59 } else if (page === 2) {
60 pagesAfter += 1;
61 }
62
63 if (page === pages) {
64 pagesBefore -= 2;
65 } else if (page === pages-1) {
66 pagesBefore -= 1;
67 }
68
69 for (let p = pagesBefore; p <= pagesAfter; p++) {
70 if (p === 0) {
71 p += 1;
72 }
73 if (p > pages) {
74 continue
75 }
76 active = page == p ? "active" : "";
[7888b17]77 str += '<li class="page-item '+active+'"><a class="page-link" href="'+url+'?page='+p+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+ p +'</a></li>';
[276a8b6]78 }
79
80 if (page < pages-1) {
81 if (page < pages-2) {
[7888b17]82 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+2)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">...</a></li>';
[276a8b6]83 }
[7888b17]84 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+pages+'&sort='+sort+'&order='+order+'&groupBy='+group+'">'+pages+'</a></li>';
[276a8b6]85 }
86 }
87
88 if (page < pages) {
[7888b17]89 str += '<li class="page-item"><a class="page-link" href="'+url+'?page='+(page+1)+'&sort='+sort+'&order='+order+'&groupBy='+group+'">Next</a></li>';
[276a8b6]90 }
91 str += '</ul>';
92
93 return str;
94}
95
96function findGetParameter(parameterName) {
97 let result = null,
98 tmp = [];
99 location.search
100 .substr(1)
101 .split("&")
102 .forEach(function (item) {
103 tmp = item.split("=");
104 if (tmp[0] === parameterName) result = tmp[1];
105 });
106 return result;
107}
Note: See TracBrowser for help on using the repository browser.