forked from IMQS/onboard-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
213 lines (194 loc) · 5.75 KB
/
app.ts
File metadata and controls
213 lines (194 loc) · 5.75 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
let numColumns: number;
let columns: any;
let recordCount: number;
let startIndex = 0; // Default value = 0;
let tableRecordCount = 20; // Default value = 20;
let timer: number;
// Canvas:
let canvas = document.createElement("div");
// Table:
let table = document.createElement("table");
let tbdy = document.createElement("tbody");
let thead = document.createElement("thead");
table.appendChild(thead);
table.appendChild(tbdy);
// Buttons and fields:
let searchbtn = document.createElement("button");
searchbtn.innerText = "Search";
let nextbtn = document.createElement("button");
nextbtn.innerText = "Next Page";
let prevbtn = document.createElement("button");
prevbtn.innerText = "Previous Page";
let fromField = document.createElement("input");
fromField.placeholder = "from";
// Append fields and buttons to canvas
canvas.appendChild(fromField);
canvas.appendChild(searchbtn);
canvas.appendChild(prevbtn);
canvas.appendChild(nextbtn);
canvas.appendChild(table);
// on clicks:
searchbtn.onclick = function () {
let from = parseInt(fromField.value, 10);
// validate field:
if (validate(from)) {
let end = tableRecordCount - 1;
startIndex = from;
if (recordCount < startIndex + tableRecordCount) {
startIndex = recordCount - tableRecordCount;
}
getRecords(startIndex, startIndex + end);
}
};
nextbtn.onclick = function () {
let end = tableRecordCount - 1;
if (recordCount > startIndex + tableRecordCount) {
startIndex += tableRecordCount;
if (recordCount < startIndex + tableRecordCount) {
startIndex = recordCount - tableRecordCount;
}
getRecords(startIndex, startIndex + end);
}
};
prevbtn.onclick = function () {
startIndex = startIndex >= tableRecordCount ? startIndex -= tableRecordCount : startIndex = 0;
getRecords(startIndex, startIndex + tableRecordCount - 1);
};
// When the window page loads for the first time:
window.onload = () => {
$("body").append(canvas);
initiate();
}
// Window resizing (using a debouncing method):
window.onresize = () => {
const time = 100;
clearInterval(timer);
timer = setInterval(function () {
clearInterval(timer);
adjustTableRecordCount();
let end = tableRecordCount - 1;
if (recordCount < startIndex + tableRecordCount) {
startIndex = recordCount - tableRecordCount;
}
getRecords(startIndex, startIndex + end);
}, time);
}
/**
* Initiates the HTTP requests to obtain the records and column values necessary for the table.
*/
const initiate = () => {
// get number of records:
$.ajax({
url: "http://localhost:2050/recordCount",
success: function (result) {
recordCount = parseInt(JSON.parse(result), 10);
// get columns:
$.ajax({
url: "http://localhost:2050/columns",
success: function (result) {
columns = JSON.parse(result);
numColumns = getSize(columns);
// generate and append the headings to the table:
generateHeadings(columns);
// get first page of records and display them:
adjustTableRecordCount();
getRecords(0, tableRecordCount - 1);
},
error: function (err) {
$("body").text("Error: " + err.status + " " + err.statusText);
}
});
},
error: function (err) {
$("body").text("Error: " + err.status + " " + err.statusText);
}
});
}
/**
* Populates the table with data, distrubuted evenly.
* @param data The data records retrieved from the server, structured as a 2D object array.
* @param columns An object array containing the column heading values.
*/
const fillTable = (data: any) => {
$("tbody").empty();
for (let i = 0; i < tableRecordCount; i++) {
let tr = document.createElement("tr");
for (let j = 0; j < numColumns; j++) {
let td = document.createElement("td");
td.appendChild(document.createTextNode(data[i][j]));
tr.appendChild(td);
}
tbdy.appendChild(tr);
}
}
/**
* Finds the number of entries within a data set.
* @param data An array of data, type is unknown (any).
*/
const getSize = (data: any) => {
let i = 0;
for (let entry in data) {
i++;
}
return i;
}
/**
* Sends out an HTTP request to retrieve data records, coupled with generating a table to display the records.
* @param from The ID value from which to start searching.
* @param to The ID value from which to stop searching.
*/
const getRecords = (from: number, to: number) => {
$.ajax({
url: "http://localhost:2050/records",
data: { from: from, to: to },
success: function (result) {
let data = JSON.parse(result);
fillTable(data);
},
error: function (err) {
$("body").text("Error: " + err.status + " " + err.statusText);
}
});
}
/**
* Generates and returns a "thead" object containing column headings.
* @param columns An object array containing the column heading values.
*/
const generateHeadings = (columns: any) => {
let tr = document.createElement("tr");
for (let j = 0; j < numColumns; j++) {
let td = document.createElement("td");
td.appendChild(document.createTextNode(columns[j]));
tr.appendChild(td);
}
thead.appendChild(tr);
}
/**
* Validates that the field value is a number.
* @param from The ID value from which to start searching.
*/
const validate = (from: number) => {
if (isNaN(from)) {
alert('"From" field does not have a number value.');
return false;
} else if (from < 0) {
alert('"From" value cannot be negative.');
return false;
} else if (from > recordCount - 1) {
alert('"From" value exceeds the record count.');
return false;
}
return true;
}
/**
* Adjust the number of records shown in the table according to the window size.
*/
const adjustTableRecordCount = () => {
let height = window.innerHeight;
let fontSize = getComputedStyle(document.documentElement).fontSize + "";
let rowHeight = parseFloat(fontSize) * 2.5;
if (rowHeight !== undefined) {
const count = tableRecordCount = Math.trunc(height / rowHeight) - 2;
tableRecordCount = count >= 1 ? count : 1;
}
}