-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwoqlPaging.js
148 lines (130 loc) · 3.71 KB
/
woqlPaging.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
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
/* eslint-disable consistent-return */
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-unused-vars */
const WOQLQuery = require('../query/woqlCore');
const UTILS = require('../utils');
/**
* Functions to manipulate and check the paging related properties of a query
*/
WOQLQuery.prototype.getLimit = function () {
return this.getPagingProperty('limit');
};
WOQLQuery.prototype.setLimit = function (l) {
return this.setPagingProperty('limit', l);
};
WOQLQuery.prototype.isPaged = function (q) {
q = (q || this.query);
for (const prop of Object.keys(q)) {
if (prop === 'limit') return true;
if (this.paging_transitive_properties.indexOf(prop) !== -1) {
return this.isPaged(q[prop][q[prop].length - 1]);
}
}
return false;
};
WOQLQuery.prototype.getPage = function () {
if (this.isPaged()) {
const psize = this.getLimit();
if (this.hasStart()) {
const s = this.getStart();
// eslint-disable-next-line radix
return (parseInt(s / psize) + 1);
}
return 1;
}
return false;
};
WOQLQuery.prototype.setPage = function (pagenum) {
const pstart = (this.getLimit() * (pagenum - 1));
if (this.hasStart()) {
this.setStart(pstart);
} else {
this.addStart(pstart);
}
return this;
};
WOQLQuery.prototype.nextPage = function () {
return this.setPage(this.getPage() + 1);
};
WOQLQuery.prototype.firstPage = function () {
return this.setPage(1);
};
WOQLQuery.prototype.previousPage = function () {
const npage = this.getPage() - 1;
if (npage > 0) this.setPage(npage);
return this;
};
WOQLQuery.prototype.setPageSize = function (size) {
this.setPagingProperty('limit', size);
if (this.hasStart()) {
this.setStart(0);
} else {
this.addStart(0);
}
return this;
};
WOQLQuery.prototype.hasSelect = function () {
return (!!this.getPagingProperty('select'));
};
WOQLQuery.prototype.getSelectVariables = function (q) {
q = (q || this.query);
for (const prop of Object.keys(q)) {
if (prop === 'select') {
const vars = q[prop].slice(0, q[prop].length - 1);
return vars;
}
if (this.paging_transitive_properties.indexOf(prop) !== -1) {
const val = this.getSelectVariables(q[prop][q[prop].length - 1]);
if (typeof val !== 'undefined') {
return val;
}
}
}
};
WOQLQuery.prototype.hasStart = function () {
return (typeof this.getPagingProperty('start') !== 'undefined');
};
WOQLQuery.prototype.getStart = function () {
return this.getPagingProperty('start');
};
WOQLQuery.prototype.setStart = function (start) {
return this.setPagingProperty('start', start);
};
WOQLQuery.prototype.addStart = function (s) {
if (this.hasStart()) this.setStart(s);
else {
const nq = { start: [s, this.query] };
this.query = nq;
}
return this;
};
/**
* Returns the value of one of the 'paging' related properties (limit, start,...)
*/
WOQLQuery.prototype.getPagingProperty = function (pageprop, q) {
q = (q || this.query);
for (const prop of Object.keys(q)) {
if (prop === pageprop) return q[prop][0];
if (this.paging_transitive_properties.indexOf(prop) !== -1) {
const val = this.getPagingProperty(pageprop, q[prop][q[prop].length - 1]);
if (typeof val !== 'undefined') {
return val;
}
}
}
};
/**
* Sets the value of one of the paging_transitive_properties properties
*/
WOQLQuery.prototype.setPagingProperty = function (pageprop, val, q) {
q = (q || this.query);
for (const prop of Object.keys(q)) {
if (prop === pageprop) {
q[prop][0] = val;
} else if (this.paging_transitive_properties.indexOf(prop) !== -1) {
this.setPagingProperty(pageprop, val, q[prop][q[prop].length - 1]);
}
}
return this;
};