annotate core.js @ 918:1e1339e51701

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