annotate core.js @ 1026:f13b6efc4513

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