Mercurial > hg > webaudioevaluationtool
comparison ape.js @ 655:640a8d827e48
Add bibliography file to repo
author | Brecht De Man <BrechtDeMan@users.noreply.github.com> |
---|---|
date | Wed, 22 Apr 2015 10:05:11 +0100 |
parents | |
children | 06fbaccf3b58 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 655:640a8d827e48 |
---|---|
1 /** | |
2 * ape.js | |
3 * Create the APE interface | |
4 */ | |
5 | |
6 /* | |
7 * | |
8 * WARNING!!! | |
9 * | |
10 * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL | |
11 * | |
12 * WARNING!!! | |
13 * | |
14 */ | |
15 | |
16 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?) | |
17 // preTest - In preTest state | |
18 // testRun-ID - In test running, test Id number at the end 'testRun-2' | |
19 // testRunPost-ID - Post test of test ID | |
20 // testRunPre-ID - Pre-test of test ID | |
21 // postTest - End of test, final submission! | |
22 | |
23 | |
24 // Once this is loaded and parsed, begin execution | |
25 loadInterface(projectXML); | |
26 | |
27 function loadInterface(xmlDoc) { | |
28 | |
29 // Get the dimensions of the screen available to the page | |
30 var width = window.innerWidth; | |
31 var height = window.innerHeight; | |
32 | |
33 // The injection point into the HTML page | |
34 var insertPoint = document.getElementById("topLevelBody"); | |
35 var testContent = document.createElement('div'); | |
36 testContent.id = 'testContent'; | |
37 | |
38 | |
39 // Decode parts of the xmlDoc that are needed | |
40 // xmlDoc MUST already be parsed by jQuery! | |
41 var xmlSetup = xmlDoc.find('setup'); | |
42 // Should put in an error function here incase of malprocessed or malformed XML | |
43 | |
44 // Extract the different test XML DOM trees | |
45 var audioHolders = xmlDoc.find('audioHolder'); | |
46 audioHolders.each(function(index,element) { | |
47 var repeatN = element.attributes['repeatCount'].value; | |
48 for (var r=0; r<=repeatN; r++) { | |
49 testXMLSetups[testXMLSetups.length] = element; | |
50 } | |
51 }); | |
52 | |
53 // New check if we need to randomise the test order | |
54 var randomise = xmlSetup[0].attributes['randomiseOrder']; | |
55 if (randomise != undefined) { | |
56 randomise = Boolean(randomise.value); | |
57 } else { | |
58 randomise = false; | |
59 } | |
60 if (randomise) | |
61 { | |
62 testXMLSetups = randomiseOrder(testXMLSetups); | |
63 } | |
64 | |
65 // Obtain the metrics enabled | |
66 var metricNode = xmlSetup.find('Metric'); | |
67 var metricNode = metricNode.find('metricEnable'); | |
68 metricNode.each(function(index,node){ | |
69 var enabled = node.textContent; | |
70 switch(enabled) | |
71 { | |
72 case 'testTimer': | |
73 sessionMetrics.prototype.enableTestTimer = true; | |
74 break; | |
75 case 'elementTimer': | |
76 sessionMetrics.prototype.enableElementTimer = true; | |
77 break; | |
78 case 'elementTracker': | |
79 sessionMetrics.prototype.enableElementTracker = true; | |
80 break; | |
81 case 'elementInitalPosition': | |
82 sessionMetrics.prototype.enableElementInitialPosition = true; | |
83 break; | |
84 case 'elementFlagListenedTo': | |
85 sessionMetrics.prototype.enableFlagListenedTo = true; | |
86 break; | |
87 case 'elementFlagMoved': | |
88 sessionMetrics.prototype.enableFlagMoved = true; | |
89 break; | |
90 case 'elementFlagComments': | |
91 sessionMetrics.prototype.enableFlagComments = true; | |
92 break; | |
93 } | |
94 }); | |
95 | |
96 // Create APE specific metric functions | |
97 audioEngineContext.metric.initialiseTest = function() | |
98 { | |
99 var sliders = document.getElementsByClassName('track-slider'); | |
100 for (var i=0; i<sliders.length; i++) | |
101 { | |
102 audioEngineContext.audioObjects[i].metric.initialised(convSliderPosToRate(i)); | |
103 } | |
104 }; | |
105 | |
106 audioEngineContext.metric.sliderMoveStart = function(id) | |
107 { | |
108 if (this.data == -1) | |
109 { | |
110 this.data = id; | |
111 } else { | |
112 console.log('ERROR: Metric tracker detecting two moves!'); | |
113 this.data = -1; | |
114 } | |
115 }; | |
116 audioEngineContext.metric.sliderMoved = function() | |
117 { | |
118 var time = audioEngineContext.timer.getTestTime(); | |
119 var id = this.data; | |
120 this.data = -1; | |
121 var position = convSliderPosToRate(id); | |
122 if (audioEngineContext.timer.testStarted) | |
123 { | |
124 audioEngineContext.audioObjects[id].metric.moved(time,position); | |
125 } | |
126 }; | |
127 | |
128 audioEngineContext.metric.sliderPlayed = function(id) | |
129 { | |
130 var time = audioEngineContext.timer.getTestTime(); | |
131 if (audioEngineContext.timer.testStarted) | |
132 { | |
133 if (this.lastClicked >= 0) | |
134 { | |
135 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); | |
136 } | |
137 this.lastClicked = id; | |
138 audioEngineContext.audioObjects[id].metric.listening(time); | |
139 } | |
140 }; | |
141 | |
142 // Create the top div for the Title element | |
143 var titleAttr = xmlSetup[0].attributes['title']; | |
144 var title = document.createElement('div'); | |
145 title.className = "title"; | |
146 title.align = "center"; | |
147 var titleSpan = document.createElement('span'); | |
148 | |
149 // Set title to that defined in XML, else set to default | |
150 if (titleAttr != undefined) { | |
151 titleSpan.innerHTML = titleAttr.value; | |
152 } else { | |
153 titleSpan.innerHTML = 'APE Tool'; | |
154 } | |
155 // Insert the titleSpan element into the title div element. | |
156 title.appendChild(titleSpan); | |
157 | |
158 var pagetitle = document.createElement('div'); | |
159 pagetitle.className = "pageTitle"; | |
160 pagetitle.align = "center"; | |
161 var titleSpan = document.createElement('span'); | |
162 titleSpan.id = "pageTitle"; | |
163 pagetitle.appendChild(titleSpan); | |
164 | |
165 // Store the return URL path in global projectReturn | |
166 projectReturn = xmlSetup[0].attributes['projectReturn'].value; | |
167 | |
168 // Create Interface buttons! | |
169 var interfaceButtons = document.createElement('div'); | |
170 interfaceButtons.id = 'interface-buttons'; | |
171 | |
172 // MANUAL DOWNLOAD POINT | |
173 // If project return is null, this MUST be specified as the location to create the download link | |
174 var downloadPoint = document.createElement('div'); | |
175 downloadPoint.id = 'download-point'; | |
176 | |
177 // Create playback start/stop points | |
178 var playback = document.createElement("button"); | |
179 playback.innerHTML = 'Start'; | |
180 playback.id = 'playback-button'; | |
181 // onclick function. Check if it is playing or not, call the correct function in the | |
182 // audioEngine, change the button text to reflect the next state. | |
183 playback.onclick = function() { | |
184 if (audioEngineContext.status == 0) { | |
185 audioEngineContext.play(); | |
186 this.innerHTML = 'Stop'; | |
187 } else { | |
188 audioEngineContext.stop(); | |
189 this.innerHTML = 'Start'; | |
190 } | |
191 }; | |
192 // Create Submit (save) button | |
193 var submit = document.createElement("button"); | |
194 submit.innerHTML = 'Submit'; | |
195 submit.onclick = buttonSubmitClick; | |
196 submit.id = 'submit-button'; | |
197 // Append the interface buttons into the interfaceButtons object. | |
198 interfaceButtons.appendChild(playback); | |
199 interfaceButtons.appendChild(submit); | |
200 interfaceButtons.appendChild(downloadPoint); | |
201 | |
202 // Now create the slider and HTML5 canvas boxes | |
203 | |
204 // Create the div box to center align | |
205 var sliderBox = document.createElement('div'); | |
206 sliderBox.className = 'sliderCanvasDiv'; | |
207 sliderBox.id = 'sliderCanvasHolder'; | |
208 sliderBox.align = 'center'; | |
209 | |
210 // Create the slider box to hold the slider elements | |
211 var canvas = document.createElement('div'); | |
212 canvas.id = 'slider'; | |
213 // Must have a known EXACT width, as this is used later to determine the ratings | |
214 canvas.style.width = width - 100 +"px"; | |
215 canvas.align = "left"; | |
216 sliderBox.appendChild(canvas); | |
217 | |
218 // Create the div to hold any scale objects | |
219 var scale = document.createElement('div'); | |
220 scale.className = 'sliderScale'; | |
221 scale.id = 'sliderScaleHolder'; | |
222 scale.align = 'left'; | |
223 sliderBox.appendChild(scale); | |
224 | |
225 // Global parent for the comment boxes on the page | |
226 var feedbackHolder = document.createElement('div'); | |
227 feedbackHolder.id = 'feedbackHolder'; | |
228 | |
229 testContent.style.zIndex = 1; | |
230 insertPoint.innerHTML = null; // Clear the current schema | |
231 | |
232 // Create pre and post test questions | |
233 var blank = document.createElement('div'); | |
234 blank.className = 'testHalt'; | |
235 | |
236 var popupHolder = document.createElement('div'); | |
237 popupHolder.id = 'popupHolder'; | |
238 popupHolder.className = 'popupHolder'; | |
239 popupHolder.style.position = 'absolute'; | |
240 popupHolder.style.left = (window.innerWidth/2)-250 + 'px'; | |
241 popupHolder.style.top = (window.innerHeight/2)-125 + 'px'; | |
242 insertPoint.appendChild(popupHolder); | |
243 insertPoint.appendChild(blank); | |
244 hidePopup(); | |
245 | |
246 var preTest = xmlSetup.find('PreTest'); | |
247 var postTest = xmlSetup.find('PostTest'); | |
248 preTest = preTest[0]; | |
249 postTest = postTest[0]; | |
250 | |
251 currentState = 'preTest'; | |
252 | |
253 // Create Pre-Test Box | |
254 if (preTest != undefined && preTest.children.length >= 1) | |
255 { | |
256 showPopup(); | |
257 preTestPopupStart(preTest); | |
258 } | |
259 | |
260 // Inject into HTML | |
261 testContent.appendChild(title); // Insert the title | |
262 testContent.appendChild(pagetitle); | |
263 testContent.appendChild(interfaceButtons); | |
264 testContent.appendChild(sliderBox); | |
265 testContent.appendChild(feedbackHolder); | |
266 insertPoint.appendChild(testContent); | |
267 | |
268 // Load the full interface | |
269 | |
270 } | |
271 | |
272 function loadTest(id) | |
273 { | |
274 // Used to load a specific test page | |
275 var textXML = testXMLSetups[id]; | |
276 | |
277 var feedbackHolder = document.getElementById('feedbackHolder'); | |
278 var canvas = document.getElementById('slider'); | |
279 feedbackHolder.innerHTML = null; | |
280 canvas.innerHTML = null; | |
281 | |
282 // Setup question title | |
283 var interfaceObj = $(textXML).find('interface'); | |
284 var titleNode = interfaceObj.find('title'); | |
285 if (titleNode[0] != undefined) | |
286 { | |
287 document.getElementById('pageTitle').textContent = titleNode[0].textContent; | |
288 } | |
289 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); | |
290 var offset = 50-8; // Half the offset of the slider (window width -100) minus the body padding of 8 | |
291 // TODO: AUTOMATE ABOVE!! | |
292 var scale = document.getElementById('sliderScaleHolder'); | |
293 scale.innerHTML = null; | |
294 interfaceObj.find('scale').each(function(index,scaleObj){ | |
295 var position = Number(scaleObj.attributes['position'].value)*0.01; | |
296 var pixelPosition = (position*positionScale)+offset; | |
297 var scaleDOM = document.createElement('span'); | |
298 scaleDOM.textContent = scaleObj.textContent; | |
299 scale.appendChild(scaleDOM); | |
300 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; | |
301 }); | |
302 | |
303 // Extract the hostURL attribute. If not set, create an empty string. | |
304 var hostURL = textXML.attributes['hostURL']; | |
305 if (hostURL == undefined) { | |
306 hostURL = ""; | |
307 } else { | |
308 hostURL = hostURL.value; | |
309 } | |
310 // Extract the sampleRate. If set, convert the string to a Number. | |
311 var hostFs = textXML.attributes['sampleRate']; | |
312 if (hostFs != undefined) { | |
313 hostFs = Number(hostFs.value); | |
314 } | |
315 | |
316 /// CHECK FOR SAMPLE RATE COMPATIBILITY | |
317 if (hostFs != undefined) { | |
318 if (Number(hostFs) != audioContext.sampleRate) { | |
319 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; | |
320 alert(errStr); | |
321 return; | |
322 } | |
323 } | |
324 | |
325 var loopPlayback = textXML.attributes['loop']; | |
326 if (loopPlayback != undefined) | |
327 { | |
328 loopPlayback = loopPlayback.value; | |
329 if (loopPlayback == 'true') { | |
330 loopPlayback = true; | |
331 } else { | |
332 loopPlayback = false; | |
333 } | |
334 } else { | |
335 loopPlayback = false; | |
336 } | |
337 audioEngineContext.loopPlayback = loopPlayback; | |
338 | |
339 // Create AudioEngine bindings for playback | |
340 if (loopPlayback) { | |
341 audioEngineContext.play = function() { | |
342 // Send play command to all playback buffers for synchronised start | |
343 // Also start timer callbacks to detect if playback has finished | |
344 if (this.status == 0) { | |
345 this.timer.startTest(); | |
346 // First get current clock | |
347 var timer = audioContext.currentTime; | |
348 // Add 3 seconds | |
349 timer += 3.0; | |
350 // Send play to all tracks | |
351 for (var i=0; i<this.audioObjects.length; i++) | |
352 { | |
353 this.audioObjects[i].play(timer); | |
354 } | |
355 this.status = 1; | |
356 } | |
357 }; | |
358 | |
359 audioEngineContext.stop = function() { | |
360 // Send stop and reset command to all playback buffers | |
361 if (this.status == 1) { | |
362 if (this.loopPlayback) { | |
363 for (var i=0; i<this.audioObjects.length; i++) | |
364 { | |
365 this.audioObjects[i].stop(); | |
366 } | |
367 } | |
368 this.status = 0; | |
369 } | |
370 }; | |
371 | |
372 audioEngineContext.selectedTrack = function(id) { | |
373 for (var i=0; i<this.audioObjects.length; i++) | |
374 { | |
375 if (id == i) { | |
376 this.audioObjects[i].outputGain.gain.value = 1.0; | |
377 } else { | |
378 this.audioObjects[i].outputGain.gain.value = 0.0; | |
379 } | |
380 } | |
381 }; | |
382 } else { | |
383 audioEngineContext.play = function() { | |
384 // Send play command to all playback buffers for synchronised start | |
385 // Also start timer callbacks to detect if playback has finished | |
386 if (this.status == 0) { | |
387 this.timer.startTest(); | |
388 this.status = 1; | |
389 } | |
390 }; | |
391 | |
392 audioEngineContext.stop = function() { | |
393 // Send stop and reset command to all playback buffers | |
394 if (this.status == 1) { | |
395 if (this.loopPlayback) { | |
396 for (var i=0; i<this.audioObjects.length; i++) | |
397 { | |
398 this.audioObjects[i].stop(); | |
399 } | |
400 } | |
401 this.status = 0; | |
402 } | |
403 }; | |
404 | |
405 audioEngineContext.selectedTrack = function(id) { | |
406 for (var i=0; i<this.audioObjects.length; i++) | |
407 { | |
408 if (id == i) { | |
409 this.audioObjects[i].outputGain.gain.value = 1.0; | |
410 this.audioObjects[i].play(audioContext.currentTime+0.01); | |
411 } else { | |
412 this.audioObjects[i].outputGain.gain.value = 0.0; | |
413 } | |
414 } | |
415 }; | |
416 } | |
417 | |
418 currentTestHolder = document.createElement('audioHolder'); | |
419 currentTestHolder.id = textXML.id; | |
420 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value; | |
421 var currentPreTestHolder = document.createElement('preTest'); | |
422 var currentPostTestHolder = document.createElement('postTest'); | |
423 currentTestHolder.appendChild(currentPreTestHolder); | |
424 currentTestHolder.appendChild(currentPostTestHolder); | |
425 | |
426 var randomise = textXML.attributes['randomiseOrder']; | |
427 if (randomise != undefined) {randomise = randomise.value;} | |
428 else {randomise = false;} | |
429 | |
430 var audioElements = $(textXML).find('audioElements'); | |
431 currentTrackOrder = []; | |
432 audioElements.each(function(index,element){ | |
433 // Find any blind-repeats | |
434 // Not implemented yet, but just incase | |
435 currentTrackOrder[index] = element; | |
436 }); | |
437 if (randomise) { | |
438 currentTrackOrder = randomiseOrder(currentTrackOrder); | |
439 } | |
440 | |
441 // Delete any previous audioObjects associated with the audioEngine | |
442 audioEngineContext.audioObjects = []; | |
443 | |
444 // Find all the audioElements from the audioHolder | |
445 $(currentTrackOrder).each(function(index,element){ | |
446 // Find URL of track | |
447 // In this jQuery loop, variable 'this' holds the current audioElement. | |
448 | |
449 // Now load each audio sample. First create the new track by passing the full URL | |
450 var trackURL = hostURL + this.attributes['url'].value; | |
451 audioEngineContext.newTrack(trackURL); | |
452 // Create document objects to hold the comment boxes | |
453 var trackComment = document.createElement('div'); | |
454 trackComment.className = 'comment-div'; | |
455 // Create a string next to each comment asking for a comment | |
456 var trackString = document.createElement('span'); | |
457 trackString.innerHTML = 'Comment on track '+index; | |
458 // Create the HTML5 comment box 'textarea' | |
459 var trackCommentBox = document.createElement('textarea'); | |
460 trackCommentBox.rows = '4'; | |
461 trackCommentBox.cols = '100'; | |
462 trackCommentBox.name = 'trackComment'+index; | |
463 trackCommentBox.className = 'trackComment'; | |
464 var br = document.createElement('br'); | |
465 // Add to the holder. | |
466 trackComment.appendChild(trackString); | |
467 trackComment.appendChild(br); | |
468 trackComment.appendChild(trackCommentBox); | |
469 feedbackHolder.appendChild(trackComment); | |
470 | |
471 // Create a slider per track | |
472 | |
473 var trackSliderObj = document.createElement('div'); | |
474 trackSliderObj.className = 'track-slider'; | |
475 trackSliderObj.id = 'track-slider-'+index; | |
476 // Distribute it randomnly | |
477 var w = window.innerWidth - 100; | |
478 w = Math.random()*w; | |
479 trackSliderObj.style.left = Math.floor(w)+50+'px'; | |
480 trackSliderObj.innerHTML = '<span>'+index+'</span>'; | |
481 trackSliderObj.draggable = true; | |
482 trackSliderObj.ondragend = dragEnd; | |
483 trackSliderObj.ondragstart = function() | |
484 { | |
485 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! | |
486 audioEngineContext.metric.sliderMoveStart(id); | |
487 }; | |
488 | |
489 // Onclick, switch playback to that track | |
490 trackSliderObj.onclick = function() { | |
491 // Get the track ID from the object ID | |
492 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! | |
493 audioEngineContext.metric.sliderPlayed(id); | |
494 audioEngineContext.selectedTrack(id); | |
495 }; | |
496 | |
497 canvas.appendChild(trackSliderObj); | |
498 }); | |
499 | |
500 // Now process any pre-test commands | |
501 | |
502 var preTest = $(testXMLSetups[id]).find('PreTest')[0]; | |
503 if (preTest.children.length > 0) | |
504 { | |
505 currentState = 'testRunPre-'+id; | |
506 preTestPopupStart(preTest); | |
507 showPopup(); | |
508 } else { | |
509 currentState = 'testRun-'+id; | |
510 } | |
511 } | |
512 | |
513 function preTestPopupStart(preTest) | |
514 { | |
515 var popupHolder = document.getElementById('popupHolder'); | |
516 popupHolder.innerHTML = null; | |
517 // Parse the first box | |
518 var preTestOption = document.createElement('div'); | |
519 preTestOption.id = 'preTest'; | |
520 preTestOption.style.marginTop = '25px'; | |
521 preTestOption.align = "center"; | |
522 var child = preTest.children[0]; | |
523 if (child.nodeName == 'statement') | |
524 { | |
525 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>'; | |
526 } else if (child.nodeName == 'question') | |
527 { | |
528 var questionId = child.attributes['id'].value; | |
529 var textHold = document.createElement('span'); | |
530 textHold.innerHTML = child.innerHTML; | |
531 textHold.id = questionId + 'response'; | |
532 var textEnter = document.createElement('textarea'); | |
533 preTestOption.appendChild(textHold); | |
534 preTestOption.appendChild(textEnter); | |
535 } | |
536 var nextButton = document.createElement('button'); | |
537 nextButton.className = 'popupButton'; | |
538 nextButton.value = '0'; | |
539 nextButton.innerHTML = 'Next'; | |
540 nextButton.onclick = popupButtonClick; | |
541 | |
542 popupHolder.appendChild(preTestOption); | |
543 popupHolder.appendChild(nextButton); | |
544 } | |
545 | |
546 function popupButtonClick() | |
547 { | |
548 // Global call from the 'Next' button click | |
549 if (currentState == 'preTest') | |
550 { | |
551 // At the start of the preTest routine! | |
552 var xmlTree = projectXML.find('setup'); | |
553 var preTest = xmlTree.find('PreTest')[0]; | |
554 this.value = preTestButtonClick(preTest,this.value); | |
555 } else if (currentState.substr(0,10) == 'testRunPre') | |
556 { | |
557 //Specific test pre-test | |
558 var testId = currentState.substr(11,currentState.length-10); | |
559 var preTest = $(testXMLSetups[testId]).find('PreTest')[0]; | |
560 this.value = preTestButtonClick(preTest,this.value); | |
561 } else if (currentState.substr(0,11) == 'testRunPost') | |
562 { | |
563 // Specific test post-test | |
564 var testId = currentState.substr(12,currentState.length-11); | |
565 var preTest = $(testXMLSetups[testId]).find('PostTest')[0]; | |
566 this.value = preTestButtonClick(preTest,this.value); | |
567 } else if (currentState == 'postTest') | |
568 { | |
569 // At the end of the test, running global post test | |
570 var xmlTree = projectXML.find('setup'); | |
571 var PostTest = xmlTree.find('PostTest')[0]; | |
572 this.value = preTestButtonClick(PostTest,this.value); | |
573 } | |
574 } | |
575 | |
576 function preTestButtonClick(preTest,index) | |
577 { | |
578 // Called on click of pre-test button | |
579 // Need to find and parse preTest again! | |
580 var preTestOption = document.getElementById('preTest'); | |
581 // Check if current state is a question! | |
582 if (preTest.children[index].nodeName == 'question') { | |
583 var questionId = preTest.children[index].attributes['id'].value; | |
584 var questionHold = document.createElement('comment'); | |
585 var questionResponse = document.getElementById(questionId + 'response'); | |
586 questionHold.id = questionId; | |
587 questionHold.innerHTML = questionResponse.value; | |
588 postPopupResponse(questionHold); | |
589 } | |
590 index++; | |
591 if (index < preTest.children.length) | |
592 { | |
593 // More to process | |
594 var child = preTest.children[index]; | |
595 if (child.nodeName == 'statement') | |
596 { | |
597 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>'; | |
598 } else if (child.nodeName == 'question') | |
599 { | |
600 var textHold = document.createElement('span'); | |
601 textHold.innerHTML = child.innerHTML; | |
602 var textEnter = document.createElement('textarea'); | |
603 textEnter.id = child.attributes['id'].value + 'response'; | |
604 var br = document.createElement('br'); | |
605 preTestOption.innerHTML = null; | |
606 preTestOption.appendChild(textHold); | |
607 preTestOption.appendChild(br); | |
608 preTestOption.appendChild(textEnter); | |
609 } | |
610 } else { | |
611 // Time to clear | |
612 preTestOption.innerHTML = null; | |
613 if (currentState != 'postTest') { | |
614 hidePopup(); | |
615 // Progress the state! | |
616 advanceState(); | |
617 } else { | |
618 a = createProjectSave(projectReturn); | |
619 preTestOption.appendChild(a); | |
620 } | |
621 } | |
622 return index; | |
623 } | |
624 | |
625 function postPopupResponse(response) | |
626 { | |
627 if (currentState == 'preTest') { | |
628 preTestQuestions.appendChild(response); | |
629 } else if (currentState == 'postTest') { | |
630 postTestQuestions.appendChild(response); | |
631 } else { | |
632 // Inside a specific test | |
633 if (currentState.substr(0,10) == 'testRunPre') { | |
634 // Pre Test | |
635 var store = $(currentTestHolder).find('preTest'); | |
636 } else { | |
637 // Post Test | |
638 var store = $(currentTestHolder).find('postTest'); | |
639 } | |
640 store[0].appendChild(response); | |
641 } | |
642 } | |
643 | |
644 function showPopup() | |
645 { | |
646 var popupHolder = document.getElementById('popupHolder'); | |
647 popupHolder.style.zIndex = 3; | |
648 popupHolder.style.visibility = 'visible'; | |
649 var blank = document.getElementsByClassName('testHalt')[0]; | |
650 blank.style.zIndex = 2; | |
651 blank.style.visibility = 'visible'; | |
652 } | |
653 | |
654 function hidePopup() | |
655 { | |
656 var popupHolder = document.getElementById('popupHolder'); | |
657 popupHolder.style.zIndex = -1; | |
658 popupHolder.style.visibility = 'hidden'; | |
659 var blank = document.getElementsByClassName('testHalt')[0]; | |
660 blank.style.zIndex = -2; | |
661 blank.style.visibility = 'hidden'; | |
662 } | |
663 | |
664 function dragEnd(ev) { | |
665 // Function call when a div has been dropped | |
666 var slider = document.getElementById('slider'); | |
667 var w = slider.style.width; | |
668 w = Number(w.substr(0,w.length-2)); | |
669 var x = ev.x; | |
670 if (x >= 42 && x < w+42) { | |
671 this.style.left = (x)+'px'; | |
672 } else { | |
673 if (x<42) { | |
674 this.style.left = '42px'; | |
675 } else { | |
676 this.style.left = (w+42) + 'px'; | |
677 } | |
678 } | |
679 audioEngineContext.metric.sliderMoved(); | |
680 } | |
681 | |
682 function advanceState() | |
683 { | |
684 console.log(currentState); | |
685 if (currentState == 'preTest') | |
686 { | |
687 // End of pre-test, begin the test | |
688 loadTest(0); | |
689 } else if (currentState.substr(0,10) == 'testRunPre') | |
690 { | |
691 // Start the test | |
692 var testId = currentState.substr(11,currentState.length-10); | |
693 currentState = 'testRun-'+testId; | |
694 } else if (currentState.substr(0,11) == 'testRunPost') | |
695 { | |
696 var testId = currentState.substr(12,currentState.length-11); | |
697 testEnded(testId); | |
698 } else if (currentState.substr(0,7) == 'testRun') | |
699 { | |
700 var testId = currentState.substr(8,currentState.length-7); | |
701 // Check if we have any post tests to perform | |
702 var postXML = $(testXMLSetups[testId]).find('PostTest')[0]; | |
703 if (postXML.children.length > 0) | |
704 { | |
705 currentState = 'testRunPost-'+testId; | |
706 showPopup(); | |
707 preTestPopupStart(postXML); | |
708 } | |
709 else { | |
710 | |
711 | |
712 // No post tests, check if we have another test to perform instead | |
713 testEnded(testId); | |
714 } | |
715 } | |
716 console.log(currentState); | |
717 } | |
718 | |
719 function testEnded(testId) | |
720 { | |
721 pageXMLSave(testId); | |
722 if (testXMLSetups.length-1 > testId) | |
723 { | |
724 // Yes we have another test to perform | |
725 testId = (Number(testId)+1); | |
726 currentState = 'testRun-'+testId; | |
727 loadTest(testId); | |
728 } else { | |
729 console.log('Testing Completed!'); | |
730 currentState = 'postTest'; | |
731 // Check for any post tests | |
732 var xmlSetup = projectXML.find('setup'); | |
733 var postTest = xmlSetup.find('PostTest')[0]; | |
734 showPopup(); | |
735 preTestPopupStart(postTest); | |
736 } | |
737 } | |
738 | |
739 function buttonSubmitClick() | |
740 { | |
741 if (audioEngineContext.status == 1) { | |
742 var playback = document.getElementById('playback-button'); | |
743 playback.click(); | |
744 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options | |
745 } else | |
746 { | |
747 if (audioEngineContext.timer.testStarted == false) | |
748 { | |
749 alert('You have not started the test! Please press start to begin the test!'); | |
750 return; | |
751 } | |
752 } | |
753 if (currentState.substr(0,7) == 'testRun') | |
754 { | |
755 audioEngineContext.timer.stopTest(); | |
756 advanceState(); | |
757 } | |
758 } | |
759 | |
760 function convSliderPosToRate(id) | |
761 { | |
762 var w = document.getElementById('slider').style.width; | |
763 var maxPix = w.substr(0,w.length-2); | |
764 var slider = document.getElementsByClassName('track-slider')[id]; | |
765 var pix = slider.style.left; | |
766 pix = pix.substr(0,pix.length-2); | |
767 var rate = (pix-42)/maxPix; | |
768 return rate; | |
769 } | |
770 | |
771 function pageXMLSave(testId) | |
772 { | |
773 // Saves a specific test page | |
774 var xmlDoc = currentTestHolder; | |
775 // Check if any session wide metrics are enabled | |
776 var metric = document.createElement('metric'); | |
777 if (audioEngineContext.metric.enableTestTimer) | |
778 { | |
779 var testTime = document.createElement('metricResult'); | |
780 testTime.id = 'testTime'; | |
781 testTime.textContent = audioEngineContext.timer.testDuration; | |
782 metric.appendChild(testTime); | |
783 } | |
784 xmlDoc.appendChild(metric); | |
785 var trackSliderObjects = document.getElementsByClassName('track-slider'); | |
786 var commentObjects = document.getElementsByClassName('comment-div'); | |
787 for (var i=0; i<trackSliderObjects.length; i++) | |
788 { | |
789 var audioElement = document.createElement('audioElement'); | |
790 audioElement.id = currentTrackOrder[i].attributes['id'].value; | |
791 audioElement.url = currentTrackOrder[i].attributes['url'].value; | |
792 var value = document.createElement('value'); | |
793 value.innerHTML = convSliderPosToRate(i); | |
794 var comment = document.createElement("comment"); | |
795 var question = document.createElement("question"); | |
796 var response = document.createElement("response"); | |
797 question.textContent = commentObjects[i].children[0].textContent; | |
798 response.textContent = commentObjects[i].children[2].value; | |
799 comment.appendChild(question); | |
800 comment.appendChild(response); | |
801 audioElement.appendChild(value); | |
802 audioElement.appendChild(comment); | |
803 // Check for any per element metrics | |
804 var metric = document.createElement('metric'); | |
805 var elementMetric = audioEngineContext.audioObjects[i].metric; | |
806 if (audioEngineContext.metric.enableElementTimer) { | |
807 var elementTimer = document.createElement('metricResult'); | |
808 elementTimer.id = 'elementTimer'; | |
809 elementTimer.textContent = elementMetric.listenedTimer; | |
810 metric.appendChild(elementTimer); | |
811 } | |
812 if (audioEngineContext.metric.enableElementTracker) { | |
813 var elementTrackerFull = document.createElement('metricResult'); | |
814 elementTrackerFull.id = 'elementTrackerFull'; | |
815 var data = elementMetric.movementTracker; | |
816 for (var k=0; k<data.length; k++) | |
817 { | |
818 var timePos = document.createElement('timePos'); | |
819 timePos.id = k; | |
820 var time = document.createElement('time'); | |
821 time.textContent = data[k][0]; | |
822 var position = document.createElement('position'); | |
823 position.textContent = data[k][1]; | |
824 timePos.appendChild(time); | |
825 timePos.appendChild(position); | |
826 elementTrackerFull.appendChild(timePos); | |
827 } | |
828 metric.appendChild(elementTrackerFull); | |
829 } | |
830 if (audioEngineContext.metric.enableElementInitialPosition) { | |
831 var elementInitial = document.createElement('metricResult'); | |
832 elementInitial.id = 'elementInitialPosition'; | |
833 elementInitial.textContent = elementMetric.initialPosition; | |
834 metric.appendChild(elementInitial); | |
835 } | |
836 if (audioEngineContext.metric.enableFlagListenedTo) { | |
837 var flagListenedTo = document.createElement('metricResult'); | |
838 flagListenedTo.id = 'elementFlagListenedTo'; | |
839 flagListenedTo.textContent = elementMetric.wasListenedTo; | |
840 metric.appendChild(flagListenedTo); | |
841 } | |
842 if (audioEngineContext.metric.enableFlagMoved) { | |
843 var flagMoved = document.createElement('metricResult'); | |
844 flagMoved.id = 'elementFlagMoved'; | |
845 flagMoved.textContent = elementMetric.wasMoved; | |
846 metric.appendChild(flagMoved); | |
847 } | |
848 if (audioEngineContext.metric.enableFlagComments) { | |
849 var flagComments = document.createElement('metricResult'); | |
850 flagComments.id = 'elementFlagComments'; | |
851 if (response.textContent.length == 0) {flag.textContent = 'false';} | |
852 else {flag.textContet = 'true';} | |
853 metric.appendChild(flagComments); | |
854 } | |
855 audioElement.appendChild(metric); | |
856 xmlDoc.appendChild(audioElement); | |
857 } | |
858 testResultsHolders[testId] = xmlDoc; | |
859 } | |
860 | |
861 // Only other global function which must be defined in the interface class. Determines how to create the XML document. | |
862 function interfaceXMLSave(){ | |
863 // Create the XML string to be exported with results | |
864 var xmlDoc = document.createElement("BrowserEvaluationResult"); | |
865 for (var i=0; i<testResultsHolders.length; i++) | |
866 { | |
867 xmlDoc.appendChild(testResultsHolders[i]); | |
868 } | |
869 // Append Pre/Post Questions | |
870 xmlDoc.appendChild(preTestQuestions); | |
871 xmlDoc.appendChild(postTestQuestions); | |
872 | |
873 return xmlDoc; | |
874 } | |
875 |