From d13d4860f3d5b51d659379aa8a38742bfe49bf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuomas=20Klav=C3=A9r?= Date: Mon, 20 Nov 2023 02:04:12 +0200 Subject: Questions now shows up. DB -> JSON -> JS -> DOM --- static/create.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ static/index.html | 1 + static/kyselma.css | 28 ++++++++++++++++++++++++ static/menu.js | 10 +++++++++ 4 files changed, 102 insertions(+) create mode 100644 static/create.js create mode 100644 static/kyselma.css (limited to 'static') diff --git a/static/create.js b/static/create.js new file mode 100644 index 0000000..6c25659 --- /dev/null +++ b/static/create.js @@ -0,0 +1,63 @@ +var questions = {} + +createQuestionDiv = ( question ) => { + const questionDiv = document.createElement('div') + questionDiv.className = 'kysQuestion' + + const qDiv = document.createElement('div') + qDiv.appendChild( document.createTextNode( question.q ) ) + qDiv.className = 'kysText' + questionDiv.appendChild( qDiv ) + + const npDiv = document.createElement('div') + npDiv.className = 'kysScale' + + const nDiv = document.createElement('div') + nDiv.appendChild( document.createTextNode( question.n ) ) + nDiv.className = 'kysNegative' + npDiv.appendChild( nDiv ) + + const sDiv = document.createElement('div') + sDiv.className = 'kysScaleSpacer' + npDiv.appendChild( sDiv ) + + const pDiv = document.createElement('div') + pDiv.appendChild( document.createTextNode( question.p ) ) + pDiv.className = 'kysPositive' + npDiv.appendChild( pDiv ) + + questionDiv.appendChild( npDiv ) + + const aDiv = document.createElement('input') + aDiv.appendChild( document.createTextNode( question.a ) ) + aDiv.className = 'kysAnswer' + aDiv.type = 'range' + aDiv.min = 0 + aDiv.max = 999 + aDiv.disabled = true + aDiv.value = question.a + questionDiv.appendChild( aDiv ) + + return questionDiv +} + +createQuestions = () => { + const questionsDiv = document.getElementById('questions') + questionsDiv.className = 'kysQuestions' + Object.keys(questions).forEach(k => { + questionsDiv.appendChild( createQuestionDiv( questions[k] ) ) + }) +} + +loadQuestions = async() => { + await fetch( 'get_questions' ) + .then( response => response.json() ) + .then( json => questions = json ) + .catch( error => { + alert("dkd") + } ) + + createQuestions() +} + +loadQuestions() diff --git a/static/index.html b/static/index.html index d726587..6c7917b 100644 --- a/static/index.html +++ b/static/index.html @@ -7,6 +7,7 @@ + diff --git a/static/kyselma.css b/static/kyselma.css new file mode 100644 index 0000000..a7a60db --- /dev/null +++ b/static/kyselma.css @@ -0,0 +1,28 @@ + +.kysQuestion { + display: flex; + flex-direction: column; + max-width: 25em; +} +.kysText { + align-self: center; +} +.kysScale { + display: flex; +} +.kysScaleSpacer { + flex-grow: 1; +} +.kysNegative input { + width: 100%; +} +.kysPositive input { + width: 100%; +} +.kysCreateText input { + width: 100%; + resize: horizontal; +} +.kysCreateText { + width: 100%; +} diff --git a/static/menu.js b/static/menu.js index ee8f977..5256ef2 100644 --- a/static/menu.js +++ b/static/menu.js @@ -145,6 +145,16 @@ hashToPage = async () => { pageElement.loaded = false; window.location.assign( pageElement.innerHTML.slice( 11 ) ); } + + // https://plnkr.co/edit/MMegiu by Allen Kim + Array.from(pageElement.querySelectorAll("script")).forEach(oldScript => { + const newScript = document.createElement("script"); + Array.from(oldScript.attributes).forEach(attr => + newScript.setAttribute(attr.name, attr.value)); + newScript.appendChild(document.createTextNode(oldScript.innerHTML)); + oldScript.parentNode.replaceChild(newScript, oldScript); + }); + document.getElementById(`${currentPage}_menuEntry`) .className = 'menuItem' document.getElementById(`${p}_menuEntry`) -- cgit v1.2.3