-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpages.js
More file actions
96 lines (87 loc) · 2.94 KB
/
pages.js
File metadata and controls
96 lines (87 loc) · 2.94 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
var pg
$(document).ready(function () {
pg = new Pg();
})
function Pg() {
var SIL = "inLeft";
var SIR = "inRight";
var SOL = "outLeft";
var SOR = "outRight";
var IN = SIL + " " + SIR;
var OUT = SOL + " " + SOR;
var PGS = $($(".pages")[0]);
this.setPg = function (element) {
var el = $($(element)[0]);
if ($(el).hasClass('pages')) {
PGS = el;
} else {
console.error("The specified element does not contain the class 'pages'.");
}
}
this.prev = function () {
var current = PGS.children(".page." + SIR + ",.page." + SIL);
var next = current.prev(".page");
if (next.length != 0) {
current.removeClass(IN);
current.addClass(SOR);
next.removeClass(OUT);
next.addClass(SIL);
next.closest(".pages").height(next.height());
}
}
this.next = function () {
var current = PGS.children(".page." + SIR + ",.page." + SIL);
var next = current.next(".page");
if (next.length != 0) {
current.removeClass(IN);
current.addClass(SOL);
next.removeClass(OUT);
next.addClass(SIR);
next.closest(".pages").height(next.height());
}
}
this.goTo = function (index) {
// validate input
var errors = [];
if (!Number.isInteger(index)) {
errors.push("The supplied index is not a valid integer.");
} else if (index < 0 || index >= $(PGS).children(".page").length) {
errors.push("The supplied index is out of bounds.")
}
if (errors.length > 0) {
for (var i = 0; i < errors.length; i++) {
console.error(errors[i]);
}
return;
}
// execution
var pages = $(PGS).children(".page");
var current = $(PGS).children(".page." + SIR + ",.page." + SIL);
var next = $(pages[index]);
for (var i = 0; i < pages.length; i++) {
if (pages[i] == current[0]) {
if (i > index) {
current.removeClass(IN);
current.addClass(SOR);
next.removeClass(OUT);
next.addClass(SIL);
next.closest(".pages").height(next.height());
} else if (i < index) {
current.removeClass(IN);
current.addClass(SOL);
next.removeClass(OUT);
next.addClass(SIR);
next.closest(".pages").height(next.height());
}
}
}
}
function setupPages() {
$(".pages").children(".page:not(:first-child)").addClass(SOR);
$(".pages").children(".page:first-child").addClass(SIL);
$(".pages").each(function (n, el) {
$(el).height($(el).children(".page:first-child").height())
});
}
setupPages()
};