-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-pagination.html
97 lines (95 loc) · 3.2 KB
/
paper-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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-button/paper-button.html">
<dom-module id="paper-pagination">
<style>
paper-item.paper-pagination,
paper-button.paper-pagination {
float: left;
}
</style>
<template>
<!--<paper-button on-tap="prev">prev</paper-button>-->
<paper-menu selected="{{current}}" selected-attribute="page">
<!--hidden item take place 0 so, first item will be 1
<paper-item class="paper-pagination" page="0" hidden="true"></paper-item>-->
<template is="dom-repeat" items="[[pages]]" restamp="true">
<paper-item class="paper-pagination" hidden$="[[item.hidden]]" page="[[item.page]]">[[item.page]]</paper-item>
</template>
</paper-menu>
<!--<paper-button on-tap="next">next</paper-button>-->
</template>
<script>
Polymer({
is: 'paper-pagination',
properties: {
pageSize: {
type: Number
},
total: {
type: Number
},
pages: {
type: Array,
readOnly: true,
computed: '_calculatePages(pageSize,total)'
},
rangeSize: {
type: Number,
value: 10,
},
current: {
type: Number,
value: 0,
reflectToAttribute: true,
notify: true,
},
offset: {
type: Number,
readOnly: true,
notify: true,
computed: '_calculateOffset(current,pageSize)'
}
},
ready:function(){
},
prev: function() {
if (this.current && this.current > 1)
this.current -= 1;
},
next: function() {
if (this.current && this.current < this.pageCount)
this.current += 1;
},
_calculateOffset: function(current, pageSize) {
return (current - 1) * pageSize;
},
_calculatePages: function(size, total, current_page) {
function start_link($p, $g, $n) {
$s = $p - Math.floor($n / 2);
if ($s < 1) {
return 1;
}
$max_s = $g - ($n - 1);
if ($s > $max_s) {
return $max_s;
}
return $s;
}
var totalPage = this.pageCount = Math.ceil(total / size);
var result = [];
var startRange = start_link(current_page, totalPage, this.rangeSize);
var endRange = Math.min(totalPage, (startRange + this.rangeSize));
for (i = 1; i <= totalPage; i++) {
hidden = (i != 1 && i < startRange || i > endRange && i != this.pageCount);
result.push({
page: i,
hidden: hidden
});
}
return result;
},
})
</script>
</dom-module>