3 function requiredBlock(context
, page
, pageCount
, fn
) {
4 if (page
> 1 || pageCount
> 1) {
11 function middleBlock(page
, pageCount
, limit
, fn
) {
12 function contextForItem(pageNumber
) {
15 active
: pageNumber
=== page
21 if (typeof limit
=== 'number') {
23 var leftCount
= Math
.ceil(limit
/ 2) - 1;
24 var rightCount
= limit
- leftCount
- 1;
25 if (page
+ rightCount
> pageCount
) leftCount
= limit
- (pageCount
- page
) - 1;
26 if (page
- leftCount
< 1) leftCount
= page
- 1;
27 var start
= page
- leftCount
;
29 while (i
< limit
&& i
< pageCount
) {
30 ret
= ret
+ fn(contextForItem(start
));
35 for (i
= 1; i
<= pageCount
; i
++) {
36 ret
= ret
+ fn(contextForItem(i
));
43 function previousBlock(page
, pageCount
, fn
) {
45 return fn({ disabled
: true, n
: 1 });
47 return fn({ n
: page
- 1 });
51 function nextBlock(page
, pageCount
, fn
) {
52 if (page
=== pageCount
) {
53 return fn({ disabled
: true, n
: pageCount
});
55 return fn({ n
: page
+ 1 });
59 function firstBlock(page
, pageCount
, fn
) {
61 return fn({ disabled
: true, n
: 1 });
67 function lastBlock(page
, pageCount
, fn
) {
68 if (page
=== pageCount
) {
69 return fn({ disabled
: true, n
: pageCount
});
71 return fn({ n
: pageCount
});
75 module
.exports = function(pagination
, options
) {
76 var type
= options
.hash
.type
|| 'middle';
78 var pageCount
= Number(pagination
.pageCount
);
79 var page
= Number(pagination
.page
);
81 if (options
.hash
.limit
) limit
= +options
.hash
.limit
;
86 return requiredBlock(this, page
, pageCount
, fn
);
89 return middleBlock(page
, pageCount
, limit
, fn
);
92 return previousBlock(page
, pageCount
, fn
);
95 return nextBlock(page
, pageCount
, fn
);
98 return firstBlock(page
, pageCount
, fn
);
101 return lastBlock(page
, pageCount
, fn
);