comparison core.js @ 1622:ee9184f7f8d5

Popup now in core.js. Needs stress testing!!
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 26 May 2015 13:01:39 +0100
parents f8220eeeeddc
children 17353d015d33
comparison
equal deleted inserted replaced
1621:f8220eeeeddc 1622:ee9184f7f8d5
6 */ 6 */
7 7
8 /* create the web audio API context and store in audioContext*/ 8 /* create the web audio API context and store in audioContext*/
9 var audioContext; // Hold the browser web audio API 9 var audioContext; // Hold the browser web audio API
10 var projectXML; // Hold the parsed setup XML 10 var projectXML; // Hold the parsed setup XML
11 11 var popup; // Hold the interfacePopup object
12 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
12 var testXMLSetups = []; // Hold the parsed test instances 13 var testXMLSetups = []; // Hold the parsed test instances
13 var testResultsHolders =[]; // Hold the results from each test for publishing to XML 14 var testResultsHolders =[]; // Hold the results from each test for publishing to XML
14 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
15 var currentTestHolder; // Hold any intermediate results during test - metrics 16 var currentTestHolder; // Hold any intermediate results during test - metrics
16 var audioEngineContext; // The custome AudioEngine object 17 var audioEngineContext; // The custome AudioEngine object
30 var AudioContext = window.AudioContext || window.webkitAudioContext; 31 var AudioContext = window.AudioContext || window.webkitAudioContext;
31 audioContext = new AudioContext; 32 audioContext = new AudioContext;
32 33
33 // Create the audio engine object 34 // Create the audio engine object
34 audioEngineContext = new AudioEngine(); 35 audioEngineContext = new AudioEngine();
36
37 // Create the popup interface object
38 popup = new interfacePopup();
35 }; 39 };
36 40
37 function createPopup() { 41 function interfacePopup() {
38 // Create popup window interface 42 // Creates an object to manage the popup
39 var insertPoint = document.getElementById("topLevelBody"); 43 this.popup = null;
40 var blank = document.createElement('div'); 44 this.popupContent = null;
41 blank.className = 'testHalt'; 45 this.popupButton = null;
42 46 this.popupOptions = null;
43 var popupHolder = document.createElement('div'); 47 this.currentIndex = null;
44 popupHolder.id = 'popupHolder'; 48 this.responses = null;
45 popupHolder.className = 'popupHolder'; 49 this.createPopup = function(){
46 popupHolder.style.position = 'absolute'; 50 // Create popup window interface
47 popupHolder.style.left = (window.innerWidth/2)-250 + 'px'; 51 var insertPoint = document.getElementById("topLevelBody");
48 popupHolder.style.top = (window.innerHeight/2)-125 + 'px'; 52 var blank = document.createElement('div');
49 insertPoint.appendChild(popupHolder); 53 blank.className = 'testHalt';
50 insertPoint.appendChild(blank); 54
51 } 55 this.popup = document.createElement('div');
52 56 this.popup.id = 'popupHolder';
53 function showPopup() 57 this.popup.className = 'popupHolder';
58 this.popup.style.position = 'absolute';
59 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
60 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
61
62 this.popupContent = document.createElement('div');
63 this.popupContent.id = 'popupContent';
64 this.popupContent.style.marginTop = '25px';
65 this.popupContent.align = 'center';
66 this.popup.appendChild(this.popupContent);
67
68 this.popupButton = document.createElement('button');
69 this.popupButton.className = 'popupButton';
70 this.popupButton.innerHTML = 'Next';
71 this.popupButton.onclick = function(){popup.buttonClicked();};
72 insertPoint.appendChild(this.popup);
73 insertPoint.appendChild(blank);
74 };
75
76 this.showPopup = function(){
77 if (this.popup == null || this.popup == undefined) {
78 this.createPopup();
79 }
80 this.popup.style.zIndex = 3;
81 this.popup.style.visibility = 'visible';
82 var blank = document.getElementsByClassName('testHalt')[0];
83 blank.style.zIndex = 2;
84 blank.style.visibility = 'visible';
85 };
86
87 this.hidePopup = function(){
88 this.popup.style.zIndex = -1;
89 this.popup.style.visibility = 'hidden';
90 var blank = document.getElementsByClassName('testHalt')[0];
91 blank.style.zIndex = -2;
92 blank.style.visibility = 'hidden';
93 };
94
95 this.postNode = function() {
96 // This will take the node from the popupOptions and display it
97 var node = this.popupOptions[this.currentIndex];
98 this.popupContent.innerHTML = null;
99 if (node.nodeName == 'statement') {
100 var span = document.createElement('span');
101 span.textContent = node.textContent;
102 this.popupContent.appendChild(span);
103 } else if (node.nodeName == 'question') {
104 var span = document.createElement('span');
105 span.textContent = node.textContent;
106 var textArea = document.createElement('textarea');
107 var br = document.createElement('br');
108 this.popupContent.appendChild(span);
109 this.popupContent.appendChild(br);
110 this.popupContent.appendChild(textArea);
111 }
112 this.popupContent.appendChild(this.popupButton);
113 }
114
115 this.initState = function(node) {
116 //Call this with your preTest and postTest nodes when needed to
117 // initialise the popup procedure.
118 this.popupOptions = $(node).children();
119 if (this.popupOptions.length > 0) {
120 if (node.nodeName == 'preTest' || node.nodeName == 'PreTest') {
121 this.responses = document.createElement('PreTest');
122 } else if (node.nodeName == 'postTest' || node.nodeName == 'PostTest') {
123 this.responses = document.createElement('PostTest');
124 } else {
125 console.log ('WARNING - popup node neither pre or post!');
126 this.responses = document.createElement('responses');
127 }
128 this.currentIndex = 0;
129 this.showPopup();
130 this.postNode();
131 }
132 }
133
134 this.buttonClicked = function() {
135 // Each time the popup button is clicked!
136 var node = this.popupOptions[this.currentIndex];
137 if (node.nodeName == 'question') {
138 // Must extract the question data
139 var mandatory = node.attributes['mandatory'];
140 if (mandatory == undefined) {
141 mandatory = false;
142 } else {
143 if (mandatory.value == 'true'){mandatory = true;}
144 else {mandatory = false;}
145 }
146 var textArea = $(popup.popupContent).find('textarea')[0];
147 if (mandatory == true && textArea.value.length == 0) {
148 alert('This question is mandatory');
149 return;
150 } else {
151 // Save the text content
152 var hold = document.createElement('comment');
153 hold.id = node.attributes['id'].value;
154 hold.innerHTML = textArea.value;
155 this.responses.appendChild(hold);
156 }
157 }
158 this.currentIndex++;
159 if (this.currentIndex < this.popupOptions.length) {
160 this.postNode();
161 } else {
162 // Reached the end of the popupOptions
163 this.hidePopup();
164 advanceState();
165 }
166 }
167 }
168
169 function advanceState()
54 { 170 {
55 var popupHolder = document.getElementById('popupHolder'); 171 console.log(currentState);
56 if (popupHolder == null || popupHolder == undefined) { 172 if (currentState == 'preTest')
57 createPopup(); 173 {
58 popupHolder = document.getElementById('popupHolder'); 174 // End of pre-test, begin the test
59 } 175 preTestQuestions = popup.responses;
60 popupHolder.style.zIndex = 3; 176 loadTest(0);
61 popupHolder.style.visibility = 'visible'; 177 } else if (currentState == 'postTest') {
62 var blank = document.getElementsByClassName('testHalt')[0]; 178 postTestQuestions = popup.responses;
63 blank.style.zIndex = 2; 179 console.log('ALL COLLECTED!');
64 blank.style.visibility = 'visible'; 180 }else if (currentState.substr(0,10) == 'testRunPre')
65 } 181 {
66 182 // Start the test
67 function hidePopup() 183 var testId = currentState.substr(11,currentState.length-10);
184 currentState = 'testRun-'+testId;
185 currentTestHolder.appendChild(popup.responses);
186 //audioEngineContext.timer.startTest();
187 //audioEngineContext.play();
188 } else if (currentState.substr(0,11) == 'testRunPost')
189 {
190 var testId = currentState.substr(12,currentState.length-11);
191 currentTestHolder.appendChild(popup.responses);
192 testEnded(testId);
193 } else if (currentState.substr(0,7) == 'testRun')
194 {
195 var testId = currentState.substr(8,currentState.length-7);
196 // Check if we have any post tests to perform
197 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
198 if (postXML == undefined || postXML.childElementCount == 0) {
199 testEnded(testId);
200 }
201 else if (postXML.childElementCount > 0)
202 {
203 currentState = 'testRunPost-'+testId;
204 popup.initState(postXML);
205 }
206 else {
207
208
209 // No post tests, check if we have another test to perform instead
210
211 }
212 }
213 console.log(currentState);
214 }
215
216 function testEnded(testId)
68 { 217 {
69 var popupHolder = document.getElementById('popupHolder'); 218 pageXMLSave(testId);
70 popupHolder.style.zIndex = -1; 219 if (testXMLSetups.length-1 > testId)
71 popupHolder.style.visibility = 'hidden'; 220 {
72 var blank = document.getElementsByClassName('testHalt')[0]; 221 // Yes we have another test to perform
73 blank.style.zIndex = -2; 222 testId = (Number(testId)+1);
74 blank.style.visibility = 'hidden'; 223 currentState = 'testRun-'+testId;
224 loadTest(testId);
225 } else {
226 console.log('Testing Completed!');
227 currentState = 'postTest';
228 // Check for any post tests
229 var xmlSetup = projectXML.find('setup');
230 var postTest = xmlSetup.find('PostTest')[0];
231 popup.initState(postTest);
232 }
75 } 233 }
76 234
77 function loadProjectSpec(url) { 235 function loadProjectSpec(url) {
78 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data 236 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
79 // If url is null, request client to upload project XML document 237 // If url is null, request client to upload project XML document