Paginator.js
6.72 KB
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
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Util = require('./Util');
var Paginator = Backbone.Model.extend({
defaults: function defaults() {
return {
hasNext: false,
hasNextJump: undefined,
hasPrevious: false,
hasPreviousJump: undefined,
leadingPageCount: 4,
nextJumpPage: undefined,
nextPage: undefined,
pages: [],
pageJump: 5,
previousJumpPage: undefined,
previousPage: undefined,
totalPages: 0,
trailingPageCount: 5,
};
},
initialize: function(data, options) {
var buildPages = _.bind(function buildPages() {
var source = this.get('source');
if (!source) {
return;
}
var currentPage = parseInt(source.get('currentPage'));
var pageSize = parseInt(source.get('pageSize'));
var totalCount = parseInt(source.get('totalCount'));
if (!totalCount) {
return;
}
var pages = [];
var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize);
if (currentPage < 0) {
source.set('currentPage', 1);
return;
} else if (currentPage > totalPages) {
source.set('currentPage', totalPages);
return;
}
function addPage(self, i) {
pages.push({
current: i === currentPage,
jump: _.bind(function() {
this.get('source').set('currentPage', i);
return true;
}, self),
number: i,
});
}
var startAt = currentPage - this.get('leadingPageCount'), endAt = currentPage + this.get('trailingPageCount');
if (startAt < 1) {
endAt += (1 - startAt);
}
if (endAt > totalPages) {
startAt -= endAt - totalPages - 1;
endAt = totalPages + 1;
}
if (startAt < 1) {
startAt = 1;
}
for (var i = startAt; i < endAt; i++) {
addPage(this, i);
}
var hasPrevious = currentPage > 1;
var hasNext = currentPage < totalPages;
var pageJump = this.get('pageJump');
var nextJumpPage, previousJumpPage, hasNextJump, hasPreviousJump;
if (pageJump) {
nextJumpPage = currentPage + pageJump;
previousJumpPage = currentPage - pageJump;
hasNextJump = nextJumpPage < totalPages;
hasPreviousJump = previousJumpPage > 0;
} else {
hasNextJump = false;
hasPreviousJump = false;
}
this.set({
hasNext: hasNext,
hasNextJump: hasNextJump,
hasPrevious: hasPrevious,
hasPreviousJump: hasPreviousJump,
nextJumpPage: hasNextJump ? nextJumpPage : undefined,
nextPage: hasNext ? currentPage + 1 : undefined,
pages: pages,
previousJumpPage: hasNextJump ? previousJumpPage : undefined,
previousPage: hasPrevious ? currentPage - 1 : undefined,
totalPages: totalPages,
});
}, this);
this.on('change:leadingPageCount change:trailingPageCount', buildPages);
var installActions = _.bind(function installActions(eventName, nextName, actNextName, hasNextName, previousName, actPreviousName, hasPreviousName, pageCount) {
this.on(eventName, function() {
var hasNext = this.get(hasNextName);
var hasPrevious = this.get(hasPreviousName);
var next, previous;
if (hasNext) {
next = _.bind(function(e) {
Util.preventDefault(e);
var source = this.get('source');
source.set('currentPage', source.get('currentPage') + pageCount);
return false;
}, this);
} else {
next = Util.eventFalse;
}
if (hasPrevious) {
previous = _.bind(function(e) {
Util.preventDefault(e);
var source = this.get('source');
source.set('currentPage', source.get('currentPage') - pageCount);
return false;
}, this);
} else {
previous = Util.eventFalse;
}
this.set(nextName, next);
this.set(previousName, previous);
}, this);
this[actNextName] = _.bind(function() {
return this.get(nextName)();
}, this);
this[actPreviousName] = _.bind(function() {
return this.get(previousName)();
}, this);
this.trigger(eventName.split(' ')[0]);
}, this);
this.on('change:pageJump', function() {
var pageJump = this.get('pageJump');
installActions('change:hasNextJump change:hasPreviousJump', 'nextJump', 'actNextJump', 'hasNextJump', 'previousJump', 'actPreviousJump', 'hasPreviousJump', pageJump);
}, this);
installActions('change:hasNext change:hasPrevious', 'next', 'actNext', 'hasNext', 'previous', 'actPrevious', 'hasPrevious', 1);
this.on('change:source', function() {
var oldSource = this.previous('source');
if (oldSource) {
this.stopListening(oldSource);
}
this.listenTo(this.get('source'), 'change:pageSize change:currentPage change:totalCount', buildPages);
});
var r = Paginator.__super__.initialize.apply(this, arguments);
buildPages();
this.trigger('change:pageJump');
this.trigger('change:source');
return r;
}
});
return Paginator;
});