Last active
December 29, 2018 14:57
-
-
Save Huluk/9f89cb371266d9dc13704edee9519bbd to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>JES 18/19 – Storkow – Manĝoj</title> | |
</head> | |
<body> | |
<input type="file" id="file-input" /> | |
<br /> | |
<input type="date" id="date" /> | |
<br /> | |
<form id='time'> | |
<input type="radio" name="time" value="0">Matenmanĝo</input> | |
<input type="radio" name="time" value="1">Tagmanĝo</input> | |
<input type="radio" name="time" value="2">Vespermanĝo</input> | |
</form> | |
<br /><hr /><br /> | |
<input type="text" id="user-id" /> | |
<h1 id="food-choice">Manĝo</h1> | |
<pre id="user-data"></pre> | |
<script> | |
var table = {}; | |
var day = 0; | |
var start_date = new Date('2018-12-27'); | |
var time = 0; | |
var id = 0; | |
function readSingleFile(e) { | |
let file = e.target.files[0]; | |
if (!file) { | |
return; | |
} | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
let lines = e.target.result.split("\n"); | |
for (i = 0, len = lines.length; i < len; i++) { | |
if (lines[i].search(/^\D/i) >= 0) { | |
continue; | |
} | |
var [key, ...elems] = lines[i].split("\t"); | |
table[key] = elems; | |
} | |
getUserData(); | |
}; | |
reader.readAsText(file); | |
document.getElementById('user-id').focus(); | |
} | |
function getIndex() { | |
// - 2 because first meal is dinner | |
return 3*day + time - 2; | |
} | |
function userLookup(e) { | |
var input = e.target.value; | |
console.log("input: " + input); | |
if (input.length == 8) { // parse scan input | |
input = input.substr(4, 3); | |
} | |
id = parseInt(input); | |
getUserData(); | |
let input_field = document.getElementById('user-id'); | |
input_field.value = ''; | |
} | |
function getUserData() { | |
let index = getIndex(); | |
var food_element = document.getElementById('food-choice'); | |
var data_element = document.getElementById('user-data'); | |
if (table[id] == undefined) { | |
console.log("undefined id " + id); | |
food_element.style = ""; | |
food_element.textContent = "Ne trovis!"; | |
data_element.textContent = ""; | |
return; | |
} | |
console.log(table[id]); | |
let [first_name, last_name, country, color, ...foods] = table[id]; | |
var food; | |
switch(foods[index]) { | |
case '1': | |
food_element.style = "background-color: green"; | |
food = "vianda" | |
break; | |
case '2': | |
food_element.style = "background-color: green"; | |
food = "vegetara" | |
break; | |
case '3': | |
food_element.style = "background-color: green"; | |
food = "vegana" | |
break; | |
default: | |
food_element.style = "background-color: red"; | |
food = "NE MANĜAS" | |
} | |
food_element.textContent = food; | |
data_element.textContent = table[id].toString(); | |
} | |
function updateDay(e) { | |
var date = new Date(e.target.value); | |
day = new Date(date - start_date).getDate() - 1; | |
console.log('day: ' + day); | |
getUserData(); | |
document.getElementById('user-id').focus(); | |
} | |
function updateTime(e) { | |
time = parseInt(e.target.value); | |
console.log('time: ' + time); | |
getUserData(); | |
document.getElementById('user-id').focus(); | |
} | |
function dateInputValue(date) { | |
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate(); | |
} | |
document.getElementById('file-input') | |
.addEventListener('change', readSingleFile); | |
document.getElementById('user-id') | |
.addEventListener('change', userLookup); | |
document.getElementById('date') | |
.addEventListener('change', updateDay); | |
document.getElementById('time') | |
.addEventListener('change', updateTime); | |
document.getElementById('date').value = dateInputValue(new Date()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment