Skip to content
Open
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
6e0a6ef
Create basic table with dummy data.
geraldtivatyi Jun 3, 2021
7349923
create basis for interfaces and classes to handle and structure data …
geraldtivatyi Jun 4, 2021
83bd5b0
Merge pull request #1 from geraldtivatyi/foundational-setup
geraldtivatyi Jun 4, 2021
1b69e97
render table records and headings to html
geraldtivatyi Jun 4, 2021
7f89a05
Some code refactoring.
geraldtivatyi Jun 7, 2021
a70d634
Make http request to get data from server.
geraldtivatyi Jun 7, 2021
888df86
Update fetching data from server
geraldtivatyi Jun 7, 2021
59f3d00
Add styling and fix bug
geraldtivatyi Jun 8, 2021
68c6db2
Merge pull request #3 from geraldtivatyi/use-data-from-server
geraldtivatyi Jun 8, 2021
fa380a5
Merge pull request #2 from geraldtivatyi/rendering-to-html
geraldtivatyi Jun 8, 2021
31961b5
Refactor code and add comments
geraldtivatyi Jun 8, 2021
0c5f435
Merge pull request #4 from geraldtivatyi/code-refactoring
geraldtivatyi Jun 8, 2021
2cd6d76
Edit code to meet code style requirements
geraldtivatyi Jun 8, 2021
5c41aaf
Merge pull request #5 from geraldtivatyi/codestyle-javascript-style-g…
geraldtivatyi Jun 8, 2021
0a3474b
fix navigation
geraldtivatyi Jun 9, 2021
d232ff5
fix navigation
geraldtivatyi Jun 9, 2021
8403aae
add filters
geraldtivatyi Jun 10, 2021
4c63762
fix filter bugs
geraldtivatyi Jun 11, 2021
4e32a4f
fix filters and navigation
geraldtivatyi Jun 11, 2021
5b98879
fix form filters
geraldtivatyi Jun 14, 2021
445ed5c
Merge pull request #6 from geraldtivatyi/fix-navigation
geraldtivatyi Jun 14, 2021
7fdc286
Update file names and refactor code
geraldtivatyi Jun 14, 2021
44ea44e
Fix file names and refactor code
geraldtivatyi Jun 14, 2021
e2bad73
Merge pull request #7 from geraldtivatyi/code-refactoring
geraldtivatyi Jun 14, 2021
b8c3324
responsive screen size
geraldtivatyi Jun 14, 2021
c9bb2dd
fix unnecessary calls and rapid firing calls
geraldtivatyi Jun 15, 2021
d62f5e8
New updates
geraldtivatyi Jun 17, 2021
bc0f8c6
Implement promises
geraldtivatyi Jun 17, 2021
d153f62
Implementing promises
geraldtivatyi Jun 18, 2021
bcc469c
Promises and fetch updates
geraldtivatyi Jun 18, 2021
ad82237
Updates
geraldtivatyi Jun 18, 2021
4b6e798
Update promises
geraldtivatyi Jun 20, 2021
40ac37d
Finalise promises
geraldtivatyi Jun 20, 2021
5615095
Updates - code refactoring
geraldtivatyi Jun 20, 2021
b24d3db
Code refactoring
geraldtivatyi Jun 20, 2021
e3ff6f0
Code refactoring
geraldtivatyi Jun 21, 2021
7b8f169
Final code refactoring
geraldtivatyi Jun 21, 2021
5178b2d
Merge pull request #8 from geraldtivatyi/new-updates
geraldtivatyi Jun 21, 2021
8d910b0
Updates to js files
geraldtivatyi Jun 21, 2021
8916976
Remove .js and .js.map files
geraldtivatyi Jun 21, 2021
5526bd2
Remove empty file
geraldtivatyi Jun 21, 2021
4060dce
Fixing code - comments
geraldtivatyi Jun 22, 2021
bf30acf
Fixing code - comments from code review
geraldtivatyi Jun 23, 2021
f63224e
Update code
geraldtivatyi Jun 23, 2021
8dbaa7b
Reworked left and right arrow functions.
geraldtivatyi Jun 23, 2021
e71702c
Remove js import statements and
geraldtivatyi Jun 28, 2021
8883bc6
Fix gitignore file
geraldtivatyi Jun 28, 2021
2ca2f32
Updates
geraldtivatyi Jun 29, 2021
2115eb4
Updates
geraldtivatyi Jun 29, 2021
329ab1a
Updates
geraldtivatyi Jun 30, 2021
1ab3ae1
Updates
geraldtivatyi Jul 1, 2021
7378b6b
Updates
geraldtivatyi Jul 1, 2021
59019f0
Updates
geraldtivatyi Jul 2, 2021
ebf4a49
Updates
geraldtivatyi Jul 2, 2021
be7b34a
Final updates
geraldtivatyi Jul 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/node_modules
/classes/*.js
/classes/*.js.map
/interfaces/*.js
/interfaces/*.js.map
app.js
app.js.map
309 changes: 309 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
import { TableHeadingString } from "./classes/table-headings.js";
import { RenderTableRows } from "./classes/render-rows.js";
import { RenderTableHeading } from "./classes/render-headings.js";
import { HasFormatMethod } from "./interfaces/has-format-method.js";
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated

let numOfRows = getNumOfRows(); // Call function to get number of rows to render based on current screen size
let totalNumofRecords: number; // Declare public variable to store total number of records from the server

// Accessing form data from front end
let fromIDElement = document.querySelector('#fromID') as HTMLParagraphElement;
let toIDElement = document.querySelector('#toID') as HTMLParagraphElement;
let numofrecords = document.querySelector('#numofrecords') as HTMLParagraphElement;

// Initializing variables for first page of data
let fromID = 1;
let toID = numOfRows;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();

// Create initial page with initial data
createInitialPage(numOfRows);

// Listen for change in window size
let screenHeight = screen.height;
window.addEventListener('resize', function(event) {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
screenHeight = window.innerHeight;

// Change number of rows based on window size
if (screenHeight >= 750) {
numOfRows = 23;
} else if ((screenHeight < 750) && (screenHeight >= 550)) {
numOfRows = 18;
} else if ((screenHeight < 550) && (screenHeight >= 265)) {
numOfRows = 8;
} else if ((screenHeight < 265) && (screenHeight >= 95)) {
numOfRows = 3;
} else if (screenHeight < 95) {
numOfRows = 2;
}
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated

// Recreate grid with new set number of rows
createInitialPage(numOfRows);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
});

// Set a timeout to use for promises when navigating through data
let clickTimeout: ReturnType<typeof setTimeout>;

// When clicking on left arrow
$( "#leftarrow" ).on( "click", function() {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
// Clear the timeout every time if you navigate and you're not at the start ID of all the data
if (fromID != 1) {
clearTimeout(clickTimeout);
}

// Clear form value in front-end when navigating data using the arrows
let startfrom = document.querySelector("#startfrom") as HTMLInputElement;
startfrom.value = "";

if ((fromID < (numOfRows-1)) && (fromID > 1)) { // If the "from" ID is within the IDs at the beginning of the dataset
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
fromID = 1;
toID = numOfRows;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
// Promise.all<string>([headings, records]).then((values) => {
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
} else if (fromID === 1) { // If the "from" ID is 1 then set the values and do not make any backend calls
fromID = 1;
toID = numOfRows;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
} else { // Else, continue creating table with the set IDs
fromID = fromID - (numOfRows-1);
toID = toID - (numOfRows-1);
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated

// Create table using the new set IDs inside a set timeout
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
}
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
});

// Listen for click on right arrow
$( "#rightarrow" ).on( "click", function() {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
// Clear the timeout every time if you navigate and you're not at the end ID of all the data
if (toID != totalNumofRecords) {
clearTimeout(clickTimeout);
}

// Clear form value in front-end when navigating data using the arrows
let startfrom = document.querySelector("#startfrom") as HTMLInputElement;
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
startfrom.value = "";

if ((fromID > (totalNumofRecords-((numOfRows-1)*2)) ) && (fromID < totalNumofRecords) ) { // If the "from" ID is within the IDs at the ending of the dataset
fromID = (totalNumofRecords-(numOfRows-1));
toID = totalNumofRecords;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
} else if (toID === totalNumofRecords) { // If the "to" ID is the ending ID, then set the values and do not make any backend calls
fromID = (totalNumofRecords-(numOfRows-1));
toID = totalNumofRecords;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
} else { // Else, continue creating table with the set IDs
fromID = fromID + (numOfRows-1);
fromIDElement.innerHTML = fromID.toString();
toID = toID + (numOfRows-1);
toIDElement.innerHTML = toID.toString();

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
}
});
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated

// Listen for submission in form and use inputs to request data from backend
$( "#submit" ).on( "click", function() {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
// Clear the timeout every time if you submit on the form
clearTimeout(clickTimeout);

// Clear form value in from end when navigating data using the arrows
let startFromIDElement = document.querySelector('#startfrom') as HTMLInputElement;
let startFrom = startFromIDElement.valueAsNumber;

// Checks to see if you request invalid values in the form
if (startFrom < 1 || startFrom > (totalNumofRecords-(numOfRows-1))) {
alert("The acceptable range is between 1 and "+(totalNumofRecords-(numOfRows-1)));
return;
} else if (isNaN(startFrom)) {
alert("You have not set a value to submit.");
} else {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
fromID = startFrom;
toID = fromID + (numOfRows-1);
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
}

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
});

// Listen for submission in form and use inputs to request data from backend
$( "#gotostart" ).on( "click", function() {
// Clear the timeout every time if you click to go to start of dataset
clearTimeout(clickTimeout);

// Clear form value in front-end when navigating data using the arrows
let startfrom = document.querySelector("#startfrom") as HTMLInputElement;
startfrom.value = "";

// Set ID values to the start of the dataset
fromID = 1;
toID = numOfRows;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
});


$( "#gotoend" ).on( "click", function() {
// Clear the timeout every time if you click to go to end of dataset
clearTimeout(clickTimeout);

// Clear form value in front-end when navigating data using the arrows
let startfrom = document.querySelector("#startfrom") as HTMLInputElement;
startfrom.value = "";

// Set ID values to the end of the dataset
fromID = totalNumofRecords-(numOfRows-1);
toID = totalNumofRecords;
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function() {
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
});


// Function to create table and render in browser
function generateTable(headingsStr: string, recordsStr: string) {
// Instantiate grid table to append innerHTML
let tble = new RenderTableHeading(document.querySelector('#table') as HTMLDivElement);

// Empty the table in the html
let emptyTable = document.querySelector('#table') as HTMLDivElement;
emptyTable.innerHTML = ""; // clear table to empty html

// Recreate table with new headings and records data
let interfaceHeading: HasFormatMethod; // variable of type interface used in creating table headings
let interfaceRecords: HasFormatMethod; // variable of type interface used in creating table rows
interfaceHeading = new TableHeadingString(headingsStr); // call method to generate string containing table heading element
tble.constructTableHeadings(interfaceHeading); // call method to render table headings element in browser
interfaceRecords = new RenderTableRows(recordsStr); // call method to generate string containing table row elements
}

// Function to target an element in html by its id
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
function el(s: string) {
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
return document.getElementById(s);
}

// Function to create the initial table when loading the page for the first time or when the window size changes
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
function createInitialPage(numOfRows: number) {
// Clear the timeout every time this function is called
clearTimeout(clickTimeout);

// Check if you're at the start or end of the data set, and reset IDs for change in window size
if (fromID > (totalNumofRecords-(numOfRows-1))) {
fromID = totalNumofRecords-(numOfRows-1);
toID = fromID + (numOfRows-1);
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
} else {
fromID = fromID;
toID = fromID + (numOfRows-1);
fromIDElement.innerHTML = fromID.toString();
toIDElement.innerHTML = toID.toString();
}

//Request for and set the total number of records
fetch("/recordCount").then(res => res.text()).then((value) => {
numofrecords.innerHTML = value;
totalNumofRecords = Number(value) - 1;
}).catch(err => console.log(err))
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated

// Create table using the new set IDs inside a set timeout
clickTimeout = setTimeout(function(){
// Use resolved promises to fetch data from backend
let headings = Promise.resolve(fetch("/columns").then(res => res.text()));
let records = Promise.resolve(fetch("/records?from="+fromID+"&to="+toID).then(res => res.text()));
Promise.all<string>([headings, records]).then((values) => {
generateTable(values[0], values[1]);
}).catch(err => console.log(err));
}, 500);
Comment thread
geraldtivatyi marked this conversation as resolved.
Outdated
}

// Function returning number of rows based on window size
function getNumOfRows() {
// Get height of screen
screenHeight = window.innerHeight;

// Checks to return correct value for number of rows
if (screenHeight >= 750) {
return 23;
} else if ((screenHeight < 750) && (screenHeight >= 550)) {
return 18;
} else if ((screenHeight < 550) && (screenHeight >= 265)) {
return 8;
} else if ((screenHeight < 265) && (screenHeight >= 95)) {
return 3;
} else if (screenHeight < 95) {
return 2;
} else {
return 23;
}
}
15 changes: 15 additions & 0 deletions classes/render-headings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HasFormatMethod } from "../interfaces/has-format-method.js";

// Create table headings and render in browser
export class RenderTableHeading {
constructor(private container: HTMLDivElement) {}

constructTableHeadings(element: HasFormatMethod) {
let div = document.createElement('div');
div.innerHTML = element.internalFormat();
div.className = "tablecell";
div.style.gridTemplateColumns = "repeat("+element.arrayLength()+", 1fr)";

this.container.append(div);
}
}
45 changes: 45 additions & 0 deletions classes/render-rows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { HasFormatMethod } from "../interfaces/has-format-method.js";

// generate html string for table rows data and render in browser
export class RenderTableRows implements HasFormatMethod {
private returnStr = "";
private arrLength = 0;

constructor( recordsStr: string ){
let table = document.querySelector('#table') as HTMLDivElement; // Target div element with ID table
let navigation = document.querySelector('#navigation') as HTMLDivElement; // Target div element with ID navigation
let myArr = JSON.parse(recordsStr); // Parse the string to create array of the data

// Edit styling of the table and navigation bar
table.style.display = "grid";
table.style.gridTemplateRows = "repeat(auto-fill, "+(100/(myArr.length+2))+"%)";
navigation.style.height = (100/(myArr.length+2))+"%";

// Create innerHTML text to be rendered to front-end in the table div
for(let i=0;i<myArr.length;i++) {
for(let j=0;j<myArr[i].length;j++) {
this.arrLength = myArr[i].length;
this.returnStr +=
"<div><p>"+myArr[i][j]+"</p></div>";
}

// Append innerHTML text to div element in front-end
let div = document.createElement('div');
div.innerHTML = this.returnStr;
div.className = "tablecell";
div.style.gridTemplateColumns = "repeat("+this.arrLength+", 1fr)";
table.append(div);
this.returnStr = "";
}
}

// Returns length of array of headings to get number of columns necessary to render
arrayLength() {
return this.arrLength;
}

// Returns formatted string to be placed in html
internalFormat() {
return this.returnStr;
}
}
Loading