diff options
Diffstat (limited to 'modules/zelfoverhoor/docent.js')
-rw-r--r-- | modules/zelfoverhoor/docent.js | 83 |
1 files changed, 80 insertions, 3 deletions
diff --git a/modules/zelfoverhoor/docent.js b/modules/zelfoverhoor/docent.js index 7987351..96ec38b 100644 --- a/modules/zelfoverhoor/docent.js +++ b/modules/zelfoverhoor/docent.js @@ -1,4 +1,5 @@ var questionsets=null; +var currentlyEditing=null; function getQuestionSets(){ var xhr=new XMLHttpRequest(); @@ -26,6 +27,7 @@ function updateQuestionSetsList(){ } for(var i=0;i<questionsets.length;i++){ var tr=document.createElement("tr"); + td=document.createElement("td"); var a=document.createElement("a"); a.setAttribute("href",location.origin+"/zelfoverhoor/qs/"+questionsets[i].id); @@ -33,19 +35,34 @@ function updateQuestionSetsList(){ a.innerHTML=questionsets[i].id; td.appendChild(a); tr.appendChild(td); + td=document.createElement("td"); td.appendChild(document.createTextNode(questionsets[i].name)); tr.appendChild(td); + td=document.createElement("td"); td.appendChild(document.createTextNode(questionsets[i].description)); tr.appendChild(td); + td=document.createElement("td"); td.appendChild(document.createTextNode(questionsets[i].questions.length)); tr.appendChild(td); + + td=document.createElement("td"); + var input=document.createElement("input"); + input.setAttribute("type","button"); + input.setAttribute("value","bewerk"); + input.addEventListener("click",(function(id){return function(){ + doEditQSet(id); + };})(questionsets[i].id)); + td.appendChild(input); + tr.appendChild(td); + td=document.createElement("td"); var input=document.createElement("input"); input.setAttribute("type","button"); input.setAttribute("value","verwijder"); + input.setAttribute("style","color:red"); input.addEventListener("click",(function(id,name){return function(){ if(confirm("Weet je zeker dat je de vragenset '"+name+"' wilt verwijderen?")){ var xhr=new XMLHttpRequest(); @@ -66,6 +83,7 @@ function updateQuestionSetsList(){ };})(questionsets[i].id,questionsets[i].name)); td.appendChild(input); tr.appendChild(td); + tbody.appendChild(tr); } } @@ -75,18 +93,63 @@ function clearElement(el){ while((c=el.lastChild))el.removeChild(c); } -function doNewQSet(){ +function openBlankSetEditor(){ document.getElementById("newqsetname").value=""; document.getElementById("newqsetdescr").value=""; - clearElement("newquestions"); + clearElement(document.getElementById("newquestions")); document.getElementById("newqsetform").classList.remove("invisible"); document.getElementById("newqsetvisible").classList.add("invisible"); + currentlyEditing=null; +} + +function doNewQSet(){ + openBlankSetEditor(); + document.getElementById("formheader").innerHTML="Nieuwe vragenset"; addNewQuestion(); } +function receiveEditQSet(qset){ + openBlankSetEditor(); + document.getElementById("newqsetname").value=qset.name; + document.getElementById("newqsetdescr").value=qset.description; + for(var i=0;i<qset.questions.length;i++){ + var div=addNewQuestion(); + var tas=div.getElementsByTagName("textarea"); + tas[0].value=qset.questions[i].q; + tas[1].value=qset.questions[i].a; + } + document.getElementById("formheader").innerHTML="Bewerk vragenset ("+qset.id+")"; + currentlyEditing=qset.id; +} + +function doEditQSet(id){ + var xhr=new XMLHttpRequest(); + xhr.onreadystatechange=function(){ + if(xhr.readyState==4){ + if(xhr.status==200){ + var obj; + try { + obj=JSON.parse(xhr.responseText); + } catch(e){ + alert("Server stuurde ongeldige data!"); + return + } + receiveEditQSet(obj) + } else if(xhr.status==404){ + alert("Vragenset lijkt niet te bestaan... Misschien even de pagina herladen?"); + } else { + alert("Vragenset ophalen lijkt niet te lukken... (error code "+xhr.status+")"); + } + } + }; + xhr.open("GET","/zelfoverhoor/qsdata/"+id); + xhr.send(); +} + function closeQSetForm(){ document.getElementById("newqsetform").classList.add("invisible"); document.getElementById("newqsetvisible").classList.remove("invisible"); + currentlyEditing=null; } function addNewQuestion(){ @@ -128,6 +191,7 @@ function addNewQuestion(){ div.appendChild(span); parent.appendChild(div); + return div; } function submitQSet(){ @@ -164,10 +228,23 @@ function submitQSet(){ } } }; - xhr.open("POST","/zelfoverhoor/docent/addset"); + if(currentlyEditing!=null){ + xhr.open("POST","/zelfoverhoor/docent/editset/"+currentlyEditing); + } else { + xhr.open("POST","/zelfoverhoor/docent/addset"); + } xhr.send(JSON.stringify(obj)); } +function doMaybeCloseForm(){ + var str; + if(currentlyEditing)str="Weet je zeker dat je de wijzigingen wilt annuleren?"; + else str="Weet je zeker dat je de nieuwe vragenset wilt weggooien?"; + if(confirm(str)){ + closeQSetForm(); + } +} + window.addEventListener("load",function(){ getQuestionSets(); }); |