annotate core.js @ 217:e69c08e35d83 Dev_main

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