forked from IMQS/onboard-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-service.ts
More file actions
118 lines (104 loc) · 3.99 KB
/
api-service.ts
File metadata and controls
118 lines (104 loc) · 3.99 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class ApiService {
private url = "http://localhost:2050"; // Backend server URL
private columnNames: string[] = []; // The names of all the columns in order
private totalRecords = 0; // The total number of records
// A 2-D array of the records retrieved
// The 1st dimension is the ID that is the index of the record
// The 2nd dimension is the ordered respective column values of the record
private dataRecords: string[][] = [[]];
private topRecordIndex = 0; // The ID of the first record in the grid displayed
private gridSize: number; // The size of the grid to display
constructor(numberOfRecords: number) {
this.gridSize = numberOfRecords; // Initialise the class with the available size of the grid
// Send AJAX request to server to retrieve the all the column names to display
$.ajax({
"url": this.url + "/columns",
"success": data => {
this.columnNames = JSON.parse(data);
}
});
}
recordCount(): Promise<void> {
return new Promise((resolve, reject) => {
$.ajax({
"url": this.url + "/recordCount",
"success": data => {
this.totalRecords = Number(data);
resolve();
},
"error": (e) => {
reject(e); // Log the error for debugging purposes
}
});
});
}
getCurrentRecords(): Promise<string[][]> {
let toId: number;
// Calculate the "to" parameter for the records to collect
if (this.topRecordIndex + (this.gridSize - 1) > (this.totalRecords - 1)) {
toId = this.totalRecords - 1;
this.topRecordIndex = this.totalRecords - this.gridSize;
} else {
toId = this.topRecordIndex + (this.gridSize - 1);
}
return new Promise((resolve, reject) => {
$.ajax({
"url": this.url + "/records",
"data": {
"from": this.topRecordIndex.toString(),
"to": toId.toString()
},
"success": data => {
resolve(this.dataRecords = JSON.parse(data)); // Store result privately
},
"error": (e) => {
reject(e); // Log the error for debugging purposes
}
});
});
}
previous(): Promise<string[][]> | null {
if (this.topRecordIndex === 0) {
return null;
}
// Check if there are records to the left of the top index of the grid before sending a request
if (this.topRecordIndex - (this.gridSize - 1) > -1) {
this.topRecordIndex -= this.gridSize;
} else {
this.topRecordIndex = 0;
}
return this.getCurrentRecords();
}
next(): Promise<string[][]> | null {
// Check if there are records to the right of the top index of the grid before sending a request
if (this.topRecordIndex + (this.gridSize - 1) < (this.totalRecords - 1)) {
this.topRecordIndex += this.gridSize;
return this.getCurrentRecords();
}
return null;
}
searchRecord(id: string): Promise<void> {
this.topRecordIndex = Number(id); // The searched value will always be the top record's ID
return this.getCurrentRecords().then((dataRecords) => {
for (let record of dataRecords) {
if (record[0] == id) {
record.unshift('searched');
}
}
}).catch((e) => {
console.log(e);
});
}
getColumnNames(): string[] {
return this.columnNames;
}
getTotalRecords(): number {
return this.totalRecords;
}
setGridSize(gridSize: number) {
this.gridSize = gridSize;
}
getDataRecords(): string[][] {
return this.dataRecords;
}
}