-
Notifications
You must be signed in to change notification settings - Fork 35
Onboarding submission #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pierrehenrinortje
wants to merge
11
commits into
IMQS:master
Choose a base branch
from
pierrehenrinortje:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
db05582
Update README.md
d1a4ab0
On window resize the table updates accordingly, order of data is not …
pierrehenrinortje be18911
changed window resize adjustment
pierrehenrinortje 43c0c67
Search of final page of records no longer displays blank cells
pierrehenrinortje 4b2adee
debounced the window resize to limit network traffic
pierrehenrinortje 7278168
Improved table responsiveness, fixed table cell resize issues
pierrehenrinortje 163d996
Code review submission
pierrehenrinortje f59346a
fixed tab indents
pierrehenrinortje 1afa832
Reworked the window onload function
pierrehenrinortje 0dbb763
Minor code fixes and improvements
pierrehenrinortje 047f256
Table is no longer removed when refreshing table
pierrehenrinortje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| input, button { | ||
| display: inline; | ||
| padding: 0.5rem; | ||
| } | ||
|
|
||
| #canvas { | ||
| width: 100vw; | ||
| height: 100vh; | ||
| } | ||
|
|
||
| table { | ||
| border-collapse: collapse; | ||
| width: 100%; | ||
| table-layout: fixed; | ||
| overflow: hidden; | ||
| font-size: 0.8rem; | ||
| } | ||
|
|
||
| td { | ||
| width: 9%; | ||
| height: 2rem; | ||
| padding: 0.2rem; | ||
| border-left: 1px darkgrey solid; | ||
| } | ||
|
|
||
| tr:hover { | ||
| background-color: lightgrey; | ||
| } | ||
|
|
||
| tr { | ||
| border: 1px darkgrey solid; | ||
| } | ||
|
|
||
| html { | ||
| overflow: hidden; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "onboard-javascript", | ||
| "version": "1.0.0", | ||
| "description": "Onboarding", | ||
| "main": "index.js", | ||
| "dependencies": { | ||
| "jquery": "^3.4.1" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls include typescript as an npm package, rather than with apt. The idea is that someone should be able to clone this repo, run npm install and have everything they need, rather than running into the error when they try to run tsc. |
||
| }, | ||
| "devDependencies": {}, | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "build": "tsc" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/pierrehenrinortje/onboard-javascript.git" | ||
| }, | ||
| "author": "pierre", | ||
| "license": "ISC", | ||
| "bugs": { | ||
| "url": "https://github.com/pierrehenrinortje/onboard-javascript/issues" | ||
| }, | ||
| "homepage": "https://github.com/pierrehenrinortje/onboard-javascript#readme" | ||
| } | ||
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a mild issue with the entirety of your logic living in this onload event handler. Especially considering that you declare another event callback (namely window.onresize) within this callback.
My issue here is primarily one of separation of concerns, you want all these sections of your implementation to interact with both the DOM and other sections of your source code in a coherent and cohesive fashion - for the sake of clarity, some other interests, but primarily for the developer that comes after you.
However to change this would require you to think about how you will manage your other data fields like numColumns, columns etc. I know that in varsity they taught us to shy away from global variables, but don't be afraid, considering you are here embracing procedural programming :)
Also remember Fritz's complaint about the spacing in your IDE being changed to use tab sizes rather than spaces.