annotate core.js @ 1573:a41d8efee6db

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