-
Notifications
You must be signed in to change notification settings - Fork 3
/
plutonium-pagination.html
219 lines (191 loc) · 7.36 KB
/
plutonium-pagination.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-button/paper-button.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="plutonium-pagination-icons.html">
<dom-module id="plutonium-pagination">
<template>
<style include="shared-styles">
:host {
display: block;
font-size: 14px;
}
div.paginator-page-container {
display: block;
@apply --layout-horizontal;
@apply --layout-center;
@apply --layout-center-justified;
@apply --layout-center-center;
}
:host paper-button {
margin: 0px 4px;
padding: 2px 8px;
/*margin: 0px;*/
display: inline-block;
position: relative;
min-width: fit-content;
}
:host paper-button {
color: var(--primary-text-color);
background-color: transparent;
border-radius: 16px;
}
:host span {
margin: 0px 4px;
}
</style>
<div class="paginator-page-container" hidden="[[!hasPage]]">
<paper-icon-button icon="plutonium-pagination-icons:fast-rewind" on-tap="onBegin" hidden="[[!hasBefore]]"></paper-icon-button>
<paper-icon-button icon="plutonium-pagination-icons:navigate-before" on-tap="onBefore" hidden="[[!hasBefore]]"></paper-icon-button>
<span>Page</span>
<template is="dom-repeat" items="[[items]]" id="palette">
<paper-button
raised="[[!isCurrent(item, page)]]"
disabled="[[isCurrent(item, page)]]"
on-tap="onChange">
[[item]]
</paper-button>
</template>
<span>of</span>
[[pages]]
<paper-icon-button icon="plutonium-pagination-icons:navigate-next" on-tap="onNext" hidden="[[!hasNext]]"></paper-icon-button>
<paper-icon-button icon="plutonium-pagination-icons:fast-forward" on-tap="onEnd" hidden="[[!hasNext]]"></paper-icon-button>
</div>
</template>
<script>
/**
* `plutonium-pagination`
* Pagination component based Polymer.
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class PlutoniumPagination extends Polymer.Element {
static get is() {
return 'plutonium-pagination';
}
static get properties() {
return {
/** Per-page limit of the elements. */
limit: {
type: Number,
notify: true
},
/** Total count of the elements. */
total: {
type: Number,
notify: true
},
/** Current page. */
page: {
type: Number,
notify: true,
value: () => 1,
observer: 'onPageChange'
},
/** Count of the pages displayed before or after the current page. */
size: {
type: Number,
notify: true,
value: () => 2
},
/** Number of paginated pages. */
pages: {
type: Number,
notify: true
},
/** Has pages before the current page. */
hasBefore: {
type: Boolean,
computed: 'computeBefore(page, pages)'
},
/** Has pages after the current page. */
hasNext: {
type: Boolean,
computed: 'computeNext(page, pages)'
},
/** Has pages. */
hasPages: {
type: Boolean,
computed: 'computeHasPage(items.length, total)'
},
/** Displayed page elements */
items: {
type: Array,
notify: true
}
};
}
static get observers() {
return [
'observePageCount(page, limit, total)',
];
}
computeBefore(page, pages) {
return page > 1;
}
computeNext(page, pages) {
return page < pages;
}
computeHasPage(itemsLength, total) {
return itemsLength < total;
}
observePageCount(page, limit, total) {
if (limit && total) {
this.set('pages', parseInt(Math.ceil(parseFloat(total) / parseFloat(limit))));
}
if (page && limit && total) {
let items = [];
let firstIndex = this._firstIndex(page, this.get('size'));
let lastIndex = this._lastIndex(page, this.get('size'));
for (var num = firstIndex; num <= lastIndex; num++) {
items.push(num);
}
this.set('items', items);
console.log('paginated', page, 'from', firstIndex, 'to', lastIndex, limit, '/', this.get('pages'), ':', items);
}
}
onPageChange(newValue, oldValue){
this.dispatchEvent(new CustomEvent('pageChange', {detail: {newPage: newValue, oldPage:oldValue}}));
}
_firstIndex(page, size) {
let index = page - size;
if (index < 1) {
return 1;
} else {
return index;
}
}
_lastIndex(page, size) {
let index = page + size;
if (index > this.get('pages')) {
return this.get('pages');
} else {
return index;
}
}
isCurrent(index, page) {
return index === page;
}
onChange(event) {
this.set('page', event.model.item);
console.log(this.id, 'pagination changed to', this.page);
this.$.palette.render();
// this.observePageCount(this.page, this.limit, this.total);
}
onBefore(event) {
this.set('page', this.get('page') > 0 ? this.get('page') - 1 : 1);
}
onNext(event) {
this.set('page', this.get('page') < this.get('pages') ? this.get('page') + 1 : this.get('pages'));
}
onBegin(event) {
this.set('page', 1);
}
onEnd(event) {
this.set('page', this.get('pages'));
}
}
window.customElements.define(PlutoniumPagination.is, PlutoniumPagination);
</script>
</dom-module>