annotate core.js @ 903:3464a477c021

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