alessandro trinca tornidor
commited on
Commit
·
a05c9de
1
Parent(s):
db1a318
feat: add a basic js frontend
Browse files- static/index.css +78 -0
- static/index.html +32 -0
- static/index.js +129 -0
static/index.css
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
h1 {
|
| 2 |
+
font-size: 24px;
|
| 3 |
+
margin-left: 10px;
|
| 4 |
+
margin-right: 10px;
|
| 5 |
+
margin-block-start: 5px;
|
| 6 |
+
margin-block-end: 5px;
|
| 7 |
+
}
|
| 8 |
+
.display-none {
|
| 9 |
+
display: none;
|
| 10 |
+
}
|
| 11 |
+
.display-block {
|
| 12 |
+
display: block;
|
| 13 |
+
}
|
| 14 |
+
.underlinedDarkTurquoise {
|
| 15 |
+
text-decoration: underline;
|
| 16 |
+
text-decoration-color: darkturquoise;
|
| 17 |
+
text-decoration-style: wavy;
|
| 18 |
+
}
|
| 19 |
+
.underlinedBlue {
|
| 20 |
+
color: blue;
|
| 21 |
+
text-decoration: underline;
|
| 22 |
+
text-decoration-color: blue;
|
| 23 |
+
}
|
| 24 |
+
.underlinedDarkViolet {
|
| 25 |
+
color: darkviolet;
|
| 26 |
+
text-decoration: underline;
|
| 27 |
+
text-decoration-color: darkviolet;
|
| 28 |
+
}
|
| 29 |
+
.display-flex {
|
| 30 |
+
display: flex;
|
| 31 |
+
}
|
| 32 |
+
.col-flex30 {
|
| 33 |
+
flex: 30%;
|
| 34 |
+
}
|
| 35 |
+
.col-flex50 {
|
| 36 |
+
flex: 50%;
|
| 37 |
+
}
|
| 38 |
+
.border-green {
|
| 39 |
+
border: 1px solid green;
|
| 40 |
+
}
|
| 41 |
+
.border-blue {
|
| 42 |
+
border: 1px solid blue;
|
| 43 |
+
}
|
| 44 |
+
.border-black {
|
| 45 |
+
border: 1px solid black;
|
| 46 |
+
}
|
| 47 |
+
.padding10px {
|
| 48 |
+
padding: 10px;
|
| 49 |
+
}
|
| 50 |
+
.margin10px {
|
| 51 |
+
margin: 10px;
|
| 52 |
+
}
|
| 53 |
+
.margin5px-left {
|
| 54 |
+
margin-left: 10px;
|
| 55 |
+
}
|
| 56 |
+
.margin5px-right {
|
| 57 |
+
margin-right: 10px;
|
| 58 |
+
}
|
| 59 |
+
.background-color-lightgray {
|
| 60 |
+
background-color: lightgray;
|
| 61 |
+
}
|
| 62 |
+
.background-color-whitesmoke {
|
| 63 |
+
background-color: whitesmoke;
|
| 64 |
+
}
|
| 65 |
+
.max-height-90vh {
|
| 66 |
+
max-height: 90vh;
|
| 67 |
+
}
|
| 68 |
+
.max-height-85vh {
|
| 69 |
+
max-height: 85vh;
|
| 70 |
+
}
|
| 71 |
+
.overflow-hidden {
|
| 72 |
+
/* needed for scroll bar*/
|
| 73 |
+
overflow: hidden;
|
| 74 |
+
}
|
| 75 |
+
.overflow-auto {
|
| 76 |
+
/* needed for scroll bar*/
|
| 77 |
+
overflow: auto;
|
| 78 |
+
}
|
static/index.html
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Word Frequency Statistics</title>
|
| 7 |
+
<link rel="stylesheet" href="/static/index.css">
|
| 8 |
+
<script src="/static/index.js"></script>
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<h1 class="h1">Word Frequency Statistics in text</h1>
|
| 12 |
+
<div class="margin10px">
|
| 13 |
+
<button id="btn4-getWordFrequency" aria-label="btn4-getWordFrequency" onclick="getWordFrequency()" type="submit">get words frequency</button>
|
| 14 |
+
<label for="id-input-file-selector" class="margin5px-left">Text file selector</label>
|
| 15 |
+
<input type="file" onchange="previewFile()" id="id-input-file-selector" aria-label="id-input-file-selector" />
|
| 16 |
+
</div>
|
| 17 |
+
<div id="id-container-row-global-editor-frequency" class="display-flex">
|
| 18 |
+
<div id="id-col1-editor" class="col-flex50 border-black padding10px margin10px overflow-hidden background-color-lightgray">
|
| 19 |
+
<span>Text Editor</span>
|
| 20 |
+
<div id="editor" contenteditable="true" class="max-height-85vh overflow-auto background-color-whitesmoke" aria-label="editor">
|
| 21 |
+
Hi there, how are you? There are some pasties for you. Can you give me also... Take a pasty from the table there!
|
| 22 |
+
</div>
|
| 23 |
+
</div>
|
| 24 |
+
<div id="id-col2-word-frequency" class="col-flex50 border-blue padding10px margin10px overflow-hidden background-color-lightgray">
|
| 25 |
+
<span>Word Frequency Table</span>
|
| 26 |
+
<span id="waiting-for-be" class="display-none">waiting for backend response...</span>
|
| 27 |
+
<span id="waiting-for-be-error" class="display-none">Error!</span>
|
| 28 |
+
<div id="words-frequency" class="max-height-85vh overflow-auto background-color-whitesmoke" aria-label="words-frequency"></div>
|
| 29 |
+
</div>
|
| 30 |
+
</div>
|
| 31 |
+
</body>
|
| 32 |
+
</html>
|
static/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
function previewFile() {
|
| 3 |
+
const editor = document.getElementById("editor");
|
| 4 |
+
const [file] = document.querySelector("input[type=file]").files;
|
| 5 |
+
const reader = new FileReader();
|
| 6 |
+
reader.addEventListener("load", () => {
|
| 7 |
+
// this will then display a text file
|
| 8 |
+
editor.innerText = reader.result;
|
| 9 |
+
}, false
|
| 10 |
+
);
|
| 11 |
+
if (file) {
|
| 12 |
+
reader.readAsText(file);
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
function setCaret(line, offsetColumn, nTotalRows, negativeOffsetPerc=0.12) {
|
| 16 |
+
let editorElement = document.getElementById("editor");
|
| 17 |
+
let validChildNodes = []
|
| 18 |
+
// use a for loop because of better performance
|
| 19 |
+
for (let i=0; i < editorElement.childNodes.length; i++) {
|
| 20 |
+
let childNode = editorElement.childNodes[i]
|
| 21 |
+
if (childNode.innerHTML !== "") {
|
| 22 |
+
validChildNodes.push(childNode)
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
let rng = document.createRange();
|
| 26 |
+
let sel = window.getSelection();
|
| 27 |
+
let col0 = offsetColumn[0]
|
| 28 |
+
let col1 = offsetColumn[1]
|
| 29 |
+
rng.setStart(validChildNodes[line], col0);
|
| 30 |
+
rng.setEnd(validChildNodes[line], col1)
|
| 31 |
+
sel.removeAllRanges();
|
| 32 |
+
sel.addRange(rng);
|
| 33 |
+
editorElement.focus();
|
| 34 |
+
// try to scroll div to row... font-size on div is 12px
|
| 35 |
+
let scrollHeight = parseFloat(editorElement.scrollHeight, 10)
|
| 36 |
+
console.log("setCaret::updatedOffsetPosition:scroll to line/height position/int height position:", line, editorElement.scrollHeight, scrollHeight, "#")
|
| 37 |
+
let offsetToScrollPerc = line / nTotalRows
|
| 38 |
+
let offsetToScroll = scrollHeight * offsetToScrollPerc
|
| 39 |
+
// if already at the end of the page, don't scroll anymore to avoid missing words in the upper side of the viewport
|
| 40 |
+
if (offsetToScrollPerc < (1-negativeOffsetPerc)) {
|
| 41 |
+
offsetToScroll -= offsetToScroll*negativeOffsetPerc
|
| 42 |
+
}
|
| 43 |
+
let offsetToScrollInt = parseInt(offsetToScroll, 10)
|
| 44 |
+
console.log("setCaret::offsetToScroll:", offsetToScrollInt, "|", offsetToScroll, "#")
|
| 45 |
+
editorElement.scrollTo(0, offsetToScrollInt)
|
| 46 |
+
}
|
| 47 |
+
const setElementCssClass = (elementId, currentClass) => {
|
| 48 |
+
let spanWaitingFor = document.getElementById(elementId)
|
| 49 |
+
spanWaitingFor.setAttribute("class", currentClass)
|
| 50 |
+
}
|
| 51 |
+
const getWordFrequency = async () => {
|
| 52 |
+
let text = document.getElementById("editor")
|
| 53 |
+
// replace repeated newlines to prepare setCaret() use
|
| 54 |
+
text.innerText = text.innerText.replace(/[\r\n]+/g, '\n')
|
| 55 |
+
let bodyRequest = {"text": text.innerText}
|
| 56 |
+
setElementCssClass("waiting-for-be-error", "display-none")
|
| 57 |
+
setElementCssClass("waiting-for-be", "display-block")
|
| 58 |
+
let wordsFrequency = document.getElementById("words-frequency")
|
| 59 |
+
wordsFrequency.innerHTML = ""
|
| 60 |
+
try {
|
| 61 |
+
let response = await fetch("/words-frequency", {
|
| 62 |
+
method: "POST",
|
| 63 |
+
body: JSON.stringify(bodyRequest)
|
| 64 |
+
})
|
| 65 |
+
console.log("getWordFreq::response.status:", response.status, "#")
|
| 66 |
+
console.assert(response.status, 200)
|
| 67 |
+
let bodyResponseJson = await response.json()
|
| 68 |
+
setElementCssClass("waiting-for-be", "display-none")
|
| 69 |
+
console.log("getWordFreq::body keys:", Object.keys(bodyResponseJson), "#")
|
| 70 |
+
let freq = bodyResponseJson["words_frequency"]
|
| 71 |
+
let nTotalRows = bodyResponseJson["n_total_rows"]
|
| 72 |
+
console.log("getWordFreq::tot;:", nTotalRows, "#")
|
| 73 |
+
populateWordFrequencyTable(freq, bodyResponseJson["n_total_rows"])
|
| 74 |
+
} catch (err) {
|
| 75 |
+
console.error("getWordFrequency::err:", err, "#")
|
| 76 |
+
setElementCssClass("waiting-for-be", "display-none")
|
| 77 |
+
setElementCssClass("waiting-for-be-error", "display-block")
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
const populateWordFrequencyTable = (wordsFrequencyObj, nTotalRows) => {
|
| 81 |
+
const wfo = JSON.parse(wordsFrequencyObj)
|
| 82 |
+
const reduced = Object.values(wfo)
|
| 83 |
+
let wordsFrequency = document.getElementById("words-frequency")
|
| 84 |
+
wordsFrequency.innerHTML = ""
|
| 85 |
+
for (let i=0; i<reduced.length; i++ ) {
|
| 86 |
+
let iReduced = reduced[i]
|
| 87 |
+
let currentTableWordFreq = document.createElement("table")
|
| 88 |
+
currentTableWordFreq.setAttribute("class", "border-black")
|
| 89 |
+
currentTableWordFreq.setAttribute("id", `id-table-${i}-nth`)
|
| 90 |
+
currentTableWordFreq.setAttribute("aria-label", `id-table-${i}-nth`)
|
| 91 |
+
currentTableWordFreq.createCaption(`count: ${iReduced["count"]}, word: '${iReduced["word_prefix"]}'`)
|
| 92 |
+
let currentTHead = document.createElement("thead")
|
| 93 |
+
let currentTHeadRow = currentTHead.insertRow()
|
| 94 |
+
currentTHeadRow.insertCell().textContent = 'word'
|
| 95 |
+
currentTHeadRow.insertCell().textContent = 'row nth'
|
| 96 |
+
currentTHeadRow.insertCell().textContent = 'offsets'
|
| 97 |
+
|
| 98 |
+
let currentTBody = document.createElement("tbody")
|
| 99 |
+
let offsetsArray = iReduced.offsets_array
|
| 100 |
+
for (let ii=0; ii < offsetsArray.length; ii++) {
|
| 101 |
+
let nthOffset = offsetsArray[ii]
|
| 102 |
+
let nthRowBody = currentTBody.insertRow()
|
| 103 |
+
nthRowBody.setAttribute("id", `id-table-${i}-row-${ii}-nth`)
|
| 104 |
+
nthRowBody.setAttribute("aria-label", `id-table-${i}-row-${ii}-nth`)
|
| 105 |
+
let currentCell = nthRowBody.insertCell()
|
| 106 |
+
let currentUrl = document.createElement("a")
|
| 107 |
+
currentUrl.addEventListener("click", function() {
|
| 108 |
+
let nRow = nthOffset["n_row"]
|
| 109 |
+
let offsetWord = nthOffset["offsets"]
|
| 110 |
+
setCaret(nRow, offsetWord, nTotalRows)
|
| 111 |
+
try {
|
| 112 |
+
let oldClassElement = document.getElementsByClassName('underlinedDarkViolet')
|
| 113 |
+
oldClassElement[0].className = "underlinedBlue"
|
| 114 |
+
} catch {
|
| 115 |
+
console.log("first click...")
|
| 116 |
+
}
|
| 117 |
+
currentUrl.className = "underlinedDarkViolet"
|
| 118 |
+
})
|
| 119 |
+
currentUrl.className = "underlinedBlue"
|
| 120 |
+
currentUrl.innerText = nthOffset["word"]
|
| 121 |
+
currentCell.appendChild(currentUrl)
|
| 122 |
+
nthRowBody.insertCell().textContent = nthOffset["n_row"]
|
| 123 |
+
nthRowBody.insertCell().textContent = nthOffset["offsets"]
|
| 124 |
+
}
|
| 125 |
+
currentTableWordFreq.appendChild(currentTHead)
|
| 126 |
+
currentTableWordFreq.appendChild(currentTBody)
|
| 127 |
+
wordsFrequency.appendChild(currentTableWordFreq)
|
| 128 |
+
}
|
| 129 |
+
}
|