annotate core.js @ 1971:e62289760587

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