Mercurial > hg > webaudioevaluationtool
comparison core.js @ 893:c40ce9c67b6b
Merge from the default branch
author | Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk> |
---|---|
date | Wed, 10 Jun 2015 14:39:15 +0100 |
parents | |
children | 1af8a548cab2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 893:c40ce9c67b6b |
---|---|
1 /** | |
2 * core.js | |
3 * | |
4 * Main script to run, calls all other core functions and manages loading/store to backend. | |
5 * Also contains all global variables. | |
6 */ | |
7 | |
8 /* create the web audio API context and store in audioContext*/ | |
9 var audioContext; // Hold the browser web audio API | |
10 var projectXML; // Hold the parsed setup XML | |
11 var specification; | |
12 var interfaceContext; | |
13 var popup; // Hold the interfacePopup object | |
14 var testState; | |
15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order | |
16 var audioEngineContext; // The custome AudioEngine object | |
17 var projectReturn; // Hold the URL for the return | |
18 | |
19 | |
20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it | |
21 AudioBufferSourceNode.prototype.owner = undefined; | |
22 | |
23 window.onload = function() { | |
24 // Function called once the browser has loaded all files. | |
25 // This should perform any initial commands such as structure / loading documents | |
26 | |
27 // Create a web audio API context | |
28 // Fixed for cross-browser support | |
29 var AudioContext = window.AudioContext || window.webkitAudioContext; | |
30 audioContext = new AudioContext; | |
31 | |
32 // Create test state | |
33 testState = new stateMachine(); | |
34 | |
35 // Create the audio engine object | |
36 audioEngineContext = new AudioEngine(); | |
37 | |
38 // Create the popup interface object | |
39 popup = new interfacePopup(); | |
40 | |
41 // Create the specification object | |
42 specification = new Specification(); | |
43 | |
44 // Create the interface object | |
45 interfaceContext = new Interface(specification); | |
46 }; | |
47 | |
48 function interfacePopup() { | |
49 // Creates an object to manage the popup | |
50 this.popup = null; | |
51 this.popupContent = null; | |
52 this.buttonProceed = null; | |
53 this.buttonPrevious = null; | |
54 this.popupOptions = null; | |
55 this.currentIndex = null; | |
56 this.responses = null; | |
57 | |
58 this.createPopup = function(){ | |
59 // Create popup window interface | |
60 var insertPoint = document.getElementById("topLevelBody"); | |
61 var blank = document.createElement('div'); | |
62 blank.className = 'testHalt'; | |
63 | |
64 this.popup = document.createElement('div'); | |
65 this.popup.id = 'popupHolder'; | |
66 this.popup.className = 'popupHolder'; | |
67 this.popup.style.position = 'absolute'; | |
68 this.popup.style.left = (window.innerWidth/2)-250 + 'px'; | |
69 this.popup.style.top = (window.innerHeight/2)-125 + 'px'; | |
70 | |
71 this.popupContent = document.createElement('div'); | |
72 this.popupContent.id = 'popupContent'; | |
73 this.popupContent.style.marginTop = '25px'; | |
74 this.popupContent.align = 'center'; | |
75 this.popup.appendChild(this.popupContent); | |
76 | |
77 this.buttonProceed = document.createElement('button'); | |
78 this.buttonProceed.className = 'popupButton'; | |
79 this.buttonProceed.style.left = '440px'; | |
80 this.buttonProceed.style.top = '215px'; | |
81 this.buttonProceed.innerHTML = 'Next'; | |
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 | |
91 this.popup.style.zIndex = -1; | |
92 this.popup.style.visibility = 'hidden'; | |
93 blank.style.zIndex = -2; | |
94 blank.style.visibility = 'hidden'; | |
95 insertPoint.appendChild(this.popup); | |
96 insertPoint.appendChild(blank); | |
97 }; | |
98 | |
99 this.showPopup = function(){ | |
100 if (this.popup == null) { | |
101 this.createPopup(); | |
102 } | |
103 this.popup.style.zIndex = 3; | |
104 this.popup.style.visibility = 'visible'; | |
105 var blank = document.getElementsByClassName('testHalt')[0]; | |
106 blank.style.zIndex = 2; | |
107 blank.style.visibility = 'visible'; | |
108 }; | |
109 | |
110 this.hidePopup = function(){ | |
111 this.popup.style.zIndex = -1; | |
112 this.popup.style.visibility = 'hidden'; | |
113 var blank = document.getElementsByClassName('testHalt')[0]; | |
114 blank.style.zIndex = -2; | |
115 blank.style.visibility = 'hidden'; | |
116 }; | |
117 | |
118 this.postNode = function() { | |
119 // This will take the node from the popupOptions and display it | |
120 var node = this.popupOptions[this.currentIndex]; | |
121 this.popupContent.innerHTML = null; | |
122 if (node.type == 'statement') { | |
123 var span = document.createElement('span'); | |
124 span.textContent = node.statement; | |
125 this.popupContent.appendChild(span); | |
126 } else if (node.type == 'question') { | |
127 var span = document.createElement('span'); | |
128 span.textContent = node.question; | |
129 var textArea = document.createElement('textarea'); | |
130 switch (node.boxsize) { | |
131 case 'small': | |
132 textArea.cols = "20"; | |
133 textArea.rows = "1"; | |
134 break; | |
135 case 'normal': | |
136 textArea.cols = "30"; | |
137 textArea.rows = "2"; | |
138 break; | |
139 case 'large': | |
140 textArea.cols = "40"; | |
141 textArea.rows = "5"; | |
142 break; | |
143 case 'huge': | |
144 textArea.cols = "50"; | |
145 textArea.rows = "10"; | |
146 break; | |
147 } | |
148 var br = document.createElement('br'); | |
149 this.popupContent.appendChild(span); | |
150 this.popupContent.appendChild(br); | |
151 this.popupContent.appendChild(textArea); | |
152 this.popupContent.childNodes[2].focus(); | |
153 } else if (node.type == 'checkbox') { | |
154 var span = document.createElement('span'); | |
155 span.textContent = node.statement; | |
156 this.popupContent.appendChild(span); | |
157 var optHold = document.createElement('div'); | |
158 optHold.id = 'option-holder'; | |
159 optHold.align = 'left'; | |
160 for (var i=0; i<node.options.length; i++) { | |
161 var option = node.options[i]; | |
162 var input = document.createElement('input'); | |
163 input.id = option.id; | |
164 input.type = 'checkbox'; | |
165 var span = document.createElement('span'); | |
166 span.textContent = option.text; | |
167 var hold = document.createElement('div'); | |
168 hold.setAttribute('name','option'); | |
169 hold.style.float = 'left'; | |
170 hold.style.padding = '4px'; | |
171 hold.appendChild(input); | |
172 hold.appendChild(span); | |
173 optHold.appendChild(hold); | |
174 } | |
175 this.popupContent.appendChild(optHold); | |
176 } else if (node.type == 'radio') { | |
177 var span = document.createElement('span'); | |
178 span.textContent = node.statement; | |
179 this.popupContent.appendChild(span); | |
180 var optHold = document.createElement('div'); | |
181 optHold.id = 'option-holder'; | |
182 optHold.align = 'none'; | |
183 optHold.style.float = 'left'; | |
184 optHold.style.width = "100%"; | |
185 for (var i=0; i<node.options.length; i++) { | |
186 var option = node.options[i]; | |
187 var input = document.createElement('input'); | |
188 input.id = option.name; | |
189 input.type = 'radio'; | |
190 input.name = node.id; | |
191 var span = document.createElement('span'); | |
192 span.textContent = option.text; | |
193 var hold = document.createElement('div'); | |
194 hold.setAttribute('name','option'); | |
195 hold.style.padding = '4px'; | |
196 hold.appendChild(input); | |
197 hold.appendChild(span); | |
198 optHold.appendChild(hold); | |
199 } | |
200 this.popupContent.appendChild(optHold); | |
201 } else if (node.type == 'number') { | |
202 var span = document.createElement('span'); | |
203 span.textContent = node.statement; | |
204 this.popupContent.appendChild(span); | |
205 this.popupContent.appendChild(document.createElement('br')); | |
206 var input = document.createElement('input'); | |
207 input.type = 'number'; | |
208 if (node.min != null) {input.min = node.min;} | |
209 if (node.max != null) {input.max = node.max;} | |
210 if (node.step != null) {input.step = node.step;} | |
211 this.popupContent.appendChild(input); | |
212 } | |
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); | |
221 }; | |
222 | |
223 this.initState = function(node) { | |
224 //Call this with your preTest and postTest nodes when needed to | |
225 // initialise the popup procedure. | |
226 this.popupOptions = node.options; | |
227 if (this.popupOptions.length > 0) { | |
228 if (node.type == 'pretest') { | |
229 this.responses = document.createElement('PreTest'); | |
230 } else if (node.type == 'posttest') { | |
231 this.responses = document.createElement('PostTest'); | |
232 } else { | |
233 console.log ('WARNING - popup node neither pre or post!'); | |
234 this.responses = document.createElement('responses'); | |
235 } | |
236 this.currentIndex = 0; | |
237 this.showPopup(); | |
238 this.postNode(); | |
239 } else { | |
240 advanceState(); | |
241 } | |
242 }; | |
243 | |
244 this.proceedClicked = function() { | |
245 // Each time the popup button is clicked! | |
246 var node = this.popupOptions[this.currentIndex]; | |
247 if (node.type == 'question') { | |
248 // Must extract the question data | |
249 var textArea = $(popup.popupContent).find('textarea')[0]; | |
250 if (node.mandatory == true && textArea.value.length == 0) { | |
251 alert('This question is mandatory'); | |
252 return; | |
253 } else { | |
254 // Save the text content | |
255 var hold = document.createElement('comment'); | |
256 hold.id = node.id; | |
257 hold.innerHTML = textArea.value; | |
258 console.log("Question: "+ node.question); | |
259 console.log("Question Response: "+ textArea.value); | |
260 this.responses.appendChild(hold); | |
261 } | |
262 } else if (node.type == 'checkbox') { | |
263 // Must extract checkbox data | |
264 var optHold = document.getElementById('option-holder'); | |
265 var hold = document.createElement('checkbox'); | |
266 console.log("Checkbox: "+ node.statement); | |
267 hold.id = node.id; | |
268 for (var i=0; i<optHold.childElementCount; i++) { | |
269 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
270 var statement = optHold.childNodes[i].getElementsByTagName('span')[0]; | |
271 var response = document.createElement('option'); | |
272 response.setAttribute('name',input.id); | |
273 response.textContent = input.checked; | |
274 hold.appendChild(response); | |
275 console.log(input.id +': '+ input.checked); | |
276 } | |
277 this.responses.appendChild(hold); | |
278 } else if (node.type == "radio") { | |
279 var optHold = document.getElementById('option-holder'); | |
280 var hold = document.createElement('radio'); | |
281 var responseID = null; | |
282 var i=0; | |
283 while(responseID == null) { | |
284 var input = optHold.childNodes[i].getElementsByTagName('input')[0]; | |
285 if (input.checked == true) { | |
286 responseID = i; | |
287 } | |
288 i++; | |
289 } | |
290 hold.id = node.id; | |
291 hold.setAttribute('name',node.options[responseID].name); | |
292 hold.textContent = node.options[responseID].text; | |
293 this.responses.appendChild(hold); | |
294 } else if (node.type == "number") { | |
295 var input = this.popupContent.getElementsByTagName('input')[0]; | |
296 if (node.mandatory == true && input.value.length == 0) { | |
297 alert('This question is mandatory. Please enter a number'); | |
298 return; | |
299 } | |
300 var enteredNumber = Number(input.value); | |
301 if (enteredNumber == undefined) { | |
302 alert('Please enter a valid number'); | |
303 return; | |
304 } | |
305 if (enteredNumber < node.min && node.min != null) { | |
306 alert('Number is below the minimum value of '+node.min); | |
307 return; | |
308 } | |
309 if (enteredNumber > node.max && node.max != null) { | |
310 alert('Number is above the maximum value of '+node.max); | |
311 return; | |
312 } | |
313 var hold = document.createElement('number'); | |
314 hold.id = node.id; | |
315 hold.textContent = input.value; | |
316 this.responses.appendChild(hold); | |
317 } | |
318 this.currentIndex++; | |
319 if (this.currentIndex < this.popupOptions.length) { | |
320 this.postNode(); | |
321 } else { | |
322 // Reached the end of the popupOptions | |
323 this.hidePopup(); | |
324 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { | |
325 testState.stateResults[testState.stateIndex] = this.responses; | |
326 } else { | |
327 testState.stateResults[testState.stateIndex].appendChild(this.responses); | |
328 } | |
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 } | |
370 } | |
371 }; | |
372 } | |
373 | |
374 function advanceState() | |
375 { | |
376 // Just for complete clarity | |
377 testState.advanceState(); | |
378 } | |
379 | |
380 function stateMachine() | |
381 { | |
382 // Object prototype for tracking and managing the test state | |
383 this.stateMap = []; | |
384 this.stateIndex = null; | |
385 this.currentStateMap = []; | |
386 this.currentIndex = null; | |
387 this.currentTestId = 0; | |
388 this.stateResults = []; | |
389 this.timerCallBackHolders = null; | |
390 this.initialise = function(){ | |
391 if (this.stateMap.length > 0) { | |
392 if(this.stateIndex != null) { | |
393 console.log('NOTE - State already initialise'); | |
394 } | |
395 this.stateIndex = -1; | |
396 var that = this; | |
397 for (var id=0; id<this.stateMap.length; id++){ | |
398 var name = this.stateMap[id].type; | |
399 var obj = document.createElement(name); | |
400 if (name == 'audioHolder') { | |
401 obj.id = this.stateMap[id].id; | |
402 } | |
403 this.stateResults.push(obj); | |
404 } | |
405 } else { | |
406 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP'); | |
407 } | |
408 }; | |
409 this.advanceState = function(){ | |
410 if (this.stateIndex == null) { | |
411 this.initialise(); | |
412 } | |
413 if (this.stateIndex == -1) { | |
414 console.log('Starting test...'); | |
415 } | |
416 if (this.currentIndex == null){ | |
417 if (this.currentStateMap.type == "audioHolder") { | |
418 // Save current page | |
419 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId); | |
420 this.currentTestId++; | |
421 } | |
422 this.stateIndex++; | |
423 if (this.stateIndex >= this.stateMap.length) { | |
424 console.log('Test Completed'); | |
425 createProjectSave(specification.projectReturn); | |
426 } else { | |
427 this.currentStateMap = this.stateMap[this.stateIndex]; | |
428 if (this.currentStateMap.type == "audioHolder") { | |
429 console.log('Loading test page'); | |
430 loadTest(this.currentStateMap); | |
431 this.initialiseInnerState(this.currentStateMap); | |
432 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { | |
433 if (this.currentStateMap.options.length >= 1) { | |
434 popup.initState(this.currentStateMap); | |
435 } else { | |
436 this.advanceState(); | |
437 } | |
438 } else { | |
439 this.advanceState(); | |
440 } | |
441 } | |
442 } else { | |
443 this.advanceInnerState(); | |
444 } | |
445 }; | |
446 | |
447 this.testPageCompleted = function(store, testXML, testId) { | |
448 // Function called each time a test page has been completed | |
449 // Can be used to over-rule default behaviour | |
450 | |
451 pageXMLSave(store, testXML); | |
452 }; | |
453 | |
454 this.initialiseInnerState = function(node) { | |
455 // Parses the received testXML for pre and post test options | |
456 this.currentStateMap = []; | |
457 var preTest = node.preTest; | |
458 var postTest = node.postTest; | |
459 if (preTest == undefined) {preTest = document.createElement("preTest");} | |
460 if (postTest == undefined){postTest= document.createElement("postTest");} | |
461 this.currentStateMap.push(preTest); | |
462 this.currentStateMap.push(node); | |
463 this.currentStateMap.push(postTest); | |
464 this.currentIndex = -1; | |
465 this.advanceInnerState(); | |
466 }; | |
467 | |
468 this.advanceInnerState = function() { | |
469 this.currentIndex++; | |
470 if (this.currentIndex >= this.currentStateMap.length) { | |
471 this.currentIndex = null; | |
472 this.currentStateMap = this.stateMap[this.stateIndex]; | |
473 this.advanceState(); | |
474 } else { | |
475 if (this.currentStateMap[this.currentIndex].type == "audioHolder") { | |
476 console.log("Loading test page"+this.currentTestId); | |
477 } else if (this.currentStateMap[this.currentIndex].type == "pretest") { | |
478 popup.initState(this.currentStateMap[this.currentIndex]); | |
479 } else if (this.currentStateMap[this.currentIndex].type == "posttest") { | |
480 popup.initState(this.currentStateMap[this.currentIndex]); | |
481 } else { | |
482 this.advanceInnerState(); | |
483 } | |
484 } | |
485 }; | |
486 | |
487 this.previousState = function(){}; | |
488 } | |
489 | |
490 function testEnded(testId) | |
491 { | |
492 pageXMLSave(testId); | |
493 if (testXMLSetups.length-1 > testId) | |
494 { | |
495 // Yes we have another test to perform | |
496 testId = (Number(testId)+1); | |
497 currentState = 'testRun-'+testId; | |
498 loadTest(testId); | |
499 } else { | |
500 console.log('Testing Completed!'); | |
501 currentState = 'postTest'; | |
502 // Check for any post tests | |
503 var xmlSetup = projectXML.find('setup'); | |
504 var postTest = xmlSetup.find('PostTest')[0]; | |
505 popup.initState(postTest); | |
506 } | |
507 } | |
508 | |
509 function loadProjectSpec(url) { | |
510 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data | |
511 // If url is null, request client to upload project XML document | |
512 var r = new XMLHttpRequest(); | |
513 r.open('GET',url,true); | |
514 r.onload = function() { | |
515 loadProjectSpecCallback(r.response); | |
516 }; | |
517 r.send(); | |
518 }; | |
519 | |
520 function loadProjectSpecCallback(response) { | |
521 // Function called after asynchronous download of XML project specification | |
522 //var decode = $.parseXML(response); | |
523 //projectXML = $(decode); | |
524 | |
525 var parse = new DOMParser(); | |
526 projectXML = parse.parseFromString(response,'text/xml'); | |
527 | |
528 // Build the specification | |
529 specification.decode(); | |
530 | |
531 testState.stateMap.push(specification.preTest); | |
532 | |
533 // New check if we need to randomise the test order | |
534 if (specification.randomiseOrder) | |
535 { | |
536 specification.audioHolders = randomiseOrder(specification.audioHolders); | |
537 } | |
538 | |
539 $(specification.audioHolders).each(function(index,elem){ | |
540 testState.stateMap.push(elem); | |
541 }); | |
542 | |
543 testState.stateMap.push(specification.postTest); | |
544 | |
545 // Obtain the metrics enabled | |
546 $(specification.metrics).each(function(index,node){ | |
547 var enabled = node.textContent; | |
548 switch(node.enabled) | |
549 { | |
550 case 'testTimer': | |
551 sessionMetrics.prototype.enableTestTimer = true; | |
552 break; | |
553 case 'elementTimer': | |
554 sessionMetrics.prototype.enableElementTimer = true; | |
555 break; | |
556 case 'elementTracker': | |
557 sessionMetrics.prototype.enableElementTracker = true; | |
558 break; | |
559 case 'elementListenTracker': | |
560 sessionMetrics.prototype.enableElementListenTracker = true; | |
561 break; | |
562 case 'elementInitialPosition': | |
563 sessionMetrics.prototype.enableElementInitialPosition = true; | |
564 break; | |
565 case 'elementFlagListenedTo': | |
566 sessionMetrics.prototype.enableFlagListenedTo = true; | |
567 break; | |
568 case 'elementFlagMoved': | |
569 sessionMetrics.prototype.enableFlagMoved = true; | |
570 break; | |
571 case 'elementFlagComments': | |
572 sessionMetrics.prototype.enableFlagComments = true; | |
573 break; | |
574 } | |
575 }); | |
576 | |
577 | |
578 | |
579 // Detect the interface to use and load the relevant javascripts. | |
580 var interfaceJS = document.createElement('script'); | |
581 interfaceJS.setAttribute("type","text/javascript"); | |
582 if (specification.interfaceType == 'APE') { | |
583 interfaceJS.setAttribute("src","ape.js"); | |
584 | |
585 // APE comes with a css file | |
586 var css = document.createElement('link'); | |
587 css.rel = 'stylesheet'; | |
588 css.type = 'text/css'; | |
589 css.href = 'ape.css'; | |
590 | |
591 document.getElementsByTagName("head")[0].appendChild(css); | |
592 } | |
593 document.getElementsByTagName("head")[0].appendChild(interfaceJS); | |
594 | |
595 // Define window callbacks for interface | |
596 window.onresize = function(event){resizeWindow(event);}; | |
597 } | |
598 | |
599 function createProjectSave(destURL) { | |
600 // Save the data from interface into XML and send to destURL | |
601 // If destURL is null then download XML in client | |
602 // Now time to render file locally | |
603 var xmlDoc = interfaceXMLSave(); | |
604 var parent = document.createElement("div"); | |
605 parent.appendChild(xmlDoc); | |
606 var file = [parent.innerHTML]; | |
607 if (destURL == "null" || destURL == undefined) { | |
608 var bb = new Blob(file,{type : 'application/xml'}); | |
609 var dnlk = window.URL.createObjectURL(bb); | |
610 var a = document.createElement("a"); | |
611 a.hidden = ''; | |
612 a.href = dnlk; | |
613 a.download = "save.xml"; | |
614 a.textContent = "Save File"; | |
615 | |
616 popup.showPopup(); | |
617 popup.popupContent.innerHTML = null; | |
618 popup.popupContent.appendChild(a); | |
619 } else { | |
620 var xmlhttp = new XMLHttpRequest; | |
621 xmlhttp.open("POST",destURL,true); | |
622 xmlhttp.setRequestHeader('Content-Type', 'text/xml'); | |
623 xmlhttp.onerror = function(){ | |
624 console.log('Error saving file to server! Presenting download locally'); | |
625 createProjectSave(null); | |
626 }; | |
627 xmlhttp.onreadystatechange = function() { | |
628 console.log(xmlhttp.status); | |
629 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { | |
630 createProjectSave(null); | |
631 } | |
632 }; | |
633 xmlhttp.send(file); | |
634 } | |
635 } | |
636 | |
637 // Only other global function which must be defined in the interface class. Determines how to create the XML document. | |
638 function interfaceXMLSave(){ | |
639 // Create the XML string to be exported with results | |
640 var xmlDoc = document.createElement("BrowserEvaluationResult"); | |
641 xmlDoc.appendChild(returnDateNode()); | |
642 for (var i=0; i<testState.stateResults.length; i++) | |
643 { | |
644 xmlDoc.appendChild(testState.stateResults[i]); | |
645 } | |
646 | |
647 return xmlDoc; | |
648 } | |
649 | |
650 function AudioEngine() { | |
651 | |
652 // Create two output paths, the main outputGain and fooGain. | |
653 // Output gain is default to 1 and any items for playback route here | |
654 // Foo gain is used for analysis to ensure paths get processed, but are not heard | |
655 // because web audio will optimise and any route which does not go to the destination gets ignored. | |
656 this.outputGain = audioContext.createGain(); | |
657 this.fooGain = audioContext.createGain(); | |
658 this.fooGain.gain = 0; | |
659 | |
660 // Use this to detect playback state: 0 - stopped, 1 - playing | |
661 this.status = 0; | |
662 this.audioObjectsReady = false; | |
663 | |
664 // Connect both gains to output | |
665 this.outputGain.connect(audioContext.destination); | |
666 this.fooGain.connect(audioContext.destination); | |
667 | |
668 // Create the timer Object | |
669 this.timer = new timer(); | |
670 // Create session metrics | |
671 this.metric = new sessionMetrics(this); | |
672 | |
673 this.loopPlayback = false; | |
674 | |
675 // Create store for new audioObjects | |
676 this.audioObjects = []; | |
677 | |
678 this.play = function() { | |
679 // Start the timer and set the audioEngine state to playing (1) | |
680 if (this.status == 0) { | |
681 // Check if all audioObjects are ready | |
682 if (this.audioObjectsReady == false) { | |
683 this.audioObjectsReady = this.checkAllReady(); | |
684 } | |
685 if (this.audioObjectsReady == true) { | |
686 this.timer.startTest(); | |
687 if (this.loopPlayback) { | |
688 for(var i=0; i<this.audioObjects.length; i++) { | |
689 this.audioObjects[i].play(this.timer.getTestTime()+1); | |
690 } | |
691 } | |
692 this.status = 1; | |
693 } | |
694 } | |
695 }; | |
696 | |
697 this.stop = function() { | |
698 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1) | |
699 if (this.status == 1) { | |
700 for (var i=0; i<this.audioObjects.length; i++) | |
701 { | |
702 this.audioObjects[i].stop(); | |
703 } | |
704 this.status = 0; | |
705 } | |
706 }; | |
707 | |
708 | |
709 this.newTrack = function(element) { | |
710 // Pull data from given URL into new audio buffer | |
711 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' | |
712 | |
713 // Create the audioObject with ID of the new track length; | |
714 audioObjectId = this.audioObjects.length; | |
715 this.audioObjects[audioObjectId] = new audioObject(audioObjectId); | |
716 | |
717 // AudioObject will get track itself. | |
718 this.audioObjects[audioObjectId].specification = element; | |
719 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url); | |
720 return this.audioObjects[audioObjectId]; | |
721 }; | |
722 | |
723 this.newTestPage = function() { | |
724 this.state = 0; | |
725 this.audioObjectsReady = false; | |
726 this.metric.reset(); | |
727 this.audioObjects = []; | |
728 }; | |
729 | |
730 this.checkAllPlayed = function() { | |
731 arr = []; | |
732 for (var id=0; id<this.audioObjects.length; id++) { | |
733 if (this.audioObjects[id].metric.wasListenedTo == false) { | |
734 arr.push(this.audioObjects[id].id); | |
735 } | |
736 } | |
737 return arr; | |
738 }; | |
739 | |
740 this.checkAllReady = function() { | |
741 var ready = true; | |
742 for (var i=0; i<this.audioObjects.length; i++) { | |
743 if (this.audioObjects[i].state == 0) { | |
744 // Track not ready | |
745 console.log('WAIT -- audioObject '+i+' not ready yet!'); | |
746 ready = false; | |
747 }; | |
748 } | |
749 return ready; | |
750 }; | |
751 | |
752 } | |
753 | |
754 function audioObject(id) { | |
755 // The main buffer object with common control nodes to the AudioEngine | |
756 | |
757 this.specification; | |
758 this.id = id; | |
759 this.state = 0; // 0 - no data, 1 - ready | |
760 this.url = null; // Hold the URL given for the output back to the results. | |
761 this.metric = new metricTracker(this); | |
762 | |
763 // Bindings for GUI | |
764 this.interfaceDOM = null; | |
765 this.commentDOM = null; | |
766 | |
767 // Create a buffer and external gain control to allow internal patching of effects and volume leveling. | |
768 this.bufferNode = undefined; | |
769 this.outputGain = audioContext.createGain(); | |
770 | |
771 // Default output gain to be zero | |
772 this.outputGain.gain.value = 0.0; | |
773 | |
774 // Connect buffer to the audio graph | |
775 this.outputGain.connect(audioEngineContext.outputGain); | |
776 | |
777 // the audiobuffer is not designed for multi-start playback | |
778 // When stopeed, the buffer node is deleted and recreated with the stored buffer. | |
779 this.buffer; | |
780 | |
781 this.loopStart = function() { | |
782 this.outputGain.gain.value = 1.0; | |
783 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
784 }; | |
785 | |
786 this.loopStop = function() { | |
787 if (this.outputGain.gain.value != 0.0) { | |
788 this.outputGain.gain.value = 0.0; | |
789 this.metric.stopListening(audioEngineContext.timer.getTestTime()); | |
790 } | |
791 }; | |
792 | |
793 this.play = function(startTime) { | |
794 this.bufferNode = audioContext.createBufferSource(); | |
795 this.bufferNode.owner = this; | |
796 this.bufferNode.connect(this.outputGain); | |
797 this.bufferNode.buffer = this.buffer; | |
798 this.bufferNode.loop = audioEngineContext.loopPlayback; | |
799 this.bufferNode.onended = function() { | |
800 // Safari does not like using 'this' to reference the calling object! | |
801 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime()); | |
802 }; | |
803 if (this.bufferNode.loop == false) { | |
804 this.metric.startListening(audioEngineContext.timer.getTestTime()); | |
805 } | |
806 this.bufferNode.start(startTime); | |
807 }; | |
808 | |
809 this.stop = function() { | |
810 if (this.bufferNode != undefined) | |
811 { | |
812 this.bufferNode.stop(0); | |
813 this.bufferNode = undefined; | |
814 this.metric.stopListening(audioEngineContext.timer.getTestTime()); | |
815 } | |
816 }; | |
817 | |
818 this.getCurrentPosition = function() { | |
819 var time = audioEngineContext.timer.getTestTime(); | |
820 if (this.bufferNode != undefined) { | |
821 if (this.bufferNode.loop == true) { | |
822 if (audioEngineContext.status == 1) { | |
823 return time%this.buffer.duration; | |
824 } else { | |
825 return 0; | |
826 } | |
827 } else { | |
828 if (this.metric.listenHold) { | |
829 return time - this.metric.listenStart; | |
830 } else { | |
831 return 0; | |
832 } | |
833 } | |
834 } else { | |
835 return 0; | |
836 } | |
837 }; | |
838 | |
839 this.constructTrack = function(url) { | |
840 var request = new XMLHttpRequest(); | |
841 this.url = url; | |
842 request.open('GET',url,true); | |
843 request.responseType = 'arraybuffer'; | |
844 | |
845 var audioObj = this; | |
846 | |
847 // Create callback to decode the data asynchronously | |
848 request.onloadend = function() { | |
849 audioContext.decodeAudioData(request.response, function(decodedData) { | |
850 audioObj.buffer = decodedData; | |
851 audioObj.state = 1; | |
852 }, function(){ | |
853 // Should only be called if there was an error, but sometimes gets called continuously | |
854 // Check here if the error is genuine | |
855 if (audioObj.state == 0 || audioObj.buffer == undefined) { | |
856 // Genuine error | |
857 console.log('FATAL - Error loading buffer on '+audioObj.id); | |
858 } | |
859 }); | |
860 }; | |
861 request.send(); | |
862 }; | |
863 | |
864 this.exportXMLDOM = function() { | |
865 var root = document.createElement('audioElement'); | |
866 root.id = this.specification.id; | |
867 root.setAttribute('url',this.url); | |
868 root.appendChild(this.interfaceDOM.exportXMLDOM()); | |
869 root.appendChild(this.commentDOM.exportXMLDOM()); | |
870 root.appendChild(this.metric.exportXMLDOM()); | |
871 return root; | |
872 }; | |
873 } | |
874 | |
875 function timer() | |
876 { | |
877 /* Timer object used in audioEngine to keep track of session timings | |
878 * Uses the timer of the web audio API, so sample resolution | |
879 */ | |
880 this.testStarted = false; | |
881 this.testStartTime = 0; | |
882 this.testDuration = 0; | |
883 this.minimumTestTime = 0; // No minimum test time | |
884 this.startTest = function() | |
885 { | |
886 if (this.testStarted == false) | |
887 { | |
888 this.testStartTime = audioContext.currentTime; | |
889 this.testStarted = true; | |
890 this.updateTestTime(); | |
891 audioEngineContext.metric.initialiseTest(); | |
892 } | |
893 }; | |
894 this.stopTest = function() | |
895 { | |
896 if (this.testStarted) | |
897 { | |
898 this.testDuration = this.getTestTime(); | |
899 this.testStarted = false; | |
900 } else { | |
901 console.log('ERR: Test tried to end before beginning'); | |
902 } | |
903 }; | |
904 this.updateTestTime = function() | |
905 { | |
906 if (this.testStarted) | |
907 { | |
908 this.testDuration = audioContext.currentTime - this.testStartTime; | |
909 } | |
910 }; | |
911 this.getTestTime = function() | |
912 { | |
913 this.updateTestTime(); | |
914 return this.testDuration; | |
915 }; | |
916 } | |
917 | |
918 function sessionMetrics(engine) | |
919 { | |
920 /* Used by audioEngine to link to audioObjects to minimise the timer call timers; | |
921 */ | |
922 this.engine = engine; | |
923 this.lastClicked = -1; | |
924 this.data = -1; | |
925 this.reset = function() { | |
926 this.lastClicked = -1; | |
927 this.data = -1; | |
928 }; | |
929 this.initialiseTest = function(){}; | |
930 } | |
931 | |
932 function metricTracker(caller) | |
933 { | |
934 /* Custom object to track and collect metric data | |
935 * Used only inside the audioObjects object. | |
936 */ | |
937 | |
938 this.listenedTimer = 0; | |
939 this.listenStart = 0; | |
940 this.listenHold = false; | |
941 this.initialPosition = -1; | |
942 this.movementTracker = []; | |
943 this.listenTracker =[]; | |
944 this.wasListenedTo = false; | |
945 this.wasMoved = false; | |
946 this.hasComments = false; | |
947 this.parent = caller; | |
948 | |
949 this.initialised = function(position) | |
950 { | |
951 if (this.initialPosition == -1) { | |
952 this.initialPosition = position; | |
953 } | |
954 }; | |
955 | |
956 this.moved = function(time,position) | |
957 { | |
958 this.wasMoved = true; | |
959 this.movementTracker[this.movementTracker.length] = [time, position]; | |
960 }; | |
961 | |
962 this.startListening = function(time) | |
963 { | |
964 if (this.listenHold == false) | |
965 { | |
966 this.wasListenedTo = true; | |
967 this.listenStart = time; | |
968 this.listenHold = true; | |
969 | |
970 var evnt = document.createElement('event'); | |
971 var testTime = document.createElement('testTime'); | |
972 testTime.setAttribute('start',time); | |
973 var bufferTime = document.createElement('bufferTime'); | |
974 bufferTime.setAttribute('start',this.parent.getCurrentPosition()); | |
975 evnt.appendChild(testTime); | |
976 evnt.appendChild(bufferTime); | |
977 this.listenTracker.push(evnt); | |
978 | |
979 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id | |
980 } | |
981 }; | |
982 | |
983 this.stopListening = function(time) | |
984 { | |
985 if (this.listenHold == true) | |
986 { | |
987 var diff = time - this.listenStart; | |
988 this.listenedTimer += (diff); | |
989 this.listenStart = 0; | |
990 this.listenHold = false; | |
991 | |
992 var evnt = this.listenTracker[this.listenTracker.length-1]; | |
993 var testTime = evnt.getElementsByTagName('testTime')[0]; | |
994 var bufferTime = evnt.getElementsByTagName('bufferTime')[0]; | |
995 testTime.setAttribute('stop',time); | |
996 bufferTime.setAttribute('stop',this.parent.getCurrentPosition()); | |
997 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id | |
998 } | |
999 }; | |
1000 | |
1001 this.exportXMLDOM = function() { | |
1002 var root = document.createElement('metric'); | |
1003 if (audioEngineContext.metric.enableElementTimer) { | |
1004 var mElementTimer = document.createElement('metricresult'); | |
1005 mElementTimer.setAttribute('name','enableElementTimer'); | |
1006 mElementTimer.textContent = this.listenedTimer; | |
1007 root.appendChild(mElementTimer); | |
1008 } | |
1009 if (audioEngineContext.metric.enableElementTracker) { | |
1010 var elementTrackerFull = document.createElement('metricResult'); | |
1011 elementTrackerFull.setAttribute('name','elementTrackerFull'); | |
1012 for (var k=0; k<this.movementTracker.length; k++) | |
1013 { | |
1014 var timePos = document.createElement('timePos'); | |
1015 timePos.id = k; | |
1016 var time = document.createElement('time'); | |
1017 time.textContent = this.movementTracker[k][0]; | |
1018 var position = document.createElement('position'); | |
1019 position.textContent = this.movementTracker[k][1]; | |
1020 timePos.appendChild(time); | |
1021 timePos.appendChild(position); | |
1022 elementTrackerFull.appendChild(timePos); | |
1023 } | |
1024 root.appendChild(elementTrackerFull); | |
1025 } | |
1026 if (audioEngineContext.metric.enableElementListenTracker) { | |
1027 var elementListenTracker = document.createElement('metricResult'); | |
1028 elementListenTracker.setAttribute('name','elementListenTracker'); | |
1029 for (var k=0; k<this.listenTracker.length; k++) { | |
1030 elementListenTracker.appendChild(this.listenTracker[k]); | |
1031 } | |
1032 root.appendChild(elementListenTracker); | |
1033 } | |
1034 if (audioEngineContext.metric.enableElementInitialPosition) { | |
1035 var elementInitial = document.createElement('metricResult'); | |
1036 elementInitial.setAttribute('name','elementInitialPosition'); | |
1037 elementInitial.textContent = this.initialPosition; | |
1038 root.appendChild(elementInitial); | |
1039 } | |
1040 if (audioEngineContext.metric.enableFlagListenedTo) { | |
1041 var flagListenedTo = document.createElement('metricResult'); | |
1042 flagListenedTo.setAttribute('name','elementFlagListenedTo'); | |
1043 flagListenedTo.textContent = this.wasListenedTo; | |
1044 root.appendChild(flagListenedTo); | |
1045 } | |
1046 if (audioEngineContext.metric.enableFlagMoved) { | |
1047 var flagMoved = document.createElement('metricResult'); | |
1048 flagMoved.setAttribute('name','elementFlagMoved'); | |
1049 flagMoved.textContent = this.wasMoved; | |
1050 root.appendChild(flagMoved); | |
1051 } | |
1052 if (audioEngineContext.metric.enableFlagComments) { | |
1053 var flagComments = document.createElement('metricResult'); | |
1054 flagComments.setAttribute('name','elementFlagComments'); | |
1055 if (this.parent.commentDOM == null) | |
1056 {flag.textContent = 'false';} | |
1057 else if (this.parent.commentDOM.textContent.length == 0) | |
1058 {flag.textContent = 'false';} | |
1059 else | |
1060 {flag.textContet = 'true';} | |
1061 root.appendChild(flagComments); | |
1062 } | |
1063 | |
1064 return root; | |
1065 }; | |
1066 } | |
1067 | |
1068 function randomiseOrder(input) | |
1069 { | |
1070 // This takes an array of information and randomises the order | |
1071 var N = input.length; | |
1072 var K = N; | |
1073 var holdArr = []; | |
1074 for (var n=0; n<N; n++) | |
1075 { | |
1076 // First pick a random number | |
1077 var r = Math.random(); | |
1078 // Multiply and floor by the number of elements left | |
1079 r = Math.floor(r*input.length); | |
1080 // Pick out that element and delete from the array | |
1081 holdArr.push(input.splice(r,1)[0]); | |
1082 } | |
1083 return holdArr; | |
1084 } | |
1085 | |
1086 function returnDateNode() | |
1087 { | |
1088 // Create an XML Node for the Date and Time a test was conducted | |
1089 // Structure is | |
1090 // <datetime> | |
1091 // <date year="##" month="##" day="##">DD/MM/YY</date> | |
1092 // <time hour="##" minute="##" sec="##">HH:MM:SS</time> | |
1093 // </datetime> | |
1094 var dateTime = new Date(); | |
1095 var year = document.createAttribute('year'); | |
1096 var month = document.createAttribute('month'); | |
1097 var day = document.createAttribute('day'); | |
1098 var hour = document.createAttribute('hour'); | |
1099 var minute = document.createAttribute('minute'); | |
1100 var secs = document.createAttribute('secs'); | |
1101 | |
1102 year.nodeValue = dateTime.getFullYear(); | |
1103 month.nodeValue = dateTime.getMonth()+1; | |
1104 day.nodeValue = dateTime.getDate(); | |
1105 hour.nodeValue = dateTime.getHours(); | |
1106 minute.nodeValue = dateTime.getMinutes(); | |
1107 secs.nodeValue = dateTime.getSeconds(); | |
1108 | |
1109 var hold = document.createElement("datetime"); | |
1110 var date = document.createElement("date"); | |
1111 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; | |
1112 var time = document.createElement("time"); | |
1113 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; | |
1114 | |
1115 date.setAttributeNode(year); | |
1116 date.setAttributeNode(month); | |
1117 date.setAttributeNode(day); | |
1118 time.setAttributeNode(hour); | |
1119 time.setAttributeNode(minute); | |
1120 time.setAttributeNode(secs); | |
1121 | |
1122 hold.appendChild(date); | |
1123 hold.appendChild(time); | |
1124 return hold | |
1125 | |
1126 } | |
1127 | |
1128 function testWaitIndicator() { | |
1129 if (audioEngineContext.checkAllReady() == false) { | |
1130 var hold = document.createElement("div"); | |
1131 hold.id = "testWaitIndicator"; | |
1132 hold.className = "indicator-box"; | |
1133 hold.style.zIndex = 3; | |
1134 var span = document.createElement("span"); | |
1135 span.textContent = "Please wait! Elements still loading"; | |
1136 hold.appendChild(span); | |
1137 var blank = document.createElement('div'); | |
1138 blank.className = 'testHalt'; | |
1139 blank.id = "testHaltBlank"; | |
1140 var body = document.getElementsByTagName('body')[0]; | |
1141 body.appendChild(hold); | |
1142 body.appendChild(blank); | |
1143 testWaitTimerIntervalHolder = setInterval(function(){ | |
1144 var ready = audioEngineContext.checkAllReady(); | |
1145 if (ready) { | |
1146 var elem = document.getElementById('testWaitIndicator'); | |
1147 var blank = document.getElementById('testHaltBlank'); | |
1148 var body = document.getElementsByTagName('body')[0]; | |
1149 body.removeChild(elem); | |
1150 body.removeChild(blank); | |
1151 clearInterval(testWaitTimerIntervalHolder); | |
1152 } | |
1153 },500,false); | |
1154 } | |
1155 } | |
1156 | |
1157 var testWaitTimerIntervalHolder = null; | |
1158 | |
1159 function Specification() { | |
1160 // Handles the decoding of the project specification XML into a simple JavaScript Object. | |
1161 | |
1162 this.interfaceType; | |
1163 this.projectReturn; | |
1164 this.randomiseOrder; | |
1165 this.collectMetrics; | |
1166 this.preTest; | |
1167 this.postTest; | |
1168 this.metrics =[]; | |
1169 | |
1170 this.audioHolders = []; | |
1171 | |
1172 this.decode = function() { | |
1173 // projectXML - DOM Parsed document | |
1174 var setupNode = projectXML.getElementsByTagName('setup')[0]; | |
1175 this.interfaceType = setupNode.getAttribute('interface'); | |
1176 this.projectReturn = setupNode.getAttribute('projectReturn'); | |
1177 if (setupNode.getAttribute('randomiseOrder') == "true") { | |
1178 this.randomiseOrder = true; | |
1179 } else {this.randomiseOrder = false;} | |
1180 if (setupNode.getAttribute('collectMetrics') == "true") { | |
1181 this.collectMetrics = true; | |
1182 } else {this.collectMetrics = false;} | |
1183 var metricCollection = setupNode.getElementsByTagName('Metric'); | |
1184 | |
1185 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest')); | |
1186 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest')); | |
1187 | |
1188 if (metricCollection.length > 0) { | |
1189 metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); | |
1190 for (var i=0; i<metricCollection.length; i++) { | |
1191 this.metrics.push(new this.metricNode(metricCollection[i].textContent)); | |
1192 } | |
1193 } | |
1194 | |
1195 var audioHolders = projectXML.getElementsByTagName('audioHolder'); | |
1196 for (var i=0; i<audioHolders.length; i++) { | |
1197 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i])); | |
1198 } | |
1199 | |
1200 }; | |
1201 | |
1202 this.prepostNode = function(type,Collection) { | |
1203 this.type = type; | |
1204 this.options = []; | |
1205 | |
1206 this.OptionNode = function(child) { | |
1207 | |
1208 this.childOption = function(element) { | |
1209 this.type = 'option'; | |
1210 this.id = element.id; | |
1211 this.name = element.getAttribute('name'); | |
1212 this.text = element.textContent; | |
1213 }; | |
1214 | |
1215 this.type = child.nodeName; | |
1216 if (child.nodeName == "question") { | |
1217 this.id = child.id; | |
1218 this.mandatory; | |
1219 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;} | |
1220 else {this.mandatory = false;} | |
1221 this.question = child.textContent; | |
1222 if (child.getAttribute('boxsize') == null) { | |
1223 this.boxsize = 'normal'; | |
1224 } else { | |
1225 this.boxsize = child.getAttribute('boxsize'); | |
1226 } | |
1227 } else if (child.nodeName == "statement") { | |
1228 this.statement = child.textContent; | |
1229 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") { | |
1230 var element = child.firstElementChild; | |
1231 this.id = child.id; | |
1232 if (element == null) { | |
1233 console.log('Malformed' +child.nodeName+ 'entry'); | |
1234 this.statement = 'Malformed' +child.nodeName+ 'entry'; | |
1235 this.type = 'statement'; | |
1236 } else { | |
1237 this.options = []; | |
1238 while (element != null) { | |
1239 if (element.nodeName == 'statement' && this.statement == undefined){ | |
1240 this.statement = element.textContent; | |
1241 } else if (element.nodeName == 'option') { | |
1242 this.options.push(new this.childOption(element)); | |
1243 } | |
1244 element = element.nextElementSibling; | |
1245 } | |
1246 } | |
1247 } else if (child.nodeName == "number") { | |
1248 this.statement = child.textContent; | |
1249 this.id = child.id; | |
1250 this.min = child.getAttribute('min'); | |
1251 this.max = child.getAttribute('max'); | |
1252 this.step = child.getAttribute('step'); | |
1253 } | |
1254 }; | |
1255 | |
1256 // On construction: | |
1257 if (Collection.length != 0) { | |
1258 Collection = Collection[0]; | |
1259 if (Collection.childElementCount != 0) { | |
1260 var child = Collection.firstElementChild; | |
1261 this.options.push(new this.OptionNode(child)); | |
1262 while (child.nextElementSibling != null) { | |
1263 child = child.nextElementSibling; | |
1264 this.options.push(new this.OptionNode(child)); | |
1265 } | |
1266 } | |
1267 } | |
1268 }; | |
1269 | |
1270 this.metricNode = function(name) { | |
1271 this.enabled = name; | |
1272 }; | |
1273 | |
1274 this.audioHolderNode = function(parent,xml) { | |
1275 this.type = 'audioHolder'; | |
1276 this.interfaceNode = function(DOM) { | |
1277 var title = DOM.getElementsByTagName('title'); | |
1278 if (title.length == 0) {this.title = null;} | |
1279 else {this.title = title[0].textContent;} | |
1280 | |
1281 var scale = DOM.getElementsByTagName('scale'); | |
1282 this.scale = []; | |
1283 for (var i=0; i<scale.length; i++) { | |
1284 var arr = [null, null]; | |
1285 arr[0] = scale[i].getAttribute('position'); | |
1286 arr[1] = scale[i].textContent; | |
1287 this.scale.push(arr); | |
1288 } | |
1289 }; | |
1290 | |
1291 this.audioElementNode = function(parent,xml) { | |
1292 this.url = xml.getAttribute('url'); | |
1293 this.id = xml.id; | |
1294 this.parent = parent; | |
1295 }; | |
1296 | |
1297 this.commentQuestionNode = function(xml) { | |
1298 this.childOption = function(element) { | |
1299 this.type = 'option'; | |
1300 this.name = element.getAttribute('name'); | |
1301 this.text = element.textContent; | |
1302 }; | |
1303 this.id = xml.id; | |
1304 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;} | |
1305 else {this.mandatory = false;} | |
1306 this.type = xml.getAttribute('type'); | |
1307 if (this.type == undefined) {this.type = 'text';} | |
1308 switch (this.type) { | |
1309 case 'text': | |
1310 this.question = xml.textContent; | |
1311 break; | |
1312 case 'radio': | |
1313 var child = xml.firstElementChild; | |
1314 this.options = []; | |
1315 while (child != undefined) { | |
1316 if (child.nodeName == 'statement' && this.statement == undefined) { | |
1317 this.statement = child.textContent; | |
1318 } else if (child.nodeName == 'option') { | |
1319 this.options.push(new this.childOption(child)); | |
1320 } | |
1321 child = child.nextElementSibling; | |
1322 } | |
1323 break; | |
1324 case 'checkbox': | |
1325 var child = xml.firstElementChild; | |
1326 this.options = []; | |
1327 while (child != undefined) { | |
1328 if (child.nodeName == 'statement' && this.statement == undefined) { | |
1329 this.statement = child.textContent; | |
1330 } else if (child.nodeName == 'option') { | |
1331 this.options.push(new this.childOption(child)); | |
1332 } | |
1333 child = child.nextElementSibling; | |
1334 } | |
1335 break; | |
1336 } | |
1337 }; | |
1338 | |
1339 this.id = xml.id; | |
1340 this.hostURL = xml.getAttribute('hostURL'); | |
1341 this.sampleRate = xml.getAttribute('sampleRate'); | |
1342 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;} | |
1343 else {this.randomiseOrder = false;} | |
1344 this.repeatCount = xml.getAttribute('repeatCount'); | |
1345 if (xml.getAttribute('loop') == 'true') {this.loop = true;} | |
1346 else {this.loop == false;} | |
1347 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;} | |
1348 else {this.elementComments = false;} | |
1349 | |
1350 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest')); | |
1351 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest')); | |
1352 | |
1353 this.interfaces = []; | |
1354 var interfaceDOM = xml.getElementsByTagName('interface'); | |
1355 for (var i=0; i<interfaceDOM.length; i++) { | |
1356 this.interfaces.push(new this.interfaceNode(interfaceDOM[i])); | |
1357 } | |
1358 | |
1359 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix'); | |
1360 if (this.commentBoxPrefix.length != 0) { | |
1361 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent; | |
1362 } else { | |
1363 this.commentBoxPrefix = "Comment on track"; | |
1364 } | |
1365 | |
1366 this.audioElements =[]; | |
1367 var audioElementsDOM = xml.getElementsByTagName('audioElements'); | |
1368 for (var i=0; i<audioElementsDOM.length; i++) { | |
1369 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i])); | |
1370 } | |
1371 | |
1372 this.commentQuestions = []; | |
1373 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion'); | |
1374 for (var i=0; i<commentQuestionsDOM.length; i++) { | |
1375 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i])); | |
1376 } | |
1377 }; | |
1378 } | |
1379 | |
1380 function Interface(specificationObject) { | |
1381 // This handles the bindings between the interface and the audioEngineContext; | |
1382 this.specification = specificationObject; | |
1383 this.insertPoint = document.getElementById("topLevelBody"); | |
1384 | |
1385 // Bounded by interface!! | |
1386 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels | |
1387 // For example, APE returns the slider position normalised in a <value> tag. | |
1388 this.interfaceObjects = []; | |
1389 this.interfaceObject = function(){}; | |
1390 | |
1391 this.commentBoxes = []; | |
1392 this.elementCommentBox = function(audioObject) { | |
1393 var element = audioObject.specification; | |
1394 this.audioObject = audioObject; | |
1395 this.id = audioObject.id; | |
1396 var audioHolderObject = audioObject.specification.parent; | |
1397 // Create document objects to hold the comment boxes | |
1398 this.trackComment = document.createElement('div'); | |
1399 this.trackComment.className = 'comment-div'; | |
1400 this.trackComment.id = 'comment-div-'+audioObject.id; | |
1401 // Create a string next to each comment asking for a comment | |
1402 this.trackString = document.createElement('span'); | |
1403 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id; | |
1404 // Create the HTML5 comment box 'textarea' | |
1405 this.trackCommentBox = document.createElement('textarea'); | |
1406 this.trackCommentBox.rows = '4'; | |
1407 this.trackCommentBox.cols = '100'; | |
1408 this.trackCommentBox.name = 'trackComment'+audioObject.id; | |
1409 this.trackCommentBox.className = 'trackComment'; | |
1410 var br = document.createElement('br'); | |
1411 // Add to the holder. | |
1412 this.trackComment.appendChild(this.trackString); | |
1413 this.trackComment.appendChild(br); | |
1414 this.trackComment.appendChild(this.trackCommentBox); | |
1415 | |
1416 this.exportXMLDOM = function() { | |
1417 var root = document.createElement('comment'); | |
1418 if (this.audioObject.specification.parent.elementComments) { | |
1419 var question = document.createElement('question'); | |
1420 question.textContent = this.trackString.textContent; | |
1421 var response = document.createElement('response'); | |
1422 response.textContent = this.trackCommentBox.value; | |
1423 root.appendChild(question); | |
1424 root.appendChild(response); | |
1425 } | |
1426 return root; | |
1427 }; | |
1428 }; | |
1429 | |
1430 this.commentQuestions = []; | |
1431 | |
1432 this.commentBox = function(commentQuestion) { | |
1433 this.specification = commentQuestion; | |
1434 // Create document objects to hold the comment boxes | |
1435 this.holder = document.createElement('div'); | |
1436 this.holder.className = 'comment-div'; | |
1437 // Create a string next to each comment asking for a comment | |
1438 this.string = document.createElement('span'); | |
1439 this.string.innerHTML = commentQuestion.question; | |
1440 // Create the HTML5 comment box 'textarea' | |
1441 this.textArea = document.createElement('textarea'); | |
1442 this.textArea.rows = '4'; | |
1443 this.textArea.cols = '100'; | |
1444 this.textArea.className = 'trackComment'; | |
1445 var br = document.createElement('br'); | |
1446 // Add to the holder. | |
1447 this.holder.appendChild(this.string); | |
1448 this.holder.appendChild(br); | |
1449 this.holder.appendChild(this.textArea); | |
1450 | |
1451 this.exportXMLDOM = function() { | |
1452 var root = document.createElement('comment'); | |
1453 root.id = this.specification.id; | |
1454 root.setAttribute('type',this.specification.type); | |
1455 root.textContent = this.textArea.value; | |
1456 return root; | |
1457 }; | |
1458 }; | |
1459 | |
1460 this.radioBox = function(commentQuestion) { | |
1461 this.specification = commentQuestion; | |
1462 // Create document objects to hold the comment boxes | |
1463 this.holder = document.createElement('div'); | |
1464 this.holder.className = 'comment-div'; | |
1465 // Create a string next to each comment asking for a comment | |
1466 this.string = document.createElement('span'); | |
1467 this.string.innerHTML = commentQuestion.statement; | |
1468 var br = document.createElement('br'); | |
1469 // Add to the holder. | |
1470 this.holder.appendChild(this.string); | |
1471 this.holder.appendChild(br); | |
1472 this.options = []; | |
1473 this.inputs = document.createElement('div'); | |
1474 this.span = document.createElement('div'); | |
1475 this.inputs.align = 'center'; | |
1476 this.inputs.style.marginLeft = '12px'; | |
1477 this.span.style.marginLeft = '12px'; | |
1478 this.span.align = 'center'; | |
1479 this.span.style.marginTop = '15px'; | |
1480 | |
1481 var optCount = commentQuestion.options.length; | |
1482 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; | |
1483 console.log(spanMargin); | |
1484 for (var i=0; i<optCount; i++) | |
1485 { | |
1486 var div = document.createElement('div'); | |
1487 div.style.width = '100px'; | |
1488 div.style.float = 'left'; | |
1489 div.style.marginRight = spanMargin; | |
1490 div.style.marginLeft = spanMargin; | |
1491 var input = document.createElement('input'); | |
1492 input.type = 'radio'; | |
1493 input.name = commentQuestion.id; | |
1494 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
1495 input.className = 'comment-radio'; | |
1496 div.appendChild(input); | |
1497 this.inputs.appendChild(div); | |
1498 | |
1499 | |
1500 div = document.createElement('div'); | |
1501 div.style.width = '100px'; | |
1502 div.style.float = 'left'; | |
1503 div.style.marginRight = spanMargin; | |
1504 div.style.marginLeft = spanMargin; | |
1505 div.align = 'center'; | |
1506 var span = document.createElement('span'); | |
1507 span.textContent = commentQuestion.options[i].text; | |
1508 span.className = 'comment-radio-span'; | |
1509 div.appendChild(span); | |
1510 this.span.appendChild(div); | |
1511 this.options.push(input); | |
1512 } | |
1513 this.holder.appendChild(this.span); | |
1514 this.holder.appendChild(this.inputs); | |
1515 | |
1516 this.exportXMLDOM = function() { | |
1517 var root = document.createElement('comment'); | |
1518 root.id = this.specification.id; | |
1519 root.setAttribute('type',this.specification.type); | |
1520 var question = document.createElement('question'); | |
1521 question.textContent = this.string.textContent; | |
1522 var response = document.createElement('response'); | |
1523 var i=0; | |
1524 while(this.options[i].checked == false) { | |
1525 i++; | |
1526 if (i >= this.options.length) { | |
1527 break; | |
1528 } | |
1529 } | |
1530 if (i >= this.options.length) { | |
1531 response.textContent = 'null'; | |
1532 } else { | |
1533 response.textContent = this.options[i].getAttribute('setvalue'); | |
1534 response.setAttribute('number',i); | |
1535 } | |
1536 console.log('Comment: '+question.textContent); | |
1537 console.log('Response: '+response.textContent); | |
1538 root.appendChild(question); | |
1539 root.appendChild(response); | |
1540 return root; | |
1541 }; | |
1542 }; | |
1543 | |
1544 this.checkboxBox = function(commentQuestion) { | |
1545 this.specification = commentQuestion; | |
1546 // Create document objects to hold the comment boxes | |
1547 this.holder = document.createElement('div'); | |
1548 this.holder.className = 'comment-div'; | |
1549 // Create a string next to each comment asking for a comment | |
1550 this.string = document.createElement('span'); | |
1551 this.string.innerHTML = commentQuestion.statement; | |
1552 var br = document.createElement('br'); | |
1553 // Add to the holder. | |
1554 this.holder.appendChild(this.string); | |
1555 this.holder.appendChild(br); | |
1556 this.options = []; | |
1557 this.inputs = document.createElement('div'); | |
1558 this.span = document.createElement('div'); | |
1559 this.inputs.align = 'center'; | |
1560 this.inputs.style.marginLeft = '12px'; | |
1561 this.span.style.marginLeft = '12px'; | |
1562 this.span.align = 'center'; | |
1563 this.span.style.marginTop = '15px'; | |
1564 | |
1565 var optCount = commentQuestion.options.length; | |
1566 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; | |
1567 console.log(spanMargin); | |
1568 for (var i=0; i<optCount; i++) | |
1569 { | |
1570 var div = document.createElement('div'); | |
1571 div.style.width = '100px'; | |
1572 div.style.float = 'left'; | |
1573 div.style.marginRight = spanMargin; | |
1574 div.style.marginLeft = spanMargin; | |
1575 var input = document.createElement('input'); | |
1576 input.type = 'checkbox'; | |
1577 input.name = commentQuestion.id; | |
1578 input.setAttribute('setvalue',commentQuestion.options[i].name); | |
1579 input.className = 'comment-radio'; | |
1580 div.appendChild(input); | |
1581 this.inputs.appendChild(div); | |
1582 | |
1583 | |
1584 div = document.createElement('div'); | |
1585 div.style.width = '100px'; | |
1586 div.style.float = 'left'; | |
1587 div.style.marginRight = spanMargin; | |
1588 div.style.marginLeft = spanMargin; | |
1589 div.align = 'center'; | |
1590 var span = document.createElement('span'); | |
1591 span.textContent = commentQuestion.options[i].text; | |
1592 span.className = 'comment-radio-span'; | |
1593 div.appendChild(span); | |
1594 this.span.appendChild(div); | |
1595 this.options.push(input); | |
1596 } | |
1597 this.holder.appendChild(this.span); | |
1598 this.holder.appendChild(this.inputs); | |
1599 | |
1600 this.exportXMLDOM = function() { | |
1601 var root = document.createElement('comment'); | |
1602 root.id = this.specification.id; | |
1603 root.setAttribute('type',this.specification.type); | |
1604 var question = document.createElement('question'); | |
1605 question.textContent = this.string.textContent; | |
1606 root.appendChild(question); | |
1607 console.log('Comment: '+question.textContent); | |
1608 for (var i=0; i<this.options.length; i++) { | |
1609 var response = document.createElement('response'); | |
1610 response.textContent = this.options[i].checked; | |
1611 response.setAttribute('name',this.options[i].getAttribute('setvalue')); | |
1612 root.appendChild(response); | |
1613 console.log('Response '+response.getAttribute('name') +': '+response.textContent); | |
1614 } | |
1615 return root; | |
1616 }; | |
1617 }; | |
1618 | |
1619 this.createCommentBox = function(audioObject) { | |
1620 var node = new this.elementCommentBox(audioObject); | |
1621 this.commentBoxes.push(node); | |
1622 audioObject.commentDOM = node; | |
1623 return node; | |
1624 }; | |
1625 | |
1626 this.sortCommentBoxes = function() { | |
1627 var holder = []; | |
1628 while (this.commentBoxes.length > 0) { | |
1629 var node = this.commentBoxes.pop(0); | |
1630 holder[node.id] = node; | |
1631 } | |
1632 this.commentBoxes = holder; | |
1633 }; | |
1634 | |
1635 this.showCommentBoxes = function(inject, sort) { | |
1636 if (sort) {interfaceContext.sortCommentBoxes();} | |
1637 for (var i=0; i<interfaceContext.commentBoxes.length; i++) { | |
1638 inject.appendChild(this.commentBoxes[i].trackComment); | |
1639 } | |
1640 }; | |
1641 | |
1642 this.createCommentQuestion = function(element) { | |
1643 var node; | |
1644 if (element.type == 'text') { | |
1645 node = new this.commentBox(element); | |
1646 } else if (element.type == 'radio') { | |
1647 node = new this.radioBox(element); | |
1648 } else if (element.type == 'checkbox') { | |
1649 node = new this.checkboxBox(element); | |
1650 } | |
1651 this.commentQuestions.push(node); | |
1652 return node; | |
1653 }; | |
1654 } | |
1655 |