-
Notifications
You must be signed in to change notification settings - Fork 2
/
pager.js
110 lines (88 loc) · 1.95 KB
/
pager.js
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
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Pager = factory();
}
} (this, function() {
'use strict';
function Pager (length, circular) {
if (isNaN(length)) {
throw 'The "length" parameter must be a number';
}
if ('undefined' !== typeof circular && 'boolean' !== typeof circular) {
throw 'The "circular" parameter must be a boolean';
}
this.current = 0;
this.length = parseInt(length);
this.circular = ('undefined' === typeof circular) ? true : circular;
}
Pager.prototype = {
constructor: Pager,
set: function(index) {
if (isNaN(index)) {
throw 'The "index" parameter must be a number';
}
var first = this.getFirst(),
last = this.getLast();
this.current = index;
if (index <= first) {
this.current = first;
} else if (index >= last) {
this.current = last;
}
},
hasPrev: function() {
return 0 !== this.current;
},
hasNext: function() {
return this.current < this.getLast();
},
getPrev: function () {
if (this.hasPrev()) {
return this.current - 1;
} else {
return (this.circular) ? this.getLast() : false;
}
},
getNext: function() {
if (this.hasNext()) {
return this.current + 1;
} else {
return (this.circular) ? 0 : false;
}
},
getFirst: function() {
return 0;
},
getLast: function() {
return this.length - 1;
},
prev: function() {
if (this.hasPrev()) {
this.current--;
} else if (this.circular) {
this.last();
}
return this.current;
},
next: function() {
if (this.hasNext()) {
this.current++;
} else if (this.circular) {
this.first();
}
return this.current;
},
first: function() {
return (this.current = this.getFirst());
},
last: function() {
return (this.current = this.getLast());
}
};
return Pager;
}));