1 |
|
2 | export var Paginator = {
|
3 | create: function(id, data, link_limit = 7) {
|
4 | return {
|
5 | _id: id,
|
6 | count: data.to - data.from + 1,
|
7 | currentPage: data.current_page,
|
8 | hasMorePages: false,
|
9 | lastPage: data.last_page,
|
10 | nextPageUrl: data.next_page_url,
|
11 | perPage: data.per_page,
|
12 | previousPageUrl: data.prev_page_url,
|
13 | total: data.total,
|
14 | from: data.from,
|
15 | to: data.to,
|
16 | url: function(page) {
|
17 | var url = '';
|
18 | var tmp_page = 1;
|
19 | if (data.next_page_url !== null) {
|
20 | url = data.next_page_url;
|
21 | tmp_page = data.current_page + 1;
|
22 | } else {
|
23 | url = data.prev_page_url;
|
24 | tmp_page = data.current_page - 1;
|
25 | }
|
26 | return url.replace('page=' + tmp_page, 'page=' + page);
|
27 | },
|
28 | createButton: function(url, cl, text) {
|
29 | var e = document.createElement('li');
|
30 | e.className = cl;
|
31 | var l = document.createElement('a');
|
32 | l.setAttribute('href', url);
|
33 | l.className = 'page-link';
|
34 | l.innerHTML = text;
|
35 | e.appendChild(l);
|
36 | return e;
|
37 | },
|
38 | redraw: function() {
|
39 | var paginator_el = document.getElementById(this._id + '_pagination');
|
40 | var f = document.createDocumentFragment();
|
41 | var cl = '';
|
42 |
|
43 | if (this.currentPage === 1) {
|
44 | cl = 'page-item disabled';
|
45 | } else {
|
46 | cl = 'page-item';
|
47 | }
|
48 | f.appendChild(this.createButton(this.url(1), cl, 'First'));
|
49 | f.appendChild(this.createButton(this.previousPageUrl, cl, '< Previous'));
|
50 |
|
51 | for (var k = 1; k <= this.lastPage; k++) {
|
52 |
|
53 | var half_total_links = Math.floor(link_limit / 2);
|
54 | var from = this.currentPage - half_total_links;
|
55 | var to = this.currentPage + half_total_links;
|
56 | if (this.currentPage < half_total_links) {
|
57 | to += half_total_links - this.currentPage;
|
58 | }
|
59 | if (this.lastPage - this.currentPage < half_total_links) {
|
60 | from -= half_total_links - (this.lastPage - this.currentPage) - 1;
|
61 | }
|
62 |
|
63 | if (from < k && k < to) {
|
64 | var e = document.createElement('li');
|
65 | if (this.currentPage === k) {
|
66 | e.className = 'page-item active';
|
67 | } else {
|
68 | e.className = 'page-item';
|
69 | }
|
70 | var l = document.createElement('a');
|
71 | l.setAttribute('href', this.url(k));
|
72 | l.className = 'page-link';
|
73 | l.innerHTML = k;
|
74 | e.appendChild(l);
|
75 | f.appendChild(e);
|
76 | }
|
77 | }
|
78 |
|
79 | if (this.currentPage === this.lastPage) {
|
80 | cl = 'page-item disabled';
|
81 | } else {
|
82 | cl = 'page-item';
|
83 | }
|
84 | f.appendChild(this.createButton(this.nextPageUrl, cl, 'Next >'));
|
85 | f.appendChild(this.createButton(this.url(this.lastPage), cl, 'Last'));
|
86 |
|
87 | while (paginator_el.firstChild) {
|
88 | paginator_el.removeChild(paginator_el.firstChild);
|
89 | }
|
90 |
|
91 | paginator_el.appendChild(f);
|
92 | },
|
93 | updateStats: function() {
|
94 | document.getElementById(this._id + '_stats_count').innerHTML = this.count;
|
95 | document.getElementById(this._id + '_stats_from').innerHTML = this.from;
|
96 | document.getElementById(this._id + '_stats_to').innerHTML = this.to;
|
97 | document.getElementById(this._id + '_stats_cur_page').innerHTML = this.currentPage;
|
98 | document.getElementById(this._id + '_stats_last_page').innerHTML = this.lastPage;
|
99 | }
|
100 | };
|
101 | }
|
102 | };
|