Skip to content

Commit 3714c6c

Browse files
added paging functions
1 parent bca5d9d commit 3714c6c

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

lib/woql.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ function WOQLQuery(query, results){
3636
this.results = (results ? results : false);
3737
this.chain_ended = false;
3838
this.contains_update = false;
39+
//operators which preserve global paging
40+
this.paging_transitive_properties = ['select', 'from', 'start', 'when', 'opt', 'limit'];
3941
return this;
4042
}
4143

@@ -61,6 +63,123 @@ WOQLQuery.prototype.last = function(){
6163
return this;
6264
}
6365

66+
WOQLQuery.prototype.isPaged = function(q){
67+
q = (q ? q : this.query);
68+
for (const prop of Object.keys(q)) {
69+
if(prop == "limit") return true;
70+
else if(paging_transitive_properties.indexOf(prop) !== -1){
71+
return this.isPaged(q[prop][q[prop].length-1])
72+
}
73+
}
74+
}
75+
76+
WOQLQuery.prototype.getPagingProperty = function(pageprop, q){
77+
q = (q ? q : this.query);
78+
for (const prop of Object.keys(q)) {
79+
if(prop == pageprop) return q[prop][0];
80+
else if(paging_transitive_properties.indexOf(prop) !== -1){
81+
var val = this.getPagingProperty(pageprop, q[prop][q[prop].length-1]);
82+
if(typeof val != "undefined"){
83+
return val;
84+
}
85+
}
86+
}
87+
}
88+
89+
WOQLQuery.prototype.setPagingProperty = function(pageprop, val, q){
90+
q = (q ? q : this.query);
91+
for (const prop of Object.keys(q)) {
92+
if(prop == pageprop) {
93+
q[prop][0] = val;
94+
}
95+
else if(paging_transitive_properties.indexOf(prop) !== -1){
96+
this.setPagingProperty(pageprop, val, q[prop][q[prop].length-1]);
97+
}
98+
}
99+
return this;
100+
}
101+
102+
WOQLQuery.prototype.addStart = function(val, q){
103+
q = (q ? q : this.query);
104+
for (const prop of Object.keys(q)) {
105+
if(prop == limit) {
106+
var nval = { "start": [0, q[prop][1]]}
107+
q[prop][1] = nval;
108+
}
109+
else if(paging_transitive_properties.indexOf(prop) !== -1){
110+
this.addStart(val, q[prop][q[prop].length-1]);
111+
}
112+
}
113+
return this;
114+
}
115+
116+
117+
118+
WOQLQuery.prototype.getLimit = function(){
119+
return this.getPagingProperty("limit");
120+
}
121+
122+
WOQLQuery.prototype.getPage = function(){
123+
if(this.isPaged()){
124+
var psize = this.getLimit();
125+
if(this.hasStart()){
126+
var s = this.getStart();
127+
return ((s % psize) + 1);
128+
}
129+
else return 1;
130+
}
131+
else return false;
132+
}
133+
134+
WOQLQuery.prototype.setPage = function(pagenum){
135+
var pstart = (this.getLimit() * (pagenum - 1));
136+
if(this.hasStart()){
137+
this.setStart(pstart);
138+
}
139+
else {
140+
this.addStart(pstart);
141+
}
142+
return this;
143+
}
144+
145+
146+
WOQLQuery.prototype.hasStart = function(){
147+
return (typeof this.getPagingProperty("start") != "undefined");
148+
}
149+
150+
WOQLQuery.prototype.getStart = function(){
151+
return this.getPagingProperty("start");
152+
}
153+
154+
WOQLQuery.prototype.nextPage = function(){
155+
return this.setPage(this.getPage() + 1);
156+
}
157+
158+
WOQLQuery.prototype.firstPage = function(){
159+
return this.setPage(1);
160+
}
161+
162+
WOQLQuery.prototype.previousPage = function(){
163+
const npage = this.getPage() - 1;
164+
if(npage > 0) this.setPage(npage);
165+
return this;
166+
}
167+
168+
WOQLQuery.prototype.setStart = function(start){
169+
return this.setPagingProperty("start", start);
170+
}
171+
172+
WOQLQuery.prototype.setPageSize = function(size){
173+
this.setPagingProperty("limit", size);
174+
if(this.hasStart()){
175+
this.setStart(0);
176+
}
177+
else {
178+
this.addStart(0);
179+
}
180+
return this;
181+
}
182+
64183
/*
65184
* Called to indicate an update that is the last chainable element in a query
66185
*/
@@ -431,6 +550,7 @@ WOQLResult.prototype.getBindings = function(){
431550
return this.bindings;
432551
}
433552

553+
434554
function WOQLViewer(client, config){
435555
this.client = client;
436556
this.config = config;

0 commit comments

Comments
 (0)