comparison js/core.js @ 2679:628f447a021b

Completed converting this.popup with safe functions for individual nodes
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Wed, 01 Mar 2017 11:32:38 +0000
parents e446c9b43cd9
children 470bbfd78a96
comparison
equal deleted inserted replaced
2678:e446c9b43cd9 2679:628f447a021b
323 specification.decode(projectXML); 323 specification.decode(projectXML);
324 // Use the original 324 // Use the original
325 storage.initialise(responseDocument); 325 storage.initialise(responseDocument);
326 } 326 }
327 /// CHECK FOR SAMPLE RATE COMPATIBILITY 327 /// CHECK FOR SAMPLE RATE COMPATIBILITY
328 if (specification.sampleRate !== undefined) { 328 if (specification.sampleRate !== null) {
329 if (Number(specification.sampleRate) != audioContext.sampleRate) { 329 if (Number(specification.sampleRate) != audioContext.sampleRate) {
330 var errStr = 'Sample rates do not match! Requested ' + Number(specification.sampleRate) + ', got ' + audioContext.sampleRate + '. Please set the sample rate to match before completing this test.'; 330 var errStr = 'Sample rates do not match! Requested ' + Number(specification.sampleRate) + ', got ' + audioContext.sampleRate + '. Please set the sample rate to match before completing this test.';
331 interfaceContext.lightbox.post("Error", errStr); 331 interfaceContext.lightbox.post("Error", errStr);
332 return; 332 return;
333 } 333 }
611 } else if (node.specification.type === "slider") { 611 } else if (node.specification.type === "slider") {
612 conditionFunction = processSliderConditional; 612 conditionFunction = processSliderConditional;
613 } else { 613 } else {
614 return; 614 return;
615 } 615 }
616 for (var i = 0; i < node.specification.conditions; i++) { 616 for (var i = 0; i < node.specification.conditions.length; i++) {
617 var condition = node.specification.conditions[i]; 617 var condition = node.specification.conditions[i];
618 var pass = conditionFunction(condition, value); 618 var pass = conditionFunction(condition, value);
619 var jumpID; 619 var jumpID;
620 if (pass) { 620 if (pass) {
621 jumpID = condition.jumpToOnPass; 621 jumpID = condition.jumpToOnPass;
622 } else { 622 } else {
623 jumpID = condition.jumpToOnFail; 623 jumpID = condition.jumpToOnFail;
624 } 624 }
625 if (jumpID !== undefined) { 625 if (jumpID !== undefined) {
626 jumpToId(jumpID); 626 jumpToId.call(this, jumpID);
627 break; 627 break;
628 } 628 }
629 } 629 }
630 } 630 }
631 631
680 } 680 }
681 return false; 681 return false;
682 } 682 }
683 683
684 function processQuestion(node) { 684 function processQuestion(node) {
685 var textArea = this.popupResponse.getAllElementsByTagName("textarea")[0]; 685 var textArea = this.popupResponse.getElementsByTagName("textarea")[0];
686 if (node.specification.mandatory === true && textArea.value.length === 0) { 686 if (node.specification.mandatory === true && textArea.value.length === 0) {
687 interfaceContext.lightbox.post("Error", "This question is mandatory"); 687 interfaceContext.lightbox.post("Error", "This question is mandatory");
688 return; 688 return false;
689 } else { 689 }
690 // Save the text content 690 // Save the text content
691 console.log("Question: " + node.specification.statement); 691 console.log("Question: " + node.specification.statement);
692 console.log("Question Response: " + textArea.value); 692 console.log("Question Response: " + textArea.value);
693 node.response = textArea.value; 693 node.response = textArea.value;
694 } 694 processConditional.call(this, node, textArea.value);
695 processConditional(node, textArea.value); 695 return true;
696 } 696 }
697 697
698 function postCheckbox(node) { 698 function postCheckbox(node) {
699 if (node.response === undefined) { 699 if (node.response === undefined) {
700 node.response = Array(node.specification.options.length); 700 node.response = Array(node.specification.options.length);
701 } 701 }
702 var index = 0;
703 var table = document.createElement("table"); 702 var table = document.createElement("table");
704 table.className = "popup-option-list"; 703 table.className = "popup-option-list";
705 table.border = "0"; 704 table.border = "0";
706 node.specification.options.forEach(function (option) { 705 node.response = [];
706 node.specification.options.forEach(function (option, index) {
707 var tr = document.createElement("tr"); 707 var tr = document.createElement("tr");
708 table.appendChild(tr); 708 table.appendChild(tr);
709 var td = document.createElement("td"); 709 var td = document.createElement("td");
710 tr.appendChild(td); 710 tr.appendChild(td);
711 var input = document.createElement('input'); 711 var input = document.createElement('input');
757 if (node.specification.min == node.specification.max) { 757 if (node.specification.min == node.specification.max) {
758 interfaceContext.lightbox.post("Error", "You must only select " + node.specification.min); 758 interfaceContext.lightbox.post("Error", "You must only select " + node.specification.min);
759 } else { 759 } else {
760 interfaceContext.lightbox.post("Error", "You must select between " + node.specification.min + " and " + node.specification.max); 760 interfaceContext.lightbox.post("Error", "You must select between " + node.specification.min + " and " + node.specification.max);
761 } 761 }
762 return; 762 return false;
763 } 763 }
764 } 764 }
765 } 765 }
766 for (i = 0; i < node.specification.options.length; i++) { 766 for (i = 0; i < node.specification.options.length; i++) {
767 node.response.push({ 767 node.response.push({
769 text: node.specification.options[i].text, 769 text: node.specification.options[i].text,
770 checked: inputs[i].checked 770 checked: inputs[i].checked
771 }); 771 });
772 console.log(node.specification.options[i].name + ": " + inputs[i].checked); 772 console.log(node.specification.options[i].name + ": " + inputs[i].checked);
773 } 773 }
774 processConditional(node, node.response); 774 processConditional.call(this, node, node.response);
775 return true;
775 } 776 }
776 777
777 function processCheckboxConditional(condition, response) { 778 function processCheckboxConditional(condition, response) {
778 switch (condition.check) { 779 switch (condition.check) {
779 case "contains": 780 case "contains":
844 var inputs = optHold.getElementsByTagName('input'); 845 var inputs = optHold.getElementsByTagName('input');
845 while (node.response === null) { 846 while (node.response === null) {
846 if (i == inputs.length) { 847 if (i == inputs.length) {
847 if (node.specification.mandatory === true) { 848 if (node.specification.mandatory === true) {
848 interfaceContext.lightbox.post("Error", "Please select one option"); 849 interfaceContext.lightbox.post("Error", "Please select one option");
849 return; 850 return false;
850 } 851 }
851 break; 852 break;
852 } 853 }
853 if (inputs[i].checked === true) { 854 if (inputs[i].checked === true) {
854 node.response = node.specification.options[i]; 855 node.response = node.specification.options[i];
855 console.log("Selected: " + node.specification.options[i].name); 856 console.log("Selected: " + node.specification.options[i].name);
856 } 857 }
857 i++; 858 i++;
858 } 859 }
859 processConditional(node, node.response); 860 processConditional.call(this, node, node.response);
861 return true;
860 } 862 }
861 863
862 function processRadioConditional(condition, response) { 864 function processRadioConditional(condition, response) {
863 switch (condition.check) { 865 switch (condition.check) {
864 case "equals": 866 case "equals":
900 902
901 function processNumber(node) { 903 function processNumber(node) {
902 var input = this.popupContent.getElementsByTagName('input')[0]; 904 var input = this.popupContent.getElementsByTagName('input')[0];
903 if (node.mandatory === true && input.value.length === 0) { 905 if (node.mandatory === true && input.value.length === 0) {
904 interfaceContext.lightbox.post("Error", 'This question is mandatory. Please enter a number'); 906 interfaceContext.lightbox.post("Error", 'This question is mandatory. Please enter a number');
905 return; 907 return false;
906 } 908 }
907 var enteredNumber = Number(input.value); 909 var enteredNumber = Number(input.value);
908 if (isNaN(enteredNumber)) { 910 if (isNaN(enteredNumber)) {
909 interfaceContext.lightbox.post("Error", 'Please enter a valid number'); 911 interfaceContext.lightbox.post("Error", 'Please enter a valid number');
910 return; 912 return false;
911 } 913 }
912 if (enteredNumber < node.min && node.min !== null) { 914 if (enteredNumber < node.min && node.min !== null) {
913 interfaceContext.lightbox.post("Error", 'Number is below the minimum value of ' + node.min); 915 interfaceContext.lightbox.post("Error", 'Number is below the minimum value of ' + node.min);
914 return; 916 return false;
915 } 917 }
916 if (enteredNumber > node.max && node.max !== null) { 918 if (enteredNumber > node.max && node.max !== null) {
917 interfaceContext.lightbox.post("Error", 'Number is above the maximum value of ' + node.max); 919 interfaceContext.lightbox.post("Error", 'Number is above the maximum value of ' + node.max);
918 return; 920 return false;
919 } 921 }
920 node.response = input.value; 922 node.response = input.value;
923 processConditional.call(this, node, node.response);
924 return true;
921 } 925 }
922 926
923 function processNumberConditional(condtion, value) { 927 function processNumberConditional(condtion, value) {
924 switch (condition.check) { 928 switch (condition.check) {
925 case "greaterThan": 929 case "greaterThan":
987 } 991 }
988 992
989 function processSlider(node) { 993 function processSlider(node) {
990 var input = this.popupContent.getElementsByTagName('input')[0]; 994 var input = this.popupContent.getElementsByTagName('input')[0];
991 node.response = input.value; 995 node.response = input.value;
996 processConditional.call(this, node, node.response);
997 return true;
992 } 998 }
993 999
994 function processSliderConditional(condition, value) { 1000 function processSliderConditional(condition, value) {
995 switch (condition.check) { 1001 switch (condition.check) {
996 case "contains": 1002 case "contains":
1072 p = new DOMParser(); 1078 p = new DOMParser();
1073 this.popupResponse.innerHTML = ""; 1079 this.popupResponse.innerHTML = "";
1074 this.popupTitle.innerHTML = ""; 1080 this.popupTitle.innerHTML = "";
1075 this.popupTitle.appendChild(p.parseFromString(converter.makeHtml(node.specification.statement), "text/html").getElementsByTagName("body")[0].firstElementChild); 1081 this.popupTitle.appendChild(p.parseFromString(converter.makeHtml(node.specification.statement), "text/html").getElementsByTagName("body")[0].firstElementChild);
1076 if (node.specification.type == 'question') { 1082 if (node.specification.type == 'question') {
1077 postQuestion(node); 1083 postQuestion.call(this, node);
1078 } else if (node.specification.type == 'checkbox') { 1084 } else if (node.specification.type == 'checkbox') {
1079 postCheckbox(node); 1085 postCheckbox.call(this, node);
1080 } else if (node.specification.type == 'radio') { 1086 } else if (node.specification.type == 'radio') {
1081 postRadio(node); 1087 postRadio.call(this, node);
1082 } else if (node.specification.type == 'number') { 1088 } else if (node.specification.type == 'number') {
1083 postNumber(node); 1089 postNumber.call(this, node);
1084 } else if (node.specification.type == "video") { 1090 } else if (node.specification.type == "video") {
1085 postVideo(node); 1091 postVideo.call(this, node);
1086 } else if (node.specification.type == "youtube") { 1092 } else if (node.specification.type == "youtube") {
1087 postYoutube(node); 1093 postYoutube.call(this, node);
1088 } else if (node.specification.type == "slider") { 1094 } else if (node.specification.type == "slider") {
1089 postSlider(node); 1095 postSlider.call(this, node);
1090 } 1096 }
1091 if (this.currentIndex + 1 == this.popupOptions.length) { 1097 if (this.currentIndex + 1 == this.popupOptions.length) {
1092 if (this.node.location == "pre") { 1098 if (this.node.location == "pre") {
1093 this.buttonProceed.textContent = 'Start'; 1099 this.buttonProceed.textContent = 'Start';
1094 } else { 1100 } else {
1129 if (testState.stateIndex === 0 && specification.calibration) { 1135 if (testState.stateIndex === 0 && specification.calibration) {
1130 interfaceContext.calibrationModuleObject.collect(); 1136 interfaceContext.calibrationModuleObject.collect();
1131 advanceState(); 1137 advanceState();
1132 return; 1138 return;
1133 } 1139 }
1134 var node = this.popupOptions[this.currentIndex]; 1140 var node = this.popupOptions[this.currentIndex],
1141 pass = true;
1135 if (node.specification.type == 'question') { 1142 if (node.specification.type == 'question') {
1136 // Must extract the question data 1143 // Must extract the question data
1137 processQuestion(node); 1144 pass = processQuestion.call(this, node);
1138 } else if (node.specification.type == 'checkbox') { 1145 } else if (node.specification.type == 'checkbox') {
1139 // Must extract checkbox data 1146 // Must extract checkbox data
1140 processCheckbox(node); 1147 pass = processCheckbox.call(this, node);
1141 } else if (node.specification.type == "radio") { 1148 } else if (node.specification.type == "radio") {
1142 // Perform the conditional 1149 // Perform the conditional
1143 processRadio(node); 1150 pass = processRadio.call(this, node);
1144 } else if (node.specification.type == "number") { 1151 } else if (node.specification.type == "number") {
1145 // Perform the conditional 1152 // Perform the conditional
1146 processNumber(node); 1153 pass = processNumber.call(this, node);
1147 } else if (node.specification.type == 'slider') { 1154 } else if (node.specification.type == 'slider') {
1148 processSlider(node); 1155 pass = processSlider.call(this, node);
1156 }
1157 if (pass === false) {
1158 return;
1149 } 1159 }
1150 this.currentIndex++; 1160 this.currentIndex++;
1151 if (this.currentIndex < this.popupOptions.length) { 1161 if (this.currentIndex < this.popupOptions.length) {
1152 this.postNode(); 1162 this.postNode();
1153 } else { 1163 } else {