comparison core.js @ 1028:aca24f269c56

Feature #1234: Added back button to pre/post surveys
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 10 Jun 2015 10:13:45 +0100
parents 4e9ab4f92f20
children d4eecacc6558
comparison
equal deleted inserted replaced
1027:4e9ab4f92f20 1028:aca24f269c56
48 function interfacePopup() { 48 function interfacePopup() {
49 // Creates an object to manage the popup 49 // Creates an object to manage the popup
50 this.popup = null; 50 this.popup = null;
51 this.popupContent = null; 51 this.popupContent = null;
52 this.buttonProceed = null; 52 this.buttonProceed = null;
53 this.buttonPrevious = null;
53 this.popupOptions = null; 54 this.popupOptions = null;
54 this.currentIndex = null; 55 this.currentIndex = null;
55 this.responses = null; 56 this.responses = null;
56 57
57 this.createPopup = function(){ 58 this.createPopup = function(){
73 this.popupContent.align = 'center'; 74 this.popupContent.align = 'center';
74 this.popup.appendChild(this.popupContent); 75 this.popup.appendChild(this.popupContent);
75 76
76 this.buttonProceed = document.createElement('button'); 77 this.buttonProceed = document.createElement('button');
77 this.buttonProceed.className = 'popupButton'; 78 this.buttonProceed.className = 'popupButton';
79 this.buttonProceed.style.left = '440px';
80 this.buttonProceed.style.top = '215px';
78 this.buttonProceed.innerHTML = 'Next'; 81 this.buttonProceed.innerHTML = 'Next';
79 this.buttonProceed.onclick = function(){popup.proceedClicked();}; 82 this.buttonProceed.onclick = function(){popup.proceedClicked();};
83
84 this.buttonPrevious = document.createElement('button');
85 this.buttonPrevious.className = 'popupButton';
86 this.buttonPrevious.style.left = '10px';
87 this.buttonPrevious.style.top = '215px';
88 this.buttonPrevious.innerHTML = 'Back';
89 this.buttonPrevious.onclick = function(){popup.previousClick();};
90
80 this.popup.style.zIndex = -1; 91 this.popup.style.zIndex = -1;
81 this.popup.style.visibility = 'hidden'; 92 this.popup.style.visibility = 'hidden';
82 blank.style.zIndex = -2; 93 blank.style.zIndex = -2;
83 blank.style.visibility = 'hidden'; 94 blank.style.visibility = 'hidden';
84 insertPoint.appendChild(this.popup); 95 insertPoint.appendChild(this.popup);
198 if (node.max != null) {input.max = node.max;} 209 if (node.max != null) {input.max = node.max;}
199 if (node.step != null) {input.step = node.step;} 210 if (node.step != null) {input.step = node.step;}
200 this.popupContent.appendChild(input); 211 this.popupContent.appendChild(input);
201 } 212 }
202 this.popupContent.appendChild(this.buttonProceed); 213 this.popupContent.appendChild(this.buttonProceed);
214 if(this.currentIndex+1 == this.popupOptions.length) {
215 this.buttonProceed.textContent = 'Submit';
216 } else {
217 this.buttonProceed.textContent = 'Next';
218 }
219 if(this.currentIndex > 0)
220 this.popupContent.appendChild(this.buttonPrevious);
203 }; 221 };
204 222
205 this.initState = function(node) { 223 this.initState = function(node) {
206 //Call this with your preTest and postTest nodes when needed to 224 //Call this with your preTest and postTest nodes when needed to
207 // initialise the popup procedure. 225 // initialise the popup procedure.
297 hold.textContent = input.value; 315 hold.textContent = input.value;
298 this.responses.appendChild(hold); 316 this.responses.appendChild(hold);
299 } 317 }
300 this.currentIndex++; 318 this.currentIndex++;
301 if (this.currentIndex < this.popupOptions.length) { 319 if (this.currentIndex < this.popupOptions.length) {
302 if(this.currentIndex+1 == this.popupOptions.length) {
303 this.buttonProceed.textContent = 'Submit';
304 }
305 this.postNode(); 320 this.postNode();
306 } else { 321 } else {
307 // Reached the end of the popupOptions 322 // Reached the end of the popupOptions
308 this.hidePopup(); 323 this.hidePopup();
309 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { 324 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
310 testState.stateResults[testState.stateIndex] = this.responses; 325 testState.stateResults[testState.stateIndex] = this.responses;
311 } else { 326 } else {
312 testState.stateResults[testState.stateIndex].appendChild(this.responses); 327 testState.stateResults[testState.stateIndex].appendChild(this.responses);
313 } 328 }
314 advanceState(); 329 advanceState();
330 }
331 };
332
333 this.previousClick = function() {
334 // Triggered when the 'Back' button is clicked in the survey
335 if (this.currentIndex > 0) {
336 this.currentIndex--;
337 var node = this.popupOptions[this.currentIndex];
338 if (node.type != 'statement') {
339 var prevResp = this.responses.childNodes[this.responses.childElementCount-1];
340 this.responses.removeChild(prevResp);
341 }
342 this.postNode();
343 if (node.type == 'question') {
344 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent;
345 } else if (node.type == 'checkbox') {
346 var options = this.popupContent.getElementsByTagName('input');
347 var savedOptions = prevResp.getElementsByTagName('option');
348 for (var i=0; i<options.length; i++) {
349 var id = options[i].id;
350 for (var j=0; j<savedOptions.length; j++) {
351 if (savedOptions[j].getAttribute('name') == id) {
352 if (savedOptions[j].textContent == 'true') {options[i].checked = true;}
353 else {options[i].checked = false;}
354 break;
355 }
356 }
357 }
358 } else if (node.type == 'number') {
359 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent;
360 } else if (node.type == 'radio') {
361 var options = this.popupContent.getElementsByTagName('input');
362 var name = prevResp.getAttribute('name');
363 for (var i=0; i<options.length; i++) {
364 if (options[i].id == name) {
365 options[i].checked = true;
366 break;
367 }
368 }
369 }
315 } 370 }
316 }; 371 };
317 } 372 }
318 373
319 function advanceState() 374 function advanceState()