startit/emne2/Vinlotterix/view.js

84 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2024-10-01 13:08:44 +02:00
function createMenu(){
return /*html*/ `
2024-10-02 10:13:18 +02:00
<button onclick="updateViewHome()">Hjem</button>
2024-10-01 13:08:44 +02:00
<button onclick="showWinnersView()">Trekk vinnere</button>
<button onclick="updateViewRules()">Sett opp regler for trekningen</button>
2024-10-02 10:13:18 +02:00
<button onclick="editList()">Legg til/fjern</button>
2024-10-01 13:08:44 +02:00
<br/>
2024-10-02 10:13:18 +02:00
<hr>
2024-10-01 13:08:44 +02:00
`;
}
function updateViewHome(){
document.getElementById('app').innerHTML = /*HTML*/ `
2024-10-02 10:13:18 +02:00
<h1>Velkommen til Vinlotterix</h1>
2024-10-01 13:08:44 +02:00
${createMenu()}
`;
}
function updateViewRules(){
app.innerHTML = /*HTML*/ `
<h2>Hello from rules</h2>
${createMenu()}
<label for="model.input.rules.numWinners">Hvor mange vinnere?</label>
<input type="number"
onchange="model.input.rules.numWinners=this.value"
min="0"
max="10"
value="${model.input.rules.numWinners || ''}"
name="NumberOfWinners"></select>
<ol>
<li>Nummer: ${model.input.rules.numWinners}</li>
</ol>
<button onclick="setRules()">Sett regler</button><br>
`;
}
2024-10-02 10:13:18 +02:00
function editList(){
2024-10-01 13:08:44 +02:00
let html = '';
const employees = model.employees;
for (let i = 0; i < employees.length; i++){
const person = employees[i];
html += /*html*/ `
2024-10-02 10:13:18 +02:00
<li>${person.id}${person.name}<button onclick="goToEditPage(${person.id})">Edit</button></li>
2024-10-01 13:08:44 +02:00
`;
}
app.innerHTML = html + 'This is the list <button onclick="updateViewHome()">Home</button>'
}
function showWinnersView(){
let winners = [];
let num = model.input.rules.numWinners;
2024-10-02 10:13:18 +02:00
app.innerHTML = /*html*/ `<h1>Vinlotterix</h1><br> ${createMenu()}
<h3>Vinnere:</h3>
`;
2024-10-01 13:08:44 +02:00
for (i = 0; i < num; i++){
index = Math.floor(Math.random() * (model.employees.length));
2024-10-02 10:13:18 +02:00
winners.push(model.employees[index]);
2024-10-01 13:08:44 +02:00
}
for (i = 0; i < winners.length; i++){
app.innerHTML += /*html*/ `
<h4>${winners[i].name}</h4>
2024-10-02 10:13:18 +02:00
`;}
2024-10-01 13:08:44 +02:00
2024-10-02 10:13:18 +02:00
}
function goToEditPage(id){
const person = model.employees[id];
let newPerson = '';
app.innerHTML = /*html*/ `
<h1>Endre eller slett</h1><br> ${createMenu()}
Navn: <input value="${person.name}" onchange="newPerson=this.value" /><button onclick="changeList(${person.id}, newPerson)">Send inn</button><br>
`;
}
function changeList(id,name){
console.log(id,name)
model.employees[id].name = name;
console.log(model.employees[id].name)
2024-10-01 13:08:44 +02:00
}