This repository was archived by the owner on Apr 27, 2020. It is now read-only.
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
182 lines (157 loc) · 6.76 KB
/
app.ts
File metadata and controls
182 lines (157 loc) · 6.76 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
//#region Global Variables
let resizeTimer = 50;
let previousCursor: number[];
let previousScale: number;
//#endregion Global Variables
//#region AppLogic
//#region API
async function getRecordCount() : Promise<number> {
const response = await fetch('http://localhost:2050/recordCount');
return await response.json();
}
async function getColumnNames() : Promise<string[]>{
const response = await fetch('http://localhost:2050/columns');
return await response.json();
}
async function getRecords(fromID: number, toID: number): Promise<string[][]> {
const response = await fetch(`http://localhost:2050/records?from=${(fromID)}&to=${(toID)}`);
return await response.json();
}
//#endregion API
//#region Data Loading methods
async function placeRecords(fromID: number, toID: number): Promise<number[]> {
const records = await getRecords(fromID, toID)
let appendable = '';
for (const record of records) {
appendable += `<tr id="table-row-${record[0]}">`;
for (const column of record) {
appendable += `<td align="center">${column}</td>`;
}
appendable += '</tr>';
}
$("#wrapper-table-content-body").empty();
$("#wrapper-table-content-body").append(appendable);
return [fromID, toID];
}
async function placeRecordsFromCursor(cursor: number[]): Promise<number[]> {
cursor = cursor.sort((a,b) => {return a-b});
return await placeRecords(cursor[0], cursor[1]);
}
//#endregion Data Loading methods
//#region Handlers
async function getPageContent(fromID: number, toID: number): Promise<number[]> {
let appendable = "";
const columns = await getColumnNames();
for (const column of columns) {
appendable += `<th align="center">${column}</th>`;
}
$("#wrapper-table-header-row").empty();
$("#wrapper-table-header-row").append(appendable);
return await placeRecords(fromID, toID);
}
function toNumber(input: string | number, parseAsInt: boolean = true) : number {
switch (typeof input) {
case ('string'):
if (parseAsInt == true) {
return parseInt(input as string);
}
return parseFloat(input as string);
case ("number"):
return input as number;
default:
return 0;
}
}
function calculateToId(fromId: number): number {
const possibleRecords = Math.floor((window.innerHeight - ($("#form-content").innerHeight() as number)) / 37);
const possibleId = fromId + possibleRecords;
let recordDisplayOffset = 0;
if (window.innerHeight <= 646) {
recordDisplayOffset = 0
} else if (window.innerHeight <= 969) {
recordDisplayOffset = 1;
} else if (window.innerHeight <= 1938) {
recordDisplayOffset = 3
} else {
recordDisplayOffset = 15
}
return recordDisplayOffset + possibleId;
}
function nextPageResize(previousCursor: number[]): number {
const fromID = toNumber(previousCursor.sort((a, b) => {return a - b})[0]);
const toID = toNumber(previousCursor.sort((a, b) => {return a - b})[1]);
const documentHeight = $(window).innerHeight() as number - ($(`#table-row-${fromID}`).height() as number);
for (let i = fromID; i <= toID; i++) {
const elementHeightOffset = ($(`#table-row-${i}`).offset() as JQueryCoordinates).top;
if (elementHeightOffset < documentHeight) continue;
return i;
}
return toID;
}
function previousPageResize(previousCursor: number[]): number[] {
const toId = calculateToId(previousCursor[0] - (nextPageResize(previousCursor) - previousCursor[0]));
return [previousCursor[0] - (nextPageResize(previousCursor) - previousCursor[0]), toId];
}
//#endregion Handlers
//#endregion AppLogic
window.onload = async () => {
previousCursor = await getPageContent(0, calculateToId(0));
$("#previous-page").click(async () => {
const recordCount = await getRecordCount();
previousCursor = previousPageResize(previousCursor);
let fromId = previousCursor[0] >= 0 ? previousCursor[0] : 0;
const possibleStep = calculateToId(fromId) - fromId;
let toId = (previousCursor[0] >= 0 ? previousCursor[1] : possibleStep);
fromId = fromId == recordCount - 1 ? fromId - possibleStep : fromId;
toId = toId <= recordCount - 1 ? toId : recordCount - 1;
previousCursor = await placeRecords(fromId, toId);
});
$("#next-page").click(async () => {
const recordCount = await getRecordCount();
const fromId = nextPageResize(previousCursor);
const possibleStep = calculateToId(fromId) - fromId;
if (fromId <= recordCount - possibleStep - 1) {
const toId = fromId + possibleStep <= recordCount - 1 ? fromId + possibleStep : recordCount - 1;
previousCursor = await placeRecords(fromId, toId);
} else if (fromId <= recordCount - 1) {
previousCursor = await placeRecords(recordCount - 1 - (calculateToId(fromId) - fromId), recordCount - 1);
alert('You reached the last record - which is shown at the bottom of the screen');
} else {
alert('You have reached the end of the list');
}
});
$("#go-to-button").click(async () => {
const recordCount = await getRecordCount();
const fromId = toNumber($("#go-to-index").val() as string, false);
const possibleStep = calculateToId(fromId) - fromId;
if (fromId < 0){
alert('You may only insert Id greater than or equal to 0');
} else {
if (Math.floor(fromId).toString() == fromId.toString() === true) {
if ( fromId > recordCount - possibleStep ) {
alert(`You may not insert a desired Id greater than ${recordCount - possibleStep}`);
} else {
let toId = (fromId) + possibleStep < recordCount ? (fromId) + possibleStep : recordCount - 1;
previousCursor = await placeRecords(fromId, toId);
}
} else {
alert('It seems you are not inserting an integer - please ensure that you are.');
}
}
});
}
window.onresize = () => {
const nextToId = calculateToId(previousCursor[0]);
clearTimeout(resizeTimer);
resizeTimer = setTimeout(async () => {
const recordCount = await getRecordCount();
if (nextToId >= recordCount - 1) {
const fromId = recordCount - 1 - (calculateToId(previousCursor[0]) - previousCursor[0]);
const toId = recordCount - 1;
previousCursor = await placeRecords(fromId, toId);
alert('Note that since you were on the last page, the final record is still at the bottom of your page');
} else {
previousCursor = await placeRecords(previousCursor[0], nextToId)
}
}, 250);
}