annotate core.js @ 1970:cf08f2881a39

Hitting enter in any pop-up goes to the next window (or submits, or starts the test)
author Brecht De Man <b.deman@qmul.ac.uk>
date Wed, 18 Nov 2015 11:08:52 +0100
parents 82f43919f385
children b9785aaab2a4
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 {
b@1478 677 popup.showPopup();
b@1478 678 popup.popupContent.innerHTML = null;
b@1478 679 popup.popupContent.textContent = "Thank you!";
b@1478 680 }
b@1478 681 };
b@1478 682 xmlhttp.send(file);
b@1478 683 }
b@1478 684 }
b@1478 685
b@1478 686 function errorSessionDump(msg){
b@1478 687 // Create the partial interface XML save
b@1478 688 // Include error node with message on why the dump occured
b@1478 689 var xmlDoc = interfaceXMLSave();
b@1478 690 var err = document.createElement('error');
b@1478 691 err.textContent = msg;
b@1478 692 xmlDoc.appendChild(err);
b@1478 693 var parent = document.createElement("div");
b@1478 694 parent.appendChild(xmlDoc);
b@1478 695 var file = [parent.innerHTML];
b@1478 696 var bb = new Blob(file,{type : 'application/xml'});
b@1478 697 var dnlk = window.URL.createObjectURL(bb);
b@1478 698 var a = document.createElement("a");
b@1478 699 a.hidden = '';
b@1478 700 a.href = dnlk;
b@1478 701 a.download = "save.xml";
b@1478 702 a.textContent = "Save File";
b@1478 703
b@1478 704 popup.showPopup();
b@1478 705 popup.popupContent.innerHTML = "ERROR : "+msg;
b@1478 706 popup.popupContent.appendChild(a);
b@1478 707 }
b@1478 708
b@1478 709 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
b@1478 710 function interfaceXMLSave(){
b@1478 711 // Create the XML string to be exported with results
b@1478 712 var xmlDoc = document.createElement("BrowserEvaluationResult");
b@1478 713 var projectDocument = specification.projectXML;
b@1478 714 projectDocument.setAttribute('file-name',url);
b@1478 715 xmlDoc.appendChild(projectDocument);
b@1478 716 xmlDoc.appendChild(returnDateNode());
b@1478 717 for (var i=0; i<testState.stateResults.length; i++)
b@1478 718 {
b@1478 719 xmlDoc.appendChild(testState.stateResults[i]);
b@1478 720 }
b@1478 721
b@1478 722 return xmlDoc;
b@1478 723 }
b@1478 724
b@1478 725 function AudioEngine() {
b@1478 726
b@1478 727 // Create two output paths, the main outputGain and fooGain.
b@1478 728 // Output gain is default to 1 and any items for playback route here
b@1478 729 // Foo gain is used for analysis to ensure paths get processed, but are not heard
b@1478 730 // because web audio will optimise and any route which does not go to the destination gets ignored.
b@1478 731 this.outputGain = audioContext.createGain();
b@1478 732 this.fooGain = audioContext.createGain();
b@1478 733 this.fooGain.gain = 0;
b@1478 734
b@1478 735 // Use this to detect playback state: 0 - stopped, 1 - playing
b@1478 736 this.status = 0;
b@1478 737
b@1478 738 // Connect both gains to output
b@1478 739 this.outputGain.connect(audioContext.destination);
b@1478 740 this.fooGain.connect(audioContext.destination);
b@1478 741
b@1478 742 // Create the timer Object
b@1478 743 this.timer = new timer();
b@1478 744 // Create session metrics
b@1478 745 this.metric = new sessionMetrics(this);
b@1478 746
b@1478 747 this.loopPlayback = false;
b@1478 748
b@1478 749 // Create store for new audioObjects
b@1478 750 this.audioObjects = [];
b@1478 751
b@1478 752 this.play = function(id) {
b@1478 753 // Start the timer and set the audioEngine state to playing (1)
b@1478 754 if (this.status == 0 && this.loopPlayback) {
b@1478 755 // Check if all audioObjects are ready
b@1478 756 if(this.checkAllReady())
b@1478 757 {
b@1478 758 this.status = 1;
b@1478 759 this.setSynchronousLoop();
b@1478 760 }
b@1478 761 }
b@1478 762 else
b@1478 763 {
b@1478 764 this.status = 1;
b@1478 765 }
b@1478 766 if (this.status== 1) {
b@1478 767 this.timer.startTest();
b@1478 768 if (id == undefined) {
b@1478 769 id = -1;
b@1478 770 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
b@1478 771 return;
b@1478 772 } else {
b@1478 773 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
b@1478 774 }
b@1478 775 if (this.loopPlayback) {
b@1478 776 for (var i=0; i<this.audioObjects.length; i++)
b@1478 777 {
b@1478 778 this.audioObjects[i].play(this.timer.getTestTime()+1);
b@1478 779 if (id == i) {
b@1478 780 this.audioObjects[i].loopStart();
b@1478 781 } else {
b@1478 782 this.audioObjects[i].loopStop();
b@1478 783 }
b@1478 784 }
b@1478 785 } else {
b@1478 786 for (var i=0; i<this.audioObjects.length; i++)
b@1478 787 {
b@1478 788 if (i != id) {
b@1478 789 this.audioObjects[i].outputGain.gain.value = 0.0;
b@1478 790 this.audioObjects[i].stop();
b@1478 791 } else if (i == id) {
b@1478 792 this.audioObjects[id].outputGain.gain.value = 1.0;
b@1478 793 this.audioObjects[id].play(audioContext.currentTime+0.01);
b@1478 794 }
b@1478 795 }
b@1478 796 }
b@1478 797 interfaceContext.playhead.start();
b@1478 798 }
b@1478 799 };
b@1478 800
b@1478 801 this.stop = function() {
b@1478 802 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
b@1478 803 if (this.status == 1) {
b@1478 804 for (var i=0; i<this.audioObjects.length; i++)
b@1478 805 {
b@1478 806 this.audioObjects[i].stop();
b@1478 807 }
b@1478 808 interfaceContext.playhead.stop();
b@1478 809 this.status = 0;
b@1478 810 }
b@1478 811 };
b@1478 812
b@1478 813 this.newTrack = function(element) {
b@1478 814 // Pull data from given URL into new audio buffer
b@1478 815 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
b@1478 816
b@1478 817 // Create the audioObject with ID of the new track length;
b@1478 818 audioObjectId = this.audioObjects.length;
b@1478 819 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
b@1478 820
b@1478 821 // AudioObject will get track itself.
b@1478 822 this.audioObjects[audioObjectId].specification = element;
b@1478 823 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
b@1478 824 return this.audioObjects[audioObjectId];
b@1478 825 };
b@1478 826
b@1478 827 this.newTestPage = function() {
b@1478 828 this.state = 0;
b@1478 829 this.audioObjectsReady = false;
b@1478 830 this.metric.reset();
b@1478 831 this.audioObjects = [];
b@1478 832 };
b@1478 833
b@1478 834 this.checkAllPlayed = function() {
b@1478 835 arr = [];
b@1478 836 for (var id=0; id<this.audioObjects.length; id++) {
b@1478 837 if (this.audioObjects[id].metric.wasListenedTo == false) {
b@1478 838 arr.push(this.audioObjects[id].id);
b@1478 839 }
b@1478 840 }
b@1478 841 return arr;
b@1478 842 };
b@1478 843
b@1478 844 this.checkAllReady = function() {
b@1478 845 var ready = true;
b@1478 846 for (var i=0; i<this.audioObjects.length; i++) {
b@1478 847 if (this.audioObjects[i].state == 0) {
b@1478 848 // Track not ready
b@1478 849 console.log('WAIT -- audioObject '+i+' not ready yet!');
b@1478 850 ready = false;
b@1478 851 };
b@1478 852 }
b@1478 853 return ready;
b@1478 854 };
b@1478 855
b@1478 856 this.setSynchronousLoop = function() {
b@1478 857 // Pads the signals so they are all exactly the same length
b@1478 858 var length = 0;
b@1478 859 var lens = [];
b@1478 860 var maxId;
b@1478 861 for (var i=0; i<this.audioObjects.length; i++)
b@1478 862 {
b@1478 863 lens.push(this.audioObjects[i].buffer.length);
b@1478 864 if (length < this.audioObjects[i].buffer.length)
b@1478 865 {
b@1478 866 length = this.audioObjects[i].buffer.length;
b@1478 867 maxId = i;
b@1478 868 }
b@1478 869 }
b@1478 870 // Perform difference
b@1478 871 for (var i=0; i<lens.length; i++)
b@1478 872 {
b@1478 873 lens[i] = length - lens[i];
b@1478 874 }
b@1478 875 // Extract the audio and zero-pad
b@1478 876 for (var i=0; i<lens.length; i++)
b@1478 877 {
b@1478 878 var orig = this.audioObjects[i].buffer;
b@1478 879 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
b@1478 880 for (var c=0; c<orig.numberOfChannels; c++)
b@1478 881 {
b@1478 882 var inData = hold.getChannelData(c);
b@1478 883 var outData = orig.getChannelData(c);
b@1478 884 for (var n=0; n<orig.length; n++)
b@1478 885 {inData[n] = outData[n];}
b@1478 886 }
b@1478 887 this.audioObjects[i].buffer = hold;
b@1478 888 delete orig;
b@1478 889 }
b@1478 890 };
b@1478 891
b@1478 892 }
b@1478 893
b@1478 894 function audioObject(id) {
b@1478 895 // The main buffer object with common control nodes to the AudioEngine
b@1478 896
b@1478 897 this.specification;
b@1478 898 this.id = id;
b@1478 899 this.state = 0; // 0 - no data, 1 - ready
b@1478 900 this.url = null; // Hold the URL given for the output back to the results.
b@1478 901 this.metric = new metricTracker(this);
b@1478 902
b@1478 903 // Bindings for GUI
b@1478 904 this.interfaceDOM = null;
b@1478 905 this.commentDOM = null;
b@1478 906
b@1478 907 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
b@1478 908 this.bufferNode = undefined;
b@1478 909 this.outputGain = audioContext.createGain();
b@1478 910
b@1478 911 // Default output gain to be zero
b@1478 912 this.outputGain.gain.value = 0.0;
b@1478 913
b@1478 914 // Connect buffer to the audio graph
b@1478 915 this.outputGain.connect(audioEngineContext.outputGain);
b@1478 916
b@1478 917 // the audiobuffer is not designed for multi-start playback
b@1478 918 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
b@1478 919 this.buffer;
b@1478 920
b@1478 921 this.loopStart = function() {
b@1478 922 this.outputGain.gain.value = 1.0;
b@1478 923 this.metric.startListening(audioEngineContext.timer.getTestTime());
b@1478 924 };
b@1478 925
b@1478 926 this.loopStop = function() {
b@1478 927 if (this.outputGain.gain.value != 0.0) {
b@1478 928 this.outputGain.gain.value = 0.0;
b@1478 929 this.metric.stopListening(audioEngineContext.timer.getTestTime());
b@1478 930 }
b@1478 931 };
b@1478 932
b@1478 933 this.play = function(startTime) {
b@1478 934 if (this.bufferNode == undefined) {
b@1478 935 this.bufferNode = audioContext.createBufferSource();
b@1478 936 this.bufferNode.owner = this;
b@1478 937 this.bufferNode.connect(this.outputGain);
b@1478 938 this.bufferNode.buffer = this.buffer;
b@1478 939 this.bufferNode.loop = audioEngineContext.loopPlayback;
b@1478 940 this.bufferNode.onended = function(event) {
b@1478 941 // Safari does not like using 'this' to reference the calling object!
b@1478 942 event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
b@1478 943 };
b@1478 944 if (this.bufferNode.loop == false) {
b@1478 945 this.metric.startListening(audioEngineContext.timer.getTestTime());
b@1478 946 }
b@1478 947 this.bufferNode.start(startTime);
b@1478 948 }
b@1478 949 };
b@1478 950
b@1478 951 this.stop = function() {
b@1478 952 if (this.bufferNode != undefined)
b@1478 953 {
b@1478 954 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
b@1478 955 this.bufferNode.stop(0);
b@1478 956 this.bufferNode = undefined;
b@1478 957 }
b@1478 958 };
b@1478 959
b@1478 960 this.getCurrentPosition = function() {
b@1478 961 var time = audioEngineContext.timer.getTestTime();
b@1478 962 if (this.bufferNode != undefined) {
b@1478 963 if (this.bufferNode.loop == true) {
b@1478 964 if (audioEngineContext.status == 1) {
b@1478 965 return time%this.buffer.duration;
b@1478 966 } else {
b@1478 967 return 0;
b@1478 968 }
b@1478 969 } else {
b@1478 970 if (this.metric.listenHold) {
b@1478 971 return time - this.metric.listenStart;
b@1478 972 } else {
b@1478 973 return 0;
b@1478 974 }
b@1478 975 }
b@1478 976 } else {
b@1478 977 return 0;
b@1478 978 }
b@1478 979 };
b@1478 980
b@1478 981 this.constructTrack = function(url) {
b@1478 982 var request = new XMLHttpRequest();
b@1478 983 this.url = url;
b@1478 984 request.open('GET',url,true);
b@1478 985 request.responseType = 'arraybuffer';
b@1478 986
b@1478 987 var audioObj = this;
b@1478 988
b@1478 989 // Create callback to decode the data asynchronously
b@1478 990 request.onloadend = function() {
b@1478 991 audioContext.decodeAudioData(request.response, function(decodedData) {
b@1478 992 audioObj.buffer = decodedData;
b@1478 993 audioObj.state = 1;
b@1478 994 if (audioObj.specification.type != 'outsidereference')
b@1478 995 {audioObj.interfaceDOM.enable();}
b@1478 996 }, function(){
b@1478 997 // Should only be called if there was an error, but sometimes gets called continuously
b@1478 998 // Check here if the error is genuine
b@1478 999 if (audioObj.state == 0 || audioObj.buffer == undefined) {
b@1478 1000 // Genuine error
b@1478 1001 console.log('FATAL - Error loading buffer on '+audioObj.id);
b@1478 1002 if (request.status == 404)
b@1478 1003 {
b@1478 1004 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
b@1478 1005 console.log('URL: '+audioObj.url);
b@1478 1006 errorSessionDump('Fragment '+audioObj.id+' 404 error');
b@1478 1007 }
b@1478 1008 }
b@1478 1009 });
b@1478 1010 };
b@1478 1011 request.send();
b@1478 1012 };
b@1478 1013
b@1478 1014 this.exportXMLDOM = function() {
b@1478 1015 var root = document.createElement('audioElement');
b@1478 1016 root.id = this.specification.id;
b@1478 1017 root.setAttribute('url',this.url);
b@1478 1018 var file = document.createElement('file');
b@1478 1019 file.setAttribute('sampleRate',this.buffer.sampleRate);
b@1478 1020 file.setAttribute('channels',this.buffer.numberOfChannels);
b@1478 1021 file.setAttribute('sampleCount',this.buffer.length);
b@1478 1022 file.setAttribute('duration',this.buffer.duration);
b@1478 1023 root.appendChild(file);
b@1478 1024 if (this.specification.type != 'outsidereference') {
b@1478 1025 root.appendChild(this.interfaceDOM.exportXMLDOM(this));
b@1478 1026 root.appendChild(this.commentDOM.exportXMLDOM(this));
b@1478 1027 if(this.specification.type == 'anchor') {
b@1478 1028 root.setAttribute('anchor',true);
b@1478 1029 } else if(this.specification.type == 'reference') {
b@1478 1030 root.setAttribute('reference',true);
b@1478 1031 }
b@1478 1032 }
b@1478 1033 root.appendChild(this.metric.exportXMLDOM());
b@1478 1034 return root;
b@1478 1035 };
b@1478 1036 }
b@1478 1037
b@1478 1038 function timer()
b@1478 1039 {
b@1478 1040 /* Timer object used in audioEngine to keep track of session timings
b@1478 1041 * Uses the timer of the web audio API, so sample resolution
b@1478 1042 */
b@1478 1043 this.testStarted = false;
b@1478 1044 this.testStartTime = 0;
b@1478 1045 this.testDuration = 0;
b@1478 1046 this.minimumTestTime = 0; // No minimum test time
b@1478 1047 this.startTest = function()
b@1478 1048 {
b@1478 1049 if (this.testStarted == false)
b@1478 1050 {
b@1478 1051 this.testStartTime = audioContext.currentTime;
b@1478 1052 this.testStarted = true;
b@1478 1053 this.updateTestTime();
b@1478 1054 audioEngineContext.metric.initialiseTest();
b@1478 1055 }
b@1478 1056 };
b@1478 1057 this.stopTest = function()
b@1478 1058 {
b@1478 1059 if (this.testStarted)
b@1478 1060 {
b@1478 1061 this.testDuration = this.getTestTime();
b@1478 1062 this.testStarted = false;
b@1478 1063 } else {
b@1478 1064 console.log('ERR: Test tried to end before beginning');
b@1478 1065 }
b@1478 1066 };
b@1478 1067 this.updateTestTime = function()
b@1478 1068 {
b@1478 1069 if (this.testStarted)
b@1478 1070 {
b@1478 1071 this.testDuration = audioContext.currentTime - this.testStartTime;
b@1478 1072 }
b@1478 1073 };
b@1478 1074 this.getTestTime = function()
b@1478 1075 {
b@1478 1076 this.updateTestTime();
b@1478 1077 return this.testDuration;
b@1478 1078 };
b@1478 1079 }
b@1478 1080
b@1478 1081 function sessionMetrics(engine)
b@1478 1082 {
b@1478 1083 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
b@1478 1084 */
b@1478 1085 this.engine = engine;
b@1478 1086 this.lastClicked = -1;
b@1478 1087 this.data = -1;
b@1478 1088 this.reset = function() {
b@1478 1089 this.lastClicked = -1;
b@1478 1090 this.data = -1;
b@1478 1091 };
b@1478 1092 this.initialiseTest = function(){};
b@1478 1093 }
b@1478 1094
b@1478 1095 function metricTracker(caller)
b@1478 1096 {
b@1478 1097 /* Custom object to track and collect metric data
b@1478 1098 * Used only inside the audioObjects object.
b@1478 1099 */
b@1478 1100
b@1478 1101 this.listenedTimer = 0;
b@1478 1102 this.listenStart = 0;
b@1478 1103 this.listenHold = false;
b@1478 1104 this.initialPosition = -1;
b@1478 1105 this.movementTracker = [];
b@1478 1106 this.listenTracker =[];
b@1478 1107 this.wasListenedTo = false;
b@1478 1108 this.wasMoved = false;
b@1478 1109 this.hasComments = false;
b@1478 1110 this.parent = caller;
b@1478 1111
b@1478 1112 this.initialised = function(position)
b@1478 1113 {
b@1478 1114 if (this.initialPosition == -1) {
b@1478 1115 this.initialPosition = position;
b@1478 1116 }
b@1478 1117 };
b@1478 1118
b@1478 1119 this.moved = function(time,position)
b@1478 1120 {
b@1478 1121 this.wasMoved = true;
b@1478 1122 this.movementTracker[this.movementTracker.length] = [time, position];
b@1478 1123 };
b@1478 1124
b@1478 1125 this.startListening = function(time)
b@1478 1126 {
b@1478 1127 if (this.listenHold == false)
b@1478 1128 {
b@1478 1129 this.wasListenedTo = true;
b@1478 1130 this.listenStart = time;
b@1478 1131 this.listenHold = true;
b@1478 1132
b@1478 1133 var evnt = document.createElement('event');
b@1478 1134 var testTime = document.createElement('testTime');
b@1478 1135 testTime.setAttribute('start',time);
b@1478 1136 var bufferTime = document.createElement('bufferTime');
b@1478 1137 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
b@1478 1138 evnt.appendChild(testTime);
b@1478 1139 evnt.appendChild(bufferTime);
b@1478 1140 this.listenTracker.push(evnt);
b@1478 1141
b@1478 1142 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
b@1478 1143 }
b@1478 1144 };
b@1478 1145
b@1478 1146 this.stopListening = function(time,bufferStopTime)
b@1478 1147 {
b@1478 1148 if (this.listenHold == true)
b@1478 1149 {
b@1478 1150 var diff = time - this.listenStart;
b@1478 1151 this.listenedTimer += (diff);
b@1478 1152 this.listenStart = 0;
b@1478 1153 this.listenHold = false;
b@1478 1154
b@1478 1155 var evnt = this.listenTracker[this.listenTracker.length-1];
b@1478 1156 var testTime = evnt.getElementsByTagName('testTime')[0];
b@1478 1157 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
b@1478 1158 testTime.setAttribute('stop',time);
b@1478 1159 if (bufferStopTime == undefined) {
b@1478 1160 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
b@1478 1161 } else {
b@1478 1162 bufferTime.setAttribute('stop',bufferStopTime);
b@1478 1163 }
b@1478 1164 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
b@1478 1165 }
b@1478 1166 };
b@1478 1167
b@1478 1168 this.exportXMLDOM = function() {
b@1478 1169 var root = document.createElement('metric');
b@1478 1170 if (audioEngineContext.metric.enableElementTimer) {
b@1478 1171 var mElementTimer = document.createElement('metricresult');
b@1478 1172 mElementTimer.setAttribute('name','enableElementTimer');
b@1478 1173 mElementTimer.textContent = this.listenedTimer;
b@1478 1174 root.appendChild(mElementTimer);
b@1478 1175 }
b@1478 1176 if (audioEngineContext.metric.enableElementTracker) {
b@1478 1177 var elementTrackerFull = document.createElement('metricResult');
b@1478 1178 elementTrackerFull.setAttribute('name','elementTrackerFull');
b@1478 1179 for (var k=0; k<this.movementTracker.length; k++)
b@1478 1180 {
b@1478 1181 var timePos = document.createElement('timePos');
b@1478 1182 timePos.id = k;
b@1478 1183 var time = document.createElement('time');
b@1478 1184 time.textContent = this.movementTracker[k][0];
b@1478 1185 var position = document.createElement('position');
b@1478 1186 position.textContent = this.movementTracker[k][1];
b@1478 1187 timePos.appendChild(time);
b@1478 1188 timePos.appendChild(position);
b@1478 1189 elementTrackerFull.appendChild(timePos);
b@1478 1190 }
b@1478 1191 root.appendChild(elementTrackerFull);
b@1478 1192 }
b@1478 1193 if (audioEngineContext.metric.enableElementListenTracker) {
b@1478 1194 var elementListenTracker = document.createElement('metricResult');
b@1478 1195 elementListenTracker.setAttribute('name','elementListenTracker');
b@1478 1196 for (var k=0; k<this.listenTracker.length; k++) {
b@1478 1197 elementListenTracker.appendChild(this.listenTracker[k]);
b@1478 1198 }
b@1478 1199 root.appendChild(elementListenTracker);
b@1478 1200 }
b@1478 1201 if (audioEngineContext.metric.enableElementInitialPosition) {
b@1478 1202 var elementInitial = document.createElement('metricResult');
b@1478 1203 elementInitial.setAttribute('name','elementInitialPosition');
b@1478 1204 elementInitial.textContent = this.initialPosition;
b@1478 1205 root.appendChild(elementInitial);
b@1478 1206 }
b@1478 1207 if (audioEngineContext.metric.enableFlagListenedTo) {
b@1478 1208 var flagListenedTo = document.createElement('metricResult');
b@1478 1209 flagListenedTo.setAttribute('name','elementFlagListenedTo');
b@1478 1210 flagListenedTo.textContent = this.wasListenedTo;
b@1478 1211 root.appendChild(flagListenedTo);
b@1478 1212 }
b@1478 1213 if (audioEngineContext.metric.enableFlagMoved) {
b@1478 1214 var flagMoved = document.createElement('metricResult');
b@1478 1215 flagMoved.setAttribute('name','elementFlagMoved');
b@1478 1216 flagMoved.textContent = this.wasMoved;
b@1478 1217 root.appendChild(flagMoved);
b@1478 1218 }
b@1478 1219 if (audioEngineContext.metric.enableFlagComments) {
b@1478 1220 var flagComments = document.createElement('metricResult');
b@1478 1221 flagComments.setAttribute('name','elementFlagComments');
b@1478 1222 if (this.parent.commentDOM == null)
b@1478 1223 {flag.textContent = 'false';}
b@1478 1224 else if (this.parent.commentDOM.textContent.length == 0)
b@1478 1225 {flag.textContent = 'false';}
b@1478 1226 else
b@1478 1227 {flag.textContet = 'true';}
b@1478 1228 root.appendChild(flagComments);
b@1478 1229 }
b@1478 1230
b@1478 1231 return root;
b@1478 1232 };
b@1478 1233 }
b@1478 1234
b@1478 1235 function randomiseOrder(input)
b@1478 1236 {
b@1478 1237 // This takes an array of information and randomises the order
b@1478 1238 var N = input.length;
b@1478 1239
b@1478 1240 var inputSequence = []; // For safety purposes: keep track of randomisation
b@1478 1241 for (var counter = 0; counter < N; ++counter)
b@1478 1242 inputSequence.push(counter) // Fill array
b@1478 1243 var inputSequenceClone = inputSequence.slice(0);
b@1478 1244
b@1478 1245 var holdArr = [];
b@1478 1246 var outputSequence = [];
b@1478 1247 for (var n=0; n<N; n++)
b@1478 1248 {
b@1478 1249 // First pick a random number
b@1478 1250 var r = Math.random();
b@1478 1251 // Multiply and floor by the number of elements left
b@1478 1252 r = Math.floor(r*input.length);
b@1478 1253 // Pick out that element and delete from the array
b@1478 1254 holdArr.push(input.splice(r,1)[0]);
b@1478 1255 // Do the same with sequence
b@1478 1256 outputSequence.push(inputSequence.splice(r,1)[0]);
b@1478 1257 }
b@1478 1258 console.log(inputSequenceClone.toString()); // print original array to console
b@1478 1259 console.log(outputSequence.toString()); // print randomised array to console
b@1478 1260 return holdArr;
b@1478 1261 }
b@1478 1262
b@1478 1263 function returnDateNode()
b@1478 1264 {
b@1478 1265 // Create an XML Node for the Date and Time a test was conducted
b@1478 1266 // Structure is
b@1478 1267 // <datetime>
b@1478 1268 // <date year="##" month="##" day="##">DD/MM/YY</date>
b@1478 1269 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
b@1478 1270 // </datetime>
b@1478 1271 var dateTime = new Date();
b@1478 1272 var year = document.createAttribute('year');
b@1478 1273 var month = document.createAttribute('month');
b@1478 1274 var day = document.createAttribute('day');
b@1478 1275 var hour = document.createAttribute('hour');
b@1478 1276 var minute = document.createAttribute('minute');
b@1478 1277 var secs = document.createAttribute('secs');
b@1478 1278
b@1478 1279 year.nodeValue = dateTime.getFullYear();
b@1478 1280 month.nodeValue = dateTime.getMonth()+1;
b@1478 1281 day.nodeValue = dateTime.getDate();
b@1478 1282 hour.nodeValue = dateTime.getHours();
b@1478 1283 minute.nodeValue = dateTime.getMinutes();
b@1478 1284 secs.nodeValue = dateTime.getSeconds();
b@1478 1285
b@1478 1286 var hold = document.createElement("datetime");
b@1478 1287 var date = document.createElement("date");
b@1478 1288 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
b@1478 1289 var time = document.createElement("time");
b@1478 1290 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
b@1478 1291
b@1478 1292 date.setAttributeNode(year);
b@1478 1293 date.setAttributeNode(month);
b@1478 1294 date.setAttributeNode(day);
b@1478 1295 time.setAttributeNode(hour);
b@1478 1296 time.setAttributeNode(minute);
b@1478 1297 time.setAttributeNode(secs);
b@1478 1298
b@1478 1299 hold.appendChild(date);
b@1478 1300 hold.appendChild(time);
b@1478 1301 return hold
b@1478 1302
b@1478 1303 }
b@1478 1304
b@1478 1305 function Specification() {
b@1478 1306 // Handles the decoding of the project specification XML into a simple JavaScript Object.
b@1478 1307
b@1478 1308 this.interfaceType;
b@1478 1309 this.commonInterface;
b@1478 1310 this.projectReturn;
b@1478 1311 this.randomiseOrder;
b@1478 1312 this.collectMetrics;
b@1478 1313 this.testPages;
b@1478 1314 this.preTest;
b@1478 1315 this.postTest;
b@1478 1316 this.metrics =[];
b@1478 1317
b@1478 1318 this.audioHolders = [];
b@1478 1319
b@1478 1320 this.decode = function() {
b@1478 1321 // projectXML - DOM Parsed document
b@1478 1322 this.projectXML = projectXML.childNodes[0];
b@1478 1323 var setupNode = projectXML.getElementsByTagName('setup')[0];
b@1478 1324 this.interfaceType = setupNode.getAttribute('interface');
b@1478 1325 this.projectReturn = setupNode.getAttribute('projectReturn');
b@1478 1326 this.testPages = setupNode.getAttribute('testPages');
b@1478 1327 if (setupNode.getAttribute('randomiseOrder') == "true") {
b@1478 1328 this.randomiseOrder = true;
b@1478 1329 } else {this.randomiseOrder = false;}
b@1478 1330 if (setupNode.getAttribute('collectMetrics') == "true") {
b@1478 1331 this.collectMetrics = true;
b@1478 1332 } else {this.collectMetrics = false;}
b@1478 1333 if (isNaN(Number(this.testPages)) || this.testPages == undefined)
b@1478 1334 {
b@1478 1335 this.testPages = null;
b@1478 1336 } else {
b@1478 1337 this.testPages = Number(this.testPages);
b@1478 1338 if (this.testPages == 0) {this.testPages = null;}
b@1478 1339 }
b@1478 1340 var metricCollection = setupNode.getElementsByTagName('Metric');
b@1478 1341
b@1478 1342 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
b@1478 1343 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
b@1478 1344
b@1478 1345 if (metricCollection.length > 0) {
b@1478 1346 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
b@1478 1347 for (var i=0; i<metricCollection.length; i++) {
b@1478 1348 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
b@1478 1349 }
b@1478 1350 }
b@1478 1351
b@1478 1352 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
b@1478 1353 if (commonInterfaceNode.length > 0) {
b@1478 1354 commonInterfaceNode = commonInterfaceNode[0];
b@1478 1355 } else {
b@1478 1356 commonInterfaceNode = undefined;
b@1478 1357 }
b@1478 1358
b@1478 1359 this.commonInterface = new function() {
b@1478 1360 this.OptionNode = function(child) {
b@1478 1361 this.type = child.nodeName;
b@1478 1362 if (this.type == 'option')
b@1478 1363 {
b@1478 1364 this.name = child.getAttribute('name');
b@1478 1365 }
b@1478 1366 else if (this.type == 'check') {
b@1478 1367 this.check = child.getAttribute('name');
b@1478 1368 if (this.check == 'scalerange') {
b@1478 1369 this.min = child.getAttribute('min');
b@1478 1370 this.max = child.getAttribute('max');
b@1478 1371 if (this.min == null) {this.min = 1;}
b@1478 1372 else if (Number(this.min) > 1 && this.min != null) {
b@1478 1373 this.min = Number(this.min)/100;
b@1478 1374 } else {
b@1478 1375 this.min = Number(this.min);
b@1478 1376 }
b@1478 1377 if (this.max == null) {this.max = 0;}
b@1478 1378 else if (Number(this.max) > 1 && this.max != null) {
b@1478 1379 this.max = Number(this.max)/100;
b@1478 1380 } else {
b@1478 1381 this.max = Number(this.max);
b@1478 1382 }
b@1478 1383 }
b@1478 1384 } else if (this.type == 'anchor' || this.type == 'reference') {
b@1478 1385 this.value = Number(child.textContent);
b@1478 1386 this.enforce = child.getAttribute('enforce');
b@1478 1387 if (this.enforce == 'true') {this.enforce = true;}
b@1478 1388 else {this.enforce = false;}
b@1478 1389 }
b@1478 1390 };
b@1478 1391 this.options = [];
b@1478 1392 if (commonInterfaceNode != undefined) {
b@1478 1393 var child = commonInterfaceNode.firstElementChild;
b@1478 1394 while (child != undefined) {
b@1478 1395 this.options.push(new this.OptionNode(child));
b@1478 1396 child = child.nextElementSibling;
b@1478 1397 }
b@1478 1398 }
b@1478 1399 };
b@1478 1400
b@1478 1401 var audioHolders = projectXML.getElementsByTagName('audioHolder');
b@1478 1402 for (var i=0; i<audioHolders.length; i++) {
b@1478 1403 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
b@1478 1404 }
b@1478 1405
b@1478 1406 // New check if we need to randomise the test order
b@1478 1407 if (this.randomiseOrder)
b@1478 1408 {
b@1478 1409 this.audioHolders = randomiseOrder(this.audioHolders);
b@1478 1410 for (var i=0; i<this.audioHolders.length; i++)
b@1478 1411 {
b@1478 1412 this.audioHolders[i].presentedId = i;
b@1478 1413 }
b@1478 1414 }
b@1478 1415
b@1478 1416 if (this.testPages != null || this.testPages != undefined)
b@1478 1417 {
b@1478 1418 if (this.testPages > audioHolders.length)
b@1478 1419 {
b@1478 1420 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!');
b@1478 1421 this.testPages = audioHolders.length;
b@1478 1422 }
b@1478 1423 var aH = this.audioHolders;
b@1478 1424 this.audioHolders = [];
b@1478 1425 for (var i=0; i<this.testPages; i++)
b@1478 1426 {
b@1478 1427 this.audioHolders.push(aH[i]);
b@1478 1428 }
b@1478 1429 }
b@1478 1430 };
b@1478 1431
b@1478 1432 this.prepostNode = function(type,Collection) {
b@1478 1433 this.type = type;
b@1478 1434 this.options = [];
b@1478 1435
b@1478 1436 this.OptionNode = function(child) {
b@1478 1437
b@1478 1438 this.childOption = function(element) {
b@1478 1439 this.type = 'option';
b@1478 1440 this.id = element.id;
b@1478 1441 this.name = element.getAttribute('name');
b@1478 1442 this.text = element.textContent;
b@1478 1443 };
b@1478 1444
b@1478 1445 this.type = child.nodeName;
b@1478 1446 if (child.nodeName == "question") {
b@1478 1447 this.id = child.id;
b@1478 1448 this.mandatory;
b@1478 1449 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
b@1478 1450 else {this.mandatory = false;}
b@1478 1451 this.question = child.textContent;
b@1478 1452 if (child.getAttribute('boxsize') == null) {
b@1478 1453 this.boxsize = 'normal';
b@1478 1454 } else {
b@1478 1455 this.boxsize = child.getAttribute('boxsize');
b@1478 1456 }
b@1478 1457 } else if (child.nodeName == "statement") {
b@1478 1458 this.statement = child.textContent;
b@1478 1459 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
b@1478 1460 var element = child.firstElementChild;
b@1478 1461 this.id = child.id;
b@1478 1462 if (element == null) {
b@1478 1463 console.log('Malformed' +child.nodeName+ 'entry');
b@1478 1464 this.statement = 'Malformed' +child.nodeName+ 'entry';
b@1478 1465 this.type = 'statement';
b@1478 1466 } else {
b@1478 1467 this.options = [];
b@1478 1468 while (element != null) {
b@1478 1469 if (element.nodeName == 'statement' && this.statement == undefined){
b@1478 1470 this.statement = element.textContent;
b@1478 1471 } else if (element.nodeName == 'option') {
b@1478 1472 this.options.push(new this.childOption(element));
b@1478 1473 }
b@1478 1474 element = element.nextElementSibling;
b@1478 1475 }
b@1478 1476 }
b@1478 1477 } else if (child.nodeName == "number") {
b@1478 1478 this.statement = child.textContent;
b@1478 1479 this.id = child.id;
b@1478 1480 this.min = child.getAttribute('min');
b@1478 1481 this.max = child.getAttribute('max');
b@1478 1482 this.step = child.getAttribute('step');
b@1478 1483 }
b@1478 1484 };
b@1478 1485
b@1478 1486 // On construction:
b@1478 1487 if (Collection.length != 0) {
b@1478 1488 Collection = Collection[0];
b@1478 1489 if (Collection.childElementCount != 0) {
b@1478 1490 var child = Collection.firstElementChild;
b@1478 1491 this.options.push(new this.OptionNode(child));
b@1478 1492 while (child.nextElementSibling != null) {
b@1478 1493 child = child.nextElementSibling;
b@1478 1494 this.options.push(new this.OptionNode(child));
b@1478 1495 }
b@1478 1496 }
b@1478 1497 }
b@1478 1498 };
b@1478 1499
b@1478 1500 this.metricNode = function(name) {
b@1478 1501 this.enabled = name;
b@1478 1502 };
b@1478 1503
b@1478 1504 this.audioHolderNode = function(parent,xml) {
b@1478 1505 this.type = 'audioHolder';
b@1478 1506 this.presentedId = parent.audioHolders.length;
b@1478 1507 this.interfaceNode = function(DOM) {
b@1478 1508 var title = DOM.getElementsByTagName('title');
b@1478 1509 if (title.length == 0) {this.title = null;}
b@1478 1510 else {this.title = title[0].textContent;}
b@1478 1511 this.options = parent.commonInterface.options;
b@1478 1512 var scale = DOM.getElementsByTagName('scale');
b@1478 1513 this.scale = [];
b@1478 1514 for (var i=0; i<scale.length; i++) {
b@1478 1515 var arr = [null, null];
b@1478 1516 arr[0] = scale[i].getAttribute('position');
b@1478 1517 arr[1] = scale[i].textContent;
b@1478 1518 this.scale.push(arr);
b@1478 1519 }
b@1478 1520 };
b@1478 1521
b@1478 1522 this.audioElementNode = function(parent,xml) {
b@1478 1523 this.url = xml.getAttribute('url');
b@1478 1524 this.id = xml.id;
b@1478 1525 this.parent = parent;
b@1478 1526 this.type = xml.getAttribute('type');
b@1478 1527 if (this.type == null) {this.type = "normal";}
b@1478 1528 if (this.type == 'anchor') {this.anchor = true;}
b@1478 1529 else {this.anchor = false;}
b@1478 1530 if (this.type == 'reference') {this.reference = true;}
b@1478 1531 else {this.reference = false;}
b@1478 1532
b@1478 1533 this.marker = xml.getAttribute('marker');
b@1478 1534 if (this.marker == null) {this.marker = undefined;}
b@1478 1535
b@1478 1536 if (this.anchor == true) {
b@1478 1537 if (this.marker != undefined) {this.enforce = true;}
b@1478 1538 else {this.enforce = enforceAnchor;}
b@1478 1539 this.marker = anchor;
b@1478 1540 }
b@1478 1541 else if (this.reference == true) {
b@1478 1542 if (this.marker != undefined) {this.enforce = true;}
b@1478 1543 else {this.enforce = enforceReference;}
b@1478 1544 this.marker = reference;
b@1478 1545 }
b@1478 1546
b@1478 1547 if (this.marker != undefined) {
b@1478 1548 this.marker = Number(this.marker);
b@1478 1549 if (this.marker > 1) {this.marker /= 100;}
b@1478 1550 }
b@1478 1551 };
b@1478 1552
b@1478 1553 this.commentQuestionNode = function(xml) {
b@1478 1554 this.childOption = function(element) {
b@1478 1555 this.type = 'option';
b@1478 1556 this.name = element.getAttribute('name');
b@1478 1557 this.text = element.textContent;
b@1478 1558 };
b@1478 1559 this.id = xml.id;
b@1478 1560 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
b@1478 1561 else {this.mandatory = false;}
b@1478 1562 this.type = xml.getAttribute('type');
b@1478 1563 if (this.type == undefined) {this.type = 'text';}
b@1478 1564 switch (this.type) {
b@1478 1565 case 'text':
b@1478 1566 this.question = xml.textContent;
b@1478 1567 break;
b@1478 1568 case 'radio':
b@1478 1569 var child = xml.firstElementChild;
b@1478 1570 this.options = [];
b@1478 1571 while (child != undefined) {
b@1478 1572 if (child.nodeName == 'statement' && this.statement == undefined) {
b@1478 1573 this.statement = child.textContent;
b@1478 1574 } else if (child.nodeName == 'option') {
b@1478 1575 this.options.push(new this.childOption(child));
b@1478 1576 }
b@1478 1577 child = child.nextElementSibling;
b@1478 1578 }
b@1478 1579 break;
b@1478 1580 case 'checkbox':
b@1478 1581 var child = xml.firstElementChild;
b@1478 1582 this.options = [];
b@1478 1583 while (child != undefined) {
b@1478 1584 if (child.nodeName == 'statement' && this.statement == undefined) {
b@1478 1585 this.statement = child.textContent;
b@1478 1586 } else if (child.nodeName == 'option') {
b@1478 1587 this.options.push(new this.childOption(child));
b@1478 1588 }
b@1478 1589 child = child.nextElementSibling;
b@1478 1590 }
b@1478 1591 break;
b@1478 1592 }
b@1478 1593 };
b@1478 1594
b@1478 1595 this.id = xml.id;
b@1478 1596 this.hostURL = xml.getAttribute('hostURL');
b@1478 1597 this.sampleRate = xml.getAttribute('sampleRate');
b@1478 1598 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
b@1478 1599 else {this.randomiseOrder = false;}
b@1478 1600 this.repeatCount = xml.getAttribute('repeatCount');
b@1478 1601 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
b@1478 1602 else {this.loop == false;}
b@1478 1603 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
b@1478 1604 else {this.elementComments = false;}
b@1478 1605
b@1478 1606 var anchor = xml.getElementsByTagName('anchor');
b@1478 1607 var enforceAnchor = false;
b@1478 1608 if (anchor.length == 0) {
b@1478 1609 // Find anchor in commonInterface;
b@1478 1610 for (var i=0; i<parent.commonInterface.options.length; i++) {
b@1478 1611 if(parent.commonInterface.options[i].type == 'anchor') {
b@1478 1612 anchor = parent.commonInterface.options[i].value;
b@1478 1613 enforceAnchor = parent.commonInterface.options[i].enforce;
b@1478 1614 break;
b@1478 1615 }
b@1478 1616 }
b@1478 1617 if (typeof(anchor) == "object") {
b@1478 1618 anchor = null;
b@1478 1619 }
b@1478 1620 } else {
b@1478 1621 anchor = anchor[0].textContent;
b@1478 1622 }
b@1478 1623
b@1478 1624 var reference = xml.getElementsByTagName('anchor');
b@1478 1625 var enforceReference = false;
b@1478 1626 if (reference.length == 0) {
b@1478 1627 // Find anchor in commonInterface;
b@1478 1628 for (var i=0; i<parent.commonInterface.options.length; i++) {
b@1478 1629 if(parent.commonInterface.options[i].type == 'reference') {
b@1478 1630 reference = parent.commonInterface.options[i].value;
b@1478 1631 enforceReference = parent.commonInterface.options[i].enforce;
b@1478 1632 break;
b@1478 1633 }
b@1478 1634 }
b@1478 1635 if (typeof(reference) == "object") {
b@1478 1636 reference = null;
b@1478 1637 }
b@1478 1638 } else {
b@1478 1639 reference = reference[0].textContent;
b@1478 1640 }
b@1478 1641
b@1478 1642 if (typeof(anchor) == 'number') {
b@1478 1643 if (anchor > 1 && anchor < 100) {anchor /= 100.0;}
b@1478 1644 }
b@1478 1645
b@1478 1646 if (typeof(reference) == 'number') {
b@1478 1647 if (reference > 1 && reference < 100) {reference /= 100.0;}
b@1478 1648 }
b@1478 1649
b@1478 1650 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
b@1478 1651 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
b@1478 1652
b@1478 1653 this.interfaces = [];
b@1478 1654 var interfaceDOM = xml.getElementsByTagName('interface');
b@1478 1655 for (var i=0; i<interfaceDOM.length; i++) {
b@1478 1656 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
b@1478 1657 }
b@1478 1658
b@1478 1659 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
b@1478 1660 if (this.commentBoxPrefix.length != 0) {
b@1478 1661 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
b@1478 1662 } else {
b@1478 1663 this.commentBoxPrefix = "Comment on track";
b@1478 1664 }
b@1478 1665
b@1478 1666 this.audioElements =[];
b@1478 1667 var audioElementsDOM = xml.getElementsByTagName('audioElements');
b@1478 1668 this.outsideReference = null;
b@1478 1669 for (var i=0; i<audioElementsDOM.length; i++) {
b@1478 1670 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
b@1478 1671 if (this.outsideReference == null) {
b@1478 1672 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]);
b@1478 1673 } else {
b@1478 1674 console.log('Error only one audioelement can be of type outsidereference per audioholder');
b@1478 1675 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
b@1478 1676 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
b@1478 1677 }
b@1478 1678 } else {
b@1478 1679 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
b@1478 1680 }
b@1478 1681 }
b@1478 1682
b@1478 1683 if (this.randomiseOrder) {
b@1478 1684 this.audioElements = randomiseOrder(this.audioElements);
b@1478 1685 }
b@1478 1686
b@1478 1687 // Check only one anchor and one reference per audioNode
b@1478 1688 var anchor = [];
b@1478 1689 var reference = [];
b@1478 1690 this.anchorId = null;
b@1478 1691 this.referenceId = null;
b@1478 1692 for (var i=0; i<this.audioElements.length; i++)
b@1478 1693 {
b@1478 1694 if (this.audioElements[i].anchor == true) {anchor.push(i);}
b@1478 1695 if (this.audioElements[i].reference == true) {reference.push(i);}
b@1478 1696 }
b@1478 1697
b@1478 1698 if (anchor.length > 1) {
b@1478 1699 console.log('Error - cannot have more than one anchor!');
b@1478 1700 console.log('Each anchor node will be a normal mode to continue the test');
b@1478 1701 for (var i=0; i<anchor.length; i++)
b@1478 1702 {
b@1478 1703 this.audioElements[anchor[i]].anchor = false;
b@1478 1704 this.audioElements[anchor[i]].value = undefined;
b@1478 1705 }
b@1478 1706 } else {this.anchorId = anchor[0];}
b@1478 1707 if (reference.length > 1) {
b@1478 1708 console.log('Error - cannot have more than one anchor!');
b@1478 1709 console.log('Each anchor node will be a normal mode to continue the test');
b@1478 1710 for (var i=0; i<reference.length; i++)
b@1478 1711 {
b@1478 1712 this.audioElements[reference[i]].reference = false;
b@1478 1713 this.audioElements[reference[i]].value = undefined;
b@1478 1714 }
b@1478 1715 } else {this.referenceId = reference[0];}
b@1478 1716
b@1478 1717 this.commentQuestions = [];
b@1478 1718 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
b@1478 1719 for (var i=0; i<commentQuestionsDOM.length; i++) {
b@1478 1720 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
b@1478 1721 }
b@1478 1722 };
b@1478 1723 }
b@1478 1724
b@1478 1725 function Interface(specificationObject) {
b@1478 1726 // This handles the bindings between the interface and the audioEngineContext;
b@1478 1727 this.specification = specificationObject;
b@1478 1728 this.insertPoint = document.getElementById("topLevelBody");
b@1478 1729
b@1478 1730 // Bounded by interface!!
b@1478 1731 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
b@1478 1732 // For example, APE returns the slider position normalised in a <value> tag.
b@1478 1733 this.interfaceObjects = [];
b@1478 1734 this.interfaceObject = function(){};
b@1478 1735
b@1478 1736 this.resizeWindow = function(event)
b@1478 1737 {
b@1478 1738 for(var i=0; i<this.commentBoxes.length; i++)
b@1478 1739 {this.commentBoxes[i].resize();}
b@1478 1740 for(var i=0; i<this.commentQuestions.length; i++)
b@1478 1741 {this.commentQuestions[i].resize();}
b@1478 1742 try
b@1478 1743 {
b@1478 1744 resizeWindow(event);
b@1478 1745 }
b@1478 1746 catch(err)
b@1478 1747 {
b@1478 1748 console.log("Warning - Interface does not have Resize option");
b@1478 1749 console.log(err);
b@1478 1750 }
b@1478 1751 };
b@1478 1752
b@1478 1753 this.commentBoxes = [];
b@1478 1754 this.elementCommentBox = function(audioObject) {
b@1478 1755 var element = audioObject.specification;
b@1478 1756 this.audioObject = audioObject;
b@1478 1757 this.id = audioObject.id;
b@1478 1758 var audioHolderObject = audioObject.specification.parent;
b@1478 1759 // Create document objects to hold the comment boxes
b@1478 1760 this.trackComment = document.createElement('div');
b@1478 1761 this.trackComment.className = 'comment-div';
b@1478 1762 this.trackComment.id = 'comment-div-'+audioObject.id;
b@1478 1763 // Create a string next to each comment asking for a comment
b@1478 1764 this.trackString = document.createElement('span');
b@1478 1765 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
b@1478 1766 // Create the HTML5 comment box 'textarea'
b@1478 1767 this.trackCommentBox = document.createElement('textarea');
b@1478 1768 this.trackCommentBox.rows = '4';
b@1478 1769 this.trackCommentBox.cols = '100';
b@1478 1770 this.trackCommentBox.name = 'trackComment'+audioObject.id;
b@1478 1771 this.trackCommentBox.className = 'trackComment';
b@1478 1772 var br = document.createElement('br');
b@1478 1773 // Add to the holder.
b@1478 1774 this.trackComment.appendChild(this.trackString);
b@1478 1775 this.trackComment.appendChild(br);
b@1478 1776 this.trackComment.appendChild(this.trackCommentBox);
b@1478 1777
b@1478 1778 this.exportXMLDOM = function() {
b@1478 1779 var root = document.createElement('comment');
b@1478 1780 if (this.audioObject.specification.parent.elementComments) {
b@1478 1781 var question = document.createElement('question');
b@1478 1782 question.textContent = this.trackString.textContent;
b@1478 1783 var response = document.createElement('response');
b@1478 1784 response.textContent = this.trackCommentBox.value;
b@1478 1785 console.log("Comment frag-"+this.id+": "+response.textContent);
b@1478 1786 root.appendChild(question);
b@1478 1787 root.appendChild(response);
b@1478 1788 }
b@1478 1789 return root;
b@1478 1790 };
b@1478 1791 this.resize = function()
b@1478 1792 {
b@1478 1793 var boxwidth = (window.innerWidth-100)/2;
b@1478 1794 if (boxwidth >= 600)
b@1478 1795 {
b@1478 1796 boxwidth = 600;
b@1478 1797 }
b@1478 1798 else if (boxwidth < 400)
b@1478 1799 {
b@1478 1800 boxwidth = 400;
b@1478 1801 }
b@1478 1802 this.trackComment.style.width = boxwidth+"px";
b@1478 1803 this.trackCommentBox.style.width = boxwidth-6+"px";
b@1478 1804 };
b@1478 1805 this.resize();
b@1478 1806 };
b@1478 1807
b@1478 1808 this.commentQuestions = [];
b@1478 1809
b@1478 1810 this.commentBox = function(commentQuestion) {
b@1478 1811 this.specification = commentQuestion;
b@1478 1812 // Create document objects to hold the comment boxes
b@1478 1813 this.holder = document.createElement('div');
b@1478 1814 this.holder.className = 'comment-div';
b@1478 1815 // Create a string next to each comment asking for a comment
b@1478 1816 this.string = document.createElement('span');
b@1478 1817 this.string.innerHTML = commentQuestion.question;
b@1478 1818 // Create the HTML5 comment box 'textarea'
b@1478 1819 this.textArea = document.createElement('textarea');
b@1478 1820 this.textArea.rows = '4';
b@1478 1821 this.textArea.cols = '100';
b@1478 1822 this.textArea.className = 'trackComment';
b@1478 1823 var br = document.createElement('br');
b@1478 1824 // Add to the holder.
b@1478 1825 this.holder.appendChild(this.string);
b@1478 1826 this.holder.appendChild(br);
b@1478 1827 this.holder.appendChild(this.textArea);
b@1478 1828
b@1478 1829 this.exportXMLDOM = function() {
b@1478 1830 var root = document.createElement('comment');
b@1478 1831 root.id = this.specification.id;
b@1478 1832 root.setAttribute('type',this.specification.type);
b@1478 1833 root.textContent = this.textArea.value;
b@1478 1834 console.log("Question: "+this.string.textContent);
b@1478 1835 console.log("Response: "+root.textContent);
b@1478 1836 return root;
b@1478 1837 };
b@1478 1838 this.resize = function()
b@1478 1839 {
b@1478 1840 var boxwidth = (window.innerWidth-100)/2;
b@1478 1841 if (boxwidth >= 600)
b@1478 1842 {
b@1478 1843 boxwidth = 600;
b@1478 1844 }
b@1478 1845 else if (boxwidth < 400)
b@1478 1846 {
b@1478 1847 boxwidth = 400;
b@1478 1848 }
b@1478 1849 this.holder.style.width = boxwidth+"px";
b@1478 1850 this.textArea.style.width = boxwidth-6+"px";
b@1478 1851 };
b@1478 1852 this.resize();
b@1478 1853 };
b@1478 1854
b@1478 1855 this.radioBox = function(commentQuestion) {
b@1478 1856 this.specification = commentQuestion;
b@1478 1857 // Create document objects to hold the comment boxes
b@1478 1858 this.holder = document.createElement('div');
b@1478 1859 this.holder.className = 'comment-div';
b@1478 1860 // Create a string next to each comment asking for a comment
b@1478 1861 this.string = document.createElement('span');
b@1478 1862 this.string.innerHTML = commentQuestion.statement;
b@1478 1863 var br = document.createElement('br');
b@1478 1864 // Add to the holder.
b@1478 1865 this.holder.appendChild(this.string);
b@1478 1866 this.holder.appendChild(br);
b@1478 1867 this.options = [];
b@1478 1868 this.inputs = document.createElement('div');
b@1478 1869 this.span = document.createElement('div');
b@1478 1870 this.inputs.align = 'center';
b@1478 1871 this.inputs.style.marginLeft = '12px';
b@1478 1872 this.span.style.marginLeft = '12px';
b@1478 1873 this.span.align = 'center';
b@1478 1874 this.span.style.marginTop = '15px';
b@1478 1875
b@1478 1876 var optCount = commentQuestion.options.length;
b@1478 1877 for (var i=0; i<optCount; i++)
b@1478 1878 {
b@1478 1879 var div = document.createElement('div');
b@1478 1880 div.style.width = '80px';
b@1478 1881 div.style.float = 'left';
b@1478 1882 var input = document.createElement('input');
b@1478 1883 input.type = 'radio';
b@1478 1884 input.name = commentQuestion.id;
b@1478 1885 input.setAttribute('setvalue',commentQuestion.options[i].name);
b@1478 1886 input.className = 'comment-radio';
b@1478 1887 div.appendChild(input);
b@1478 1888 this.inputs.appendChild(div);
b@1478 1889
b@1478 1890
b@1478 1891 div = document.createElement('div');
b@1478 1892 div.style.width = '80px';
b@1478 1893 div.style.float = 'left';
b@1478 1894 div.align = 'center';
b@1478 1895 var span = document.createElement('span');
b@1478 1896 span.textContent = commentQuestion.options[i].text;
b@1478 1897 span.className = 'comment-radio-span';
b@1478 1898 div.appendChild(span);
b@1478 1899 this.span.appendChild(div);
b@1478 1900 this.options.push(input);
b@1478 1901 }
b@1478 1902 this.holder.appendChild(this.span);
b@1478 1903 this.holder.appendChild(this.inputs);
b@1478 1904
b@1478 1905 this.exportXMLDOM = function() {
b@1478 1906 var root = document.createElement('comment');
b@1478 1907 root.id = this.specification.id;
b@1478 1908 root.setAttribute('type',this.specification.type);
b@1478 1909 var question = document.createElement('question');
b@1478 1910 question.textContent = this.string.textContent;
b@1478 1911 var response = document.createElement('response');
b@1478 1912 var i=0;
b@1478 1913 while(this.options[i].checked == false) {
b@1478 1914 i++;
b@1478 1915 if (i >= this.options.length) {
b@1478 1916 break;
b@1478 1917 }
b@1478 1918 }
b@1478 1919 if (i >= this.options.length) {
b@1478 1920 response.textContent = 'null';
b@1478 1921 } else {
b@1478 1922 response.textContent = this.options[i].getAttribute('setvalue');
b@1478 1923 response.setAttribute('number',i);
b@1478 1924 }
b@1478 1925 console.log('Comment: '+question.textContent);
b@1478 1926 console.log('Response: '+response.textContent);
b@1478 1927 root.appendChild(question);
b@1478 1928 root.appendChild(response);
b@1478 1929 return root;
b@1478 1930 };
b@1478 1931 this.resize = function()
b@1478 1932 {
b@1478 1933 var boxwidth = (window.innerWidth-100)/2;
b@1478 1934 if (boxwidth >= 600)
b@1478 1935 {
b@1478 1936 boxwidth = 600;
b@1478 1937 }
b@1478 1938 else if (boxwidth < 400)
b@1478 1939 {
b@1478 1940 boxwidth = 400;
b@1478 1941 }
b@1478 1942 this.holder.style.width = boxwidth+"px";
b@1478 1943 var text = this.holder.children[2];
b@1478 1944 var options = this.holder.children[3];
b@1478 1945 var optCount = options.children.length;
b@1478 1946 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
b@1478 1947 var options = options.firstChild;
b@1478 1948 var text = text.firstChild;
b@1478 1949 options.style.marginRight = spanMargin;
b@1478 1950 options.style.marginLeft = spanMargin;
b@1478 1951 text.style.marginRight = spanMargin;
b@1478 1952 text.style.marginLeft = spanMargin;
b@1478 1953 while(options.nextSibling != undefined)
b@1478 1954 {
b@1478 1955 options = options.nextSibling;
b@1478 1956 text = text.nextSibling;
b@1478 1957 options.style.marginRight = spanMargin;
b@1478 1958 options.style.marginLeft = spanMargin;
b@1478 1959 text.style.marginRight = spanMargin;
b@1478 1960 text.style.marginLeft = spanMargin;
b@1478 1961 }
b@1478 1962 };
b@1478 1963 this.resize();
b@1478 1964 };
b@1478 1965
b@1478 1966 this.checkboxBox = function(commentQuestion) {
b@1478 1967 this.specification = commentQuestion;
b@1478 1968 // Create document objects to hold the comment boxes
b@1478 1969 this.holder = document.createElement('div');
b@1478 1970 this.holder.className = 'comment-div';
b@1478 1971 // Create a string next to each comment asking for a comment
b@1478 1972 this.string = document.createElement('span');
b@1478 1973 this.string.innerHTML = commentQuestion.statement;
b@1478 1974 var br = document.createElement('br');
b@1478 1975 // Add to the holder.
b@1478 1976 this.holder.appendChild(this.string);
b@1478 1977 this.holder.appendChild(br);
b@1478 1978 this.options = [];
b@1478 1979 this.inputs = document.createElement('div');
b@1478 1980 this.span = document.createElement('div');
b@1478 1981 this.inputs.align = 'center';
b@1478 1982 this.inputs.style.marginLeft = '12px';
b@1478 1983 this.span.style.marginLeft = '12px';
b@1478 1984 this.span.align = 'center';
b@1478 1985 this.span.style.marginTop = '15px';
b@1478 1986
b@1478 1987 var optCount = commentQuestion.options.length;
b@1478 1988 for (var i=0; i<optCount; i++)
b@1478 1989 {
b@1478 1990 var div = document.createElement('div');
b@1478 1991 div.style.width = '80px';
b@1478 1992 div.style.float = 'left';
b@1478 1993 var input = document.createElement('input');
b@1478 1994 input.type = 'checkbox';
b@1478 1995 input.name = commentQuestion.id;
b@1478 1996 input.setAttribute('setvalue',commentQuestion.options[i].name);
b@1478 1997 input.className = 'comment-radio';
b@1478 1998 div.appendChild(input);
b@1478 1999 this.inputs.appendChild(div);
b@1478 2000
b@1478 2001
b@1478 2002 div = document.createElement('div');
b@1478 2003 div.style.width = '80px';
b@1478 2004 div.style.float = 'left';
b@1478 2005 div.align = 'center';
b@1478 2006 var span = document.createElement('span');
b@1478 2007 span.textContent = commentQuestion.options[i].text;
b@1478 2008 span.className = 'comment-radio-span';
b@1478 2009 div.appendChild(span);
b@1478 2010 this.span.appendChild(div);
b@1478 2011 this.options.push(input);
b@1478 2012 }
b@1478 2013 this.holder.appendChild(this.span);
b@1478 2014 this.holder.appendChild(this.inputs);
b@1478 2015
b@1478 2016 this.exportXMLDOM = function() {
b@1478 2017 var root = document.createElement('comment');
b@1478 2018 root.id = this.specification.id;
b@1478 2019 root.setAttribute('type',this.specification.type);
b@1478 2020 var question = document.createElement('question');
b@1478 2021 question.textContent = this.string.textContent;
b@1478 2022 root.appendChild(question);
b@1478 2023 console.log('Comment: '+question.textContent);
b@1478 2024 for (var i=0; i<this.options.length; i++) {
b@1478 2025 var response = document.createElement('response');
b@1478 2026 response.textContent = this.options[i].checked;
b@1478 2027 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
b@1478 2028 root.appendChild(response);
b@1478 2029 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
b@1478 2030 }
b@1478 2031 return root;
b@1478 2032 };
b@1478 2033 this.resize = function()
b@1478 2034 {
b@1478 2035 var boxwidth = (window.innerWidth-100)/2;
b@1478 2036 if (boxwidth >= 600)
b@1478 2037 {
b@1478 2038 boxwidth = 600;
b@1478 2039 }
b@1478 2040 else if (boxwidth < 400)
b@1478 2041 {
b@1478 2042 boxwidth = 400;
b@1478 2043 }
b@1478 2044 this.holder.style.width = boxwidth+"px";
b@1478 2045 var text = this.holder.children[2];
b@1478 2046 var options = this.holder.children[3];
b@1478 2047 var optCount = options.children.length;
b@1478 2048 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
b@1478 2049 var options = options.firstChild;
b@1478 2050 var text = text.firstChild;
b@1478 2051 options.style.marginRight = spanMargin;
b@1478 2052 options.style.marginLeft = spanMargin;
b@1478 2053 text.style.marginRight = spanMargin;
b@1478 2054 text.style.marginLeft = spanMargin;
b@1478 2055 while(options.nextSibling != undefined)
b@1478 2056 {
b@1478 2057 options = options.nextSibling;
b@1478 2058 text = text.nextSibling;
b@1478 2059 options.style.marginRight = spanMargin;
b@1478 2060 options.style.marginLeft = spanMargin;
b@1478 2061 text.style.marginRight = spanMargin;
b@1478 2062 text.style.marginLeft = spanMargin;
b@1478 2063 }
b@1478 2064 };
b@1478 2065 this.resize();
b@1478 2066 };
b@1478 2067
b@1478 2068 this.createCommentBox = function(audioObject) {
b@1478 2069 var node = new this.elementCommentBox(audioObject);
b@1478 2070 this.commentBoxes.push(node);
b@1478 2071 audioObject.commentDOM = node;
b@1478 2072 return node;
b@1478 2073 };
b@1478 2074
b@1478 2075 this.sortCommentBoxes = function() {
b@1478 2076 var holder = [];
b@1478 2077 while (this.commentBoxes.length > 0) {
b@1478 2078 var node = this.commentBoxes.pop(0);
b@1478 2079 holder[node.id] = node;
b@1478 2080 }
b@1478 2081 this.commentBoxes = holder;
b@1478 2082 };
b@1478 2083
b@1478 2084 this.showCommentBoxes = function(inject, sort) {
b@1478 2085 if (sort) {interfaceContext.sortCommentBoxes();}
b@1478 2086 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
b@1478 2087 inject.appendChild(this.commentBoxes[i].trackComment);
b@1478 2088 }
b@1478 2089 };
b@1478 2090
b@1478 2091 this.deleteCommentBoxes = function() {
b@1478 2092 this.commentBoxes = [];
b@1478 2093 };
b@1478 2094
b@1478 2095 this.createCommentQuestion = function(element) {
b@1478 2096 var node;
b@1478 2097 if (element.type == 'text') {
b@1478 2098 node = new this.commentBox(element);
b@1478 2099 } else if (element.type == 'radio') {
b@1478 2100 node = new this.radioBox(element);
b@1478 2101 } else if (element.type == 'checkbox') {
b@1478 2102 node = new this.checkboxBox(element);
b@1478 2103 }
b@1478 2104 this.commentQuestions.push(node);
b@1478 2105 return node;
b@1478 2106 };
b@1478 2107
b@1478 2108 this.deleteCommentQuestions = function()
b@1478 2109 {
b@1478 2110 this.commentQuestions = [];
b@1478 2111 };
b@1478 2112
b@1478 2113 this.playhead = new function()
b@1478 2114 {
b@1478 2115 this.object = document.createElement('div');
b@1478 2116 this.object.className = 'playhead';
b@1478 2117 this.object.align = 'left';
b@1478 2118 var curTime = document.createElement('div');
b@1478 2119 curTime.style.width = '50px';
b@1478 2120 this.curTimeSpan = document.createElement('span');
b@1478 2121 this.curTimeSpan.textContent = '00:00';
b@1478 2122 curTime.appendChild(this.curTimeSpan);
b@1478 2123 this.object.appendChild(curTime);
b@1478 2124 this.scrubberTrack = document.createElement('div');
b@1478 2125 this.scrubberTrack.className = 'playhead-scrub-track';
b@1478 2126
b@1478 2127 this.scrubberHead = document.createElement('div');
b@1478 2128 this.scrubberHead.id = 'playhead-scrubber';
b@1478 2129 this.scrubberTrack.appendChild(this.scrubberHead);
b@1478 2130 this.object.appendChild(this.scrubberTrack);
b@1478 2131
b@1478 2132 this.timePerPixel = 0;
b@1478 2133 this.maxTime = 0;
b@1478 2134
b@1478 2135 this.playbackObject;
b@1478 2136
b@1478 2137 this.setTimePerPixel = function(audioObject) {
b@1478 2138 //maxTime must be in seconds
b@1478 2139 this.playbackObject = audioObject;
b@1478 2140 this.maxTime = audioObject.buffer.duration;
b@1478 2141 var width = 490; //500 - 10, 5 each side of the tracker head
b@1478 2142 this.timePerPixel = this.maxTime/490;
b@1478 2143 if (this.maxTime < 60) {
b@1478 2144 this.curTimeSpan.textContent = '0.00';
b@1478 2145 } else {
b@1478 2146 this.curTimeSpan.textContent = '00:00';
b@1478 2147 }
b@1478 2148 };
b@1478 2149
b@1478 2150 this.update = function() {
b@1478 2151 // Update the playhead position, startPlay must be called
b@1478 2152 if (this.timePerPixel > 0) {
b@1478 2153 var time = this.playbackObject.getCurrentPosition();
b@1478 2154 if (time > 0) {
b@1478 2155 var width = 490;
b@1478 2156 var pix = Math.floor(time/this.timePerPixel);
b@1478 2157 this.scrubberHead.style.left = pix+'px';
b@1478 2158 if (this.maxTime > 60.0) {
b@1478 2159 var secs = time%60;
b@1478 2160 var mins = Math.floor((time-secs)/60);
b@1478 2161 secs = secs.toString();
b@1478 2162 secs = secs.substr(0,2);
b@1478 2163 mins = mins.toString();
b@1478 2164 this.curTimeSpan.textContent = mins+':'+secs;
b@1478 2165 } else {
b@1478 2166 time = time.toString();
b@1478 2167 this.curTimeSpan.textContent = time.substr(0,4);
b@1478 2168 }
b@1478 2169 } else {
b@1478 2170 this.scrubberHead.style.left = '0px';
b@1478 2171 if (this.maxTime < 60) {
b@1478 2172 this.curTimeSpan.textContent = '0.00';
b@1478 2173 } else {
b@1478 2174 this.curTimeSpan.textContent = '00:00';
b@1478 2175 }
b@1478 2176 }
b@1478 2177 }
b@1478 2178 };
b@1478 2179
b@1478 2180 this.interval = undefined;
b@1478 2181
b@1478 2182 this.start = function() {
b@1478 2183 if (this.playbackObject != undefined && this.interval == undefined) {
b@1478 2184 if (this.maxTime < 60) {
b@1478 2185 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
b@1478 2186 } else {
b@1478 2187 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
b@1478 2188 }
b@1478 2189 }
b@1478 2190 };
b@1478 2191 this.stop = function() {
b@1478 2192 clearInterval(this.interval);
b@1478 2193 this.interval = undefined;
b@1478 2194 if (this.maxTime < 60) {
b@1478 2195 this.curTimeSpan.textContent = '0.00';
b@1478 2196 } else {
b@1478 2197 this.curTimeSpan.textContent = '00:00';
b@1478 2198 }
b@1478 2199 };
b@1478 2200 };
b@1478 2201
b@1478 2202 // Global Checkers
b@1478 2203 // These functions will help enforce the checkers
b@1478 2204 this.checkHiddenAnchor = function()
b@1478 2205 {
b@1478 2206 var audioHolder = testState.currentStateMap[testState.currentIndex];
b@1478 2207 if (audioHolder.anchorId != null)
b@1478 2208 {
b@1478 2209 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
b@1478 2210 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
b@1478 2211 {
b@1478 2212 // Anchor is not set below
b@1478 2213 console.log('Anchor node not below marker value');
b@1478 2214 alert('Please keep listening');
b@1478 2215 return false;
b@1478 2216 }
b@1478 2217 }
b@1478 2218 return true;
b@1478 2219 };
b@1478 2220
b@1478 2221 this.checkHiddenReference = function()
b@1478 2222 {
b@1478 2223 var audioHolder = testState.currentStateMap[testState.currentIndex];
b@1478 2224 if (audioHolder.referenceId != null)
b@1478 2225 {
b@1478 2226 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
b@1478 2227 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
b@1478 2228 {
b@1478 2229 // Anchor is not set below
b@1478 2230 console.log('Reference node not above marker value');
b@1478 2231 alert('Please keep listening');
b@1478 2232 return false;
b@1478 2233 }
b@1478 2234 }
b@1478 2235 return true;
b@1478 2236 };
b@1478 2237 }