annotate core.js @ 904:b94bb53ce4ac

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