annotate core.js @ 1478:82f43919f385

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