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