annotate core.js @ 1574:c4f9299295db

Added min/max range check to number box survey
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 08 Jun 2015 12:09:53 +0100
parents a41d8efee6db
children 4e9ab4f92f20
rev   line source
nickjillings@1571 1 /**
nickjillings@1571 2 * core.js
nickjillings@1571 3 *
nickjillings@1571 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1571 5 * Also contains all global variables.
nickjillings@1571 6 */
nickjillings@1571 7
nickjillings@1571 8 /* create the web audio API context and store in audioContext*/
nickjillings@1571 9 var audioContext; // Hold the browser web audio API
nickjillings@1571 10 var projectXML; // Hold the parsed setup XML
nickjillings@1571 11 var specification;
nickjillings@1571 12 var interfaceContext;
nickjillings@1571 13 var popup; // Hold the interfacePopup object
nickjillings@1571 14 var testState;
nickjillings@1571 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1571 16 var audioEngineContext; // The custome AudioEngine object
nickjillings@1571 17 var projectReturn; // Hold the URL for the return
nickjillings@1571 18
nickjillings@1571 19
nickjillings@1571 20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1571 21 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1571 22
nickjillings@1571 23 window.onload = function() {
nickjillings@1571 24 // Function called once the browser has loaded all files.
nickjillings@1571 25 // This should perform any initial commands such as structure / loading documents
nickjillings@1571 26
nickjillings@1571 27 // Create a web audio API context
nickjillings@1571 28 // Fixed for cross-browser support
nickjillings@1571 29 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1571 30 audioContext = new AudioContext;
nickjillings@1571 31
nickjillings@1571 32 // Create test state
nickjillings@1571 33 testState = new stateMachine();
nickjillings@1571 34
nickjillings@1571 35 // Create the audio engine object
nickjillings@1571 36 audioEngineContext = new AudioEngine();
nickjillings@1571 37
nickjillings@1571 38 // Create the popup interface object
nickjillings@1571 39 popup = new interfacePopup();
nickjillings@1571 40
nickjillings@1571 41 // Create the specification object
nickjillings@1571 42 specification = new Specification();
nickjillings@1571 43
nickjillings@1571 44 // Create the interface object
nickjillings@1571 45 interfaceContext = new Interface(specification);
nickjillings@1571 46 };
nickjillings@1571 47
nickjillings@1571 48 function interfacePopup() {
nickjillings@1571 49 // Creates an object to manage the popup
nickjillings@1571 50 this.popup = null;
nickjillings@1571 51 this.popupContent = null;
nickjillings@1574 52 this.buttonProceed = null;
nickjillings@1571 53 this.popupOptions = null;
nickjillings@1571 54 this.currentIndex = null;
nickjillings@1571 55 this.responses = null;
nickjillings@1571 56
nickjillings@1571 57 this.createPopup = function(){
nickjillings@1571 58 // Create popup window interface
nickjillings@1571 59 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1571 60 var blank = document.createElement('div');
nickjillings@1571 61 blank.className = 'testHalt';
nickjillings@1571 62
nickjillings@1571 63 this.popup = document.createElement('div');
nickjillings@1571 64 this.popup.id = 'popupHolder';
nickjillings@1571 65 this.popup.className = 'popupHolder';
nickjillings@1571 66 this.popup.style.position = 'absolute';
nickjillings@1571 67 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1571 68 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1571 69
nickjillings@1571 70 this.popupContent = document.createElement('div');
nickjillings@1571 71 this.popupContent.id = 'popupContent';
nickjillings@1571 72 this.popupContent.style.marginTop = '25px';
nickjillings@1571 73 this.popupContent.align = 'center';
nickjillings@1571 74 this.popup.appendChild(this.popupContent);
nickjillings@1571 75
nickjillings@1574 76 this.buttonProceed = document.createElement('button');
nickjillings@1574 77 this.buttonProceed.className = 'popupButton';
nickjillings@1574 78 this.buttonProceed.innerHTML = 'Next';
nickjillings@1574 79 this.buttonProceed.onclick = function(){popup.proceedClicked();};
nickjillings@1571 80 this.popup.style.zIndex = -1;
nickjillings@1571 81 this.popup.style.visibility = 'hidden';
nickjillings@1571 82 blank.style.zIndex = -2;
nickjillings@1571 83 blank.style.visibility = 'hidden';
nickjillings@1571 84 insertPoint.appendChild(this.popup);
nickjillings@1571 85 insertPoint.appendChild(blank);
nickjillings@1571 86 };
nickjillings@1571 87
nickjillings@1571 88 this.showPopup = function(){
nickjillings@1571 89 if (this.popup == null) {
nickjillings@1571 90 this.createPopup();
nickjillings@1571 91 }
nickjillings@1571 92 this.popup.style.zIndex = 3;
nickjillings@1571 93 this.popup.style.visibility = 'visible';
nickjillings@1571 94 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1571 95 blank.style.zIndex = 2;
nickjillings@1571 96 blank.style.visibility = 'visible';
nickjillings@1571 97 };
nickjillings@1571 98
nickjillings@1571 99 this.hidePopup = function(){
nickjillings@1571 100 this.popup.style.zIndex = -1;
nickjillings@1571 101 this.popup.style.visibility = 'hidden';
nickjillings@1571 102 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1571 103 blank.style.zIndex = -2;
nickjillings@1571 104 blank.style.visibility = 'hidden';
nickjillings@1571 105 };
nickjillings@1571 106
nickjillings@1571 107 this.postNode = function() {
nickjillings@1571 108 // This will take the node from the popupOptions and display it
nickjillings@1571 109 var node = this.popupOptions[this.currentIndex];
nickjillings@1571 110 this.popupContent.innerHTML = null;
nickjillings@1571 111 if (node.type == 'statement') {
nickjillings@1571 112 var span = document.createElement('span');
nickjillings@1571 113 span.textContent = node.statement;
nickjillings@1571 114 this.popupContent.appendChild(span);
nickjillings@1571 115 } else if (node.type == 'question') {
nickjillings@1571 116 var span = document.createElement('span');
nickjillings@1571 117 span.textContent = node.question;
nickjillings@1571 118 var textArea = document.createElement('textarea');
nickjillings@1571 119 switch (node.boxsize) {
nickjillings@1571 120 case 'small':
nickjillings@1571 121 textArea.cols = "20";
nickjillings@1571 122 textArea.rows = "1";
nickjillings@1571 123 break;
nickjillings@1571 124 case 'normal':
nickjillings@1571 125 textArea.cols = "30";
nickjillings@1571 126 textArea.rows = "2";
nickjillings@1571 127 break;
nickjillings@1571 128 case 'large':
nickjillings@1571 129 textArea.cols = "40";
nickjillings@1571 130 textArea.rows = "5";
nickjillings@1571 131 break;
nickjillings@1571 132 case 'huge':
nickjillings@1571 133 textArea.cols = "50";
nickjillings@1571 134 textArea.rows = "10";
nickjillings@1571 135 break;
nickjillings@1571 136 }
nickjillings@1571 137 var br = document.createElement('br');
nickjillings@1571 138 this.popupContent.appendChild(span);
nickjillings@1571 139 this.popupContent.appendChild(br);
nickjillings@1571 140 this.popupContent.appendChild(textArea);
nickjillings@1571 141 this.popupContent.childNodes[2].focus();
nickjillings@1571 142 } else if (node.type == 'checkbox') {
nickjillings@1571 143 var span = document.createElement('span');
nickjillings@1571 144 span.textContent = node.statement;
nickjillings@1571 145 this.popupContent.appendChild(span);
nickjillings@1571 146 var optHold = document.createElement('div');
nickjillings@1571 147 optHold.id = 'option-holder';
nickjillings@1571 148 optHold.align = 'left';
nickjillings@1571 149 for (var i=0; i<node.options.length; i++) {
nickjillings@1571 150 var option = node.options[i];
nickjillings@1571 151 var input = document.createElement('input');
nickjillings@1571 152 input.id = option.id;
nickjillings@1571 153 input.type = 'checkbox';
nickjillings@1571 154 var span = document.createElement('span');
nickjillings@1571 155 span.textContent = option.text;
nickjillings@1571 156 var hold = document.createElement('div');
nickjillings@1571 157 hold.setAttribute('name','option');
nickjillings@1571 158 hold.style.float = 'left';
nickjillings@1571 159 hold.style.padding = '4px';
nickjillings@1571 160 hold.appendChild(input);
nickjillings@1571 161 hold.appendChild(span);
nickjillings@1571 162 optHold.appendChild(hold);
nickjillings@1571 163 }
nickjillings@1571 164 this.popupContent.appendChild(optHold);
nickjillings@1571 165 } else if (node.type == 'radio') {
nickjillings@1571 166 var span = document.createElement('span');
nickjillings@1571 167 span.textContent = node.statement;
nickjillings@1571 168 this.popupContent.appendChild(span);
nickjillings@1571 169 var optHold = document.createElement('div');
nickjillings@1571 170 optHold.id = 'option-holder';
nickjillings@1571 171 optHold.align = 'none';
nickjillings@1571 172 optHold.style.float = 'left';
nickjillings@1571 173 optHold.style.width = "100%";
nickjillings@1571 174 for (var i=0; i<node.options.length; i++) {
nickjillings@1571 175 var option = node.options[i];
nickjillings@1571 176 var input = document.createElement('input');
nickjillings@1571 177 input.id = option.name;
nickjillings@1571 178 input.type = 'radio';
nickjillings@1571 179 input.name = node.id;
nickjillings@1571 180 var span = document.createElement('span');
nickjillings@1571 181 span.textContent = option.text;
nickjillings@1571 182 var hold = document.createElement('div');
nickjillings@1571 183 hold.setAttribute('name','option');
nickjillings@1571 184 hold.style.padding = '4px';
nickjillings@1571 185 hold.appendChild(input);
nickjillings@1571 186 hold.appendChild(span);
nickjillings@1571 187 optHold.appendChild(hold);
nickjillings@1571 188 }
nickjillings@1571 189 this.popupContent.appendChild(optHold);
nickjillings@1573 190 } else if (node.type == 'number') {
nickjillings@1573 191 var span = document.createElement('span');
nickjillings@1573 192 span.textContent = node.statement;
nickjillings@1573 193 this.popupContent.appendChild(span);
nickjillings@1573 194 this.popupContent.appendChild(document.createElement('br'));
nickjillings@1573 195 var input = document.createElement('input');
nickjillings@1573 196 input.type = 'number';
nickjillings@1573 197 if (node.min != null) {input.min = node.min;}
nickjillings@1573 198 if (node.max != null) {input.max = node.max;}
nickjillings@1573 199 if (node.step != null) {input.step = node.step;}
nickjillings@1573 200 this.popupContent.appendChild(input);
nickjillings@1571 201 }
nickjillings@1574 202 this.popupContent.appendChild(this.buttonProceed);
nickjillings@1571 203 };
nickjillings@1571 204
nickjillings@1571 205 this.initState = function(node) {
nickjillings@1571 206 //Call this with your preTest and postTest nodes when needed to
nickjillings@1571 207 // initialise the popup procedure.
nickjillings@1571 208 this.popupOptions = node.options;
nickjillings@1571 209 if (this.popupOptions.length > 0) {
nickjillings@1571 210 if (node.type == 'pretest') {
nickjillings@1571 211 this.responses = document.createElement('PreTest');
nickjillings@1571 212 } else if (node.type == 'posttest') {
nickjillings@1571 213 this.responses = document.createElement('PostTest');
nickjillings@1571 214 } else {
nickjillings@1571 215 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1571 216 this.responses = document.createElement('responses');
nickjillings@1571 217 }
nickjillings@1571 218 this.currentIndex = 0;
nickjillings@1571 219 this.showPopup();
nickjillings@1571 220 this.postNode();
nickjillings@1571 221 } else {
nickjillings@1571 222 advanceState();
nickjillings@1571 223 }
nickjillings@1571 224 };
nickjillings@1571 225
nickjillings@1574 226 this.proceedClicked = function() {
nickjillings@1571 227 // Each time the popup button is clicked!
nickjillings@1571 228 var node = this.popupOptions[this.currentIndex];
nickjillings@1571 229 if (node.type == 'question') {
nickjillings@1571 230 // Must extract the question data
nickjillings@1571 231 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1571 232 if (node.mandatory == true && textArea.value.length == 0) {
nickjillings@1571 233 alert('This question is mandatory');
nickjillings@1571 234 return;
nickjillings@1571 235 } else {
nickjillings@1571 236 // Save the text content
nickjillings@1571 237 var hold = document.createElement('comment');
nickjillings@1571 238 hold.id = node.id;
nickjillings@1571 239 hold.innerHTML = textArea.value;
nickjillings@1572 240 console.log("Question: "+ node.question);
nickjillings@1571 241 console.log("Question Response: "+ textArea.value);
nickjillings@1571 242 this.responses.appendChild(hold);
nickjillings@1571 243 }
nickjillings@1571 244 } else if (node.type == 'checkbox') {
nickjillings@1571 245 // Must extract checkbox data
nickjillings@1571 246 var optHold = document.getElementById('option-holder');
nickjillings@1571 247 var hold = document.createElement('checkbox');
nickjillings@1571 248 console.log("Checkbox: "+ node.statement);
nickjillings@1571 249 hold.id = node.id;
nickjillings@1571 250 for (var i=0; i<optHold.childElementCount; i++) {
nickjillings@1571 251 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1571 252 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
nickjillings@1571 253 var response = document.createElement('option');
nickjillings@1572 254 response.setAttribute('name',input.id);
nickjillings@1572 255 response.textContent = input.checked;
nickjillings@1571 256 hold.appendChild(response);
nickjillings@1571 257 console.log(input.id +': '+ input.checked);
nickjillings@1571 258 }
nickjillings@1571 259 this.responses.appendChild(hold);
nickjillings@1571 260 } else if (node.type == "radio") {
nickjillings@1571 261 var optHold = document.getElementById('option-holder');
nickjillings@1571 262 var hold = document.createElement('radio');
nickjillings@1571 263 var responseID = null;
nickjillings@1571 264 var i=0;
nickjillings@1571 265 while(responseID == null) {
nickjillings@1571 266 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1571 267 if (input.checked == true) {
nickjillings@1571 268 responseID = i;
nickjillings@1571 269 }
nickjillings@1571 270 i++;
nickjillings@1571 271 }
nickjillings@1571 272 hold.id = node.id;
nickjillings@1571 273 hold.setAttribute('name',node.options[responseID].name);
nickjillings@1571 274 hold.textContent = node.options[responseID].text;
nickjillings@1571 275 this.responses.appendChild(hold);
nickjillings@1573 276 } else if (node.type == "number") {
nickjillings@1573 277 var input = this.popupContent.getElementsByTagName('input')[0];
nickjillings@1573 278 if (node.mandatory == true && input.value.length == 0) {
nickjillings@1574 279 alert('This question is mandatory. Please enter a number');
nickjillings@1574 280 return;
nickjillings@1574 281 }
nickjillings@1574 282 var enteredNumber = Number(input.value);
nickjillings@1574 283 if (enteredNumber == undefined) {
nickjillings@1574 284 alert('Please enter a valid number');
nickjillings@1574 285 return;
nickjillings@1574 286 }
nickjillings@1574 287 if (enteredNumber < node.min && node.min != null) {
nickjillings@1574 288 alert('Number is below the minimum value of '+node.min);
nickjillings@1574 289 return;
nickjillings@1574 290 }
nickjillings@1574 291 if (enteredNumber > node.max && node.max != null) {
nickjillings@1574 292 alert('Number is above the maximum value of '+node.max);
nickjillings@1573 293 return;
nickjillings@1573 294 }
nickjillings@1573 295 var hold = document.createElement('number');
nickjillings@1573 296 hold.id = node.id;
nickjillings@1573 297 hold.textContent = input.value;
nickjillings@1573 298 this.responses.appendChild(hold);
nickjillings@1571 299 }
nickjillings@1571 300 this.currentIndex++;
nickjillings@1571 301 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1574 302 if(this.currentIndex+1 == this.popupOptions.length) {
nickjillings@1574 303 this.buttonProceed.textContent = 'Submit';
nickjillings@1574 304 }
nickjillings@1571 305 this.postNode();
nickjillings@1571 306 } else {
nickjillings@1571 307 // Reached the end of the popupOptions
nickjillings@1571 308 this.hidePopup();
nickjillings@1571 309 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1571 310 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1571 311 } else {
nickjillings@1571 312 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1571 313 }
nickjillings@1571 314 advanceState();
nickjillings@1571 315 }
nickjillings@1571 316 };
nickjillings@1571 317 }
nickjillings@1571 318
nickjillings@1571 319 function advanceState()
nickjillings@1571 320 {
nickjillings@1571 321 // Just for complete clarity
nickjillings@1571 322 testState.advanceState();
nickjillings@1571 323 }
nickjillings@1571 324
nickjillings@1571 325 function stateMachine()
nickjillings@1571 326 {
nickjillings@1571 327 // Object prototype for tracking and managing the test state
nickjillings@1571 328 this.stateMap = [];
nickjillings@1571 329 this.stateIndex = null;
nickjillings@1571 330 this.currentStateMap = [];
nickjillings@1571 331 this.currentIndex = null;
nickjillings@1571 332 this.currentTestId = 0;
nickjillings@1571 333 this.stateResults = [];
nickjillings@1571 334 this.timerCallBackHolders = null;
nickjillings@1571 335 this.initialise = function(){
nickjillings@1571 336 if (this.stateMap.length > 0) {
nickjillings@1571 337 if(this.stateIndex != null) {
nickjillings@1571 338 console.log('NOTE - State already initialise');
nickjillings@1571 339 }
nickjillings@1571 340 this.stateIndex = -1;
nickjillings@1571 341 var that = this;
nickjillings@1571 342 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1571 343 var name = this.stateMap[id].type;
nickjillings@1571 344 var obj = document.createElement(name);
nickjillings@1571 345 if (name == 'audioHolder') {
nickjillings@1571 346 obj.id = this.stateMap[id].id;
nickjillings@1571 347 }
nickjillings@1571 348 this.stateResults.push(obj);
nickjillings@1571 349 }
nickjillings@1571 350 } else {
nickjillings@1571 351 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1571 352 }
nickjillings@1571 353 };
nickjillings@1571 354 this.advanceState = function(){
nickjillings@1571 355 if (this.stateIndex == null) {
nickjillings@1571 356 this.initialise();
nickjillings@1571 357 }
nickjillings@1571 358 if (this.stateIndex == -1) {
nickjillings@1571 359 console.log('Starting test...');
nickjillings@1571 360 }
nickjillings@1571 361 if (this.currentIndex == null){
nickjillings@1571 362 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1571 363 // Save current page
nickjillings@1571 364 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nickjillings@1571 365 this.currentTestId++;
nickjillings@1571 366 }
nickjillings@1571 367 this.stateIndex++;
nickjillings@1571 368 if (this.stateIndex >= this.stateMap.length) {
nickjillings@1571 369 console.log('Test Completed');
nickjillings@1571 370 createProjectSave(specification.projectReturn);
nickjillings@1571 371 } else {
nickjillings@1571 372 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1571 373 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1571 374 console.log('Loading test page');
nickjillings@1571 375 loadTest(this.currentStateMap);
nickjillings@1571 376 this.initialiseInnerState(this.currentStateMap);
nickjillings@1571 377 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
nickjillings@1571 378 if (this.currentStateMap.options.length >= 1) {
nickjillings@1571 379 popup.initState(this.currentStateMap);
nickjillings@1571 380 } else {
nickjillings@1571 381 this.advanceState();
nickjillings@1571 382 }
nickjillings@1571 383 } else {
nickjillings@1571 384 this.advanceState();
nickjillings@1571 385 }
nickjillings@1571 386 }
nickjillings@1571 387 } else {
nickjillings@1571 388 this.advanceInnerState();
nickjillings@1571 389 }
nickjillings@1571 390 };
nickjillings@1571 391
nickjillings@1571 392 this.testPageCompleted = function(store, testXML, testId) {
nickjillings@1571 393 // Function called each time a test page has been completed
nickjillings@1571 394 // Can be used to over-rule default behaviour
nickjillings@1571 395
nickjillings@1571 396 pageXMLSave(store, testXML);
nickjillings@1571 397 };
nickjillings@1571 398
nickjillings@1571 399 this.initialiseInnerState = function(node) {
nickjillings@1571 400 // Parses the received testXML for pre and post test options
nickjillings@1571 401 this.currentStateMap = [];
nickjillings@1571 402 var preTest = node.preTest;
nickjillings@1571 403 var postTest = node.postTest;
nickjillings@1571 404 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1571 405 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1571 406 this.currentStateMap.push(preTest);
nickjillings@1571 407 this.currentStateMap.push(node);
nickjillings@1571 408 this.currentStateMap.push(postTest);
nickjillings@1571 409 this.currentIndex = -1;
nickjillings@1571 410 this.advanceInnerState();
nickjillings@1571 411 };
nickjillings@1571 412
nickjillings@1571 413 this.advanceInnerState = function() {
nickjillings@1571 414 this.currentIndex++;
nickjillings@1571 415 if (this.currentIndex >= this.currentStateMap.length) {
nickjillings@1571 416 this.currentIndex = null;
nickjillings@1571 417 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1571 418 this.advanceState();
nickjillings@1571 419 } else {
nickjillings@1571 420 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
nickjillings@1571 421 console.log("Loading test page"+this.currentTestId);
nickjillings@1571 422 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
nickjillings@1571 423 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1571 424 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
nickjillings@1571 425 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1571 426 } else {
nickjillings@1571 427 this.advanceInnerState();
nickjillings@1571 428 }
nickjillings@1571 429 }
nickjillings@1571 430 };
nickjillings@1571 431
nickjillings@1571 432 this.previousState = function(){};
nickjillings@1571 433 }
nickjillings@1571 434
nickjillings@1571 435 function testEnded(testId)
nickjillings@1571 436 {
nickjillings@1571 437 pageXMLSave(testId);
nickjillings@1571 438 if (testXMLSetups.length-1 > testId)
nickjillings@1571 439 {
nickjillings@1571 440 // Yes we have another test to perform
nickjillings@1571 441 testId = (Number(testId)+1);
nickjillings@1571 442 currentState = 'testRun-'+testId;
nickjillings@1571 443 loadTest(testId);
nickjillings@1571 444 } else {
nickjillings@1571 445 console.log('Testing Completed!');
nickjillings@1571 446 currentState = 'postTest';
nickjillings@1571 447 // Check for any post tests
nickjillings@1571 448 var xmlSetup = projectXML.find('setup');
nickjillings@1571 449 var postTest = xmlSetup.find('PostTest')[0];
nickjillings@1571 450 popup.initState(postTest);
nickjillings@1571 451 }
nickjillings@1571 452 }
nickjillings@1571 453
nickjillings@1571 454 function loadProjectSpec(url) {
nickjillings@1571 455 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1571 456 // If url is null, request client to upload project XML document
nickjillings@1571 457 var r = new XMLHttpRequest();
nickjillings@1571 458 r.open('GET',url,true);
nickjillings@1571 459 r.onload = function() {
nickjillings@1571 460 loadProjectSpecCallback(r.response);
nickjillings@1571 461 };
nickjillings@1571 462 r.send();
nickjillings@1571 463 };
nickjillings@1571 464
nickjillings@1571 465 function loadProjectSpecCallback(response) {
nickjillings@1571 466 // Function called after asynchronous download of XML project specification
nickjillings@1571 467 //var decode = $.parseXML(response);
nickjillings@1571 468 //projectXML = $(decode);
nickjillings@1571 469
nickjillings@1571 470 var parse = new DOMParser();
nickjillings@1571 471 projectXML = parse.parseFromString(response,'text/xml');
nickjillings@1571 472
nickjillings@1571 473 // Build the specification
nickjillings@1571 474 specification.decode();
nickjillings@1571 475
nickjillings@1571 476 testState.stateMap.push(specification.preTest);
nickjillings@1571 477
nickjillings@1571 478 // New check if we need to randomise the test order
nickjillings@1571 479 if (specification.randomiseOrder)
nickjillings@1571 480 {
nickjillings@1571 481 specification.audioHolders = randomiseOrder(specification.audioHolders);
nickjillings@1571 482 }
nickjillings@1571 483
nickjillings@1571 484 $(specification.audioHolders).each(function(index,elem){
nickjillings@1571 485 testState.stateMap.push(elem);
nickjillings@1571 486 });
nickjillings@1571 487
nickjillings@1571 488 testState.stateMap.push(specification.postTest);
nickjillings@1571 489
nickjillings@1571 490 // Obtain the metrics enabled
nickjillings@1571 491 $(specification.metrics).each(function(index,node){
nickjillings@1571 492 var enabled = node.textContent;
nickjillings@1571 493 switch(node.enabled)
nickjillings@1571 494 {
nickjillings@1571 495 case 'testTimer':
nickjillings@1571 496 sessionMetrics.prototype.enableTestTimer = true;
nickjillings@1571 497 break;
nickjillings@1571 498 case 'elementTimer':
nickjillings@1571 499 sessionMetrics.prototype.enableElementTimer = true;
nickjillings@1571 500 break;
nickjillings@1571 501 case 'elementTracker':
nickjillings@1571 502 sessionMetrics.prototype.enableElementTracker = true;
nickjillings@1571 503 break;
nickjillings@1571 504 case 'elementListenTracker':
nickjillings@1571 505 sessionMetrics.prototype.enableElementListenTracker = true;
nickjillings@1571 506 break;
nickjillings@1571 507 case 'elementInitialPosition':
nickjillings@1571 508 sessionMetrics.prototype.enableElementInitialPosition = true;
nickjillings@1571 509 break;
nickjillings@1571 510 case 'elementFlagListenedTo':
nickjillings@1571 511 sessionMetrics.prototype.enableFlagListenedTo = true;
nickjillings@1571 512 break;
nickjillings@1571 513 case 'elementFlagMoved':
nickjillings@1571 514 sessionMetrics.prototype.enableFlagMoved = true;
nickjillings@1571 515 break;
nickjillings@1571 516 case 'elementFlagComments':
nickjillings@1571 517 sessionMetrics.prototype.enableFlagComments = true;
nickjillings@1571 518 break;
nickjillings@1571 519 }
nickjillings@1571 520 });
nickjillings@1571 521
nickjillings@1571 522
nickjillings@1571 523
nickjillings@1571 524 // Detect the interface to use and load the relevant javascripts.
nickjillings@1571 525 var interfaceJS = document.createElement('script');
nickjillings@1571 526 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1571 527 if (specification.interfaceType == 'APE') {
nickjillings@1571 528 interfaceJS.setAttribute("src","ape.js");
nickjillings@1571 529
nickjillings@1571 530 // APE comes with a css file
nickjillings@1571 531 var css = document.createElement('link');
nickjillings@1571 532 css.rel = 'stylesheet';
nickjillings@1571 533 css.type = 'text/css';
nickjillings@1571 534 css.href = 'ape.css';
nickjillings@1571 535
nickjillings@1571 536 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1571 537 }
nickjillings@1571 538 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1571 539
nickjillings@1571 540 // Define window callbacks for interface
nickjillings@1571 541 window.onresize = function(event){resizeWindow(event);};
nickjillings@1571 542 }
nickjillings@1571 543
nickjillings@1571 544 function createProjectSave(destURL) {
nickjillings@1571 545 // Save the data from interface into XML and send to destURL
nickjillings@1571 546 // If destURL is null then download XML in client
nickjillings@1571 547 // Now time to render file locally
nickjillings@1571 548 var xmlDoc = interfaceXMLSave();
nickjillings@1571 549 var parent = document.createElement("div");
nickjillings@1571 550 parent.appendChild(xmlDoc);
nickjillings@1571 551 var file = [parent.innerHTML];
nickjillings@1571 552 if (destURL == "null" || destURL == undefined) {
nickjillings@1571 553 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1571 554 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1571 555 var a = document.createElement("a");
nickjillings@1571 556 a.hidden = '';
nickjillings@1571 557 a.href = dnlk;
nickjillings@1571 558 a.download = "save.xml";
nickjillings@1571 559 a.textContent = "Save File";
nickjillings@1571 560
nickjillings@1571 561 popup.showPopup();
nickjillings@1571 562 popup.popupContent.innerHTML = null;
nickjillings@1571 563 popup.popupContent.appendChild(a);
nickjillings@1571 564 } else {
nickjillings@1571 565 var xmlhttp = new XMLHttpRequest;
nickjillings@1571 566 xmlhttp.open("POST",destURL,true);
nickjillings@1571 567 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1571 568 xmlhttp.onerror = function(){
nickjillings@1571 569 console.log('Error saving file to server! Presenting download locally');
nickjillings@1571 570 createProjectSave(null);
nickjillings@1571 571 };
nickjillings@1571 572 xmlhttp.onreadystatechange = function() {
nickjillings@1571 573 console.log(xmlhttp.status);
nickjillings@1571 574 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
nickjillings@1571 575 createProjectSave(null);
nickjillings@1571 576 }
nickjillings@1571 577 };
nickjillings@1571 578 xmlhttp.send(file);
nickjillings@1571 579 }
nickjillings@1571 580 }
nickjillings@1571 581
nickjillings@1571 582 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1571 583 function interfaceXMLSave(){
nickjillings@1571 584 // Create the XML string to be exported with results
nickjillings@1571 585 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1571 586 xmlDoc.appendChild(returnDateNode());
nickjillings@1571 587 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1571 588 {
nickjillings@1571 589 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1571 590 }
nickjillings@1571 591
nickjillings@1571 592 return xmlDoc;
nickjillings@1571 593 }
nickjillings@1571 594
nickjillings@1571 595 function AudioEngine() {
nickjillings@1571 596
nickjillings@1571 597 // Create two output paths, the main outputGain and fooGain.
nickjillings@1571 598 // Output gain is default to 1 and any items for playback route here
nickjillings@1571 599 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1571 600 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1571 601 this.outputGain = audioContext.createGain();
nickjillings@1571 602 this.fooGain = audioContext.createGain();
nickjillings@1571 603 this.fooGain.gain = 0;
nickjillings@1571 604
nickjillings@1571 605 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1571 606 this.status = 0;
nickjillings@1571 607 this.audioObjectsReady = false;
nickjillings@1571 608
nickjillings@1571 609 // Connect both gains to output
nickjillings@1571 610 this.outputGain.connect(audioContext.destination);
nickjillings@1571 611 this.fooGain.connect(audioContext.destination);
nickjillings@1571 612
nickjillings@1571 613 // Create the timer Object
nickjillings@1571 614 this.timer = new timer();
nickjillings@1571 615 // Create session metrics
nickjillings@1571 616 this.metric = new sessionMetrics(this);
nickjillings@1571 617
nickjillings@1571 618 this.loopPlayback = false;
nickjillings@1571 619
nickjillings@1571 620 // Create store for new audioObjects
nickjillings@1571 621 this.audioObjects = [];
nickjillings@1571 622
nickjillings@1571 623 this.play = function() {
nickjillings@1571 624 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1571 625 if (this.status == 0) {
nickjillings@1571 626 // Check if all audioObjects are ready
nickjillings@1571 627 if (this.audioObjectsReady == false) {
nickjillings@1571 628 this.audioObjectsReady = this.checkAllReady();
nickjillings@1571 629 }
nickjillings@1571 630 if (this.audioObjectsReady == true) {
nickjillings@1571 631 this.timer.startTest();
nickjillings@1571 632 if (this.loopPlayback) {
nickjillings@1571 633 for(var i=0; i<this.audioObjects.length; i++) {
nickjillings@1571 634 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1571 635 }
nickjillings@1571 636 }
nickjillings@1571 637 this.status = 1;
nickjillings@1571 638 }
nickjillings@1571 639 }
nickjillings@1571 640 };
nickjillings@1571 641
nickjillings@1571 642 this.stop = function() {
nickjillings@1571 643 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1571 644 if (this.status == 1) {
nickjillings@1571 645 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1571 646 {
nickjillings@1571 647 this.audioObjects[i].stop();
nickjillings@1571 648 }
nickjillings@1571 649 this.status = 0;
nickjillings@1571 650 }
nickjillings@1571 651 };
nickjillings@1571 652
nickjillings@1571 653
nickjillings@1571 654 this.newTrack = function(element) {
nickjillings@1571 655 // Pull data from given URL into new audio buffer
nickjillings@1571 656 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1571 657
nickjillings@1571 658 // Create the audioObject with ID of the new track length;
nickjillings@1571 659 audioObjectId = this.audioObjects.length;
nickjillings@1571 660 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1571 661
nickjillings@1571 662 // AudioObject will get track itself.
nickjillings@1571 663 this.audioObjects[audioObjectId].specification = element;
nickjillings@1571 664 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
nickjillings@1571 665 return this.audioObjects[audioObjectId];
nickjillings@1571 666 };
nickjillings@1571 667
nickjillings@1571 668 this.newTestPage = function() {
nickjillings@1571 669 this.state = 0;
nickjillings@1571 670 this.audioObjectsReady = false;
nickjillings@1571 671 this.metric.reset();
nickjillings@1571 672 this.audioObjects = [];
nickjillings@1571 673 };
nickjillings@1571 674
nickjillings@1571 675 this.checkAllPlayed = function() {
nickjillings@1571 676 arr = [];
nickjillings@1571 677 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1571 678 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1571 679 arr.push(this.audioObjects[id].id);
nickjillings@1571 680 }
nickjillings@1571 681 }
nickjillings@1571 682 return arr;
nickjillings@1571 683 };
nickjillings@1571 684
nickjillings@1571 685 this.checkAllReady = function() {
nickjillings@1571 686 var ready = true;
nickjillings@1571 687 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1571 688 if (this.audioObjects[i].state == 0) {
nickjillings@1571 689 // Track not ready
nickjillings@1571 690 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1571 691 ready = false;
nickjillings@1571 692 };
nickjillings@1571 693 }
nickjillings@1571 694 return ready;
nickjillings@1571 695 };
nickjillings@1571 696
nickjillings@1571 697 }
nickjillings@1571 698
nickjillings@1571 699 function audioObject(id) {
nickjillings@1571 700 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1571 701
nickjillings@1571 702 this.specification;
nickjillings@1571 703 this.id = id;
nickjillings@1571 704 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1571 705 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1571 706 this.metric = new metricTracker(this);
nickjillings@1571 707
nickjillings@1571 708 // Bindings for GUI
nickjillings@1571 709 this.interfaceDOM = null;
nickjillings@1571 710 this.commentDOM = null;
nickjillings@1571 711
nickjillings@1571 712 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1571 713 this.bufferNode = undefined;
nickjillings@1571 714 this.outputGain = audioContext.createGain();
nickjillings@1571 715
nickjillings@1571 716 // Default output gain to be zero
nickjillings@1571 717 this.outputGain.gain.value = 0.0;
nickjillings@1571 718
nickjillings@1571 719 // Connect buffer to the audio graph
nickjillings@1571 720 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1571 721
nickjillings@1571 722 // the audiobuffer is not designed for multi-start playback
nickjillings@1571 723 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1571 724 this.buffer;
nickjillings@1571 725
nickjillings@1571 726 this.loopStart = function() {
nickjillings@1571 727 this.outputGain.gain.value = 1.0;
nickjillings@1571 728 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1571 729 };
nickjillings@1571 730
nickjillings@1571 731 this.loopStop = function() {
nickjillings@1571 732 if (this.outputGain.gain.value != 0.0) {
nickjillings@1571 733 this.outputGain.gain.value = 0.0;
nickjillings@1571 734 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1571 735 }
nickjillings@1571 736 };
nickjillings@1571 737
nickjillings@1571 738 this.play = function(startTime) {
nickjillings@1571 739 this.bufferNode = audioContext.createBufferSource();
nickjillings@1571 740 this.bufferNode.owner = this;
nickjillings@1571 741 this.bufferNode.connect(this.outputGain);
nickjillings@1571 742 this.bufferNode.buffer = this.buffer;
nickjillings@1571 743 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1571 744 this.bufferNode.onended = function() {
nickjillings@1571 745 // Safari does not like using 'this' to reference the calling object!
nickjillings@1571 746 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1571 747 };
nickjillings@1571 748 if (this.bufferNode.loop == false) {
nickjillings@1571 749 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1571 750 }
nickjillings@1571 751 this.bufferNode.start(startTime);
nickjillings@1571 752 };
nickjillings@1571 753
nickjillings@1571 754 this.stop = function() {
nickjillings@1571 755 if (this.bufferNode != undefined)
nickjillings@1571 756 {
nickjillings@1571 757 this.bufferNode.stop(0);
nickjillings@1571 758 this.bufferNode = undefined;
nickjillings@1571 759 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1571 760 }
nickjillings@1571 761 };
nickjillings@1571 762
nickjillings@1571 763 this.getCurrentPosition = function() {
nickjillings@1571 764 var time = audioEngineContext.timer.getTestTime();
nickjillings@1571 765 if (this.bufferNode != undefined) {
nickjillings@1571 766 if (this.bufferNode.loop == true) {
nickjillings@1571 767 if (audioEngineContext.status == 1) {
nickjillings@1571 768 return time%this.buffer.duration;
nickjillings@1571 769 } else {
nickjillings@1571 770 return 0;
nickjillings@1571 771 }
nickjillings@1571 772 } else {
nickjillings@1571 773 if (this.metric.listenHold) {
nickjillings@1571 774 return time - this.metric.listenStart;
nickjillings@1571 775 } else {
nickjillings@1571 776 return 0;
nickjillings@1571 777 }
nickjillings@1571 778 }
nickjillings@1571 779 } else {
nickjillings@1571 780 return 0;
nickjillings@1571 781 }
nickjillings@1571 782 };
nickjillings@1571 783
nickjillings@1571 784 this.constructTrack = function(url) {
nickjillings@1571 785 var request = new XMLHttpRequest();
nickjillings@1571 786 this.url = url;
nickjillings@1571 787 request.open('GET',url,true);
nickjillings@1571 788 request.responseType = 'arraybuffer';
nickjillings@1571 789
nickjillings@1571 790 var audioObj = this;
nickjillings@1571 791
nickjillings@1571 792 // Create callback to decode the data asynchronously
nickjillings@1571 793 request.onloadend = function() {
nickjillings@1571 794 audioContext.decodeAudioData(request.response, function(decodedData) {
nickjillings@1571 795 audioObj.buffer = decodedData;
nickjillings@1571 796 audioObj.state = 1;
nickjillings@1571 797 }, function(){
nickjillings@1571 798 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1571 799 // Check here if the error is genuine
nickjillings@1571 800 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nickjillings@1571 801 // Genuine error
nickjillings@1571 802 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1571 803 }
nickjillings@1571 804 });
nickjillings@1571 805 };
nickjillings@1571 806 request.send();
nickjillings@1571 807 };
nickjillings@1571 808
nickjillings@1571 809 this.exportXMLDOM = function() {
nickjillings@1571 810 var root = document.createElement('audioElement');
nickjillings@1571 811 root.id = this.specification.id;
nickjillings@1571 812 root.setAttribute('url',this.url);
nickjillings@1571 813 root.appendChild(this.interfaceDOM.exportXMLDOM());
nickjillings@1571 814 root.appendChild(this.commentDOM.exportXMLDOM());
nickjillings@1571 815 root.appendChild(this.metric.exportXMLDOM());
nickjillings@1571 816 return root;
nickjillings@1571 817 };
nickjillings@1571 818 }
nickjillings@1571 819
nickjillings@1571 820 function timer()
nickjillings@1571 821 {
nickjillings@1571 822 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1571 823 * Uses the timer of the web audio API, so sample resolution
nickjillings@1571 824 */
nickjillings@1571 825 this.testStarted = false;
nickjillings@1571 826 this.testStartTime = 0;
nickjillings@1571 827 this.testDuration = 0;
nickjillings@1571 828 this.minimumTestTime = 0; // No minimum test time
nickjillings@1571 829 this.startTest = function()
nickjillings@1571 830 {
nickjillings@1571 831 if (this.testStarted == false)
nickjillings@1571 832 {
nickjillings@1571 833 this.testStartTime = audioContext.currentTime;
nickjillings@1571 834 this.testStarted = true;
nickjillings@1571 835 this.updateTestTime();
nickjillings@1571 836 audioEngineContext.metric.initialiseTest();
nickjillings@1571 837 }
nickjillings@1571 838 };
nickjillings@1571 839 this.stopTest = function()
nickjillings@1571 840 {
nickjillings@1571 841 if (this.testStarted)
nickjillings@1571 842 {
nickjillings@1571 843 this.testDuration = this.getTestTime();
nickjillings@1571 844 this.testStarted = false;
nickjillings@1571 845 } else {
nickjillings@1571 846 console.log('ERR: Test tried to end before beginning');
nickjillings@1571 847 }
nickjillings@1571 848 };
nickjillings@1571 849 this.updateTestTime = function()
nickjillings@1571 850 {
nickjillings@1571 851 if (this.testStarted)
nickjillings@1571 852 {
nickjillings@1571 853 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1571 854 }
nickjillings@1571 855 };
nickjillings@1571 856 this.getTestTime = function()
nickjillings@1571 857 {
nickjillings@1571 858 this.updateTestTime();
nickjillings@1571 859 return this.testDuration;
nickjillings@1571 860 };
nickjillings@1571 861 }
nickjillings@1571 862
nickjillings@1571 863 function sessionMetrics(engine)
nickjillings@1571 864 {
nickjillings@1571 865 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1571 866 */
nickjillings@1571 867 this.engine = engine;
nickjillings@1571 868 this.lastClicked = -1;
nickjillings@1571 869 this.data = -1;
nickjillings@1571 870 this.reset = function() {
nickjillings@1571 871 this.lastClicked = -1;
nickjillings@1571 872 this.data = -1;
nickjillings@1571 873 };
nickjillings@1571 874 this.initialiseTest = function(){};
nickjillings@1571 875 }
nickjillings@1571 876
nickjillings@1571 877 function metricTracker(caller)
nickjillings@1571 878 {
nickjillings@1571 879 /* Custom object to track and collect metric data
nickjillings@1571 880 * Used only inside the audioObjects object.
nickjillings@1571 881 */
nickjillings@1571 882
nickjillings@1571 883 this.listenedTimer = 0;
nickjillings@1571 884 this.listenStart = 0;
nickjillings@1571 885 this.listenHold = false;
nickjillings@1571 886 this.initialPosition = -1;
nickjillings@1571 887 this.movementTracker = [];
nickjillings@1571 888 this.listenTracker =[];
nickjillings@1571 889 this.wasListenedTo = false;
nickjillings@1571 890 this.wasMoved = false;
nickjillings@1571 891 this.hasComments = false;
nickjillings@1571 892 this.parent = caller;
nickjillings@1571 893
nickjillings@1571 894 this.initialised = function(position)
nickjillings@1571 895 {
nickjillings@1571 896 if (this.initialPosition == -1) {
nickjillings@1571 897 this.initialPosition = position;
nickjillings@1571 898 }
nickjillings@1571 899 };
nickjillings@1571 900
nickjillings@1571 901 this.moved = function(time,position)
nickjillings@1571 902 {
nickjillings@1571 903 this.wasMoved = true;
nickjillings@1571 904 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1571 905 };
nickjillings@1571 906
nickjillings@1571 907 this.startListening = function(time)
nickjillings@1571 908 {
nickjillings@1571 909 if (this.listenHold == false)
nickjillings@1571 910 {
nickjillings@1571 911 this.wasListenedTo = true;
nickjillings@1571 912 this.listenStart = time;
nickjillings@1571 913 this.listenHold = true;
nickjillings@1571 914
nickjillings@1571 915 var evnt = document.createElement('event');
nickjillings@1571 916 var testTime = document.createElement('testTime');
nickjillings@1571 917 testTime.setAttribute('start',time);
nickjillings@1571 918 var bufferTime = document.createElement('bufferTime');
nickjillings@1571 919 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@1571 920 evnt.appendChild(testTime);
nickjillings@1571 921 evnt.appendChild(bufferTime);
nickjillings@1571 922 this.listenTracker.push(evnt);
nickjillings@1571 923
nickjillings@1571 924 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1571 925 }
nickjillings@1571 926 };
nickjillings@1571 927
nickjillings@1571 928 this.stopListening = function(time)
nickjillings@1571 929 {
nickjillings@1571 930 if (this.listenHold == true)
nickjillings@1571 931 {
nickjillings@1571 932 var diff = time - this.listenStart;
nickjillings@1571 933 this.listenedTimer += (diff);
nickjillings@1571 934 this.listenStart = 0;
nickjillings@1571 935 this.listenHold = false;
nickjillings@1571 936
nickjillings@1571 937 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@1571 938 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@1571 939 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@1571 940 testTime.setAttribute('stop',time);
nickjillings@1571 941 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1571 942 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1571 943 }
nickjillings@1571 944 };
nickjillings@1571 945
nickjillings@1571 946 this.exportXMLDOM = function() {
nickjillings@1571 947 var root = document.createElement('metric');
nickjillings@1571 948 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1571 949 var mElementTimer = document.createElement('metricresult');
nickjillings@1571 950 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1571 951 mElementTimer.textContent = this.listenedTimer;
nickjillings@1571 952 root.appendChild(mElementTimer);
nickjillings@1571 953 }
nickjillings@1571 954 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1571 955 var elementTrackerFull = document.createElement('metricResult');
nickjillings@1571 956 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1571 957 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1571 958 {
nickjillings@1571 959 var timePos = document.createElement('timePos');
nickjillings@1571 960 timePos.id = k;
nickjillings@1571 961 var time = document.createElement('time');
nickjillings@1571 962 time.textContent = this.movementTracker[k][0];
nickjillings@1571 963 var position = document.createElement('position');
nickjillings@1571 964 position.textContent = this.movementTracker[k][1];
nickjillings@1571 965 timePos.appendChild(time);
nickjillings@1571 966 timePos.appendChild(position);
nickjillings@1571 967 elementTrackerFull.appendChild(timePos);
nickjillings@1571 968 }
nickjillings@1571 969 root.appendChild(elementTrackerFull);
nickjillings@1571 970 }
nickjillings@1571 971 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1571 972 var elementListenTracker = document.createElement('metricResult');
nickjillings@1571 973 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1571 974 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1571 975 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1571 976 }
nickjillings@1571 977 root.appendChild(elementListenTracker);
nickjillings@1571 978 }
nickjillings@1571 979 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1571 980 var elementInitial = document.createElement('metricResult');
nickjillings@1571 981 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1571 982 elementInitial.textContent = this.initialPosition;
nickjillings@1571 983 root.appendChild(elementInitial);
nickjillings@1571 984 }
nickjillings@1571 985 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1571 986 var flagListenedTo = document.createElement('metricResult');
nickjillings@1571 987 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1571 988 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1571 989 root.appendChild(flagListenedTo);
nickjillings@1571 990 }
nickjillings@1571 991 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1571 992 var flagMoved = document.createElement('metricResult');
nickjillings@1571 993 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1571 994 flagMoved.textContent = this.wasMoved;
nickjillings@1571 995 root.appendChild(flagMoved);
nickjillings@1571 996 }
nickjillings@1571 997 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1571 998 var flagComments = document.createElement('metricResult');
nickjillings@1571 999 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1571 1000 if (this.parent.commentDOM == null)
nickjillings@1571 1001 {flag.textContent = 'false';}
nickjillings@1571 1002 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1571 1003 {flag.textContent = 'false';}
nickjillings@1571 1004 else
nickjillings@1571 1005 {flag.textContet = 'true';}
nickjillings@1571 1006 root.appendChild(flagComments);
nickjillings@1571 1007 }
nickjillings@1571 1008
nickjillings@1571 1009 return root;
nickjillings@1571 1010 };
nickjillings@1571 1011 }
nickjillings@1571 1012
nickjillings@1571 1013 function randomiseOrder(input)
nickjillings@1571 1014 {
nickjillings@1571 1015 // This takes an array of information and randomises the order
nickjillings@1571 1016 var N = input.length;
nickjillings@1571 1017 var K = N;
nickjillings@1571 1018 var holdArr = [];
nickjillings@1571 1019 for (var n=0; n<N; n++)
nickjillings@1571 1020 {
nickjillings@1571 1021 // First pick a random number
nickjillings@1571 1022 var r = Math.random();
nickjillings@1571 1023 // Multiply and floor by the number of elements left
nickjillings@1571 1024 r = Math.floor(r*input.length);
nickjillings@1571 1025 // Pick out that element and delete from the array
nickjillings@1571 1026 holdArr.push(input.splice(r,1)[0]);
nickjillings@1571 1027 }
nickjillings@1571 1028 return holdArr;
nickjillings@1571 1029 }
nickjillings@1571 1030
nickjillings@1571 1031 function returnDateNode()
nickjillings@1571 1032 {
nickjillings@1571 1033 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1571 1034 // Structure is
nickjillings@1571 1035 // <datetime>
nickjillings@1571 1036 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1571 1037 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1571 1038 // </datetime>
nickjillings@1571 1039 var dateTime = new Date();
nickjillings@1571 1040 var year = document.createAttribute('year');
nickjillings@1571 1041 var month = document.createAttribute('month');
nickjillings@1571 1042 var day = document.createAttribute('day');
nickjillings@1571 1043 var hour = document.createAttribute('hour');
nickjillings@1571 1044 var minute = document.createAttribute('minute');
nickjillings@1571 1045 var secs = document.createAttribute('secs');
nickjillings@1571 1046
nickjillings@1571 1047 year.nodeValue = dateTime.getFullYear();
nickjillings@1571 1048 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1571 1049 day.nodeValue = dateTime.getDate();
nickjillings@1571 1050 hour.nodeValue = dateTime.getHours();
nickjillings@1571 1051 minute.nodeValue = dateTime.getMinutes();
nickjillings@1571 1052 secs.nodeValue = dateTime.getSeconds();
nickjillings@1571 1053
nickjillings@1571 1054 var hold = document.createElement("datetime");
nickjillings@1571 1055 var date = document.createElement("date");
nickjillings@1571 1056 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1571 1057 var time = document.createElement("time");
nickjillings@1571 1058 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1571 1059
nickjillings@1571 1060 date.setAttributeNode(year);
nickjillings@1571 1061 date.setAttributeNode(month);
nickjillings@1571 1062 date.setAttributeNode(day);
nickjillings@1571 1063 time.setAttributeNode(hour);
nickjillings@1571 1064 time.setAttributeNode(minute);
nickjillings@1571 1065 time.setAttributeNode(secs);
nickjillings@1571 1066
nickjillings@1571 1067 hold.appendChild(date);
nickjillings@1571 1068 hold.appendChild(time);
nickjillings@1571 1069 return hold
nickjillings@1571 1070
nickjillings@1571 1071 }
nickjillings@1571 1072
nickjillings@1571 1073 function testWaitIndicator() {
nickjillings@1571 1074 if (audioEngineContext.checkAllReady() == false) {
nickjillings@1571 1075 var hold = document.createElement("div");
nickjillings@1571 1076 hold.id = "testWaitIndicator";
nickjillings@1571 1077 hold.className = "indicator-box";
nickjillings@1571 1078 hold.style.zIndex = 3;
nickjillings@1571 1079 var span = document.createElement("span");
nickjillings@1571 1080 span.textContent = "Please wait! Elements still loading";
nickjillings@1571 1081 hold.appendChild(span);
nickjillings@1571 1082 var blank = document.createElement('div');
nickjillings@1571 1083 blank.className = 'testHalt';
nickjillings@1571 1084 blank.id = "testHaltBlank";
nickjillings@1571 1085 var body = document.getElementsByTagName('body')[0];
nickjillings@1571 1086 body.appendChild(hold);
nickjillings@1571 1087 body.appendChild(blank);
nickjillings@1571 1088 testWaitTimerIntervalHolder = setInterval(function(){
nickjillings@1571 1089 var ready = audioEngineContext.checkAllReady();
nickjillings@1571 1090 if (ready) {
nickjillings@1571 1091 var elem = document.getElementById('testWaitIndicator');
nickjillings@1571 1092 var blank = document.getElementById('testHaltBlank');
nickjillings@1571 1093 var body = document.getElementsByTagName('body')[0];
nickjillings@1571 1094 body.removeChild(elem);
nickjillings@1571 1095 body.removeChild(blank);
nickjillings@1571 1096 clearInterval(testWaitTimerIntervalHolder);
nickjillings@1571 1097 }
nickjillings@1571 1098 },500,false);
nickjillings@1571 1099 }
nickjillings@1571 1100 }
nickjillings@1571 1101
nickjillings@1571 1102 var testWaitTimerIntervalHolder = null;
nickjillings@1571 1103
nickjillings@1571 1104 function Specification() {
nickjillings@1571 1105 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1571 1106
nickjillings@1571 1107 this.interfaceType;
nickjillings@1571 1108 this.projectReturn;
nickjillings@1571 1109 this.randomiseOrder;
nickjillings@1571 1110 this.collectMetrics;
nickjillings@1571 1111 this.preTest;
nickjillings@1571 1112 this.postTest;
nickjillings@1571 1113 this.metrics =[];
nickjillings@1571 1114
nickjillings@1571 1115 this.audioHolders = [];
nickjillings@1571 1116
nickjillings@1571 1117 this.decode = function() {
nickjillings@1571 1118 // projectXML - DOM Parsed document
nickjillings@1571 1119 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1571 1120 this.interfaceType = setupNode.getAttribute('interface');
nickjillings@1571 1121 this.projectReturn = setupNode.getAttribute('projectReturn');
nickjillings@1571 1122 if (setupNode.getAttribute('randomiseOrder') == "true") {
nickjillings@1571 1123 this.randomiseOrder = true;
nickjillings@1571 1124 } else {this.randomiseOrder = false;}
nickjillings@1571 1125 if (setupNode.getAttribute('collectMetrics') == "true") {
nickjillings@1571 1126 this.collectMetrics = true;
nickjillings@1571 1127 } else {this.collectMetrics = false;}
nickjillings@1571 1128 var metricCollection = setupNode.getElementsByTagName('Metric');
nickjillings@1571 1129
nickjillings@1571 1130 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
nickjillings@1571 1131 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
nickjillings@1571 1132
nickjillings@1571 1133 if (metricCollection.length > 0) {
nickjillings@1571 1134 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nickjillings@1571 1135 for (var i=0; i<metricCollection.length; i++) {
nickjillings@1571 1136 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nickjillings@1571 1137 }
nickjillings@1571 1138 }
nickjillings@1571 1139
nickjillings@1571 1140 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nickjillings@1571 1141 for (var i=0; i<audioHolders.length; i++) {
nickjillings@1571 1142 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
nickjillings@1571 1143 }
nickjillings@1571 1144
nickjillings@1571 1145 };
nickjillings@1571 1146
nickjillings@1571 1147 this.prepostNode = function(type,Collection) {
nickjillings@1571 1148 this.type = type;
nickjillings@1571 1149 this.options = [];
nickjillings@1571 1150
nickjillings@1571 1151 this.OptionNode = function(child) {
nickjillings@1571 1152
nickjillings@1571 1153 this.childOption = function(element) {
nickjillings@1571 1154 this.type = 'option';
nickjillings@1571 1155 this.id = element.id;
nickjillings@1571 1156 this.name = element.getAttribute('name');
nickjillings@1571 1157 this.text = element.textContent;
nickjillings@1571 1158 };
nickjillings@1571 1159
nickjillings@1571 1160 this.type = child.nodeName;
nickjillings@1571 1161 if (child.nodeName == "question") {
nickjillings@1571 1162 this.id = child.id;
nickjillings@1571 1163 this.mandatory;
nickjillings@1571 1164 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nickjillings@1571 1165 else {this.mandatory = false;}
nickjillings@1571 1166 this.question = child.textContent;
nickjillings@1571 1167 if (child.getAttribute('boxsize') == null) {
nickjillings@1571 1168 this.boxsize = 'normal';
nickjillings@1571 1169 } else {
nickjillings@1571 1170 this.boxsize = child.getAttribute('boxsize');
nickjillings@1571 1171 }
nickjillings@1571 1172 } else if (child.nodeName == "statement") {
nickjillings@1571 1173 this.statement = child.textContent;
nickjillings@1571 1174 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nickjillings@1571 1175 var element = child.firstElementChild;
nickjillings@1571 1176 this.id = child.id;
nickjillings@1571 1177 if (element == null) {
nickjillings@1571 1178 console.log('Malformed' +child.nodeName+ 'entry');
nickjillings@1571 1179 this.statement = 'Malformed' +child.nodeName+ 'entry';
nickjillings@1571 1180 this.type = 'statement';
nickjillings@1571 1181 } else {
nickjillings@1571 1182 this.options = [];
nickjillings@1571 1183 while (element != null) {
nickjillings@1571 1184 if (element.nodeName == 'statement' && this.statement == undefined){
nickjillings@1571 1185 this.statement = element.textContent;
nickjillings@1571 1186 } else if (element.nodeName == 'option') {
nickjillings@1571 1187 this.options.push(new this.childOption(element));
nickjillings@1571 1188 }
nickjillings@1571 1189 element = element.nextElementSibling;
nickjillings@1571 1190 }
nickjillings@1571 1191 }
nickjillings@1573 1192 } else if (child.nodeName == "number") {
nickjillings@1573 1193 this.statement = child.textContent;
nickjillings@1573 1194 this.id = child.id;
nickjillings@1573 1195 this.min = child.getAttribute('min');
nickjillings@1573 1196 this.max = child.getAttribute('max');
nickjillings@1573 1197 this.step = child.getAttribute('step');
nickjillings@1571 1198 }
nickjillings@1571 1199 };
nickjillings@1571 1200
nickjillings@1571 1201 // On construction:
nickjillings@1571 1202 if (Collection.length != 0) {
nickjillings@1571 1203 Collection = Collection[0];
nickjillings@1571 1204 if (Collection.childElementCount != 0) {
nickjillings@1571 1205 var child = Collection.firstElementChild;
nickjillings@1571 1206 this.options.push(new this.OptionNode(child));
nickjillings@1571 1207 while (child.nextElementSibling != null) {
nickjillings@1571 1208 child = child.nextElementSibling;
nickjillings@1571 1209 this.options.push(new this.OptionNode(child));
nickjillings@1571 1210 }
nickjillings@1571 1211 }
nickjillings@1571 1212 }
nickjillings@1571 1213 };
nickjillings@1571 1214
nickjillings@1571 1215 this.metricNode = function(name) {
nickjillings@1571 1216 this.enabled = name;
nickjillings@1571 1217 };
nickjillings@1571 1218
nickjillings@1571 1219 this.audioHolderNode = function(parent,xml) {
nickjillings@1571 1220 this.type = 'audioHolder';
nickjillings@1571 1221 this.interfaceNode = function(DOM) {
nickjillings@1571 1222 var title = DOM.getElementsByTagName('title');
nickjillings@1571 1223 if (title.length == 0) {this.title = null;}
nickjillings@1571 1224 else {this.title = title[0].textContent;}
nickjillings@1571 1225
nickjillings@1571 1226 var scale = DOM.getElementsByTagName('scale');
nickjillings@1571 1227 this.scale = [];
nickjillings@1571 1228 for (var i=0; i<scale.length; i++) {
nickjillings@1571 1229 var arr = [null, null];
nickjillings@1571 1230 arr[0] = scale[i].getAttribute('position');
nickjillings@1571 1231 arr[1] = scale[i].textContent;
nickjillings@1571 1232 this.scale.push(arr);
nickjillings@1571 1233 }
nickjillings@1571 1234 };
nickjillings@1571 1235
nickjillings@1571 1236 this.audioElementNode = function(parent,xml) {
nickjillings@1571 1237 this.url = xml.getAttribute('url');
nickjillings@1571 1238 this.id = xml.id;
nickjillings@1571 1239 this.parent = parent;
nickjillings@1571 1240 };
nickjillings@1571 1241
nickjillings@1571 1242 this.commentQuestionNode = function(xml) {
nickjillings@1571 1243 this.childOption = function(element) {
nickjillings@1571 1244 this.type = 'option';
nickjillings@1571 1245 this.name = element.getAttribute('name');
nickjillings@1571 1246 this.text = element.textContent;
nickjillings@1571 1247 };
nickjillings@1571 1248 this.id = xml.id;
nickjillings@1571 1249 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nickjillings@1571 1250 else {this.mandatory = false;}
nickjillings@1571 1251 this.type = xml.getAttribute('type');
nickjillings@1571 1252 if (this.type == undefined) {this.type = 'text';}
nickjillings@1571 1253 switch (this.type) {
nickjillings@1571 1254 case 'text':
nickjillings@1571 1255 this.question = xml.textContent;
nickjillings@1571 1256 break;
nickjillings@1571 1257 case 'radio':
nickjillings@1571 1258 var child = xml.firstElementChild;
nickjillings@1571 1259 this.options = [];
nickjillings@1571 1260 while (child != undefined) {
nickjillings@1571 1261 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1571 1262 this.statement = child.textContent;
nickjillings@1571 1263 } else if (child.nodeName == 'option') {
nickjillings@1571 1264 this.options.push(new this.childOption(child));
nickjillings@1571 1265 }
nickjillings@1571 1266 child = child.nextElementSibling;
nickjillings@1571 1267 }
nickjillings@1572 1268 break;
nickjillings@1572 1269 case 'checkbox':
nickjillings@1572 1270 var child = xml.firstElementChild;
nickjillings@1572 1271 this.options = [];
nickjillings@1572 1272 while (child != undefined) {
nickjillings@1572 1273 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1572 1274 this.statement = child.textContent;
nickjillings@1572 1275 } else if (child.nodeName == 'option') {
nickjillings@1572 1276 this.options.push(new this.childOption(child));
nickjillings@1572 1277 }
nickjillings@1572 1278 child = child.nextElementSibling;
nickjillings@1572 1279 }
nickjillings@1572 1280 break;
nickjillings@1571 1281 }
nickjillings@1571 1282 };
nickjillings@1571 1283
nickjillings@1571 1284 this.id = xml.id;
nickjillings@1571 1285 this.hostURL = xml.getAttribute('hostURL');
nickjillings@1571 1286 this.sampleRate = xml.getAttribute('sampleRate');
nickjillings@1571 1287 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nickjillings@1571 1288 else {this.randomiseOrder = false;}
nickjillings@1571 1289 this.repeatCount = xml.getAttribute('repeatCount');
nickjillings@1571 1290 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nickjillings@1571 1291 else {this.loop == false;}
nickjillings@1571 1292 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nickjillings@1571 1293 else {this.elementComments = false;}
nickjillings@1571 1294
nickjillings@1571 1295 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
nickjillings@1571 1296 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
nickjillings@1571 1297
nickjillings@1571 1298 this.interfaces = [];
nickjillings@1571 1299 var interfaceDOM = xml.getElementsByTagName('interface');
nickjillings@1571 1300 for (var i=0; i<interfaceDOM.length; i++) {
nickjillings@1571 1301 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
nickjillings@1571 1302 }
nickjillings@1571 1303
nickjillings@1571 1304 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nickjillings@1571 1305 if (this.commentBoxPrefix.length != 0) {
nickjillings@1571 1306 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nickjillings@1571 1307 } else {
nickjillings@1571 1308 this.commentBoxPrefix = "Comment on track";
nickjillings@1571 1309 }
nickjillings@1571 1310
nickjillings@1571 1311 this.audioElements =[];
nickjillings@1571 1312 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nickjillings@1571 1313 for (var i=0; i<audioElementsDOM.length; i++) {
nickjillings@1571 1314 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nickjillings@1571 1315 }
nickjillings@1571 1316
nickjillings@1571 1317 this.commentQuestions = [];
nickjillings@1571 1318 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nickjillings@1571 1319 for (var i=0; i<commentQuestionsDOM.length; i++) {
nickjillings@1571 1320 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
nickjillings@1571 1321 }
nickjillings@1571 1322 };
nickjillings@1571 1323 }
nickjillings@1571 1324
nickjillings@1571 1325 function Interface(specificationObject) {
nickjillings@1571 1326 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1571 1327 this.specification = specificationObject;
nickjillings@1571 1328 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1571 1329
nickjillings@1571 1330 // Bounded by interface!!
nickjillings@1571 1331 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1571 1332 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1571 1333 this.interfaceObjects = [];
nickjillings@1571 1334 this.interfaceObject = function(){};
nickjillings@1571 1335
nickjillings@1571 1336 this.commentBoxes = [];
nickjillings@1571 1337 this.elementCommentBox = function(audioObject) {
nickjillings@1571 1338 var element = audioObject.specification;
nickjillings@1571 1339 this.audioObject = audioObject;
nickjillings@1571 1340 this.id = audioObject.id;
nickjillings@1571 1341 var audioHolderObject = audioObject.specification.parent;
nickjillings@1571 1342 // Create document objects to hold the comment boxes
nickjillings@1571 1343 this.trackComment = document.createElement('div');
nickjillings@1571 1344 this.trackComment.className = 'comment-div';
nickjillings@1571 1345 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1571 1346 // Create a string next to each comment asking for a comment
nickjillings@1571 1347 this.trackString = document.createElement('span');
nickjillings@1571 1348 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nickjillings@1571 1349 // Create the HTML5 comment box 'textarea'
nickjillings@1571 1350 this.trackCommentBox = document.createElement('textarea');
nickjillings@1571 1351 this.trackCommentBox.rows = '4';
nickjillings@1571 1352 this.trackCommentBox.cols = '100';
nickjillings@1571 1353 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1571 1354 this.trackCommentBox.className = 'trackComment';
nickjillings@1571 1355 var br = document.createElement('br');
nickjillings@1571 1356 // Add to the holder.
nickjillings@1571 1357 this.trackComment.appendChild(this.trackString);
nickjillings@1571 1358 this.trackComment.appendChild(br);
nickjillings@1571 1359 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1571 1360
nickjillings@1571 1361 this.exportXMLDOM = function() {
nickjillings@1571 1362 var root = document.createElement('comment');
nickjillings@1571 1363 if (this.audioObject.specification.parent.elementComments) {
nickjillings@1571 1364 var question = document.createElement('question');
nickjillings@1571 1365 question.textContent = this.trackString.textContent;
nickjillings@1571 1366 var response = document.createElement('response');
nickjillings@1571 1367 response.textContent = this.trackCommentBox.value;
nickjillings@1571 1368 root.appendChild(question);
nickjillings@1571 1369 root.appendChild(response);
nickjillings@1571 1370 }
nickjillings@1571 1371 return root;
nickjillings@1571 1372 };
nickjillings@1571 1373 };
nickjillings@1571 1374
nickjillings@1571 1375 this.commentQuestions = [];
nickjillings@1571 1376
nickjillings@1571 1377 this.commentBox = function(commentQuestion) {
nickjillings@1571 1378 this.specification = commentQuestion;
nickjillings@1571 1379 // Create document objects to hold the comment boxes
nickjillings@1571 1380 this.holder = document.createElement('div');
nickjillings@1571 1381 this.holder.className = 'comment-div';
nickjillings@1571 1382 // Create a string next to each comment asking for a comment
nickjillings@1571 1383 this.string = document.createElement('span');
nickjillings@1571 1384 this.string.innerHTML = commentQuestion.question;
nickjillings@1571 1385 // Create the HTML5 comment box 'textarea'
nickjillings@1571 1386 this.textArea = document.createElement('textarea');
nickjillings@1571 1387 this.textArea.rows = '4';
nickjillings@1571 1388 this.textArea.cols = '100';
nickjillings@1571 1389 this.textArea.className = 'trackComment';
nickjillings@1571 1390 var br = document.createElement('br');
nickjillings@1571 1391 // Add to the holder.
nickjillings@1571 1392 this.holder.appendChild(this.string);
nickjillings@1571 1393 this.holder.appendChild(br);
nickjillings@1571 1394 this.holder.appendChild(this.textArea);
nickjillings@1571 1395
nickjillings@1571 1396 this.exportXMLDOM = function() {
nickjillings@1571 1397 var root = document.createElement('comment');
nickjillings@1571 1398 root.id = this.specification.id;
nickjillings@1571 1399 root.setAttribute('type',this.specification.type);
nickjillings@1571 1400 root.textContent = this.textArea.value;
nickjillings@1571 1401 return root;
nickjillings@1571 1402 };
nickjillings@1571 1403 };
nickjillings@1571 1404
nickjillings@1571 1405 this.radioBox = function(commentQuestion) {
nickjillings@1571 1406 this.specification = commentQuestion;
nickjillings@1571 1407 // Create document objects to hold the comment boxes
nickjillings@1571 1408 this.holder = document.createElement('div');
nickjillings@1571 1409 this.holder.className = 'comment-div';
nickjillings@1571 1410 // Create a string next to each comment asking for a comment
nickjillings@1571 1411 this.string = document.createElement('span');
nickjillings@1571 1412 this.string.innerHTML = commentQuestion.statement;
nickjillings@1571 1413 var br = document.createElement('br');
nickjillings@1571 1414 // Add to the holder.
nickjillings@1571 1415 this.holder.appendChild(this.string);
nickjillings@1571 1416 this.holder.appendChild(br);
nickjillings@1571 1417 this.options = [];
nickjillings@1571 1418 this.inputs = document.createElement('div');
nickjillings@1571 1419 this.span = document.createElement('div');
nickjillings@1571 1420 this.inputs.align = 'center';
nickjillings@1571 1421 this.inputs.style.marginLeft = '12px';
nickjillings@1571 1422 this.span.style.marginLeft = '12px';
nickjillings@1571 1423 this.span.align = 'center';
nickjillings@1571 1424 this.span.style.marginTop = '15px';
nickjillings@1571 1425
nickjillings@1571 1426 var optCount = commentQuestion.options.length;
nickjillings@1571 1427 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nickjillings@1571 1428 console.log(spanMargin);
nickjillings@1571 1429 for (var i=0; i<optCount; i++)
nickjillings@1571 1430 {
nickjillings@1571 1431 var div = document.createElement('div');
nickjillings@1571 1432 div.style.width = '100px';
nickjillings@1571 1433 div.style.float = 'left';
nickjillings@1571 1434 div.style.marginRight = spanMargin;
nickjillings@1571 1435 div.style.marginLeft = spanMargin;
nickjillings@1571 1436 var input = document.createElement('input');
nickjillings@1571 1437 input.type = 'radio';
nickjillings@1571 1438 input.name = commentQuestion.id;
nickjillings@1571 1439 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1571 1440 input.className = 'comment-radio';
nickjillings@1571 1441 div.appendChild(input);
nickjillings@1571 1442 this.inputs.appendChild(div);
nickjillings@1571 1443
nickjillings@1571 1444
nickjillings@1571 1445 div = document.createElement('div');
nickjillings@1571 1446 div.style.width = '100px';
nickjillings@1571 1447 div.style.float = 'left';
nickjillings@1571 1448 div.style.marginRight = spanMargin;
nickjillings@1571 1449 div.style.marginLeft = spanMargin;
nickjillings@1571 1450 div.align = 'center';
nickjillings@1571 1451 var span = document.createElement('span');
nickjillings@1571 1452 span.textContent = commentQuestion.options[i].text;
nickjillings@1571 1453 span.className = 'comment-radio-span';
nickjillings@1571 1454 div.appendChild(span);
nickjillings@1571 1455 this.span.appendChild(div);
nickjillings@1571 1456 this.options.push(input);
nickjillings@1571 1457 }
nickjillings@1571 1458 this.holder.appendChild(this.span);
nickjillings@1571 1459 this.holder.appendChild(this.inputs);
nickjillings@1571 1460
nickjillings@1571 1461 this.exportXMLDOM = function() {
nickjillings@1571 1462 var root = document.createElement('comment');
nickjillings@1571 1463 root.id = this.specification.id;
nickjillings@1571 1464 root.setAttribute('type',this.specification.type);
nickjillings@1571 1465 var question = document.createElement('question');
nickjillings@1571 1466 question.textContent = this.string.textContent;
nickjillings@1571 1467 var response = document.createElement('response');
nickjillings@1571 1468 var i=0;
nickjillings@1571 1469 while(this.options[i].checked == false) {
nickjillings@1571 1470 i++;
nickjillings@1571 1471 if (i >= this.options.length) {
nickjillings@1571 1472 break;
nickjillings@1571 1473 }
nickjillings@1571 1474 }
nickjillings@1571 1475 if (i >= this.options.length) {
nickjillings@1571 1476 response.textContent = 'null';
nickjillings@1571 1477 } else {
nickjillings@1571 1478 response.textContent = this.options[i].getAttribute('setvalue');
nickjillings@1571 1479 response.setAttribute('number',i);
nickjillings@1571 1480 }
nickjillings@1572 1481 console.log('Comment: '+question.textContent);
nickjillings@1572 1482 console.log('Response: '+response.textContent);
nickjillings@1571 1483 root.appendChild(question);
nickjillings@1571 1484 root.appendChild(response);
nickjillings@1571 1485 return root;
nickjillings@1571 1486 };
nickjillings@1571 1487 };
nickjillings@1571 1488
nickjillings@1572 1489 this.checkboxBox = function(commentQuestion) {
nickjillings@1572 1490 this.specification = commentQuestion;
nickjillings@1572 1491 // Create document objects to hold the comment boxes
nickjillings@1572 1492 this.holder = document.createElement('div');
nickjillings@1572 1493 this.holder.className = 'comment-div';
nickjillings@1572 1494 // Create a string next to each comment asking for a comment
nickjillings@1572 1495 this.string = document.createElement('span');
nickjillings@1572 1496 this.string.innerHTML = commentQuestion.statement;
nickjillings@1572 1497 var br = document.createElement('br');
nickjillings@1572 1498 // Add to the holder.
nickjillings@1572 1499 this.holder.appendChild(this.string);
nickjillings@1572 1500 this.holder.appendChild(br);
nickjillings@1572 1501 this.options = [];
nickjillings@1572 1502 this.inputs = document.createElement('div');
nickjillings@1572 1503 this.span = document.createElement('div');
nickjillings@1572 1504 this.inputs.align = 'center';
nickjillings@1572 1505 this.inputs.style.marginLeft = '12px';
nickjillings@1572 1506 this.span.style.marginLeft = '12px';
nickjillings@1572 1507 this.span.align = 'center';
nickjillings@1572 1508 this.span.style.marginTop = '15px';
nickjillings@1572 1509
nickjillings@1572 1510 var optCount = commentQuestion.options.length;
nickjillings@1572 1511 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
nickjillings@1572 1512 console.log(spanMargin);
nickjillings@1572 1513 for (var i=0; i<optCount; i++)
nickjillings@1572 1514 {
nickjillings@1572 1515 var div = document.createElement('div');
nickjillings@1572 1516 div.style.width = '100px';
nickjillings@1572 1517 div.style.float = 'left';
nickjillings@1572 1518 div.style.marginRight = spanMargin;
nickjillings@1572 1519 div.style.marginLeft = spanMargin;
nickjillings@1572 1520 var input = document.createElement('input');
nickjillings@1572 1521 input.type = 'checkbox';
nickjillings@1572 1522 input.name = commentQuestion.id;
nickjillings@1572 1523 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1572 1524 input.className = 'comment-radio';
nickjillings@1572 1525 div.appendChild(input);
nickjillings@1572 1526 this.inputs.appendChild(div);
nickjillings@1572 1527
nickjillings@1572 1528
nickjillings@1572 1529 div = document.createElement('div');
nickjillings@1572 1530 div.style.width = '100px';
nickjillings@1572 1531 div.style.float = 'left';
nickjillings@1572 1532 div.style.marginRight = spanMargin;
nickjillings@1572 1533 div.style.marginLeft = spanMargin;
nickjillings@1572 1534 div.align = 'center';
nickjillings@1572 1535 var span = document.createElement('span');
nickjillings@1572 1536 span.textContent = commentQuestion.options[i].text;
nickjillings@1572 1537 span.className = 'comment-radio-span';
nickjillings@1572 1538 div.appendChild(span);
nickjillings@1572 1539 this.span.appendChild(div);
nickjillings@1572 1540 this.options.push(input);
nickjillings@1572 1541 }
nickjillings@1572 1542 this.holder.appendChild(this.span);
nickjillings@1572 1543 this.holder.appendChild(this.inputs);
nickjillings@1572 1544
nickjillings@1572 1545 this.exportXMLDOM = function() {
nickjillings@1572 1546 var root = document.createElement('comment');
nickjillings@1572 1547 root.id = this.specification.id;
nickjillings@1572 1548 root.setAttribute('type',this.specification.type);
nickjillings@1572 1549 var question = document.createElement('question');
nickjillings@1572 1550 question.textContent = this.string.textContent;
nickjillings@1572 1551 root.appendChild(question);
nickjillings@1572 1552 console.log('Comment: '+question.textContent);
nickjillings@1572 1553 for (var i=0; i<this.options.length; i++) {
nickjillings@1572 1554 var response = document.createElement('response');
nickjillings@1572 1555 response.textContent = this.options[i].checked;
nickjillings@1572 1556 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nickjillings@1572 1557 root.appendChild(response);
nickjillings@1572 1558 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nickjillings@1572 1559 }
nickjillings@1572 1560 return root;
nickjillings@1572 1561 };
nickjillings@1572 1562 };
nickjillings@1571 1563
nickjillings@1571 1564 this.createCommentBox = function(audioObject) {
nickjillings@1571 1565 var node = new this.elementCommentBox(audioObject);
nickjillings@1571 1566 this.commentBoxes.push(node);
nickjillings@1571 1567 audioObject.commentDOM = node;
nickjillings@1571 1568 return node;
nickjillings@1571 1569 };
nickjillings@1571 1570
nickjillings@1571 1571 this.sortCommentBoxes = function() {
nickjillings@1571 1572 var holder = [];
nickjillings@1571 1573 while (this.commentBoxes.length > 0) {
nickjillings@1571 1574 var node = this.commentBoxes.pop(0);
nickjillings@1571 1575 holder[node.id] = node;
nickjillings@1571 1576 }
nickjillings@1571 1577 this.commentBoxes = holder;
nickjillings@1571 1578 };
nickjillings@1571 1579
nickjillings@1571 1580 this.showCommentBoxes = function(inject, sort) {
nickjillings@1571 1581 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1571 1582 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nickjillings@1571 1583 inject.appendChild(this.commentBoxes[i].trackComment);
nickjillings@1571 1584 }
nickjillings@1571 1585 };
nickjillings@1571 1586
nickjillings@1571 1587 this.createCommentQuestion = function(element) {
nickjillings@1571 1588 var node;
nickjillings@1571 1589 if (element.type == 'text') {
nickjillings@1571 1590 node = new this.commentBox(element);
nickjillings@1571 1591 } else if (element.type == 'radio') {
nickjillings@1571 1592 node = new this.radioBox(element);
nickjillings@1572 1593 } else if (element.type == 'checkbox') {
nickjillings@1572 1594 node = new this.checkboxBox(element);
nickjillings@1571 1595 }
nickjillings@1571 1596 this.commentQuestions.push(node);
nickjillings@1571 1597 return node;
nickjillings@1571 1598 };
nickjillings@1571 1599 }
nickjillings@1571 1600