annotate core.js @ 769:30adbf6a6d50

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