annotate core.js @ 1483:d99c580e2683

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