annotate core.js @ 2106:2ffbf9ea5435

Bug #1583 Fix. Popup buttons hidden on successful submission.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 22 Feb 2016 12:44:55 +0000
parents 03b2ff7308fa
children 27be75c51b5c
rev   line source
nickjillings@1682 1 /**
nickjillings@1682 2 * core.js
nickjillings@1682 3 *
nickjillings@1682 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1682 5 * Also contains all global variables.
nickjillings@1682 6 */
nickjillings@1682 7
nickjillings@1682 8 /* create the web audio API context and store in audioContext*/
nickjillings@1643 9 var audioContext; // Hold the browser web audio API
nickjillings@1643 10 var projectXML; // Hold the parsed setup XML
nickjillings@1324 11 var schemaXSD; // Hold the parsed schema XSD
nickjillings@1581 12 var specification;
nickjillings@1582 13 var interfaceContext;
nickjillings@1324 14 var storage;
nickjillings@1622 15 var popup; // Hold the interfacePopup object
nickjillings@1634 16 var testState;
nickjillings@1655 17 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1643 18 var audioEngineContext; // The custome AudioEngine object
nickjillings@1643 19 var projectReturn; // Hold the URL for the return
nickjillings@2013 20
nickjillings@1318 21
nickjillings@1318 22 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1318 23 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@2101 24 // Add a prototype to the bufferSourceNode to hold when the object was given a play command
nickjillings@2101 25 AudioBufferSourceNode.prototype.playbackStartTime = undefined;
nickjillings@1318 26 // Add a prototype to the bufferNode to hold the desired LINEAR gain
nickjillings@1320 27 AudioBuffer.prototype.playbackGain = undefined;
nickjillings@1318 28 // Add a prototype to the bufferNode to hold the computed LUFS loudness
nickjillings@1318 29 AudioBuffer.prototype.lufs = undefined;
nickjillings@1318 30
nickjillings@1348 31 // Firefox does not have an XMLDocument.prototype.getElementsByName
nickjillings@1348 32 // and there is no searchAll style command, this custom function will
nickjillings@1348 33 // search all children recusrively for the name. Used for XSD where all
nickjillings@1348 34 // element nodes must have a name and therefore can pull the schema node
nickjillings@1348 35 XMLDocument.prototype.getAllElementsByName = function(name)
nickjillings@1348 36 {
nickjillings@1348 37 name = String(name);
nickjillings@1348 38 var selected = this.documentElement.getAllElementsByName(name);
nickjillings@1348 39 return selected;
nickjillings@1348 40 }
nickjillings@1348 41
nickjillings@1348 42 Element.prototype.getAllElementsByName = function(name)
nickjillings@1348 43 {
nickjillings@1348 44 name = String(name);
nickjillings@1348 45 var selected = [];
nickjillings@1348 46 var node = this.firstElementChild;
nickjillings@1348 47 while(node != null)
nickjillings@1348 48 {
nickjillings@1348 49 if (node.getAttribute('name') == name)
nickjillings@1348 50 {
nickjillings@1348 51 selected.push(node);
nickjillings@1348 52 }
nickjillings@1348 53 if (node.childElementCount > 0)
nickjillings@1348 54 {
nickjillings@1348 55 selected = selected.concat(node.getAllElementsByName(name));
nickjillings@1348 56 }
nickjillings@1348 57 node = node.nextElementSibling;
nickjillings@1348 58 }
nickjillings@1348 59 return selected;
nickjillings@1348 60 }
nickjillings@1348 61
nickjillings@1348 62 XMLDocument.prototype.getAllElementsByTagName = function(name)
nickjillings@1348 63 {
nickjillings@1348 64 name = String(name);
nickjillings@1348 65 var selected = this.documentElement.getAllElementsByTagName(name);
nickjillings@1348 66 return selected;
nickjillings@1348 67 }
nickjillings@1348 68
nickjillings@1348 69 Element.prototype.getAllElementsByTagName = function(name)
nickjillings@1348 70 {
nickjillings@1348 71 name = String(name);
nickjillings@1348 72 var selected = [];
nickjillings@1348 73 var node = this.firstElementChild;
nickjillings@1348 74 while(node != null)
nickjillings@1348 75 {
nickjillings@1348 76 if (node.nodeName == name)
nickjillings@1348 77 {
nickjillings@1348 78 selected.push(node);
nickjillings@1348 79 }
nickjillings@1348 80 if (node.childElementCount > 0)
nickjillings@1348 81 {
nickjillings@1348 82 selected = selected.concat(node.getAllElementsByTagName(name));
nickjillings@1348 83 }
nickjillings@1348 84 node = node.nextElementSibling;
nickjillings@1348 85 }
nickjillings@1348 86 return selected;
nickjillings@1348 87 }
nickjillings@1348 88
nickjillings@1348 89 // Firefox does not have an XMLDocument.prototype.getElementsByName
nickjillings@1348 90 if (typeof XMLDocument.prototype.getElementsByName != "function") {
nickjillings@1348 91 XMLDocument.prototype.getElementsByName = function(name)
nickjillings@1348 92 {
nickjillings@1348 93 name = String(name);
nickjillings@1348 94 var node = this.documentElement.firstElementChild;
nickjillings@1348 95 var selected = [];
nickjillings@1348 96 while(node != null)
nickjillings@1348 97 {
nickjillings@1348 98 if (node.getAttribute('name') == name)
nickjillings@1348 99 {
nickjillings@1348 100 selected.push(node);
nickjillings@1348 101 }
nickjillings@1348 102 node = node.nextElementSibling;
nickjillings@1348 103 }
nickjillings@1348 104 return selected;
nickjillings@1348 105 }
nickjillings@1348 106 }
nickjillings@1348 107
nickjillings@1682 108 window.onload = function() {
nickjillings@1682 109 // Function called once the browser has loaded all files.
nickjillings@1682 110 // This should perform any initial commands such as structure / loading documents
nickjillings@1682 111
nickjillings@1682 112 // Create a web audio API context
nickjillings@1701 113 // Fixed for cross-browser support
nickjillings@1701 114 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1688 115 audioContext = new AudioContext;
nickjillings@1682 116
nickjillings@1634 117 // Create test state
nickjillings@1634 118 testState = new stateMachine();
nickjillings@1634 119
nickjillings@1622 120 // Create the popup interface object
nickjillings@1622 121 popup = new interfacePopup();
nickjillings@1370 122
nickjillings@1370 123 // Create the specification object
nickjillings@1581 124 specification = new Specification();
nickjillings@1582 125
nickjillings@1582 126 // Create the interface object
nickjillings@1582 127 interfaceContext = new Interface(specification);
nickjillings@1324 128
nickjillings@1324 129 // Create the storage object
nickjillings@1324 130 storage = new Storage();
nickjillings@1410 131 // Define window callbacks for interface
nickjillings@1410 132 window.onresize = function(event){interfaceContext.resizeWindow(event);};
nickjillings@1697 133 };
nickjillings@1682 134
nickjillings@1408 135 function loadProjectSpec(url) {
nickjillings@1408 136 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1408 137 // If url is null, request client to upload project XML document
nickjillings@1324 138 var xmlhttp = new XMLHttpRequest();
nickjillings@1324 139 xmlhttp.open("GET",'test-schema.xsd',true);
nickjillings@1324 140 xmlhttp.onload = function()
nickjillings@1324 141 {
nickjillings@1324 142 schemaXSD = xmlhttp.response;
nickjillings@1324 143 var parse = new DOMParser();
nickjillings@1324 144 specification.schema = parse.parseFromString(xmlhttp.response,'text/xml');
nickjillings@1324 145 var r = new XMLHttpRequest();
nickjillings@1324 146 r.open('GET',url,true);
nickjillings@1324 147 r.onload = function() {
nickjillings@1324 148 loadProjectSpecCallback(r.response);
nickjillings@1324 149 };
nickjillings@1324 150 r.send();
nickjillings@1408 151 };
nickjillings@1324 152 xmlhttp.send();
nickjillings@1408 153 };
nickjillings@1408 154
nickjillings@1408 155 function loadProjectSpecCallback(response) {
nickjillings@1408 156 // Function called after asynchronous download of XML project specification
nickjillings@1408 157 //var decode = $.parseXML(response);
nickjillings@1408 158 //projectXML = $(decode);
nickjillings@1408 159
nickjillings@1324 160 // First perform XML schema validation
nickjillings@1324 161 var Module = {
nickjillings@1324 162 xml: response,
nickjillings@1324 163 schema: schemaXSD,
nickjillings@1324 164 arguments:["--noout", "--schema", 'test-schema.xsd','document.xml']
nickjillings@1324 165 };
nickjillings@1324 166
nickjillings@1324 167 var xmllint = validateXML(Module);
nickjillings@1324 168 console.log(xmllint);
nickjillings@1324 169 if(xmllint != 'document.xml validates\n')
nickjillings@1324 170 {
nickjillings@1324 171 document.getElementsByTagName('body')[0].innerHTML = null;
nickjillings@1324 172 var msg = document.createElement("h3");
nickjillings@1324 173 msg.textContent = "FATAL ERROR";
nickjillings@1324 174 var span = document.createElement("h4");
nickjillings@1324 175 span.textContent = "The XML validator returned the following errors when decoding your XML file";
nickjillings@1324 176 document.getElementsByTagName('body')[0].appendChild(msg);
nickjillings@1324 177 document.getElementsByTagName('body')[0].appendChild(span);
nickjillings@1324 178 xmllint = xmllint.split('\n');
nickjillings@1324 179 for (var i in xmllint)
nickjillings@1324 180 {
nickjillings@1324 181 document.getElementsByTagName('body')[0].appendChild(document.createElement('br'));
nickjillings@1324 182 var span = document.createElement("span");
nickjillings@1324 183 span.textContent = xmllint[i];
nickjillings@1324 184 document.getElementsByTagName('body')[0].appendChild(span);
nickjillings@1324 185 }
nickjillings@1324 186 return;
nickjillings@1324 187 }
nickjillings@1324 188
nickjillings@1408 189 var parse = new DOMParser();
nickjillings@1408 190 projectXML = parse.parseFromString(response,'text/xml');
nickjillings@1443 191 var errorNode = projectXML.getElementsByTagName('parsererror');
nickjillings@1443 192 if (errorNode.length >= 1)
nickjillings@1443 193 {
nickjillings@1443 194 var msg = document.createElement("h3");
nickjillings@1443 195 msg.textContent = "FATAL ERROR";
nickjillings@1443 196 var span = document.createElement("span");
nickjillings@1443 197 span.textContent = "The XML parser returned the following errors when decoding your XML file";
nickjillings@1445 198 document.getElementsByTagName('body')[0].innerHTML = null;
nickjillings@1443 199 document.getElementsByTagName('body')[0].appendChild(msg);
nickjillings@1443 200 document.getElementsByTagName('body')[0].appendChild(span);
nickjillings@1443 201 document.getElementsByTagName('body')[0].appendChild(errorNode[0]);
nickjillings@1443 202 return;
nickjillings@1443 203 }
nickjillings@1408 204
nickjillings@1408 205 // Build the specification
nickjillings@1408 206 specification.decode(projectXML);
nickjillings@1324 207 storage.initialise();
nickjillings@1339 208 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nickjillings@1339 209 if (specification.sampleRate != undefined) {
nickjillings@1339 210 if (Number(specification.sampleRate) != audioContext.sampleRate) {
nickjillings@1339 211 var errStr = 'Sample rates do not match! Requested '+Number(specification.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nickjillings@1339 212 alert(errStr);
nickjillings@1339 213 return;
nickjillings@1339 214 }
nickjillings@1339 215 }
nickjillings@1408 216
nickjillings@1408 217 // Detect the interface to use and load the relevant javascripts.
nickjillings@1408 218 var interfaceJS = document.createElement('script');
nickjillings@1408 219 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1329 220 switch(specification.interface)
nickjillings@1329 221 {
nickjillings@1329 222 case "APE":
nickjillings@1341 223 interfaceJS.setAttribute("src","interfaces/ape.js");
nickjillings@1408 224
nickjillings@1408 225 // APE comes with a css file
nickjillings@1408 226 var css = document.createElement('link');
nickjillings@1408 227 css.rel = 'stylesheet';
nickjillings@1408 228 css.type = 'text/css';
nickjillings@1341 229 css.href = 'interfaces/ape.css';
nickjillings@1408 230
nickjillings@1408 231 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1329 232 break;
nickjillings@1329 233
nickjillings@1329 234 case "MUSHRA":
nickjillings@1341 235 interfaceJS.setAttribute("src","interfaces/mushra.js");
nickjillings@1408 236
nickjillings@1408 237 // MUSHRA comes with a css file
nickjillings@1408 238 var css = document.createElement('link');
nickjillings@1408 239 css.rel = 'stylesheet';
nickjillings@1408 240 css.type = 'text/css';
nickjillings@1341 241 css.href = 'interfaces/mushra.css';
nickjillings@1408 242
nickjillings@1408 243 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1329 244 break;
nickjillings@1329 245
nickjillings@1329 246 case "AB":
nickjillings@1341 247 interfaceJS.setAttribute("src","interfaces/AB.js");
nickjillings@1329 248
nickjillings@1329 249 // AB comes with a css file
nickjillings@1329 250 var css = document.createElement('link');
nickjillings@1329 251 css.rel = 'stylesheet';
nickjillings@1329 252 css.type = 'text/css';
nickjillings@1341 253 css.href = 'interfaces/AB.css';
nickjillings@1329 254
nickjillings@1329 255 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1345 256 break;
nickjillings@1345 257 case "Bipolar":
nickjillings@1345 258 case "ACR":
nickjillings@1345 259 case "DCR":
nickjillings@1345 260 case "CCR":
nickjillings@1343 261 case "ABC":
nickjillings@1343 262 // Above enumerate to horizontal sliders
nickjillings@1343 263 interfaceJS.setAttribute("src","interfaces/horizontal-sliders.js");
nickjillings@1343 264
nickjillings@1343 265 // horizontal-sliders comes with a css file
nickjillings@1343 266 var css = document.createElement('link');
nickjillings@1343 267 css.rel = 'stylesheet';
nickjillings@1343 268 css.type = 'text/css';
nickjillings@1343 269 css.href = 'interfaces/horizontal-sliders.css';
nickjillings@1343 270
nickjillings@1343 271 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1345 272 break;
nickjillings@1345 273 case "discrete":
nickjillings@1345 274 case "likert":
nickjillings@1345 275 // Above enumerate to horizontal discrete radios
nickjillings@1345 276 interfaceJS.setAttribute("src","interfaces/discrete.js");
nickjillings@1345 277
nickjillings@1345 278 // horizontal-sliders comes with a css file
nickjillings@1345 279 var css = document.createElement('link');
nickjillings@1345 280 css.rel = 'stylesheet';
nickjillings@1345 281 css.type = 'text/css';
nickjillings@1345 282 css.href = 'interfaces/discrete.css';
nickjillings@1345 283
nickjillings@1345 284 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1345 285 break;
nickjillings@1408 286 }
nickjillings@1408 287 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1408 288
nickjillings@1410 289 // Create the audio engine object
nickjillings@1410 290 audioEngineContext = new AudioEngine(specification);
nickjillings@1410 291
nickjillings@1324 292 $(specification.pages).each(function(index,elem){
nickjillings@1410 293 $(elem.audioElements).each(function(i,audioElem){
nickjillings@1324 294 var URL = elem.hostURL + audioElem.url;
nickjillings@1410 295 var buffer = null;
nickjillings@1410 296 for (var i=0; i<audioEngineContext.buffers.length; i++)
nickjillings@1410 297 {
nickjillings@1410 298 if (URL == audioEngineContext.buffers[i].url)
nickjillings@1410 299 {
nickjillings@1410 300 buffer = audioEngineContext.buffers[i];
nickjillings@1410 301 break;
nickjillings@1410 302 }
nickjillings@1410 303 }
nickjillings@1410 304 if (buffer == null)
nickjillings@1410 305 {
nickjillings@1430 306 buffer = new audioEngineContext.bufferObj();
nickjillings@1430 307 buffer.getMedia(URL);
nickjillings@1410 308 audioEngineContext.buffers.push(buffer);
nickjillings@1410 309 }
nickjillings@1410 310 });
nickjillings@1410 311 });
nickjillings@1408 312 }
nickjillings@1408 313
nickjillings@1408 314 function createProjectSave(destURL) {
nickjillings@1408 315 // Save the data from interface into XML and send to destURL
nickjillings@1408 316 // If destURL is null then download XML in client
nickjillings@1408 317 // Now time to render file locally
nickjillings@1408 318 var xmlDoc = interfaceXMLSave();
nickjillings@1408 319 var parent = document.createElement("div");
nickjillings@1408 320 parent.appendChild(xmlDoc);
nickjillings@1408 321 var file = [parent.innerHTML];
nickjillings@1408 322 if (destURL == "null" || destURL == undefined) {
nickjillings@1408 323 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1408 324 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1408 325 var a = document.createElement("a");
nickjillings@1408 326 a.hidden = '';
nickjillings@1408 327 a.href = dnlk;
nickjillings@1408 328 a.download = "save.xml";
nickjillings@1408 329 a.textContent = "Save File";
nickjillings@1408 330
nickjillings@1408 331 popup.showPopup();
nickjillings@1332 332 popup.popupContent.innerHTML = "</span>Please save the file below to give to your test supervisor</span><br>";
nickjillings@1408 333 popup.popupContent.appendChild(a);
nickjillings@1408 334 } else {
nickjillings@1408 335 var xmlhttp = new XMLHttpRequest;
nickjillings@1408 336 xmlhttp.open("POST",destURL,true);
nickjillings@1408 337 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1408 338 xmlhttp.onerror = function(){
nickjillings@1408 339 console.log('Error saving file to server! Presenting download locally');
nickjillings@1408 340 createProjectSave(null);
nickjillings@1408 341 };
nickjillings@1408 342 xmlhttp.onreadystatechange = function() {
nickjillings@1408 343 console.log(xmlhttp.status);
nickjillings@1408 344 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
nickjillings@1408 345 createProjectSave(null);
nickjillings@1408 346 } else {
giuliomoro@1304 347 var parser = new DOMParser();
giuliomoro@1304 348 var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
giuliomoro@1304 349 if (xmlDoc == null)
nickjillings@1408 350 {
nickjillings@1324 351 createProjectSave('null');
nickjillings@1408 352 }
giuliomoro@1304 353 var response = xmlDoc.childNodes[0];
nickjillings@1408 354 if (response.getAttribute('state') == "OK")
nickjillings@1408 355 {
nickjillings@1408 356 var file = response.getElementsByTagName('file')[0];
nickjillings@1408 357 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B');
nickjillings@1408 358 popup.showPopup();
nickjillings@1408 359 popup.popupContent.innerHTML = null;
nickjillings@1408 360 popup.popupContent.textContent = "Thank you!";
nickjillings@1408 361 } else {
nickjillings@1408 362 var message = response.getElementsByTagName('message')[0];
nickjillings@1408 363 errorSessionDump(message.textContent);
nickjillings@1408 364 }
nickjillings@1408 365 }
nickjillings@1408 366 };
nickjillings@1408 367 xmlhttp.send(file);
nickjillings@1332 368 popup.showPopup();
nickjillings@1332 369 popup.popupContent.innerHTML = null;
nickjillings@1332 370 popup.popupContent.textContent = "Submitting. Please Wait";
nickjillings@2106 371 popup.hideNextButton();
nickjillings@2106 372 popup.hidePreviousButton();
nickjillings@1408 373 }
nickjillings@1408 374 }
nickjillings@1408 375
nickjillings@1408 376 function errorSessionDump(msg){
nickjillings@1408 377 // Create the partial interface XML save
nickjillings@1408 378 // Include error node with message on why the dump occured
nickjillings@1443 379 popup.showPopup();
nickjillings@1443 380 popup.popupContent.innerHTML = null;
nickjillings@1443 381 var err = document.createElement('error');
nickjillings@1443 382 var parent = document.createElement("div");
nickjillings@1443 383 if (typeof msg === "object")
nickjillings@1443 384 {
nickjillings@1443 385 err.appendChild(msg);
nickjillings@1443 386 popup.popupContent.appendChild(msg);
nickjillings@1443 387
nickjillings@1443 388 } else {
nickjillings@1443 389 err.textContent = msg;
nickjillings@1443 390 popup.popupContent.innerHTML = "ERROR : "+msg;
nickjillings@1443 391 }
nickjillings@1408 392 var xmlDoc = interfaceXMLSave();
nickjillings@1408 393 xmlDoc.appendChild(err);
nickjillings@1408 394 parent.appendChild(xmlDoc);
nickjillings@1408 395 var file = [parent.innerHTML];
nickjillings@1408 396 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1408 397 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1408 398 var a = document.createElement("a");
nickjillings@1408 399 a.hidden = '';
nickjillings@1408 400 a.href = dnlk;
nickjillings@1408 401 a.download = "save.xml";
nickjillings@1408 402 a.textContent = "Save File";
nickjillings@1408 403
nickjillings@1443 404
nickjillings@1443 405
nickjillings@1408 406 popup.popupContent.appendChild(a);
nickjillings@1408 407 }
nickjillings@1408 408
nickjillings@1408 409 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1408 410 function interfaceXMLSave(){
nickjillings@1408 411 // Create the XML string to be exported with results
nickjillings@1324 412 return storage.finish();
nickjillings@1408 413 }
nickjillings@1408 414
nickjillings@1426 415 function linearToDecibel(gain)
nickjillings@1426 416 {
nickjillings@1426 417 return 20.0*Math.log10(gain);
nickjillings@1426 418 }
nickjillings@1426 419
nickjillings@1426 420 function decibelToLinear(gain)
nickjillings@1426 421 {
nickjillings@1426 422 return Math.pow(10,gain/20.0);
nickjillings@1426 423 }
nickjillings@1426 424
nickjillings@1622 425 function interfacePopup() {
nickjillings@1622 426 // Creates an object to manage the popup
nickjillings@1622 427 this.popup = null;
nickjillings@1622 428 this.popupContent = null;
nickjillings@1526 429 this.popupTitle = null;
nickjillings@1526 430 this.popupResponse = null;
nickjillings@1574 431 this.buttonProceed = null;
nickjillings@2034 432 this.buttonPrevious = null;
nickjillings@1622 433 this.popupOptions = null;
nickjillings@1622 434 this.currentIndex = null;
nickjillings@1324 435 this.node = null;
nickjillings@1324 436 this.store = null;
nickjillings@1422 437 $(window).keypress(function(e){
nickjillings@1422 438 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible')
nickjillings@1422 439 {
nickjillings@1422 440 console.log(e);
nickjillings@1422 441 popup.buttonProceed.onclick();
nickjillings@1424 442 e.preventDefault();
nickjillings@1422 443 }
nickjillings@1422 444 });
nickjillings@1581 445
nickjillings@1622 446 this.createPopup = function(){
nickjillings@1622 447 // Create popup window interface
nickjillings@1622 448 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1622 449
nickjillings@1379 450 this.popup = document.getElementById('popupHolder');
nickjillings@1622 451 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1622 452 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1622 453
nickjillings@1379 454 this.popupContent = document.getElementById('popupContent');
nickjillings@1622 455
nickjillings@1379 456 this.popupTitle = document.getElementById('popupTitle');
nickjillings@1526 457
nickjillings@1379 458 this.popupResponse = document.getElementById('popupResponse');
nickjillings@1526 459
nickjillings@1379 460 this.buttonProceed = document.getElementById('popup-proceed');
nickjillings@1574 461 this.buttonProceed.onclick = function(){popup.proceedClicked();};
nickjillings@2034 462
nickjillings@1379 463 this.buttonPrevious = document.getElementById('popup-previous');
nickjillings@2034 464 this.buttonPrevious.onclick = function(){popup.previousClick();};
nickjillings@2034 465
nickjillings@1379 466 this.hidePopup();
nickjillings@1379 467
nickjillings@1581 468 this.popup.style.zIndex = -1;
nickjillings@1581 469 this.popup.style.visibility = 'hidden';
nickjillings@1622 470 };
nickjillings@1621 471
nickjillings@1622 472 this.showPopup = function(){
nickjillings@1581 473 if (this.popup == null) {
nickjillings@1622 474 this.createPopup();
nickjillings@1622 475 }
nickjillings@1622 476 this.popup.style.zIndex = 3;
nickjillings@1622 477 this.popup.style.visibility = 'visible';
nickjillings@1622 478 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 479 blank.style.zIndex = 2;
nickjillings@1622 480 blank.style.visibility = 'visible';
nickjillings@1622 481 };
nickjillings@1622 482
nickjillings@1622 483 this.hidePopup = function(){
nickjillings@1622 484 this.popup.style.zIndex = -1;
nickjillings@1622 485 this.popup.style.visibility = 'hidden';
nickjillings@1622 486 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1622 487 blank.style.zIndex = -2;
nickjillings@1622 488 blank.style.visibility = 'hidden';
nickjillings@1526 489 this.buttonPrevious.style.visibility = 'inherit';
nickjillings@1622 490 };
nickjillings@1622 491
nickjillings@1622 492 this.postNode = function() {
nickjillings@1622 493 // This will take the node from the popupOptions and display it
nickjillings@1622 494 var node = this.popupOptions[this.currentIndex];
nickjillings@1526 495 this.popupResponse.innerHTML = null;
nickjillings@1324 496 this.popupTitle.textContent = node.specification.statement;
nickjillings@1324 497 if (node.specification.type == 'question') {
nickjillings@1622 498 var textArea = document.createElement('textarea');
nickjillings@1324 499 switch (node.specification.boxsize) {
nickjillings@2030 500 case 'small':
nickjillings@2030 501 textArea.cols = "20";
nickjillings@2030 502 textArea.rows = "1";
nickjillings@2030 503 break;
nickjillings@2030 504 case 'normal':
nickjillings@2030 505 textArea.cols = "30";
nickjillings@2030 506 textArea.rows = "2";
nickjillings@2030 507 break;
nickjillings@2030 508 case 'large':
nickjillings@2030 509 textArea.cols = "40";
nickjillings@2030 510 textArea.rows = "5";
nickjillings@2030 511 break;
nickjillings@2030 512 case 'huge':
nickjillings@2030 513 textArea.cols = "50";
nickjillings@2030 514 textArea.rows = "10";
nickjillings@2030 515 break;
nickjillings@2030 516 }
nickjillings@1361 517 if (node.response == undefined) {
nickjillings@1361 518 node.response = "";
nickjillings@1361 519 } else {
nickjillings@1361 520 textArea.value = node.response;
nickjillings@1361 521 }
nickjillings@1526 522 this.popupResponse.appendChild(textArea);
nickjillings@1526 523 textArea.focus();
nickjillings@1382 524 this.popupResponse.style.textAlign="center";
nickjillings@1382 525 this.popupResponse.style.left="0%";
nickjillings@1324 526 } else if (node.specification.type == 'checkbox') {
nickjillings@1361 527 if (node.response == undefined) {
nickjillings@1361 528 node.response = Array(node.specification.options.length);
nickjillings@1361 529 }
nickjillings@1361 530 var index = 0;
nickjillings@1382 531 var max_w = 0;
nickjillings@1324 532 for (var option of node.specification.options) {
nickjillings@1588 533 var input = document.createElement('input');
nickjillings@2093 534 input.id = option.name;
nickjillings@1588 535 input.type = 'checkbox';
nickjillings@1588 536 var span = document.createElement('span');
nickjillings@1588 537 span.textContent = option.text;
nickjillings@1588 538 var hold = document.createElement('div');
nickjillings@1588 539 hold.setAttribute('name','option');
nickjillings@1588 540 hold.style.padding = '4px';
nickjillings@1588 541 hold.appendChild(input);
nickjillings@1588 542 hold.appendChild(span);
nickjillings@1324 543 this.popupResponse.appendChild(hold);
nickjillings@1361 544 if (node.response[index] != undefined){
nickjillings@1361 545 if (node.response[index].checked == true) {
nickjillings@1361 546 input.checked = "true";
nickjillings@1361 547 }
nickjillings@1361 548 }
nickjillings@1382 549 var w = $(span).width();
nickjillings@1382 550 if (w > max_w)
nickjillings@1382 551 max_w = w;
nickjillings@1361 552 index++;
nickjillings@1588 553 }
nickjillings@1382 554 max_w += 12;
nickjillings@1382 555 this.popupResponse.style.textAlign="";
nickjillings@1382 556 var leftP = ((max_w/500)/2)*100;
nickjillings@1382 557 this.popupResponse.style.left=leftP+"%";
nickjillings@1324 558 } else if (node.specification.type == 'radio') {
nickjillings@1361 559 if (node.response == undefined) {
nickjillings@1361 560 node.response = {name: "", text: ""};
nickjillings@1361 561 }
nickjillings@1361 562 var index = 0;
nickjillings@1382 563 var max_w = 0;
nickjillings@1324 564 for (var option of node.specification.options) {
nickjillings@1589 565 var input = document.createElement('input');
nickjillings@1589 566 input.id = option.name;
nickjillings@1589 567 input.type = 'radio';
nickjillings@1324 568 input.name = node.specification.id;
nickjillings@1589 569 var span = document.createElement('span');
nickjillings@1589 570 span.textContent = option.text;
nickjillings@1589 571 var hold = document.createElement('div');
nickjillings@1589 572 hold.setAttribute('name','option');
nickjillings@1589 573 hold.style.padding = '4px';
nickjillings@1589 574 hold.appendChild(input);
nickjillings@1589 575 hold.appendChild(span);
nickjillings@1324 576 this.popupResponse.appendChild(hold);
nickjillings@1361 577 if (input.id == node.response.name) {
nickjillings@1361 578 input.checked = "true";
nickjillings@1361 579 }
nickjillings@1382 580 var w = $(span).width();
nickjillings@1382 581 if (w > max_w)
nickjillings@1382 582 max_w = w;
nickjillings@1589 583 }
nickjillings@1382 584 max_w += 12;
nickjillings@1382 585 this.popupResponse.style.textAlign="";
nickjillings@1382 586 var leftP = ((max_w/500)/2)*100;
nickjillings@1382 587 this.popupResponse.style.left=leftP+"%";
nickjillings@1324 588 } else if (node.specification.type == 'number') {
nickjillings@1573 589 var input = document.createElement('input');
nickjillings@2044 590 input.type = 'textarea';
nickjillings@1324 591 if (node.min != null) {input.min = node.specification.min;}
nickjillings@1324 592 if (node.max != null) {input.max = node.specification.max;}
nickjillings@1324 593 if (node.step != null) {input.step = node.specification.step;}
nickjillings@1361 594 if (node.response != undefined) {
nickjillings@1361 595 input.value = node.response;
nickjillings@1361 596 }
nickjillings@1526 597 this.popupResponse.appendChild(input);
nickjillings@1382 598 this.popupResponse.style.textAlign="center";
nickjillings@1382 599 this.popupResponse.style.left="0%";
nickjillings@1622 600 }
nickjillings@2034 601 if(this.currentIndex+1 == this.popupOptions.length) {
nickjillings@1324 602 if (this.node.location == "pre") {
nickjillings@1531 603 this.buttonProceed.textContent = 'Start';
nickjillings@1531 604 } else {
nickjillings@1531 605 this.buttonProceed.textContent = 'Submit';
nickjillings@1531 606 }
nickjillings@2034 607 } else {
nickjillings@2034 608 this.buttonProceed.textContent = 'Next';
nickjillings@2034 609 }
nickjillings@2034 610 if(this.currentIndex > 0)
nickjillings@1526 611 this.buttonPrevious.style.visibility = 'visible';
nickjillings@1526 612 else
nickjillings@1526 613 this.buttonPrevious.style.visibility = 'hidden';
nickjillings@2015 614 };
nickjillings@1622 615
nickjillings@1324 616 this.initState = function(node,store) {
nickjillings@1622 617 //Call this with your preTest and postTest nodes when needed to
nickjillings@1622 618 // initialise the popup procedure.
nickjillings@1324 619 if (node.options.length > 0) {
nickjillings@1324 620 this.popupOptions = [];
nickjillings@1324 621 this.node = node;
nickjillings@1324 622 this.store = store;
nickjillings@1324 623 for (var opt of node.options)
nickjillings@1324 624 {
nickjillings@1324 625 this.popupOptions.push({
nickjillings@1324 626 specification: opt,
nickjillings@1324 627 response: null
nickjillings@1324 628 });
nickjillings@1324 629 }
nickjillings@1622 630 this.currentIndex = 0;
nickjillings@1622 631 this.showPopup();
nickjillings@1622 632 this.postNode();
nickjillings@1581 633 } else {
nickjillings@1581 634 advanceState();
nickjillings@1622 635 }
nickjillings@2015 636 };
nickjillings@1622 637
nickjillings@1574 638 this.proceedClicked = function() {
nickjillings@1622 639 // Each time the popup button is clicked!
nickjillings@1622 640 var node = this.popupOptions[this.currentIndex];
nickjillings@1324 641 if (node.specification.type == 'question') {
nickjillings@1622 642 // Must extract the question data
nickjillings@1622 643 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1324 644 if (node.specification.mandatory == true && textArea.value.length == 0) {
nickjillings@1622 645 alert('This question is mandatory');
nickjillings@1622 646 return;
nickjillings@1622 647 } else {
nickjillings@1622 648 // Save the text content
nickjillings@1324 649 console.log("Question: "+ node.specification.statement);
nickjillings@1623 650 console.log("Question Response: "+ textArea.value);
nickjillings@1324 651 node.response = textArea.value;
nickjillings@1622 652 }
nickjillings@1324 653 } else if (node.specification.type == 'checkbox') {
nickjillings@1588 654 // Must extract checkbox data
nickjillings@1326 655 console.log("Checkbox: "+ node.specification.statement);
nickjillings@1324 656 var inputs = this.popupResponse.getElementsByTagName('input');
nickjillings@1324 657 node.response = [];
nickjillings@1324 658 for (var i=0; i<node.specification.options.length; i++) {
nickjillings@1324 659 node.response.push({
nickjillings@1324 660 name: node.specification.options[i].name,
nickjillings@1324 661 text: node.specification.options[i].text,
nickjillings@1324 662 checked: inputs[i].checked
nickjillings@1324 663 });
nickjillings@1326 664 console.log(node.specification.options[i].name+": "+ inputs[i].checked);
nickjillings@1324 665 }
nickjillings@1324 666 } else if (node.specification.type == "radio") {
nickjillings@1526 667 var optHold = this.popupResponse;
nickjillings@1324 668 console.log("Radio: "+ node.specification.statement);
nickjillings@1324 669 node.response = null;
nickjillings@1589 670 var i=0;
nickjillings@1324 671 var inputs = optHold.getElementsByTagName('input');
nickjillings@1324 672 while(node.response == null) {
nickjillings@1324 673 if (i == inputs.length)
nickjillings@1324 674 {
nickjillings@1324 675 if (node.specification.mandatory == true)
nickjillings@1324 676 {
nickjillings@1324 677 alert("This radio is mandatory");
nickjillings@1324 678 } else {
nickjillings@1324 679 node.response = -1;
nickjillings@1324 680 }
nickjillings@1324 681 return;
nickjillings@1324 682 }
nickjillings@1324 683 if (inputs[i].checked == true) {
nickjillings@1324 684 node.response = node.specification.options[i];
nickjillings@1324 685 console.log("Selected: "+ node.specification.options[i].name);
nickjillings@1589 686 }
nickjillings@1589 687 i++;
nickjillings@1589 688 }
nickjillings@1324 689 } else if (node.specification.type == "number") {
nickjillings@1573 690 var input = this.popupContent.getElementsByTagName('input')[0];
nickjillings@1573 691 if (node.mandatory == true && input.value.length == 0) {
nickjillings@1574 692 alert('This question is mandatory. Please enter a number');
nickjillings@1574 693 return;
nickjillings@1574 694 }
nickjillings@1574 695 var enteredNumber = Number(input.value);
nickjillings@2044 696 if (isNaN(enteredNumber)) {
nickjillings@1574 697 alert('Please enter a valid number');
nickjillings@1574 698 return;
nickjillings@1574 699 }
nickjillings@1574 700 if (enteredNumber < node.min && node.min != null) {
nickjillings@1574 701 alert('Number is below the minimum value of '+node.min);
nickjillings@1574 702 return;
nickjillings@1574 703 }
nickjillings@1574 704 if (enteredNumber > node.max && node.max != null) {
nickjillings@1574 705 alert('Number is above the maximum value of '+node.max);
nickjillings@1573 706 return;
nickjillings@1573 707 }
nickjillings@1324 708 node.response = input.value;
nickjillings@1622 709 }
nickjillings@1622 710 this.currentIndex++;
nickjillings@1622 711 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1622 712 this.postNode();
nickjillings@1622 713 } else {
nickjillings@1622 714 // Reached the end of the popupOptions
nickjillings@1622 715 this.hidePopup();
nickjillings@1324 716 for (var node of this.popupOptions)
nickjillings@1324 717 {
nickjillings@1324 718 this.store.postResult(node);
nickjillings@1634 719 }
nickjillings@1622 720 advanceState();
nickjillings@1622 721 }
nickjillings@2015 722 };
nickjillings@2034 723
nickjillings@2034 724 this.previousClick = function() {
nickjillings@2034 725 // Triggered when the 'Back' button is clicked in the survey
nickjillings@2034 726 if (this.currentIndex > 0) {
nickjillings@2034 727 this.currentIndex--;
nickjillings@2034 728 this.postNode();
nickjillings@2034 729 }
nickjillings@2034 730 };
nickjillings@1421 731
nickjillings@1421 732 this.resize = function(event)
nickjillings@1421 733 {
nickjillings@1421 734 // Called on window resize;
nickjillings@1344 735 if (this.popup != null) {
nickjillings@1344 736 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1344 737 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1344 738 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1344 739 blank.style.width = window.innerWidth;
nickjillings@1344 740 blank.style.height = window.innerHeight;
nickjillings@1344 741 }
nickjillings@1421 742 };
nickjillings@2106 743 this.hideNextButton = function() {
nickjillings@2106 744 this.buttonProceed.style.visibility = "hidden";
nickjillings@2106 745 }
nickjillings@2106 746 this.hidePreviousButton = function() {
nickjillings@2106 747 this.buttonPrevious.style.visibility = "hidden";
nickjillings@2106 748 }
nickjillings@2106 749 this.showNextButton = function() {
nickjillings@2106 750 this.buttonProceed.style.visibility = "visible";
nickjillings@2106 751 }
nickjillings@2106 752 this.showPreviousButton = function() {
nickjillings@2106 753 this.buttonPrevious.style.visibility = "visible";
nickjillings@2106 754 }
nickjillings@1621 755 }
nickjillings@1621 756
nickjillings@1622 757 function advanceState()
nickjillings@1621 758 {
nickjillings@1634 759 // Just for complete clarity
nickjillings@1634 760 testState.advanceState();
nickjillings@1634 761 }
nickjillings@1634 762
nickjillings@1634 763 function stateMachine()
nickjillings@1634 764 {
nickjillings@1634 765 // Object prototype for tracking and managing the test state
nickjillings@1634 766 this.stateMap = [];
nickjillings@1324 767 this.preTestSurvey = null;
nickjillings@1324 768 this.postTestSurvey = null;
nickjillings@1634 769 this.stateIndex = null;
nickjillings@1324 770 this.currentStateMap = null;
nickjillings@1324 771 this.currentStatePosition = null;
nickjillings@1354 772 this.currentStore = null;
nickjillings@1634 773 this.initialise = function(){
nickjillings@1324 774
nickjillings@1324 775 // Get the data from Specification
nickjillings@1324 776 var pageHolder = [];
nickjillings@1324 777 for (var page of specification.pages)
nickjillings@1324 778 {
nickjillings@1380 779 var repeat = page.repeatCount;
nickjillings@1380 780 while(repeat >= 0)
nickjillings@1380 781 {
nickjillings@1380 782 pageHolder.push(page);
nickjillings@1380 783 repeat--;
nickjillings@1380 784 }
nickjillings@1324 785 }
nickjillings@1324 786 if (specification.randomiseOrder)
nickjillings@1324 787 {
nickjillings@1324 788 pageHolder = randomiseOrder(pageHolder);
nickjillings@1324 789 }
nickjillings@1324 790 for (var i=0; i<pageHolder.length; i++)
nickjillings@1324 791 {
nickjillings@1324 792 pageHolder[i].presentedId = i;
nickjillings@1324 793 }
nickjillings@1324 794 for (var i=0; i<specification.pages.length; i++)
nickjillings@1324 795 {
nickjillings@1324 796 if (specification.testPages < i && specification.testPages != 0) {break;}
nickjillings@1324 797 this.stateMap.push(pageHolder[i]);
nickjillings@1324 798
nickjillings@1324 799 }
nickjillings@1324 800 if (specification.preTest != null) {this.preTestSurvey = specification.preTest;}
nickjillings@1324 801 if (specification.postTest != null) {this.postTestSurvey = specification.postTest;}
nickjillings@1324 802
nickjillings@1634 803 if (this.stateMap.length > 0) {
nickjillings@1634 804 if(this.stateIndex != null) {
nickjillings@1634 805 console.log('NOTE - State already initialise');
nickjillings@1634 806 }
nickjillings@1634 807 this.stateIndex = -1;
nickjillings@1634 808 } else {
b@2059 809 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1622 810 }
nickjillings@1634 811 };
nickjillings@1634 812 this.advanceState = function(){
nickjillings@1634 813 if (this.stateIndex == null) {
nickjillings@1634 814 this.initialise();
nickjillings@1634 815 }
nickjillings@1634 816 if (this.stateIndex == -1) {
nickjillings@1342 817 this.stateIndex++;
nickjillings@1634 818 console.log('Starting test...');
nickjillings@1324 819 if (this.preTestSurvey != null)
nickjillings@1324 820 {
nickjillings@1324 821 popup.initState(this.preTestSurvey,storage.globalPreTest);
nickjillings@1342 822 } else {
nickjillings@1342 823 this.advanceState();
nickjillings@1318 824 }
nickjillings@1324 825 } else if (this.stateIndex == this.stateMap.length)
nickjillings@1324 826 {
nickjillings@1324 827 // All test pages complete, post test
nickjillings@1324 828 console.log('Ending test ...');
nickjillings@1324 829 this.stateIndex++;
nickjillings@1324 830 if (this.postTestSurvey == null) {
nickjillings@1324 831 this.advanceState();
nickjillings@1318 832 } else {
nickjillings@1324 833 popup.initState(this.postTestSurvey,storage.globalPostTest);
nickjillings@1324 834 }
nickjillings@1324 835 } else if (this.stateIndex > this.stateMap.length)
nickjillings@1324 836 {
nickjillings@1324 837 createProjectSave(specification.projectReturn);
nickjillings@1634 838 }
nickjillings@1324 839 else
nickjillings@1324 840 {
nickjillings@1324 841 if (this.currentStateMap == null)
nickjillings@1324 842 {
nickjillings@1318 843 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1334 844 if (this.currentStateMap.randomiseOrder)
nickjillings@1334 845 {
nickjillings@1334 846 this.currentStateMap.audioElements = randomiseOrder(this.currentStateMap.audioElements);
nickjillings@1334 847 }
nickjillings@1354 848 this.currentStore = storage.createTestPageStore(this.currentStateMap);
nickjillings@1324 849 if (this.currentStateMap.preTest != null)
nickjillings@1324 850 {
nickjillings@1324 851 this.currentStatePosition = 'pre';
nickjillings@1324 852 popup.initState(this.currentStateMap.preTest,storage.testPages[this.stateIndex].preTest);
nickjillings@1318 853 } else {
nickjillings@1324 854 this.currentStatePosition = 'test';
nickjillings@1324 855 }
nickjillings@1324 856 interfaceContext.newPage(this.currentStateMap,storage.testPages[this.stateIndex]);
nickjillings@1324 857 return;
nickjillings@1634 858 }
nickjillings@1324 859 switch(this.currentStatePosition)
nickjillings@1324 860 {
nickjillings@1324 861 case 'pre':
nickjillings@1324 862 this.currentStatePosition = 'test';
nickjillings@1324 863 break;
nickjillings@1324 864 case 'test':
nickjillings@1324 865 this.currentStatePosition = 'post';
nickjillings@1324 866 // Save the data
nickjillings@1324 867 this.testPageCompleted();
nickjillings@1324 868 if (this.currentStateMap.postTest == null)
nickjillings@1324 869 {
nickjillings@1318 870 this.advanceState();
nickjillings@1324 871 return;
nickjillings@1634 872 } else {
nickjillings@1324 873 popup.initState(this.currentStateMap.postTest,storage.testPages[this.stateIndex].postTest);
nickjillings@1634 874 }
nickjillings@1324 875 break;
nickjillings@1324 876 case 'post':
nickjillings@1324 877 this.stateIndex++;
nickjillings@1324 878 this.currentStateMap = null;
nickjillings@1324 879 this.advanceState();
nickjillings@1324 880 break;
nickjillings@1324 881 };
nickjillings@1634 882 }
nickjillings@1634 883 };
nickjillings@1634 884
nickjillings@1324 885 this.testPageCompleted = function() {
nickjillings@1634 886 // Function called each time a test page has been completed
nickjillings@1324 887 var storePoint = storage.testPages[this.stateIndex];
nickjillings@1324 888 // First get the test metric
nickjillings@1324 889
nickjillings@1324 890 var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0];
nickjillings@1412 891 if (audioEngineContext.metric.enableTestTimer)
nickjillings@1412 892 {
nickjillings@1324 893 var testTime = storePoint.parent.document.createElement('metricresult');
nickjillings@1412 894 testTime.id = 'testTime';
nickjillings@1412 895 testTime.textContent = audioEngineContext.timer.testDuration;
nickjillings@1412 896 metric.appendChild(testTime);
nickjillings@1412 897 }
nickjillings@1324 898
nickjillings@1412 899 var audioObjects = audioEngineContext.audioObjects;
nickjillings@1324 900 for (var ao of audioEngineContext.audioObjects)
nickjillings@1412 901 {
nickjillings@1324 902 ao.exportXMLDOM();
nickjillings@1412 903 }
nickjillings@1324 904 for (var element of interfaceContext.commentQuestions)
nickjillings@1324 905 {
nickjillings@1324 906 element.exportXMLDOM(storePoint);
nickjillings@1324 907 }
nickjillings@1324 908 pageXMLSave(storePoint.XMLDOM, this.currentStateMap);
nickjillings@1576 909 };
nickjillings@1621 910 }
nickjillings@1621 911
nickjillings@1408 912 function AudioEngine(specification) {
nickjillings@1682 913
nickjillings@1682 914 // Create two output paths, the main outputGain and fooGain.
nickjillings@1682 915 // Output gain is default to 1 and any items for playback route here
nickjillings@1682 916 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1682 917 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1682 918 this.outputGain = audioContext.createGain();
nickjillings@1682 919 this.fooGain = audioContext.createGain();
nickjillings@1682 920 this.fooGain.gain = 0;
nickjillings@1682 921
nickjillings@1688 922 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1688 923 this.status = 0;
nickjillings@1688 924
nickjillings@1682 925 // Connect both gains to output
nickjillings@1682 926 this.outputGain.connect(audioContext.destination);
nickjillings@1682 927 this.fooGain.connect(audioContext.destination);
nickjillings@1682 928
nickjillings@1659 929 // Create the timer Object
nickjillings@1659 930 this.timer = new timer();
nickjillings@1659 931 // Create session metrics
nickjillings@1408 932 this.metric = new sessionMetrics(this,specification);
nickjillings@1659 933
nickjillings@1667 934 this.loopPlayback = false;
nickjillings@1667 935
nickjillings@1324 936 this.pageStore = null;
nickjillings@1324 937
nickjillings@1682 938 // Create store for new audioObjects
nickjillings@1682 939 this.audioObjects = [];
nickjillings@1682 940
nickjillings@1410 941 this.buffers = [];
nickjillings@1430 942 this.bufferObj = function()
nickjillings@1410 943 {
nickjillings@1430 944 this.url = null;
nickjillings@1410 945 this.buffer = null;
nickjillings@1410 946 this.xmlRequest = new XMLHttpRequest();
nickjillings@1396 947 this.xmlRequest.parent = this;
nickjillings@1410 948 this.users = [];
nickjillings@1316 949 this.progress = 0;
nickjillings@1316 950 this.status = 0;
nickjillings@1342 951 this.ready = function()
nickjillings@1342 952 {
nickjillings@1316 953 if (this.status >= 2)
nickjillings@1316 954 {
nickjillings@1316 955 this.status = 3;
nickjillings@1316 956 }
nickjillings@1342 957 for (var i=0; i<this.users.length; i++)
nickjillings@1342 958 {
nickjillings@1342 959 this.users[i].state = 1;
nickjillings@1342 960 if (this.users[i].interfaceDOM != null)
nickjillings@1342 961 {
nickjillings@1342 962 this.users[i].bufferLoaded(this);
nickjillings@1342 963 }
nickjillings@1342 964 }
nickjillings@1342 965 };
nickjillings@1430 966 this.getMedia = function(url) {
nickjillings@1430 967 this.url = url;
nickjillings@1430 968 this.xmlRequest.open('GET',this.url,true);
nickjillings@1430 969 this.xmlRequest.responseType = 'arraybuffer';
nickjillings@1430 970
nickjillings@1430 971 var bufferObj = this;
nickjillings@1430 972
nickjillings@1430 973 // Create callback to decode the data asynchronously
nickjillings@1430 974 this.xmlRequest.onloadend = function() {
nickjillings@1353 975 // Use inbuilt WAVE decoder first
nickjillings@1353 976 var waveObj = new WAVE();
nickjillings@1353 977 if (waveObj.open(bufferObj.xmlRequest.response) == 0)
nickjillings@1353 978 {
nickjillings@1353 979 bufferObj.buffer = audioContext.createBuffer(waveObj.num_channels,waveObj.num_samples,waveObj.sample_rate);
nickjillings@1353 980 for (var c=0; c<waveObj.num_channels; c++)
nickjillings@1353 981 {
nickjillings@1353 982 var buffer_ptr = bufferObj.buffer.getChannelData(c);
nickjillings@1353 983 for (var n=0; n<waveObj.num_samples; n++)
nickjillings@1353 984 {
nickjillings@1353 985 buffer_ptr[n] = waveObj.decoded_data[c][n];
nickjillings@1353 986 }
nickjillings@1353 987 }
nickjillings@1316 988
nickjillings@1353 989 delete waveObj;
nickjillings@1353 990 } else {
nickjillings@1353 991 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) {
nickjillings@1353 992 bufferObj.buffer = decodedData;
nickjillings@1353 993 }, function(e){
nickjillings@1353 994 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1353 995 // Check here if the error is genuine
nickjillings@1353 996 if (bufferObj.xmlRequest.response == undefined) {
nickjillings@1353 997 // Genuine error
nickjillings@1353 998 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1353 999 if (request.status == 404)
nickjillings@1353 1000 {
nickjillings@1353 1001 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
nickjillings@1353 1002 console.log('URL: '+audioObj.url);
nickjillings@1353 1003 errorSessionDump('Fragment '+audioObj.id+' 404 error');
nickjillings@1353 1004 }
nickjillings@1316 1005 this.status = -1;
nickjillings@1353 1006 }
nickjillings@1353 1007 });
nickjillings@1353 1008 }
nickjillings@1353 1009 if (bufferObj.buffer != undefined)
nickjillings@1353 1010 {
nickjillings@1316 1011 bufferObj.status = 2;
nickjillings@1353 1012 calculateLoudness(bufferObj,"I");
nickjillings@1353 1013 }
nickjillings@1430 1014 };
nickjillings@1433 1015 this.progress = 0;
nickjillings@1433 1016 this.progressCallback = function(event){
nickjillings@1433 1017 if (event.lengthComputable)
nickjillings@1433 1018 {
nickjillings@1396 1019 this.parent.progress = event.loaded / event.total;
nickjillings@1396 1020 for (var i=0; i<this.parent.users.length; i++)
nickjillings@1396 1021 {
nickjillings@1396 1022 if(this.parent.users[i].interfaceDOM != null)
nickjillings@1396 1023 {
nickjillings@1396 1024 if (typeof this.parent.users[i].interfaceDOM.updateLoading === "function")
nickjillings@1396 1025 {
nickjillings@1396 1026 this.parent.users[i].interfaceDOM.updateLoading(this.parent.progress*100);
nickjillings@1396 1027 }
nickjillings@1396 1028 }
nickjillings@1396 1029 }
nickjillings@1433 1030 }
nickjillings@1433 1031 };
nickjillings@1433 1032 this.xmlRequest.addEventListener("progress", this.progressCallback);
nickjillings@1316 1033 this.status = 1;
nickjillings@1430 1034 this.xmlRequest.send();
nickjillings@1410 1035 };
nickjillings@1316 1036
nickjillings@1316 1037 this.registerAudioObject = function(audioObject)
nickjillings@1316 1038 {
nickjillings@1316 1039 // Called by an audioObject to register to the buffer for use
nickjillings@1316 1040 // First check if already in the register pool
nickjillings@1316 1041 for (var objects of this.users)
nickjillings@1316 1042 {
nickjillings@1316 1043 if (audioObject.id == objects.id){return 0;}
nickjillings@1316 1044 }
nickjillings@1316 1045 this.users.push(audioObject);
nickjillings@1316 1046 if (this.status == 3)
nickjillings@1316 1047 {
nickjillings@1316 1048 // The buffer is already ready, trigger bufferLoaded
nickjillings@1316 1049 audioObject.bufferLoaded(this);
nickjillings@1316 1050 }
nickjillings@1316 1051 }
nickjillings@1410 1052 };
nickjillings@1410 1053
nickjillings@1565 1054 this.play = function(id) {
nickjillings@1620 1055 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1523 1056 if (this.status == 0 && this.loopPlayback) {
nickjillings@1620 1057 // Check if all audioObjects are ready
nickjillings@1523 1058 if(this.checkAllReady())
nickjillings@1523 1059 {
nickjillings@1565 1060 this.status = 1;
nickjillings@1523 1061 this.setSynchronousLoop();
nickjillings@1565 1062 }
nickjillings@1565 1063 }
nickjillings@1523 1064 else
nickjillings@1523 1065 {
nickjillings@1523 1066 this.status = 1;
nickjillings@1523 1067 }
nickjillings@1565 1068 if (this.status== 1) {
nickjillings@1523 1069 this.timer.startTest();
nickjillings@1567 1070 if (id == undefined) {
nickjillings@1567 1071 id = -1;
nickjillings@1523 1072 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
nickjillings@1523 1073 return;
nickjillings@1567 1074 } else {
nickjillings@1567 1075 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
nickjillings@1567 1076 }
nickjillings@1565 1077 if (this.loopPlayback) {
nickjillings@2099 1078 var setTime = audioContext.currentTime;
nickjillings@1565 1079 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1565 1080 {
nickjillings@2099 1081 this.audioObjects[i].play(setTime);
nickjillings@1565 1082 if (id == i) {
nickjillings@1360 1083 this.audioObjects[i].loopStart(setTime);
nickjillings@1565 1084 } else {
nickjillings@1360 1085 this.audioObjects[i].loopStop(setTime);
nickjillings@1636 1086 }
nickjillings@1636 1087 }
nickjillings@1565 1088 } else {
nickjillings@1360 1089 var setTime = audioContext.currentTime+0.1;
nickjillings@1565 1090 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1565 1091 {
nickjillings@1565 1092 if (i != id) {
nickjillings@1360 1093 this.audioObjects[i].stop(setTime);
nickjillings@1565 1094 } else if (i == id) {
nickjillings@1360 1095 this.audioObjects[id].play(setTime);
nickjillings@1565 1096 }
nickjillings@1565 1097 }
nickjillings@1620 1098 }
nickjillings@1567 1099 interfaceContext.playhead.start();
nickjillings@1620 1100 }
nickjillings@1620 1101 };
nickjillings@1682 1102
nickjillings@1620 1103 this.stop = function() {
nickjillings@1378 1104 // Send stop and reset command to all playback buffers
nickjillings@1620 1105 if (this.status == 1) {
nickjillings@1360 1106 var setTime = audioContext.currentTime+0.1;
nickjillings@1620 1107 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1620 1108 {
nickjillings@1360 1109 this.audioObjects[i].stop(setTime);
nickjillings@1620 1110 }
nickjillings@1567 1111 interfaceContext.playhead.stop();
nickjillings@1620 1112 }
nickjillings@1620 1113 };
nickjillings@1689 1114
nickjillings@1582 1115 this.newTrack = function(element) {
nickjillings@1682 1116 // Pull data from given URL into new audio buffer
nickjillings@1682 1117 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1688 1118
nickjillings@1682 1119 // Create the audioObject with ID of the new track length;
nickjillings@1659 1120 audioObjectId = this.audioObjects.length;
nickjillings@1682 1121 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1688 1122
nickjillings@1410 1123 // Check if audioObject buffer is currently stored by full URL
nickjillings@1324 1124 var URL = testState.currentStateMap.hostURL + element.url;
nickjillings@1410 1125 var buffer = null;
nickjillings@1410 1126 for (var i=0; i<this.buffers.length; i++)
nickjillings@1410 1127 {
nickjillings@1410 1128 if (URL == this.buffers[i].url)
nickjillings@1410 1129 {
nickjillings@1410 1130 buffer = this.buffers[i];
nickjillings@1410 1131 break;
nickjillings@1410 1132 }
nickjillings@1410 1133 }
nickjillings@1410 1134 if (buffer == null)
nickjillings@1410 1135 {
nickjillings@1426 1136 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL);
nickjillings@1430 1137 buffer = new this.bufferObj();
nickjillings@1316 1138 this.buffers.push(buffer);
nickjillings@1430 1139 buffer.getMedia(URL);
nickjillings@1410 1140 }
nickjillings@1582 1141 this.audioObjects[audioObjectId].specification = element;
nickjillings@1426 1142 this.audioObjects[audioObjectId].url = URL;
nickjillings@1324 1143 // Obtain store node
nickjillings@1324 1144 var aeNodes = this.pageStore.XMLDOM.getElementsByTagName('audioelement');
nickjillings@1324 1145 for (var i=0; i<aeNodes.length; i++)
nickjillings@1410 1146 {
nickjillings@1324 1147 if(aeNodes[i].id == element.id)
nickjillings@1324 1148 {
nickjillings@1324 1149 this.audioObjects[audioObjectId].storeDOM = aeNodes[i];
nickjillings@1324 1150 break;
nickjillings@1324 1151 }
nickjillings@1410 1152 }
nickjillings@1316 1153 buffer.registerAudioObject(this.audioObjects[audioObjectId]);
nickjillings@1579 1154 return this.audioObjects[audioObjectId];
nickjillings@1697 1155 };
nickjillings@1682 1156
nickjillings@1369 1157 this.newTestPage = function(audioHolderObject,store) {
nickjillings@1324 1158 this.pageStore = store;
nickjillings@1620 1159 this.state = 0;
nickjillings@1620 1160 this.audioObjectsReady = false;
nickjillings@1620 1161 this.metric.reset();
nickjillings@1410 1162 for (var i=0; i < this.buffers.length; i++)
nickjillings@1410 1163 {
nickjillings@1410 1164 this.buffers[i].users = [];
nickjillings@1410 1165 }
nickjillings@1620 1166 this.audioObjects = [];
nickjillings@1369 1167 this.timer = new timer();
nickjillings@1369 1168 this.loopPlayback = audioHolderObject.loop;
nickjillings@1620 1169 };
nickjillings@1620 1170
nickjillings@1614 1171 this.checkAllPlayed = function() {
nickjillings@1614 1172 arr = [];
nickjillings@1614 1173 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@2008 1174 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1614 1175 arr.push(this.audioObjects[id].id);
nickjillings@1614 1176 }
nickjillings@1614 1177 }
nickjillings@1614 1178 return arr;
nickjillings@1614 1179 };
nickjillings@1614 1180
nickjillings@1620 1181 this.checkAllReady = function() {
nickjillings@1620 1182 var ready = true;
nickjillings@1620 1183 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1620 1184 if (this.audioObjects[i].state == 0) {
nickjillings@1620 1185 // Track not ready
nickjillings@1620 1186 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1620 1187 ready = false;
nickjillings@1620 1188 };
nickjillings@1620 1189 }
nickjillings@1620 1190 return ready;
nickjillings@1620 1191 };
nickjillings@1620 1192
nickjillings@1535 1193 this.setSynchronousLoop = function() {
nickjillings@1535 1194 // Pads the signals so they are all exactly the same length
nickjillings@1523 1195 var length = 0;
nickjillings@1523 1196 var maxId;
nickjillings@1523 1197 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1535 1198 {
nickjillings@1413 1199 if (length < this.audioObjects[i].buffer.buffer.length)
nickjillings@1535 1200 {
nickjillings@1413 1201 length = this.audioObjects[i].buffer.buffer.length;
nickjillings@1523 1202 maxId = i;
nickjillings@1535 1203 }
nickjillings@1523 1204 }
nickjillings@1404 1205 // Extract the audio and zero-pad
nickjillings@1430 1206 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1523 1207 {
nickjillings@1413 1208 var orig = this.audioObjects[i].buffer.buffer;
nickjillings@1523 1209 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
nickjillings@1523 1210 for (var c=0; c<orig.numberOfChannels; c++)
nickjillings@1535 1211 {
nickjillings@1523 1212 var inData = hold.getChannelData(c);
nickjillings@1523 1213 var outData = orig.getChannelData(c);
nickjillings@1523 1214 for (var n=0; n<orig.length; n++)
nickjillings@1523 1215 {inData[n] = outData[n];}
nickjillings@1535 1216 }
nickjillings@1320 1217 hold.playbackGain = orig.playbackGain;
nickjillings@1430 1218 hold.lufs = orig.lufs;
nickjillings@1413 1219 this.audioObjects[i].buffer.buffer = hold;
nickjillings@1535 1220 }
nickjillings@1535 1221 };
nickjillings@1370 1222
nickjillings@1370 1223 this.exportXML = function()
nickjillings@1370 1224 {
nickjillings@1370 1225
nickjillings@1370 1226 };
nickjillings@1535 1227
nickjillings@1682 1228 }
nickjillings@1682 1229
nickjillings@1682 1230 function audioObject(id) {
nickjillings@1682 1231 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1682 1232
nickjillings@1582 1233 this.specification;
nickjillings@1682 1234 this.id = id;
nickjillings@1682 1235 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1704 1236 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1602 1237 this.metric = new metricTracker(this);
nickjillings@1324 1238 this.storeDOM = null;
nickjillings@1682 1239
nickjillings@1577 1240 // Bindings for GUI
nickjillings@1583 1241 this.interfaceDOM = null;
nickjillings@1577 1242 this.commentDOM = null;
nickjillings@1577 1243
nickjillings@1682 1244 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1667 1245 this.bufferNode = undefined;
nickjillings@1682 1246 this.outputGain = audioContext.createGain();
nickjillings@1682 1247
nickjillings@1324 1248 this.onplayGain = 1.0;
nickjillings@1689 1249
nickjillings@1682 1250 // Connect buffer to the audio graph
nickjillings@1682 1251 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1682 1252
nickjillings@1682 1253 // the audiobuffer is not designed for multi-start playback
nickjillings@1682 1254 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1682 1255 this.buffer;
nickjillings@1434 1256
nickjillings@1434 1257 this.bufferLoaded = function(callee)
nickjillings@1434 1258 {
nickjillings@1434 1259 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the
nickjillings@1434 1260 // audioObject and trigger the interfaceDOM.enable() function for user feedback
nickjillings@1434 1261 if (audioEngineContext.loopPlayback){
nickjillings@1434 1262 // First copy the buffer into this.buffer
nickjillings@1434 1263 this.buffer = new audioEngineContext.bufferObj();
nickjillings@1434 1264 this.buffer.url = callee.url;
nickjillings@1434 1265 this.buffer.buffer = audioContext.createBuffer(callee.buffer.numberOfChannels, callee.buffer.length, callee.buffer.sampleRate);
nickjillings@1434 1266 for (var c=0; c<callee.buffer.numberOfChannels; c++)
nickjillings@1434 1267 {
nickjillings@1434 1268 var src = callee.buffer.getChannelData(c);
nickjillings@1434 1269 var dst = this.buffer.buffer.getChannelData(c);
nickjillings@1434 1270 for (var n=0; n<src.length; n++)
nickjillings@1434 1271 {
nickjillings@1434 1272 dst[n] = src[n];
nickjillings@1434 1273 }
nickjillings@1434 1274 }
nickjillings@1434 1275 } else {
nickjillings@1434 1276 this.buffer = callee;
nickjillings@1434 1277 }
nickjillings@1434 1278 this.state = 1;
nickjillings@1320 1279 this.buffer.buffer.playbackGain = callee.buffer.playbackGain;
nickjillings@1434 1280 this.buffer.buffer.lufs = callee.buffer.lufs;
nickjillings@1348 1281 var targetLUFS = this.specification.parent.loudness || specification.loudness;
nickjillings@1434 1282 if (typeof targetLUFS === "number")
nickjillings@1434 1283 {
nickjillings@1320 1284 this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
nickjillings@1434 1285 } else {
nickjillings@1320 1286 this.buffer.buffer.playbackGain = 1.0;
nickjillings@1434 1287 }
nickjillings@1434 1288 if (this.interfaceDOM != null) {
nickjillings@1434 1289 this.interfaceDOM.enable();
nickjillings@1434 1290 }
nickjillings@1324 1291 this.onplayGain = decibelToLinear(this.specification.gain)*this.buffer.buffer.playbackGain;
nickjillings@1324 1292 this.storeDOM.setAttribute('playGain',linearToDecibel(this.onplayGain));
nickjillings@1318 1293 };
nickjillings@1325 1294
nickjillings@1325 1295 this.bindInterface = function(interfaceObject)
nickjillings@1325 1296 {
nickjillings@1325 1297 this.interfaceDOM = interfaceObject;
nickjillings@1325 1298 this.metric.initialise(interfaceObject.getValue());
nickjillings@1325 1299 if (this.state == 1)
nickjillings@1325 1300 {
nickjillings@1325 1301 this.interfaceDOM.enable();
nickjillings@1325 1302 }
nickjillings@1338 1303 this.storeDOM.setAttribute('presentedId',interfaceObject.getPresentedId());
nickjillings@1434 1304 };
b@1639 1305
nickjillings@1360 1306 this.loopStart = function(setTime) {
nickjillings@1360 1307 this.outputGain.gain.linearRampToValueAtTime(this.onplayGain,setTime);
nickjillings@1637 1308 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1360 1309 this.interfaceDOM.startPlayback();
nickjillings@1577 1310 };
nickjillings@1637 1311
nickjillings@1360 1312 this.loopStop = function(setTime) {
nickjillings@1637 1313 if (this.outputGain.gain.value != 0.0) {
nickjillings@1360 1314 this.outputGain.gain.linearRampToValueAtTime(0.0,setTime);
nickjillings@1637 1315 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1637 1316 }
nickjillings@1360 1317 this.interfaceDOM.stopPlayback();
nickjillings@1577 1318 };
nickjillings@1637 1319
nickjillings@1682 1320 this.play = function(startTime) {
nickjillings@1410 1321 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
nickjillings@1565 1322 this.bufferNode = audioContext.createBufferSource();
nickjillings@1565 1323 this.bufferNode.owner = this;
nickjillings@1565 1324 this.bufferNode.connect(this.outputGain);
nickjillings@1410 1325 this.bufferNode.buffer = this.buffer.buffer;
nickjillings@1565 1326 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1522 1327 this.bufferNode.onended = function(event) {
nickjillings@1565 1328 // Safari does not like using 'this' to reference the calling object!
nickjillings@1458 1329 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
nickjillings@1360 1330 event.currentTarget.owner.stop(audioContext.currentTime+1);
nickjillings@1565 1331 };
nickjillings@1565 1332 if (this.bufferNode.loop == false) {
nickjillings@1565 1333 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1360 1334 this.outputGain.gain.setValueAtTime(this.onplayGain,startTime);
nickjillings@1360 1335 this.interfaceDOM.startPlayback();
nickjillings@1360 1336 } else {
nickjillings@1360 1337 this.outputGain.gain.setValueAtTime(0.0,startTime);
nickjillings@1360 1338 }
nickjillings@1565 1339 this.bufferNode.start(startTime);
nickjillings@2101 1340 this.bufferNode.playbackStartTime = startTime;
nickjillings@1617 1341 }
nickjillings@1697 1342 };
nickjillings@1682 1343
nickjillings@1360 1344 this.stop = function(stopTime) {
nickjillings@1360 1345 this.outputGain.gain.cancelScheduledValues(audioContext.currentTime);
nickjillings@2002 1346 if (this.bufferNode != undefined)
nickjillings@2002 1347 {
nickjillings@1566 1348 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
nickjillings@1360 1349 this.bufferNode.stop(stopTime);
nickjillings@2002 1350 this.bufferNode = undefined;
nickjillings@2002 1351 }
nickjillings@1360 1352 this.outputGain.gain.value = 0.0;
nickjillings@1360 1353 this.interfaceDOM.stopPlayback();
nickjillings@1697 1354 };
nickjillings@2019 1355
nickjillings@2019 1356 this.getCurrentPosition = function() {
nickjillings@2019 1357 var time = audioEngineContext.timer.getTestTime();
nickjillings@2019 1358 if (this.bufferNode != undefined) {
nickjillings@2101 1359 return (time - this.bufferNode.playbackStartTime)%this.buffer.buffer.duration;
nickjillings@2019 1360 } else {
nickjillings@2019 1361 return 0;
nickjillings@2019 1362 }
nickjillings@2019 1363 };
nickjillings@1688 1364
nickjillings@1583 1365 this.exportXMLDOM = function() {
nickjillings@1324 1366 var file = storage.document.createElement('file');
nickjillings@1416 1367 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
nickjillings@1416 1368 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
nickjillings@1416 1369 file.setAttribute('sampleCount',this.buffer.buffer.length);
nickjillings@1416 1370 file.setAttribute('duration',this.buffer.buffer.duration);
nickjillings@1324 1371 this.storeDOM.appendChild(file);
nickjillings@1324 1372 if (this.specification.type != 'outside-reference') {
nickjillings@1413 1373 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
nickjillings@1340 1374 if (interfaceXML != null)
nickjillings@1340 1375 {
nickjillings@1340 1376 if (interfaceXML.length == undefined) {
nickjillings@1340 1377 this.storeDOM.appendChild(interfaceXML);
nickjillings@1340 1378 } else {
nickjillings@1340 1379 for (var i=0; i<interfaceXML.length; i++)
nickjillings@1340 1380 {
nickjillings@1340 1381 this.storeDOM.appendChild(interfaceXML[i]);
nickjillings@1340 1382 }
nickjillings@1413 1383 }
nickjillings@1413 1384 }
nickjillings@1330 1385 if (this.commentDOM != null) {
nickjillings@1330 1386 this.storeDOM.appendChild(this.commentDOM.exportXMLDOM(this));
nickjillings@1529 1387 }
nickjillings@2050 1388 }
nickjillings@1324 1389 var nodes = this.metric.exportXMLDOM();
nickjillings@1324 1390 var mroot = this.storeDOM.getElementsByTagName('metric')[0];
nickjillings@1324 1391 for (var i=0; i<nodes.length; i++)
nickjillings@1324 1392 {
nickjillings@1324 1393 mroot.appendChild(nodes[i]);
nickjillings@1324 1394 }
nickjillings@1583 1395 };
nickjillings@1659 1396 }
nickjillings@1659 1397
nickjillings@1659 1398 function timer()
nickjillings@1659 1399 {
nickjillings@1659 1400 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1659 1401 * Uses the timer of the web audio API, so sample resolution
nickjillings@1659 1402 */
nickjillings@1659 1403 this.testStarted = false;
nickjillings@1659 1404 this.testStartTime = 0;
nickjillings@1659 1405 this.testDuration = 0;
nickjillings@1659 1406 this.minimumTestTime = 0; // No minimum test time
nickjillings@1659 1407 this.startTest = function()
nickjillings@1659 1408 {
nickjillings@1659 1409 if (this.testStarted == false)
nickjillings@1659 1410 {
nickjillings@1659 1411 this.testStartTime = audioContext.currentTime;
nickjillings@1659 1412 this.testStarted = true;
nickjillings@1659 1413 this.updateTestTime();
nickjillings@1662 1414 audioEngineContext.metric.initialiseTest();
nickjillings@1659 1415 }
nickjillings@1659 1416 };
nickjillings@1659 1417 this.stopTest = function()
nickjillings@1659 1418 {
nickjillings@1659 1419 if (this.testStarted)
nickjillings@1659 1420 {
nickjillings@1659 1421 this.testDuration = this.getTestTime();
nickjillings@1659 1422 this.testStarted = false;
nickjillings@1659 1423 } else {
nickjillings@1659 1424 console.log('ERR: Test tried to end before beginning');
nickjillings@1659 1425 }
nickjillings@1659 1426 };
nickjillings@1659 1427 this.updateTestTime = function()
nickjillings@1659 1428 {
nickjillings@1659 1429 if (this.testStarted)
nickjillings@1659 1430 {
nickjillings@1659 1431 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1659 1432 }
nickjillings@1659 1433 };
nickjillings@1659 1434 this.getTestTime = function()
nickjillings@1659 1435 {
nickjillings@1659 1436 this.updateTestTime();
nickjillings@1659 1437 return this.testDuration;
nickjillings@1659 1438 };
nickjillings@1659 1439 }
nickjillings@1659 1440
nickjillings@1408 1441 function sessionMetrics(engine,specification)
nickjillings@1659 1442 {
nickjillings@1659 1443 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1659 1444 */
nickjillings@1659 1445 this.engine = engine;
nickjillings@1659 1446 this.lastClicked = -1;
nickjillings@1659 1447 this.data = -1;
nickjillings@1620 1448 this.reset = function() {
nickjillings@1620 1449 this.lastClicked = -1;
nickjillings@1620 1450 this.data = -1;
nickjillings@1620 1451 };
nickjillings@1408 1452
nickjillings@1408 1453 this.enableElementInitialPosition = false;
nickjillings@1408 1454 this.enableElementListenTracker = false;
nickjillings@1408 1455 this.enableElementTimer = false;
nickjillings@1408 1456 this.enableElementTracker = false;
nickjillings@1408 1457 this.enableFlagListenedTo = false;
nickjillings@1408 1458 this.enableFlagMoved = false;
nickjillings@1408 1459 this.enableTestTimer = false;
nickjillings@1408 1460 // Obtain the metrics enabled
nickjillings@1324 1461 for (var i=0; i<specification.metrics.enabled.length; i++)
nickjillings@1408 1462 {
nickjillings@1324 1463 var node = specification.metrics.enabled[i];
nickjillings@1324 1464 switch(node)
nickjillings@1408 1465 {
nickjillings@1408 1466 case 'testTimer':
nickjillings@1408 1467 this.enableTestTimer = true;
nickjillings@1408 1468 break;
nickjillings@1408 1469 case 'elementTimer':
nickjillings@1408 1470 this.enableElementTimer = true;
nickjillings@1408 1471 break;
nickjillings@1408 1472 case 'elementTracker':
nickjillings@1408 1473 this.enableElementTracker = true;
nickjillings@1408 1474 break;
nickjillings@1408 1475 case 'elementListenTracker':
nickjillings@1408 1476 this.enableElementListenTracker = true;
nickjillings@1408 1477 break;
nickjillings@1408 1478 case 'elementInitialPosition':
nickjillings@1408 1479 this.enableElementInitialPosition = true;
nickjillings@1408 1480 break;
nickjillings@1408 1481 case 'elementFlagListenedTo':
nickjillings@1408 1482 this.enableFlagListenedTo = true;
nickjillings@1408 1483 break;
nickjillings@1408 1484 case 'elementFlagMoved':
nickjillings@1408 1485 this.enableFlagMoved = true;
nickjillings@1408 1486 break;
nickjillings@1408 1487 case 'elementFlagComments':
nickjillings@1408 1488 this.enableFlagComments = true;
nickjillings@1408 1489 break;
nickjillings@1408 1490 }
nickjillings@1408 1491 }
nickjillings@1662 1492 this.initialiseTest = function(){};
nickjillings@1659 1493 }
nickjillings@1659 1494
nickjillings@1602 1495 function metricTracker(caller)
nickjillings@1659 1496 {
nickjillings@1659 1497 /* Custom object to track and collect metric data
nickjillings@1659 1498 * Used only inside the audioObjects object.
nickjillings@1659 1499 */
nickjillings@1659 1500
nickjillings@1659 1501 this.listenedTimer = 0;
nickjillings@1659 1502 this.listenStart = 0;
nickjillings@1617 1503 this.listenHold = false;
nickjillings@1661 1504 this.initialPosition = -1;
nickjillings@1659 1505 this.movementTracker = [];
nickjillings@2019 1506 this.listenTracker =[];
nickjillings@1659 1507 this.wasListenedTo = false;
nickjillings@1659 1508 this.wasMoved = false;
nickjillings@1659 1509 this.hasComments = false;
nickjillings@1602 1510 this.parent = caller;
nickjillings@1659 1511
nickjillings@1324 1512 this.initialise = function(position)
nickjillings@1659 1513 {
nickjillings@1661 1514 if (this.initialPosition == -1) {
nickjillings@1661 1515 this.initialPosition = position;
nickjillings@1325 1516 this.moved(0,position);
nickjillings@1661 1517 }
nickjillings@1659 1518 };
nickjillings@1659 1519
nickjillings@1659 1520 this.moved = function(time,position)
nickjillings@1659 1521 {
nickjillings@1325 1522 if (time > 0) {this.wasMoved = true;}
nickjillings@1659 1523 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1659 1524 };
nickjillings@1659 1525
nickjillings@1637 1526 this.startListening = function(time)
nickjillings@1659 1527 {
nickjillings@1617 1528 if (this.listenHold == false)
nickjillings@1659 1529 {
nickjillings@1659 1530 this.wasListenedTo = true;
nickjillings@1659 1531 this.listenStart = time;
nickjillings@1617 1532 this.listenHold = true;
nickjillings@2019 1533
nickjillings@2019 1534 var evnt = document.createElement('event');
nickjillings@2019 1535 var testTime = document.createElement('testTime');
nickjillings@2019 1536 testTime.setAttribute('start',time);
nickjillings@2019 1537 var bufferTime = document.createElement('bufferTime');
nickjillings@2019 1538 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@2019 1539 evnt.appendChild(testTime);
nickjillings@2019 1540 evnt.appendChild(bufferTime);
nickjillings@2019 1541 this.listenTracker.push(evnt);
nickjillings@2019 1542
nickjillings@1602 1543 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1602 1544 }
nickjillings@1602 1545 };
nickjillings@1637 1546
nickjillings@1566 1547 this.stopListening = function(time,bufferStopTime)
nickjillings@1637 1548 {
nickjillings@1637 1549 if (this.listenHold == true)
nickjillings@1637 1550 {
nickjillings@2019 1551 var diff = time - this.listenStart;
nickjillings@2019 1552 this.listenedTimer += (diff);
nickjillings@1659 1553 this.listenStart = 0;
nickjillings@1617 1554 this.listenHold = false;
nickjillings@2019 1555
nickjillings@2019 1556 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@2019 1557 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@2019 1558 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@2019 1559 testTime.setAttribute('stop',time);
nickjillings@1566 1560 if (bufferStopTime == undefined) {
nickjillings@1566 1561 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1566 1562 } else {
nickjillings@1566 1563 bufferTime.setAttribute('stop',bufferStopTime);
nickjillings@1566 1564 }
nickjillings@2019 1565 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1659 1566 }
nickjillings@1659 1567 };
nickjillings@1577 1568
nickjillings@1577 1569 this.exportXMLDOM = function() {
nickjillings@1324 1570 var storeDOM = [];
nickjillings@1577 1571 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1324 1572 var mElementTimer = storage.document.createElement('metricresult');
nickjillings@1577 1573 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1577 1574 mElementTimer.textContent = this.listenedTimer;
nickjillings@1324 1575 storeDOM.push(mElementTimer);
nickjillings@1577 1576 }
nickjillings@1577 1577 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1324 1578 var elementTrackerFull = storage.document.createElement('metricResult');
nickjillings@1577 1579 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1577 1580 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1577 1581 {
nickjillings@1324 1582 var timePos = storage.document.createElement('timePos');
nickjillings@1577 1583 timePos.id = k;
nickjillings@1324 1584 var time = storage.document.createElement('time');
nickjillings@1577 1585 time.textContent = this.movementTracker[k][0];
nickjillings@1577 1586 var position = document.createElement('position');
nickjillings@1577 1587 position.textContent = this.movementTracker[k][1];
nickjillings@1577 1588 timePos.appendChild(time);
nickjillings@1577 1589 timePos.appendChild(position);
nickjillings@1577 1590 elementTrackerFull.appendChild(timePos);
nickjillings@1577 1591 }
nickjillings@1324 1592 storeDOM.push(elementTrackerFull);
nickjillings@1577 1593 }
nickjillings@1577 1594 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1324 1595 var elementListenTracker = storage.document.createElement('metricResult');
nickjillings@1577 1596 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1577 1597 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1577 1598 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1577 1599 }
nickjillings@1324 1600 storeDOM.push(elementListenTracker);
nickjillings@1577 1601 }
nickjillings@1577 1602 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1324 1603 var elementInitial = storage.document.createElement('metricResult');
nickjillings@1577 1604 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1577 1605 elementInitial.textContent = this.initialPosition;
nickjillings@1324 1606 storeDOM.push(elementInitial);
nickjillings@1577 1607 }
nickjillings@1577 1608 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1324 1609 var flagListenedTo = storage.document.createElement('metricResult');
nickjillings@1577 1610 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1577 1611 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1324 1612 storeDOM.push(flagListenedTo);
nickjillings@1577 1613 }
nickjillings@1577 1614 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1324 1615 var flagMoved = storage.document.createElement('metricResult');
nickjillings@1577 1616 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1577 1617 flagMoved.textContent = this.wasMoved;
nickjillings@1324 1618 storeDOM.push(flagMoved);
nickjillings@1577 1619 }
nickjillings@1577 1620 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1324 1621 var flagComments = storage.document.createElement('metricResult');
nickjillings@1577 1622 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1577 1623 if (this.parent.commentDOM == null)
nickjillings@1577 1624 {flag.textContent = 'false';}
nickjillings@1577 1625 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1577 1626 {flag.textContent = 'false';}
nickjillings@1577 1627 else
nickjillings@1577 1628 {flag.textContet = 'true';}
nickjillings@1324 1629 storeDOM.push(flagComments);
nickjillings@1577 1630 }
nickjillings@1324 1631 return storeDOM;
nickjillings@1577 1632 };
nickjillings@1664 1633 }
nickjillings@1664 1634
nickjillings@1664 1635 function randomiseOrder(input)
nickjillings@1664 1636 {
nickjillings@1664 1637 // This takes an array of information and randomises the order
nickjillings@1664 1638 var N = input.length;
b@2037 1639
b@2037 1640 var inputSequence = []; // For safety purposes: keep track of randomisation
b@2037 1641 for (var counter = 0; counter < N; ++counter)
b@2037 1642 inputSequence.push(counter) // Fill array
b@2037 1643 var inputSequenceClone = inputSequence.slice(0);
b@2037 1644
nickjillings@1664 1645 var holdArr = [];
b@2037 1646 var outputSequence = [];
nickjillings@1664 1647 for (var n=0; n<N; n++)
nickjillings@1664 1648 {
nickjillings@1664 1649 // First pick a random number
nickjillings@1664 1650 var r = Math.random();
nickjillings@1664 1651 // Multiply and floor by the number of elements left
nickjillings@1664 1652 r = Math.floor(r*input.length);
nickjillings@1664 1653 // Pick out that element and delete from the array
nickjillings@1664 1654 holdArr.push(input.splice(r,1)[0]);
b@2037 1655 // Do the same with sequence
b@2037 1656 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1664 1657 }
b@2037 1658 console.log(inputSequenceClone.toString()); // print original array to console
b@2037 1659 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1664 1660 return holdArr;
nickjillings@1631 1661 }
nickjillings@1631 1662
nickjillings@1631 1663 function returnDateNode()
nickjillings@1631 1664 {
nickjillings@1631 1665 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1631 1666 // Structure is
nickjillings@1631 1667 // <datetime>
nickjillings@1631 1668 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1631 1669 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1631 1670 // </datetime>
nickjillings@1631 1671 var dateTime = new Date();
nickjillings@1631 1672 var year = document.createAttribute('year');
nickjillings@1631 1673 var month = document.createAttribute('month');
nickjillings@1631 1674 var day = document.createAttribute('day');
nickjillings@1631 1675 var hour = document.createAttribute('hour');
nickjillings@1631 1676 var minute = document.createAttribute('minute');
nickjillings@1631 1677 var secs = document.createAttribute('secs');
nickjillings@1631 1678
nickjillings@1631 1679 year.nodeValue = dateTime.getFullYear();
nickjillings@1631 1680 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1631 1681 day.nodeValue = dateTime.getDate();
nickjillings@1631 1682 hour.nodeValue = dateTime.getHours();
nickjillings@1631 1683 minute.nodeValue = dateTime.getMinutes();
nickjillings@1631 1684 secs.nodeValue = dateTime.getSeconds();
nickjillings@1631 1685
nickjillings@1631 1686 var hold = document.createElement("datetime");
nickjillings@1631 1687 var date = document.createElement("date");
nickjillings@1631 1688 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1631 1689 var time = document.createElement("time");
nickjillings@1631 1690 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1631 1691
nickjillings@1631 1692 date.setAttributeNode(year);
nickjillings@1631 1693 date.setAttributeNode(month);
nickjillings@1631 1694 date.setAttributeNode(day);
nickjillings@1631 1695 time.setAttributeNode(hour);
nickjillings@1631 1696 time.setAttributeNode(minute);
nickjillings@1631 1697 time.setAttributeNode(secs);
nickjillings@1631 1698
nickjillings@1631 1699 hold.appendChild(date);
nickjillings@1631 1700 hold.appendChild(time);
nickjillings@1408 1701 return hold;
nickjillings@1631 1702
nickjillings@1604 1703 }
nickjillings@1604 1704
nickjillings@1580 1705 function Specification() {
nickjillings@1580 1706 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1580 1707
nickjillings@1324 1708 this.interface = null;
nickjillings@1373 1709 this.projectReturn = "null";
nickjillings@1324 1710 this.randomiseOrder = null;
nickjillings@1324 1711 this.testPages = null;
nickjillings@1324 1712 this.pages = [];
nickjillings@1324 1713 this.metrics = null;
nickjillings@1324 1714 this.interfaces = null;
nickjillings@1324 1715 this.loudness = null;
nickjillings@1324 1716 this.errors = [];
nickjillings@1324 1717 this.schema = null;
nickjillings@1318 1718
nickjillings@1324 1719 this.processAttribute = function(attribute,schema)
nickjillings@1406 1720 {
nickjillings@1324 1721 // attribute is the string returned from getAttribute on the XML
nickjillings@1324 1722 // schema is the <xs:attribute> node
nickjillings@1324 1723 if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined)
nickjillings@1406 1724 {
nickjillings@1348 1725 schema = this.schema.getAllElementsByName(schema.getAttribute('ref'))[0];
nickjillings@1324 1726 }
nickjillings@1324 1727 var defaultOpt = schema.getAttribute('default');
nickjillings@1324 1728 if (attribute == null) {
nickjillings@1324 1729 attribute = defaultOpt;
nickjillings@1324 1730 }
nickjillings@1324 1731 var dataType = schema.getAttribute('type');
nickjillings@1324 1732 if (typeof dataType == "string") { dataType = dataType.substr(3);}
nickjillings@1324 1733 else {dataType = "string";}
nickjillings@1324 1734 if (attribute == null)
nickjillings@1324 1735 {
nickjillings@1324 1736 return attribute;
nickjillings@1324 1737 }
nickjillings@1324 1738 switch(dataType)
nickjillings@1324 1739 {
nickjillings@1324 1740 case "boolean":
nickjillings@1324 1741 if (attribute == 'true'){attribute = true;}else{attribute=false;}
nickjillings@1324 1742 break;
nickjillings@1324 1743 case "negativeInteger":
nickjillings@1324 1744 case "positiveInteger":
nickjillings@1324 1745 case "nonNegativeInteger":
nickjillings@1324 1746 case "nonPositiveInteger":
nickjillings@1324 1747 case "integer":
nickjillings@1324 1748 case "decimal":
nickjillings@1324 1749 case "short":
nickjillings@1324 1750 attribute = Number(attribute);
nickjillings@1324 1751 break;
nickjillings@1324 1752 case "string":
nickjillings@1324 1753 default:
nickjillings@1324 1754 attribute = String(attribute);
nickjillings@1324 1755 break;
nickjillings@1324 1756 }
nickjillings@1324 1757 return attribute;
nickjillings@1406 1758 };
nickjillings@1411 1759
nickjillings@1406 1760 this.decode = function(projectXML) {
nickjillings@1324 1761 this.errors = [];
nickjillings@1580 1762 // projectXML - DOM Parsed document
nickjillings@2052 1763 this.projectXML = projectXML.childNodes[0];
nickjillings@1580 1764 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1348 1765 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
nickjillings@1324 1766 // First decode the attributes
nickjillings@1348 1767 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
nickjillings@1324 1768 for (var i in attributes)
nickjillings@1520 1769 {
nickjillings@1324 1770 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1771 var attributeName = attributes[i].getAttribute('name');
nickjillings@1324 1772 var projectAttr = setupNode.getAttribute(attributeName);
nickjillings@1324 1773 projectAttr = this.processAttribute(projectAttr,attributes[i]);
nickjillings@1324 1774 switch(typeof projectAttr)
nickjillings@1432 1775 {
nickjillings@1324 1776 case "number":
nickjillings@1324 1777 case "boolean":
nickjillings@1324 1778 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 1779 break;
nickjillings@1324 1780 case "string":
nickjillings@1324 1781 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1782 break;
nickjillings@1432 1783 }
nickjillings@1324 1784
nickjillings@1406 1785 }
nickjillings@1406 1786
nickjillings@1370 1787 this.metrics = new this.metricNode();
nickjillings@1580 1788
nickjillings@1324 1789 this.metrics.decode(this,setupNode.getElementsByTagName('metric')[0]);
nickjillings@1324 1790
nickjillings@1324 1791 // Now process the survey node options
nickjillings@1324 1792 var survey = setupNode.getElementsByTagName('survey');
nickjillings@1324 1793 for (var i in survey) {
nickjillings@1324 1794 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 1795 var location = survey[i].getAttribute('location');
nickjillings@1324 1796 if (location == 'pre' || location == 'before')
nickjillings@1324 1797 {
nickjillings@1324 1798 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
nickjillings@1324 1799 else {
nickjillings@1324 1800 this.preTest = new this.surveyNode();
nickjillings@1370 1801 this.preTest.decode(this,survey[i]);
nickjillings@1324 1802 }
nickjillings@1324 1803 } else if (location == 'post' || location == 'after') {
nickjillings@1324 1804 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
nickjillings@1324 1805 else {
nickjillings@1324 1806 this.postTest = new this.surveyNode();
nickjillings@1370 1807 this.postTest.decode(this,survey[i]);
nickjillings@1324 1808 }
nickjillings@1580 1809 }
nickjillings@1580 1810 }
nickjillings@1580 1811
nickjillings@1324 1812 var interfaceNode = setupNode.getElementsByTagName('interface');
nickjillings@1324 1813 if (interfaceNode.length > 1)
nickjillings@1324 1814 {
nickjillings@1324 1815 this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
nickjillings@1324 1816 }
nickjillings@1324 1817 this.interfaces = new this.interfaceNode();
nickjillings@1324 1818 if (interfaceNode.length != 0)
nickjillings@1324 1819 {
nickjillings@1324 1820 interfaceNode = interfaceNode[0];
nickjillings@1348 1821 this.interfaces.decode(this,interfaceNode,this.schema.getAllElementsByName('interface')[1]);
nickjillings@1556 1822 }
nickjillings@1556 1823
nickjillings@1324 1824 // Page tags
nickjillings@1324 1825 var pageTags = projectXML.getElementsByTagName('page');
nickjillings@1348 1826 var pageSchema = this.schema.getAllElementsByName('page')[0];
nickjillings@1324 1827 for (var i=0; i<pageTags.length; i++)
nickjillings@1520 1828 {
nickjillings@1324 1829 var node = new this.page();
nickjillings@1324 1830 node.decode(this,pageTags[i],pageSchema);
nickjillings@1324 1831 this.pages.push(node);
nickjillings@1520 1832 }
nickjillings@1580 1833 };
nickjillings@1580 1834
nickjillings@1406 1835 this.encode = function()
nickjillings@1406 1836 {
nickjillings@1372 1837 var RootDocument = document.implementation.createDocument(null,"waet");
nickjillings@1372 1838 var root = RootDocument.children[0];
nickjillings@1372 1839 root.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
nickjillings@1372 1840 root.setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
nickjillings@1324 1841 // Build setup node
nickjillings@1372 1842 var setup = RootDocument.createElement("setup");
nickjillings@1372 1843 var schemaSetup = this.schema.getAllElementsByName('setup')[0];
nickjillings@1372 1844 // First decode the attributes
nickjillings@1372 1845 var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
nickjillings@1372 1846 for (var i=0; i<attributes.length; i++)
nickjillings@1372 1847 {
nickjillings@1372 1848 var name = attributes[i].getAttribute("name");
nickjillings@1372 1849 if (name == undefined) {
nickjillings@1372 1850 name = attributes[i].getAttribute("ref");
nickjillings@1372 1851 }
nickjillings@1372 1852 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
nickjillings@1372 1853 {
nickjillings@1372 1854 eval("setup.setAttribute('"+name+"',this."+name+")");
nickjillings@1372 1855 }
nickjillings@1372 1856 }
nickjillings@1372 1857 root.appendChild(setup);
nickjillings@1372 1858 // Survey node
nickjillings@1372 1859 setup.appendChild(this.preTest.encode(RootDocument));
nickjillings@1372 1860 setup.appendChild(this.postTest.encode(RootDocument));
nickjillings@1372 1861 setup.appendChild(this.metrics.encode(RootDocument));
nickjillings@1372 1862 setup.appendChild(this.interfaces.encode(RootDocument));
nickjillings@1372 1863 for (var page of this.pages)
nickjillings@1372 1864 {
nickjillings@1372 1865 root.appendChild(page.encode(RootDocument));
nickjillings@1372 1866 }
nickjillings@1372 1867 return RootDocument;
nickjillings@1406 1868 };
nickjillings@1406 1869
nickjillings@1324 1870 this.surveyNode = function() {
nickjillings@1324 1871 this.location = null;
nickjillings@1580 1872 this.options = [];
nickjillings@1370 1873 this.schema = specification.schema.getAllElementsByName('survey')[0];
nickjillings@1580 1874
nickjillings@1406 1875 this.OptionNode = function() {
nickjillings@1406 1876 this.type = undefined;
nickjillings@1370 1877 this.schema = specification.schema.getAllElementsByName('surveyentry')[0];
nickjillings@1406 1878 this.id = undefined;
nickjillings@1406 1879 this.mandatory = undefined;
nickjillings@1406 1880 this.statement = undefined;
nickjillings@1406 1881 this.boxsize = undefined;
nickjillings@1406 1882 this.options = [];
nickjillings@1406 1883 this.min = undefined;
nickjillings@1406 1884 this.max = undefined;
nickjillings@1406 1885 this.step = undefined;
nickjillings@1406 1886
nickjillings@1370 1887 this.decode = function(parent,child)
nickjillings@1406 1888 {
nickjillings@1370 1889 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nickjillings@1324 1890 for (var i in attributeMap){
nickjillings@1324 1891 if(isNaN(Number(i)) == true){break;}
nickjillings@1324 1892 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 1893 var projectAttr = child.getAttribute(attributeName);
nickjillings@1324 1894 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 1895 switch(typeof projectAttr)
nickjillings@1324 1896 {
nickjillings@1324 1897 case "number":
nickjillings@1324 1898 case "boolean":
nickjillings@1324 1899 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 1900 break;
nickjillings@1324 1901 case "string":
nickjillings@1324 1902 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 1903 break;
nickjillings@1406 1904 }
nickjillings@1324 1905 }
nickjillings@1324 1906 this.statement = child.getElementsByTagName('statement')[0].textContent;
nickjillings@1324 1907 if (this.type == "checkbox" || this.type == "radio") {
nickjillings@1324 1908 var children = child.getElementsByTagName('option');
nickjillings@1324 1909 if (children.length == null) {
nickjillings@1406 1910 console.log('Malformed' +child.nodeName+ 'entry');
nickjillings@1406 1911 this.statement = 'Malformed' +child.nodeName+ 'entry';
nickjillings@1406 1912 this.type = 'statement';
nickjillings@1406 1913 } else {
nickjillings@1406 1914 this.options = [];
nickjillings@1324 1915 for (var i in children)
nickjillings@1324 1916 {
nickjillings@1324 1917 if (isNaN(Number(i))==true){break;}
nickjillings@1324 1918 this.options.push({
nickjillings@1324 1919 name: children[i].getAttribute('name'),
nickjillings@1324 1920 text: children[i].textContent
nickjillings@1324 1921 });
nickjillings@1406 1922 }
nickjillings@1406 1923 }
nickjillings@2030 1924 }
nickjillings@1406 1925 };
nickjillings@1406 1926
nickjillings@1372 1927 this.exportXML = function(doc)
nickjillings@1406 1928 {
nickjillings@1372 1929 var node = doc.createElement('surveyelement');
nickjillings@1324 1930 node.setAttribute('type',this.type);
nickjillings@1372 1931 var statement = doc.createElement('statement');
nickjillings@1324 1932 statement.textContent = this.statement;
nickjillings@1324 1933 node.appendChild(statement);
nickjillings@1406 1934 switch(this.type)
nickjillings@1406 1935 {
nickjillings@1406 1936 case "statement":
nickjillings@1406 1937 break;
nickjillings@1406 1938 case "question":
nickjillings@1406 1939 node.id = this.id;
nickjillings@1406 1940 node.setAttribute("mandatory",this.mandatory);
nickjillings@1406 1941 node.setAttribute("boxsize",this.boxsize);
nickjillings@1406 1942 break;
nickjillings@1406 1943 case "number":
nickjillings@1406 1944 node.id = this.id;
nickjillings@1406 1945 node.setAttribute("mandatory",this.mandatory);
nickjillings@1406 1946 node.setAttribute("min", this.min);
nickjillings@1406 1947 node.setAttribute("max", this.max);
nickjillings@1406 1948 node.setAttribute("step", this.step);
nickjillings@1406 1949 break;
nickjillings@1406 1950 case "checkbox":
nickjillings@1318 1951 case "radio":
nickjillings@1406 1952 node.id = this.id;
nickjillings@1406 1953 for (var i=0; i<this.options.length; i++)
nickjillings@1406 1954 {
nickjillings@1406 1955 var option = this.options[i];
nickjillings@1372 1956 var optionNode = doc.createElement("option");
nickjillings@1406 1957 optionNode.setAttribute("name",option.name);
nickjillings@1406 1958 optionNode.textContent = option.text;
nickjillings@1406 1959 node.appendChild(optionNode);
nickjillings@1406 1960 }
nickjillings@1406 1961 break;
nickjillings@1588 1962 }
nickjillings@1406 1963 return node;
nickjillings@1406 1964 };
nickjillings@1406 1965 };
nickjillings@1370 1966 this.decode = function(parent,xml) {
nickjillings@1324 1967 this.location = xml.getAttribute('location');
nickjillings@1324 1968 if (this.location == 'before'){this.location = 'pre';}
nickjillings@1324 1969 else if (this.location == 'after'){this.location = 'post';}
nickjillings@1324 1970 for (var i in xml.children)
nickjillings@1324 1971 {
nickjillings@1324 1972 if(isNaN(Number(i))==true){break;}
nickjillings@1406 1973 var node = new this.OptionNode();
nickjillings@1370 1974 node.decode(parent,xml.children[i]);
nickjillings@1406 1975 this.options.push(node);
nickjillings@1324 1976 }
nickjillings@1324 1977 };
nickjillings@1372 1978 this.encode = function(doc) {
nickjillings@1372 1979 var node = doc.createElement('survey');
nickjillings@1324 1980 node.setAttribute('location',this.location);
nickjillings@1324 1981 for (var i=0; i<this.options.length; i++)
nickjillings@1324 1982 {
nickjillings@1372 1983 node.appendChild(this.options[i].exportXML(doc));
nickjillings@1324 1984 }
nickjillings@1324 1985 return node;
nickjillings@1324 1986 };
nickjillings@1324 1987 };
nickjillings@1324 1988
nickjillings@1324 1989 this.interfaceNode = function()
nickjillings@1324 1990 {
nickjillings@1324 1991 this.title = null;
nickjillings@1324 1992 this.name = null;
nickjillings@1324 1993 this.options = [];
nickjillings@1324 1994 this.scales = [];
nickjillings@1370 1995 this.schema = specification.schema.getAllElementsByName('interface')[1];
nickjillings@1324 1996
nickjillings@1370 1997 this.decode = function(parent,xml) {
nickjillings@1324 1998 this.name = xml.getAttribute('name');
nickjillings@1324 1999 var titleNode = xml.getElementsByTagName('title');
nickjillings@1324 2000 if (titleNode.length == 1)
nickjillings@1324 2001 {
nickjillings@1324 2002 this.title = titleNode[0].textContent;
nickjillings@1324 2003 }
nickjillings@1324 2004 var interfaceOptionNodes = xml.getElementsByTagName('interfaceoption');
nickjillings@1324 2005 // Extract interfaceoption node schema
nickjillings@1370 2006 var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
nickjillings@1348 2007 var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
nickjillings@1324 2008 for (var i=0; i<interfaceOptionNodes.length; i++)
nickjillings@1324 2009 {
nickjillings@1324 2010 var ioNode = interfaceOptionNodes[i];
nickjillings@1324 2011 var option = {};
nickjillings@1324 2012 for (var j=0; j<attributeMap.length; j++)
nickjillings@1324 2013 {
nickjillings@1324 2014 var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
nickjillings@1324 2015 var projectAttr = ioNode.getAttribute(attributeName);
nickjillings@1324 2016 projectAttr = parent.processAttribute(projectAttr,attributeMap[j]);
nickjillings@1324 2017 switch(typeof projectAttr)
nickjillings@1324 2018 {
nickjillings@1324 2019 case "number":
nickjillings@1324 2020 case "boolean":
nickjillings@1324 2021 eval('option.'+attributeName+' = '+projectAttr);
nickjillings@1324 2022 break;
nickjillings@1324 2023 case "string":
nickjillings@1324 2024 eval('option.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 2025 break;
nickjillings@1324 2026 }
nickjillings@1324 2027 }
nickjillings@1324 2028 this.options.push(option);
nickjillings@1324 2029 }
nickjillings@1324 2030
nickjillings@1324 2031 // Now the scales nodes
nickjillings@1324 2032 var scaleParent = xml.getElementsByTagName('scales');
nickjillings@1324 2033 if (scaleParent.length == 1) {
nickjillings@1324 2034 scaleParent = scaleParent[0];
nickjillings@1324 2035 for (var i=0; i<scaleParent.children.length; i++) {
nickjillings@1324 2036 var child = scaleParent.children[i];
nickjillings@1324 2037 this.scales.push({
nickjillings@1324 2038 text: child.textContent,
nickjillings@1324 2039 position: Number(child.getAttribute('position'))
nickjillings@1324 2040 });
nickjillings@1406 2041 }
nickjillings@1580 2042 }
nickjillings@1580 2043 };
nickjillings@1324 2044
nickjillings@1372 2045 this.encode = function(doc) {
nickjillings@1372 2046 var node = doc.createElement("interface");
nickjillings@1372 2047 if (typeof name == "string")
nickjillings@1372 2048 node.setAttribute("name",this.name);
nickjillings@1372 2049 for (var option of this.options)
nickjillings@1372 2050 {
nickjillings@1372 2051 var child = doc.createElement("interfaceoption");
nickjillings@1372 2052 child.setAttribute("type",option.type);
nickjillings@1372 2053 child.setAttribute("name",option.name);
nickjillings@1372 2054 node.appendChild(child);
nickjillings@1372 2055 }
nickjillings@1372 2056 if (this.scales.length != 0) {
nickjillings@1372 2057 var scales = doc.createElement("scales");
nickjillings@1372 2058 for (var scale of this.scales)
nickjillings@1372 2059 {
nickjillings@1372 2060 var child = doc.createElement("scalelabel");
nickjillings@1372 2061 child.setAttribute("position",scale.position);
nickjillings@1372 2062 child.textContent = scale.text;
nickjillings@1372 2063 scales.appendChild(child);
nickjillings@1372 2064 }
nickjillings@1372 2065 node.appendChild(scales);
nickjillings@1372 2066 }
nickjillings@1372 2067 return node;
nickjillings@1324 2068 };
nickjillings@1580 2069 };
nickjillings@1580 2070
nickjillings@1370 2071 this.metricNode = function() {
nickjillings@1370 2072 this.enabled = [];
nickjillings@1370 2073 this.decode = function(parent, xml) {
nickjillings@1370 2074 var children = xml.getElementsByTagName('metricenable');
nickjillings@1370 2075 for (var i in children) {
nickjillings@1370 2076 if (isNaN(Number(i)) == true){break;}
nickjillings@1370 2077 this.enabled.push(children[i].textContent);
nickjillings@1370 2078 }
nickjillings@1370 2079 }
nickjillings@1372 2080 this.encode = function(doc) {
nickjillings@1372 2081 var node = doc.createElement('metric');
nickjillings@1370 2082 for (var i in this.enabled)
nickjillings@1370 2083 {
nickjillings@1370 2084 if (isNaN(Number(i)) == true){break;}
nickjillings@1372 2085 var child = doc.createElement('metricenable');
nickjillings@1370 2086 child.textContent = this.enabled[i];
nickjillings@1370 2087 node.appendChild(child);
nickjillings@1370 2088 }
nickjillings@1370 2089 return node;
nickjillings@1370 2090 }
nickjillings@1370 2091 }
nickjillings@1370 2092
nickjillings@1324 2093 this.page = function() {
nickjillings@1406 2094 this.presentedId = undefined;
nickjillings@1406 2095 this.id = undefined;
nickjillings@1406 2096 this.hostURL = undefined;
nickjillings@1406 2097 this.randomiseOrder = undefined;
nickjillings@1406 2098 this.loop = undefined;
nickjillings@1324 2099 this.showElementComments = undefined;
nickjillings@1406 2100 this.outsideReference = null;
nickjillings@1432 2101 this.loudness = null;
nickjillings@1324 2102 this.preTest = null;
nickjillings@1324 2103 this.postTest = null;
nickjillings@1406 2104 this.interfaces = [];
nickjillings@1406 2105 this.commentBoxPrefix = "Comment on track";
nickjillings@1406 2106 this.audioElements = [];
nickjillings@1406 2107 this.commentQuestions = [];
nickjillings@1370 2108 this.schema = specification.schema.getAllElementsByName("page")[0];
nickjillings@1406 2109
nickjillings@1406 2110 this.decode = function(parent,xml)
nickjillings@1406 2111 {
nickjillings@1348 2112 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nickjillings@1324 2113 for (var i=0; i<attributeMap.length; i++)
nickjillings@1432 2114 {
nickjillings@1324 2115 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 2116 var projectAttr = xml.getAttribute(attributeName);
nickjillings@1324 2117 projectAttr = parent.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 2118 switch(typeof projectAttr)
nickjillings@1395 2119 {
nickjillings@1324 2120 case "number":
nickjillings@1324 2121 case "boolean":
nickjillings@1324 2122 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 2123 break;
nickjillings@1324 2124 case "string":
nickjillings@1324 2125 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 2126 break;
nickjillings@1406 2127 }
nickjillings@1406 2128 }
nickjillings@1406 2129
nickjillings@1324 2130 // Get the Comment Box Prefix
nickjillings@1324 2131 var CBP = xml.getElementsByTagName('commentboxprefix');
nickjillings@1324 2132 if (CBP.length != 0) {
nickjillings@1324 2133 this.commentBoxPrefix = CBP[0].textContent;
nickjillings@1441 2134 }
nickjillings@1441 2135
nickjillings@1324 2136 // Now decode the interfaces
nickjillings@1324 2137 var interfaceNode = xml.getElementsByTagName('interface');
nickjillings@1324 2138 for (var i=0; i<interfaceNode.length; i++)
nickjillings@1324 2139 {
nickjillings@1324 2140 var node = new parent.interfaceNode();
nickjillings@1348 2141 node.decode(this,interfaceNode[i],parent.schema.getAllElementsByName('interface')[1]);
nickjillings@1324 2142 this.interfaces.push(node);
nickjillings@1324 2143 }
nickjillings@1411 2144
nickjillings@1324 2145 // Now process the survey node options
nickjillings@1324 2146 var survey = xml.getElementsByTagName('survey');
nickjillings@1348 2147 var surveySchema = parent.schema.getAllElementsByName('survey')[0];
nickjillings@1324 2148 for (var i in survey) {
nickjillings@1324 2149 if (isNaN(Number(i)) == true){break;}
nickjillings@1324 2150 var location = survey[i].getAttribute('location');
nickjillings@1324 2151 if (location == 'pre' || location == 'before')
nickjillings@1324 2152 {
nickjillings@1324 2153 if (this.preTest != null){this.errors.push("Already a pre/before test survey defined! Ignoring second!!");}
nickjillings@1324 2154 else {
nickjillings@1324 2155 this.preTest = new parent.surveyNode();
nickjillings@1324 2156 this.preTest.decode(parent,survey[i],surveySchema);
nickjillings@1324 2157 }
nickjillings@1324 2158 } else if (location == 'post' || location == 'after') {
nickjillings@1324 2159 if (this.postTest != null){this.errors.push("Already a post/after test survey defined! Ignoring second!!");}
nickjillings@1324 2160 else {
nickjillings@1324 2161 this.postTest = new parent.surveyNode();
nickjillings@1324 2162 this.postTest.decode(parent,survey[i],surveySchema);
nickjillings@1324 2163 }
nickjillings@1324 2164 }
nickjillings@1324 2165 }
nickjillings@1324 2166
nickjillings@1324 2167 // Now process the audioelement tags
nickjillings@1324 2168 var audioElements = xml.getElementsByTagName('audioelement');
nickjillings@1324 2169 for (var i=0; i<audioElements.length; i++)
nickjillings@1324 2170 {
nickjillings@1324 2171 var node = new this.audioElementNode();
nickjillings@1370 2172 node.decode(this,audioElements[i]);
nickjillings@1324 2173 this.audioElements.push(node);
nickjillings@1324 2174 }
nickjillings@1324 2175
nickjillings@1324 2176 // Now decode the commentquestions
nickjillings@1324 2177 var commentQuestions = xml.getElementsByTagName('commentquestion');
nickjillings@1324 2178 for (var i=0; i<commentQuestions.length; i++)
nickjillings@1324 2179 {
nickjillings@1406 2180 var node = new this.commentQuestionNode();
nickjillings@1370 2181 node.decode(parent,commentQuestions[i]);
nickjillings@1406 2182 this.commentQuestions.push(node);
nickjillings@1580 2183 }
nickjillings@1580 2184 };
nickjillings@1580 2185
nickjillings@1406 2186 this.encode = function(root)
nickjillings@1406 2187 {
nickjillings@1372 2188 var AHNode = root.createElement("page");
nickjillings@1372 2189 // First decode the attributes
nickjillings@1372 2190 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nickjillings@1372 2191 for (var i=0; i<attributes.length; i++)
nickjillings@1372 2192 {
nickjillings@1372 2193 var name = attributes[i].getAttribute("name");
nickjillings@1372 2194 if (name == undefined) {
nickjillings@1372 2195 name = attributes[i].getAttribute("ref");
nickjillings@1372 2196 }
nickjillings@1372 2197 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
nickjillings@1372 2198 {
nickjillings@1372 2199 eval("AHNode.setAttribute('"+name+"',this."+name+")");
nickjillings@1372 2200 }
nickjillings@1372 2201 }
nickjillings@1432 2202 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nickjillings@1372 2203 // <commentboxprefix>
nickjillings@1372 2204 var commentboxprefix = root.createElement("commentboxprefix");
nickjillings@1372 2205 commentboxprefix.textContent = this.commentBoxPrefix;
nickjillings@1372 2206 AHNode.appendChild(commentboxprefix);
nickjillings@1372 2207
nickjillings@1406 2208 for (var i=0; i<this.interfaces.length; i++)
nickjillings@1404 2209 {
nickjillings@1406 2210 AHNode.appendChild(this.interfaces[i].encode(root));
nickjillings@1406 2211 }
nickjillings@1558 2212
nickjillings@1406 2213 for (var i=0; i<this.audioElements.length; i++) {
nickjillings@1406 2214 AHNode.appendChild(this.audioElements[i].encode(root));
nickjillings@1406 2215 }
nickjillings@1406 2216 // Create <CommentQuestion>
nickjillings@1406 2217 for (var i=0; i<this.commentQuestions.length; i++)
nickjillings@1455 2218 {
nickjillings@1372 2219 AHNode.appendChild(this.commentQuestions[i].encode(root));
nickjillings@1406 2220 }
nickjillings@1406 2221
nickjillings@1372 2222 AHNode.appendChild(this.preTest.encode(root));
nickjillings@1372 2223 AHNode.appendChild(this.postTest.encode(root));
nickjillings@1406 2224 return AHNode;
nickjillings@1406 2225 };
nickjillings@1406 2226
nickjillings@1324 2227 this.commentQuestionNode = function() {
nickjillings@1324 2228 this.id = null;
nickjillings@1324 2229 this.type = undefined;
nickjillings@1406 2230 this.options = [];
nickjillings@1324 2231 this.statement = undefined;
nickjillings@1370 2232 this.schema = specification.schema.getAllElementsByName('commentquestion')[0];
nickjillings@1370 2233 this.decode = function(parent,xml)
nickjillings@1406 2234 {
nickjillings@1324 2235 this.id = xml.id;
nickjillings@1324 2236 this.type = xml.getAttribute('type');
nickjillings@1324 2237 this.statement = xml.getElementsByTagName('statement')[0].textContent;
nickjillings@1324 2238 var optNodes = xml.getElementsByTagName('option');
nickjillings@1324 2239 for (var i=0; i<optNodes.length; i++)
nickjillings@1324 2240 {
nickjillings@1324 2241 var optNode = optNodes[i];
nickjillings@1324 2242 this.options.push({
nickjillings@1324 2243 name: optNode.getAttribute('name'),
nickjillings@1324 2244 text: optNode.textContent
nickjillings@1324 2245 });
nickjillings@1406 2246 }
nickjillings@1406 2247 };
nickjillings@1324 2248
nickjillings@1406 2249 this.encode = function(root)
nickjillings@1406 2250 {
nickjillings@1372 2251 var node = root.createElement("commentquestion");
nickjillings@1372 2252 node.id = this.id;
nickjillings@1372 2253 node.setAttribute("type",this.type);
nickjillings@1372 2254 var statement = root.createElement("statement");
nickjillings@1372 2255 statement.textContent = this.statement;
nickjillings@1372 2256 node.appendChild(statement);
nickjillings@1372 2257 for (var option of this.options)
nickjillings@1372 2258 {
nickjillings@1372 2259 var child = root.createElement("option");
nickjillings@1372 2260 child.setAttribute("name",option.name);
nickjillings@1372 2261 child.textContent = option.text;
nickjillings@1372 2262 node.appendChild(child);
nickjillings@1372 2263 }
nickjillings@1372 2264 return node;
nickjillings@1406 2265 };
nickjillings@1406 2266 };
nickjillings@1406 2267
nickjillings@1406 2268 this.audioElementNode = function() {
nickjillings@1406 2269 this.url = null;
nickjillings@1406 2270 this.id = null;
nickjillings@1406 2271 this.parent = null;
nickjillings@1324 2272 this.type = null;
nickjillings@1310 2273 this.marker = null;
nickjillings@1406 2274 this.enforce = false;
nickjillings@1426 2275 this.gain = 1.0;
nickjillings@1370 2276 this.schema = specification.schema.getAllElementsByName('audioelement')[0];;
nickjillings@1324 2277 this.parent = null;
nickjillings@1406 2278 this.decode = function(parent,xml)
nickjillings@1406 2279 {
nickjillings@1406 2280 this.parent = parent;
nickjillings@1348 2281 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
nickjillings@1324 2282 for (var i=0; i<attributeMap.length; i++)
nickjillings@1426 2283 {
nickjillings@1324 2284 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
nickjillings@1324 2285 var projectAttr = xml.getAttribute(attributeName);
nickjillings@1324 2286 projectAttr = specification.processAttribute(projectAttr,attributeMap[i]);
nickjillings@1324 2287 switch(typeof projectAttr)
nickjillings@1406 2288 {
nickjillings@1324 2289 case "number":
nickjillings@1324 2290 case "boolean":
nickjillings@1324 2291 eval('this.'+attributeName+' = '+projectAttr);
nickjillings@1324 2292 break;
nickjillings@1324 2293 case "string":
nickjillings@1324 2294 eval('this.'+attributeName+' = "'+projectAttr+'"');
nickjillings@1324 2295 break;
nickjillings@1455 2296 }
nickjillings@1455 2297 }
nickjillings@1324 2298
nickjillings@1406 2299 };
nickjillings@1406 2300 this.encode = function(root)
nickjillings@1406 2301 {
nickjillings@1372 2302 var AENode = root.createElement("audioelement");
nickjillings@1372 2303 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
nickjillings@1372 2304 for (var i=0; i<attributes.length; i++)
nickjillings@1372 2305 {
nickjillings@1372 2306 var name = attributes[i].getAttribute("name");
nickjillings@1372 2307 if (name == undefined) {
nickjillings@1372 2308 name = attributes[i].getAttribute("ref");
nickjillings@1372 2309 }
nickjillings@1372 2310 if(eval("this."+name+" != undefined") || attributes[i].getAttribute("use") == "required")
nickjillings@1372 2311 {
nickjillings@1372 2312 eval("AENode.setAttribute('"+name+"',this."+name+")");
nickjillings@1372 2313 }
nickjillings@1372 2314 }
nickjillings@1406 2315 return AENode;
nickjillings@1406 2316 };
nickjillings@1580 2317 };
nickjillings@1580 2318 };
nickjillings@1580 2319 }
nickjillings@1406 2320
nickjillings@1582 2321 function Interface(specificationObject) {
nickjillings@1580 2322 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1582 2323 this.specification = specificationObject;
nickjillings@1582 2324 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1580 2325
nickjillings@1324 2326 this.newPage = function(audioHolderObject,store)
nickjillings@1407 2327 {
nickjillings@1369 2328 audioEngineContext.newTestPage(audioHolderObject,store);
nickjillings@1407 2329 interfaceContext.deleteCommentBoxes();
nickjillings@1407 2330 interfaceContext.deleteCommentQuestions();
nickjillings@1324 2331 loadTest(audioHolderObject,store);
nickjillings@1407 2332 };
nickjillings@1407 2333
nickjillings@1582 2334 // Bounded by interface!!
nickjillings@1582 2335 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1582 2336 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1582 2337 this.interfaceObjects = [];
nickjillings@1582 2338 this.interfaceObject = function(){};
nickjillings@1582 2339
nickjillings@1525 2340 this.resizeWindow = function(event)
nickjillings@1525 2341 {
nickjillings@1421 2342 popup.resize(event);
nickjillings@1525 2343 for(var i=0; i<this.commentBoxes.length; i++)
nickjillings@1525 2344 {this.commentBoxes[i].resize();}
nickjillings@1525 2345 for(var i=0; i<this.commentQuestions.length; i++)
nickjillings@1525 2346 {this.commentQuestions[i].resize();}
nickjillings@1525 2347 try
nickjillings@1525 2348 {
nickjillings@1525 2349 resizeWindow(event);
nickjillings@1525 2350 }
nickjillings@1525 2351 catch(err)
nickjillings@1525 2352 {
nickjillings@1525 2353 console.log("Warning - Interface does not have Resize option");
nickjillings@1525 2354 console.log(err);
nickjillings@1525 2355 }
nickjillings@1525 2356 };
nickjillings@1525 2357
nickjillings@1465 2358 this.returnNavigator = function()
nickjillings@1465 2359 {
nickjillings@1362 2360 var node = storage.document.createElement("navigator");
nickjillings@1362 2361 var platform = storage.document.createElement("platform");
nickjillings@1465 2362 platform.textContent = navigator.platform;
nickjillings@1362 2363 var vendor = storage.document.createElement("vendor");
nickjillings@1465 2364 vendor.textContent = navigator.vendor;
nickjillings@1362 2365 var userAgent = storage.document.createElement("uagent");
nickjillings@1465 2366 userAgent.textContent = navigator.userAgent;
nickjillings@1362 2367 var screen = storage.document.createElement("window");
nickjillings@1362 2368 screen.setAttribute('innerWidth',window.innerWidth);
nickjillings@1362 2369 screen.setAttribute('innerHeight',window.innerHeight);
nickjillings@1465 2370 node.appendChild(platform);
nickjillings@1465 2371 node.appendChild(vendor);
nickjillings@1465 2372 node.appendChild(userAgent);
nickjillings@1362 2373 node.appendChild(screen);
nickjillings@1465 2374 return node;
nickjillings@1465 2375 };
nickjillings@1465 2376
nickjillings@1582 2377 this.commentBoxes = [];
nickjillings@2032 2378 this.elementCommentBox = function(audioObject) {
nickjillings@1582 2379 var element = audioObject.specification;
nickjillings@1583 2380 this.audioObject = audioObject;
nickjillings@1582 2381 this.id = audioObject.id;
nickjillings@1582 2382 var audioHolderObject = audioObject.specification.parent;
nickjillings@1582 2383 // Create document objects to hold the comment boxes
nickjillings@1582 2384 this.trackComment = document.createElement('div');
nickjillings@1582 2385 this.trackComment.className = 'comment-div';
nickjillings@1582 2386 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1582 2387 // Create a string next to each comment asking for a comment
nickjillings@1583 2388 this.trackString = document.createElement('span');
nickjillings@1316 2389 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.interfaceDOM.getPresentedId();
nickjillings@1582 2390 // Create the HTML5 comment box 'textarea'
nickjillings@1583 2391 this.trackCommentBox = document.createElement('textarea');
nickjillings@1583 2392 this.trackCommentBox.rows = '4';
nickjillings@1583 2393 this.trackCommentBox.cols = '100';
nickjillings@1583 2394 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1583 2395 this.trackCommentBox.className = 'trackComment';
nickjillings@1582 2396 var br = document.createElement('br');
nickjillings@1582 2397 // Add to the holder.
nickjillings@1583 2398 this.trackComment.appendChild(this.trackString);
nickjillings@1582 2399 this.trackComment.appendChild(br);
nickjillings@1583 2400 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1583 2401
nickjillings@1583 2402 this.exportXMLDOM = function() {
nickjillings@1583 2403 var root = document.createElement('comment');
nickjillings@2097 2404 var question = document.createElement('question');
nickjillings@2097 2405 question.textContent = this.trackString.textContent;
nickjillings@2097 2406 var response = document.createElement('response');
nickjillings@2097 2407 response.textContent = this.trackCommentBox.value;
nickjillings@2097 2408 console.log("Comment frag-"+this.id+": "+response.textContent);
nickjillings@2097 2409 root.appendChild(question);
nickjillings@2097 2410 root.appendChild(response);
nickjillings@1583 2411 return root;
nickjillings@1583 2412 };
nickjillings@1525 2413 this.resize = function()
nickjillings@1525 2414 {
nickjillings@1525 2415 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1525 2416 if (boxwidth >= 600)
nickjillings@1525 2417 {
nickjillings@1525 2418 boxwidth = 600;
nickjillings@1525 2419 }
nickjillings@1525 2420 else if (boxwidth < 400)
nickjillings@1525 2421 {
nickjillings@1525 2422 boxwidth = 400;
nickjillings@1525 2423 }
nickjillings@1525 2424 this.trackComment.style.width = boxwidth+"px";
nickjillings@1525 2425 this.trackCommentBox.style.width = boxwidth-6+"px";
nickjillings@1525 2426 };
nickjillings@1525 2427 this.resize();
nickjillings@1582 2428 };
nickjillings@1582 2429
nickjillings@2032 2430 this.commentQuestions = [];
nickjillings@2032 2431
nickjillings@2032 2432 this.commentBox = function(commentQuestion) {
nickjillings@2032 2433 this.specification = commentQuestion;
nickjillings@2032 2434 // Create document objects to hold the comment boxes
nickjillings@2032 2435 this.holder = document.createElement('div');
nickjillings@2032 2436 this.holder.className = 'comment-div';
nickjillings@2032 2437 // Create a string next to each comment asking for a comment
nickjillings@2032 2438 this.string = document.createElement('span');
nickjillings@1324 2439 this.string.innerHTML = commentQuestion.statement;
nickjillings@2032 2440 // Create the HTML5 comment box 'textarea'
nickjillings@2032 2441 this.textArea = document.createElement('textarea');
nickjillings@2032 2442 this.textArea.rows = '4';
nickjillings@2032 2443 this.textArea.cols = '100';
nickjillings@2032 2444 this.textArea.className = 'trackComment';
nickjillings@2032 2445 var br = document.createElement('br');
nickjillings@2032 2446 // Add to the holder.
nickjillings@2032 2447 this.holder.appendChild(this.string);
nickjillings@2032 2448 this.holder.appendChild(br);
nickjillings@2032 2449 this.holder.appendChild(this.textArea);
nickjillings@2032 2450
nickjillings@2097 2451 this.exportXMLDOM = function(storePoint) {
nickjillings@2097 2452 var root = storePoint.parent.document.createElement('comment');
nickjillings@2032 2453 root.id = this.specification.id;
nickjillings@2032 2454 root.setAttribute('type',this.specification.type);
b@2059 2455 console.log("Question: "+this.string.textContent);
b@2059 2456 console.log("Response: "+root.textContent);
nickjillings@2097 2457 var question = storePoint.parent.document.createElement('question');
nickjillings@2097 2458 question.textContent = this.string.textContent;
nickjillings@2097 2459 var response = storePoint.parent.document.createElement('response');
nickjillings@2097 2460 response.textContent = this.textArea.value;
nickjillings@2097 2461 root.appendChild(question);
nickjillings@2097 2462 root.appendChild(response);
nickjillings@2097 2463 storePoint.XMLDOM.appendChild(root);
nickjillings@2032 2464 return root;
nickjillings@2032 2465 };
nickjillings@1525 2466 this.resize = function()
nickjillings@1525 2467 {
nickjillings@1525 2468 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1525 2469 if (boxwidth >= 600)
nickjillings@1525 2470 {
nickjillings@1525 2471 boxwidth = 600;
nickjillings@1525 2472 }
nickjillings@1525 2473 else if (boxwidth < 400)
nickjillings@1525 2474 {
nickjillings@1525 2475 boxwidth = 400;
nickjillings@1525 2476 }
nickjillings@1525 2477 this.holder.style.width = boxwidth+"px";
nickjillings@1525 2478 this.textArea.style.width = boxwidth-6+"px";
nickjillings@1525 2479 };
nickjillings@1525 2480 this.resize();
nickjillings@2032 2481 };
nickjillings@2032 2482
nickjillings@2032 2483 this.radioBox = function(commentQuestion) {
nickjillings@2032 2484 this.specification = commentQuestion;
nickjillings@2032 2485 // Create document objects to hold the comment boxes
nickjillings@2032 2486 this.holder = document.createElement('div');
nickjillings@2032 2487 this.holder.className = 'comment-div';
nickjillings@2032 2488 // Create a string next to each comment asking for a comment
nickjillings@2032 2489 this.string = document.createElement('span');
nickjillings@2032 2490 this.string.innerHTML = commentQuestion.statement;
nickjillings@2032 2491 var br = document.createElement('br');
nickjillings@2032 2492 // Add to the holder.
nickjillings@2032 2493 this.holder.appendChild(this.string);
nickjillings@2032 2494 this.holder.appendChild(br);
nickjillings@2032 2495 this.options = [];
nickjillings@2032 2496 this.inputs = document.createElement('div');
nickjillings@2032 2497 this.span = document.createElement('div');
nickjillings@2032 2498 this.inputs.align = 'center';
nickjillings@2032 2499 this.inputs.style.marginLeft = '12px';
nickjillings@2032 2500 this.span.style.marginLeft = '12px';
nickjillings@2032 2501 this.span.align = 'center';
nickjillings@2032 2502 this.span.style.marginTop = '15px';
nickjillings@2032 2503
nickjillings@2032 2504 var optCount = commentQuestion.options.length;
nickjillings@1324 2505 for (var optNode of commentQuestion.options)
nickjillings@2032 2506 {
nickjillings@2032 2507 var div = document.createElement('div');
nickjillings@1524 2508 div.style.width = '80px';
nickjillings@2032 2509 div.style.float = 'left';
nickjillings@2032 2510 var input = document.createElement('input');
nickjillings@2032 2511 input.type = 'radio';
nickjillings@2032 2512 input.name = commentQuestion.id;
nickjillings@1324 2513 input.setAttribute('setvalue',optNode.name);
nickjillings@2032 2514 input.className = 'comment-radio';
nickjillings@2032 2515 div.appendChild(input);
nickjillings@2032 2516 this.inputs.appendChild(div);
nickjillings@2032 2517
nickjillings@2032 2518
nickjillings@2032 2519 div = document.createElement('div');
nickjillings@1524 2520 div.style.width = '80px';
nickjillings@2032 2521 div.style.float = 'left';
nickjillings@2032 2522 div.align = 'center';
nickjillings@2032 2523 var span = document.createElement('span');
nickjillings@1324 2524 span.textContent = optNode.text;
nickjillings@2032 2525 span.className = 'comment-radio-span';
nickjillings@2032 2526 div.appendChild(span);
nickjillings@2032 2527 this.span.appendChild(div);
nickjillings@2032 2528 this.options.push(input);
nickjillings@2032 2529 }
nickjillings@2032 2530 this.holder.appendChild(this.span);
nickjillings@2032 2531 this.holder.appendChild(this.inputs);
nickjillings@2032 2532
nickjillings@2097 2533 this.exportXMLDOM = function(storePoint) {
nickjillings@2097 2534 var root = storePoint.parent.document.createElement('comment');
nickjillings@2032 2535 root.id = this.specification.id;
nickjillings@2032 2536 root.setAttribute('type',this.specification.type);
nickjillings@2032 2537 var question = document.createElement('question');
nickjillings@2032 2538 question.textContent = this.string.textContent;
nickjillings@2032 2539 var response = document.createElement('response');
nickjillings@2032 2540 var i=0;
nickjillings@2032 2541 while(this.options[i].checked == false) {
nickjillings@2032 2542 i++;
nickjillings@2032 2543 if (i >= this.options.length) {
nickjillings@2032 2544 break;
nickjillings@2032 2545 }
nickjillings@2032 2546 }
nickjillings@2032 2547 if (i >= this.options.length) {
nickjillings@2032 2548 response.textContent = 'null';
nickjillings@2032 2549 } else {
nickjillings@2032 2550 response.textContent = this.options[i].getAttribute('setvalue');
nickjillings@2032 2551 response.setAttribute('number',i);
nickjillings@2032 2552 }
nickjillings@1572 2553 console.log('Comment: '+question.textContent);
nickjillings@1572 2554 console.log('Response: '+response.textContent);
nickjillings@2032 2555 root.appendChild(question);
nickjillings@2032 2556 root.appendChild(response);
nickjillings@2097 2557 storePoint.XMLDOM.appendChild(root);
nickjillings@2032 2558 return root;
nickjillings@2032 2559 };
nickjillings@1525 2560 this.resize = function()
nickjillings@1525 2561 {
nickjillings@1525 2562 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1525 2563 if (boxwidth >= 600)
nickjillings@1525 2564 {
nickjillings@1525 2565 boxwidth = 600;
nickjillings@1525 2566 }
nickjillings@1525 2567 else if (boxwidth < 400)
nickjillings@1525 2568 {
nickjillings@1525 2569 boxwidth = 400;
nickjillings@1525 2570 }
nickjillings@1525 2571 this.holder.style.width = boxwidth+"px";
nickjillings@1525 2572 var text = this.holder.children[2];
nickjillings@1525 2573 var options = this.holder.children[3];
nickjillings@1525 2574 var optCount = options.children.length;
nickjillings@1525 2575 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1525 2576 var options = options.firstChild;
nickjillings@1525 2577 var text = text.firstChild;
nickjillings@1525 2578 options.style.marginRight = spanMargin;
nickjillings@1525 2579 options.style.marginLeft = spanMargin;
nickjillings@1525 2580 text.style.marginRight = spanMargin;
nickjillings@1525 2581 text.style.marginLeft = spanMargin;
nickjillings@1525 2582 while(options.nextSibling != undefined)
nickjillings@1525 2583 {
nickjillings@1525 2584 options = options.nextSibling;
nickjillings@1525 2585 text = text.nextSibling;
nickjillings@1525 2586 options.style.marginRight = spanMargin;
nickjillings@1525 2587 options.style.marginLeft = spanMargin;
nickjillings@1525 2588 text.style.marginRight = spanMargin;
nickjillings@1525 2589 text.style.marginLeft = spanMargin;
nickjillings@1525 2590 }
nickjillings@1525 2591 };
nickjillings@1525 2592 this.resize();
nickjillings@2032 2593 };
nickjillings@2032 2594
nickjillings@1572 2595 this.checkboxBox = function(commentQuestion) {
nickjillings@1572 2596 this.specification = commentQuestion;
nickjillings@1572 2597 // Create document objects to hold the comment boxes
nickjillings@1572 2598 this.holder = document.createElement('div');
nickjillings@1572 2599 this.holder.className = 'comment-div';
nickjillings@1572 2600 // Create a string next to each comment asking for a comment
nickjillings@1572 2601 this.string = document.createElement('span');
nickjillings@1572 2602 this.string.innerHTML = commentQuestion.statement;
nickjillings@1572 2603 var br = document.createElement('br');
nickjillings@1572 2604 // Add to the holder.
nickjillings@1572 2605 this.holder.appendChild(this.string);
nickjillings@1572 2606 this.holder.appendChild(br);
nickjillings@1572 2607 this.options = [];
nickjillings@1572 2608 this.inputs = document.createElement('div');
nickjillings@1572 2609 this.span = document.createElement('div');
nickjillings@1572 2610 this.inputs.align = 'center';
nickjillings@1572 2611 this.inputs.style.marginLeft = '12px';
nickjillings@1572 2612 this.span.style.marginLeft = '12px';
nickjillings@1572 2613 this.span.align = 'center';
nickjillings@1572 2614 this.span.style.marginTop = '15px';
nickjillings@1572 2615
nickjillings@1572 2616 var optCount = commentQuestion.options.length;
nickjillings@1572 2617 for (var i=0; i<optCount; i++)
nickjillings@1572 2618 {
nickjillings@1572 2619 var div = document.createElement('div');
nickjillings@1524 2620 div.style.width = '80px';
nickjillings@1572 2621 div.style.float = 'left';
nickjillings@1572 2622 var input = document.createElement('input');
nickjillings@1572 2623 input.type = 'checkbox';
nickjillings@1572 2624 input.name = commentQuestion.id;
nickjillings@1572 2625 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1572 2626 input.className = 'comment-radio';
nickjillings@1572 2627 div.appendChild(input);
nickjillings@1572 2628 this.inputs.appendChild(div);
nickjillings@1572 2629
nickjillings@1572 2630
nickjillings@1572 2631 div = document.createElement('div');
nickjillings@1524 2632 div.style.width = '80px';
nickjillings@1572 2633 div.style.float = 'left';
nickjillings@1572 2634 div.align = 'center';
nickjillings@1572 2635 var span = document.createElement('span');
nickjillings@1572 2636 span.textContent = commentQuestion.options[i].text;
nickjillings@1572 2637 span.className = 'comment-radio-span';
nickjillings@1572 2638 div.appendChild(span);
nickjillings@1572 2639 this.span.appendChild(div);
nickjillings@1572 2640 this.options.push(input);
nickjillings@1572 2641 }
nickjillings@1572 2642 this.holder.appendChild(this.span);
nickjillings@1572 2643 this.holder.appendChild(this.inputs);
nickjillings@1572 2644
nickjillings@2097 2645 this.exportXMLDOM = function(storePoint) {
nickjillings@2097 2646 var root = storePoint.parent.document.createElement('comment');
nickjillings@1572 2647 root.id = this.specification.id;
nickjillings@1572 2648 root.setAttribute('type',this.specification.type);
nickjillings@1572 2649 var question = document.createElement('question');
nickjillings@1572 2650 question.textContent = this.string.textContent;
nickjillings@1572 2651 root.appendChild(question);
nickjillings@1572 2652 console.log('Comment: '+question.textContent);
nickjillings@1572 2653 for (var i=0; i<this.options.length; i++) {
nickjillings@1572 2654 var response = document.createElement('response');
nickjillings@1572 2655 response.textContent = this.options[i].checked;
nickjillings@1572 2656 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nickjillings@1572 2657 root.appendChild(response);
nickjillings@1572 2658 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nickjillings@1572 2659 }
nickjillings@2097 2660 storePoint.XMLDOM.appendChild(root);
nickjillings@1572 2661 return root;
nickjillings@1572 2662 };
nickjillings@1525 2663 this.resize = function()
nickjillings@1525 2664 {
nickjillings@1525 2665 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1525 2666 if (boxwidth >= 600)
nickjillings@1525 2667 {
nickjillings@1525 2668 boxwidth = 600;
nickjillings@1525 2669 }
nickjillings@1525 2670 else if (boxwidth < 400)
nickjillings@1525 2671 {
nickjillings@1525 2672 boxwidth = 400;
nickjillings@1525 2673 }
nickjillings@1525 2674 this.holder.style.width = boxwidth+"px";
nickjillings@1525 2675 var text = this.holder.children[2];
nickjillings@1525 2676 var options = this.holder.children[3];
nickjillings@1525 2677 var optCount = options.children.length;
nickjillings@1525 2678 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1525 2679 var options = options.firstChild;
nickjillings@1525 2680 var text = text.firstChild;
nickjillings@1525 2681 options.style.marginRight = spanMargin;
nickjillings@1525 2682 options.style.marginLeft = spanMargin;
nickjillings@1525 2683 text.style.marginRight = spanMargin;
nickjillings@1525 2684 text.style.marginLeft = spanMargin;
nickjillings@1525 2685 while(options.nextSibling != undefined)
nickjillings@1525 2686 {
nickjillings@1525 2687 options = options.nextSibling;
nickjillings@1525 2688 text = text.nextSibling;
nickjillings@1525 2689 options.style.marginRight = spanMargin;
nickjillings@1525 2690 options.style.marginLeft = spanMargin;
nickjillings@1525 2691 text.style.marginRight = spanMargin;
nickjillings@1525 2692 text.style.marginLeft = spanMargin;
nickjillings@1525 2693 }
nickjillings@1525 2694 };
nickjillings@1525 2695 this.resize();
nickjillings@1572 2696 };
nickjillings@2032 2697
nickjillings@1582 2698 this.createCommentBox = function(audioObject) {
nickjillings@2032 2699 var node = new this.elementCommentBox(audioObject);
nickjillings@1582 2700 this.commentBoxes.push(node);
nickjillings@1582 2701 audioObject.commentDOM = node;
nickjillings@1582 2702 return node;
nickjillings@1582 2703 };
nickjillings@1582 2704
nickjillings@1582 2705 this.sortCommentBoxes = function() {
nickjillings@1338 2706 this.commentBoxes.sort(function(a,b){return a.id - b.id;});
nickjillings@1582 2707 };
nickjillings@1582 2708
nickjillings@1582 2709 this.showCommentBoxes = function(inject, sort) {
nickjillings@1582 2710 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1338 2711 for (var box of interfaceContext.commentBoxes) {
nickjillings@1338 2712 inject.appendChild(box.trackComment);
nickjillings@1582 2713 }
nickjillings@1582 2714 };
nickjillings@2032 2715
nickjillings@1570 2716 this.deleteCommentBoxes = function() {
nickjillings@1570 2717 this.commentBoxes = [];
nickjillings@2051 2718 };
nickjillings@1570 2719
nickjillings@2032 2720 this.createCommentQuestion = function(element) {
nickjillings@2032 2721 var node;
nickjillings@1324 2722 if (element.type == 'question') {
nickjillings@2032 2723 node = new this.commentBox(element);
nickjillings@2032 2724 } else if (element.type == 'radio') {
nickjillings@2032 2725 node = new this.radioBox(element);
nickjillings@1572 2726 } else if (element.type == 'checkbox') {
nickjillings@1572 2727 node = new this.checkboxBox(element);
nickjillings@2032 2728 }
nickjillings@2032 2729 this.commentQuestions.push(node);
nickjillings@2032 2730 return node;
nickjillings@2032 2731 };
nickjillings@1564 2732
nickjillings@2051 2733 this.deleteCommentQuestions = function()
nickjillings@2051 2734 {
nickjillings@2051 2735 this.commentQuestions = [];
nickjillings@2051 2736 };
nickjillings@2051 2737
nickjillings@1564 2738 this.playhead = new function()
nickjillings@1564 2739 {
nickjillings@1564 2740 this.object = document.createElement('div');
nickjillings@1564 2741 this.object.className = 'playhead';
nickjillings@1564 2742 this.object.align = 'left';
nickjillings@1564 2743 var curTime = document.createElement('div');
nickjillings@1564 2744 curTime.style.width = '50px';
nickjillings@1564 2745 this.curTimeSpan = document.createElement('span');
nickjillings@1564 2746 this.curTimeSpan.textContent = '00:00';
nickjillings@1564 2747 curTime.appendChild(this.curTimeSpan);
nickjillings@1564 2748 this.object.appendChild(curTime);
nickjillings@1564 2749 this.scrubberTrack = document.createElement('div');
nickjillings@1564 2750 this.scrubberTrack.className = 'playhead-scrub-track';
nickjillings@1564 2751
nickjillings@1564 2752 this.scrubberHead = document.createElement('div');
nickjillings@1564 2753 this.scrubberHead.id = 'playhead-scrubber';
nickjillings@1564 2754 this.scrubberTrack.appendChild(this.scrubberHead);
nickjillings@1564 2755 this.object.appendChild(this.scrubberTrack);
nickjillings@1564 2756
nickjillings@1564 2757 this.timePerPixel = 0;
nickjillings@1564 2758 this.maxTime = 0;
nickjillings@1564 2759
nickjillings@1567 2760 this.playbackObject;
nickjillings@1567 2761
nickjillings@1567 2762 this.setTimePerPixel = function(audioObject) {
nickjillings@1564 2763 //maxTime must be in seconds
nickjillings@1567 2764 this.playbackObject = audioObject;
nickjillings@1410 2765 this.maxTime = audioObject.buffer.buffer.duration;
nickjillings@1564 2766 var width = 490; //500 - 10, 5 each side of the tracker head
nickjillings@1567 2767 this.timePerPixel = this.maxTime/490;
nickjillings@1567 2768 if (this.maxTime < 60) {
nickjillings@1564 2769 this.curTimeSpan.textContent = '0.00';
nickjillings@1564 2770 } else {
nickjillings@1564 2771 this.curTimeSpan.textContent = '00:00';
nickjillings@1564 2772 }
nickjillings@1564 2773 };
nickjillings@1564 2774
nickjillings@1567 2775 this.update = function() {
nickjillings@1564 2776 // Update the playhead position, startPlay must be called
nickjillings@1564 2777 if (this.timePerPixel > 0) {
nickjillings@1567 2778 var time = this.playbackObject.getCurrentPosition();
nickjillings@1367 2779 if (time > 0 && time < this.maxTime) {
nickjillings@1530 2780 var width = 490;
nickjillings@1530 2781 var pix = Math.floor(time/this.timePerPixel);
nickjillings@1530 2782 this.scrubberHead.style.left = pix+'px';
nickjillings@1530 2783 if (this.maxTime > 60.0) {
nickjillings@1530 2784 var secs = time%60;
nickjillings@1530 2785 var mins = Math.floor((time-secs)/60);
nickjillings@1530 2786 secs = secs.toString();
nickjillings@1530 2787 secs = secs.substr(0,2);
nickjillings@1530 2788 mins = mins.toString();
nickjillings@1530 2789 this.curTimeSpan.textContent = mins+':'+secs;
nickjillings@1530 2790 } else {
nickjillings@1530 2791 time = time.toString();
nickjillings@1530 2792 this.curTimeSpan.textContent = time.substr(0,4);
nickjillings@1530 2793 }
nickjillings@1564 2794 } else {
nickjillings@1530 2795 this.scrubberHead.style.left = '0px';
nickjillings@1530 2796 if (this.maxTime < 60) {
nickjillings@1530 2797 this.curTimeSpan.textContent = '0.00';
nickjillings@1530 2798 } else {
nickjillings@1530 2799 this.curTimeSpan.textContent = '00:00';
nickjillings@1530 2800 }
nickjillings@1564 2801 }
nickjillings@1564 2802 }
nickjillings@1564 2803 };
nickjillings@1567 2804
nickjillings@1567 2805 this.interval = undefined;
nickjillings@1567 2806
nickjillings@1567 2807 this.start = function() {
nickjillings@1567 2808 if (this.playbackObject != undefined && this.interval == undefined) {
nickjillings@1530 2809 if (this.maxTime < 60) {
nickjillings@1530 2810 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nickjillings@1530 2811 } else {
nickjillings@1530 2812 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nickjillings@1530 2813 }
nickjillings@1567 2814 }
nickjillings@1567 2815 };
nickjillings@1567 2816 this.stop = function() {
nickjillings@1567 2817 clearInterval(this.interval);
nickjillings@1567 2818 this.interval = undefined;
nickjillings@2101 2819 this.scrubberHead.style.left = '0px';
nickjillings@1530 2820 if (this.maxTime < 60) {
nickjillings@1530 2821 this.curTimeSpan.textContent = '0.00';
nickjillings@1530 2822 } else {
nickjillings@1530 2823 this.curTimeSpan.textContent = '00:00';
nickjillings@1530 2824 }
nickjillings@1567 2825 };
nickjillings@1564 2826 };
nickjillings@1354 2827
nickjillings@1354 2828 this.volume = new function()
nickjillings@1354 2829 {
nickjillings@1354 2830 // An in-built volume module which can be viewed on page
nickjillings@1354 2831 // Includes trackers on page-by-page data
nickjillings@1354 2832 // Volume does NOT reset to 0dB on each page load
nickjillings@1354 2833 this.valueLin = 1.0;
nickjillings@1354 2834 this.valueDB = 0.0;
nickjillings@1354 2835 this.object = document.createElement('div');
nickjillings@1354 2836 this.object.id = 'master-volume-holder';
nickjillings@1354 2837 this.slider = document.createElement('input');
nickjillings@1354 2838 this.slider.id = 'master-volume-control';
nickjillings@1354 2839 this.slider.type = 'range';
nickjillings@1354 2840 this.valueText = document.createElement('span');
nickjillings@1354 2841 this.valueText.id = 'master-volume-feedback';
nickjillings@1354 2842 this.valueText.textContent = '0dB';
nickjillings@1354 2843
nickjillings@1354 2844 this.slider.min = -60;
nickjillings@1354 2845 this.slider.max = 12;
nickjillings@1354 2846 this.slider.value = 0;
nickjillings@1354 2847 this.slider.step = 1;
nickjillings@1354 2848 this.slider.onmousemove = function(event)
nickjillings@1354 2849 {
nickjillings@1354 2850 interfaceContext.volume.valueDB = event.currentTarget.value;
nickjillings@1354 2851 interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB);
nickjillings@1354 2852 interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB+'dB';
nickjillings@1354 2853 audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin;
nickjillings@1354 2854 }
nickjillings@1354 2855 this.slider.onmouseup = function(event)
nickjillings@1354 2856 {
nickjillings@2100 2857 var storePoint = testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].getAllElementsByName('volumeTracker');
nickjillings@1354 2858 if (storePoint.length == 0)
nickjillings@1354 2859 {
nickjillings@1354 2860 storePoint = storage.document.createElement('metricresult');
nickjillings@1354 2861 storePoint.setAttribute('name','volumeTracker');
nickjillings@2100 2862 testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].appendChild(storePoint);
nickjillings@1354 2863 }
nickjillings@1354 2864 else {
nickjillings@1354 2865 storePoint = storePoint[0];
nickjillings@1354 2866 }
nickjillings@1354 2867 var node = storage.document.createElement('movement');
nickjillings@1354 2868 node.setAttribute('test-time',audioEngineContext.timer.getTestTime());
nickjillings@1354 2869 node.setAttribute('volume',interfaceContext.volume.valueDB);
nickjillings@1354 2870 node.setAttribute('format','dBFS');
nickjillings@1354 2871 storePoint.appendChild(node);
nickjillings@1354 2872 }
nickjillings@1354 2873
nickjillings@1355 2874 var title = document.createElement('div');
nickjillings@1355 2875 title.innerHTML = '<span>Master Volume Control</span>';
nickjillings@1355 2876 title.style.fontSize = '0.75em';
nickjillings@1355 2877 title.style.width = "100%";
nickjillings@1355 2878 title.align = 'center';
nickjillings@1355 2879 this.object.appendChild(title);
nickjillings@1355 2880
nickjillings@1354 2881 this.object.appendChild(this.slider);
nickjillings@1354 2882 this.object.appendChild(this.valueText);
nickjillings@1354 2883 }
nickjillings@2049 2884 // Global Checkers
nickjillings@2049 2885 // These functions will help enforce the checkers
nickjillings@2049 2886 this.checkHiddenAnchor = function()
nickjillings@2049 2887 {
nickjillings@1324 2888 for (var ao of audioEngineContext.audioObjects)
nickjillings@2049 2889 {
nickjillings@1324 2890 if (ao.specification.type == "anchor")
nickjillings@2049 2891 {
nickjillings@1325 2892 if (ao.interfaceDOM.getValue() > (ao.specification.marker/100) && ao.specification.marker > 0) {
nickjillings@1324 2893 // Anchor is not set below
nickjillings@1324 2894 console.log('Anchor node not below marker value');
nickjillings@1324 2895 alert('Please keep listening');
nickjillings@1367 2896 this.storeErrorNode('Anchor node not below marker value');
nickjillings@1324 2897 return false;
nickjillings@1324 2898 }
nickjillings@2049 2899 }
nickjillings@2049 2900 }
nickjillings@2049 2901 return true;
nickjillings@2049 2902 };
nickjillings@2049 2903
nickjillings@2049 2904 this.checkHiddenReference = function()
nickjillings@2049 2905 {
nickjillings@1324 2906 for (var ao of audioEngineContext.audioObjects)
nickjillings@2049 2907 {
nickjillings@1324 2908 if (ao.specification.type == "reference")
nickjillings@2049 2909 {
nickjillings@1325 2910 if (ao.interfaceDOM.getValue() < (ao.specification.marker/100) && ao.specification.marker > 0) {
nickjillings@1324 2911 // Anchor is not set below
nickjillings@1367 2912 console.log('Reference node not above marker value');
nickjillings@1367 2913 this.storeErrorNode('Reference node not above marker value');
nickjillings@1324 2914 alert('Please keep listening');
nickjillings@1324 2915 return false;
nickjillings@1324 2916 }
nickjillings@2049 2917 }
nickjillings@2049 2918 }
nickjillings@2049 2919 return true;
nickjillings@2049 2920 };
nickjillings@1474 2921
nickjillings@1474 2922 this.checkFragmentsFullyPlayed = function ()
nickjillings@1474 2923 {
nickjillings@1474 2924 // Checks the entire file has been played back
nickjillings@1474 2925 // NOTE ! This will return true IF playback is Looped!!!
nickjillings@1474 2926 if (audioEngineContext.loopPlayback)
nickjillings@1474 2927 {
nickjillings@1474 2928 console.log("WARNING - Looped source: Cannot check fragments are fully played");
nickjillings@1474 2929 return true;
nickjillings@1474 2930 }
nickjillings@1474 2931 var check_pass = true;
nickjillings@1474 2932 var error_obj = [];
nickjillings@1474 2933 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
nickjillings@1474 2934 {
nickjillings@1474 2935 var object = audioEngineContext.audioObjects[i];
nickjillings@1393 2936 var time = object.buffer.buffer.duration;
nickjillings@1474 2937 var metric = object.metric;
nickjillings@1474 2938 var passed = false;
nickjillings@1474 2939 for (var j=0; j<metric.listenTracker.length; j++)
nickjillings@1474 2940 {
nickjillings@1474 2941 var bt = metric.listenTracker[j].getElementsByTagName('buffertime');
nickjillings@1474 2942 var start_time = Number(bt[0].getAttribute('start'));
nickjillings@1474 2943 var stop_time = Number(bt[0].getAttribute('stop'));
nickjillings@1474 2944 var delta = stop_time - start_time;
nickjillings@1474 2945 if (delta >= time)
nickjillings@1474 2946 {
nickjillings@1474 2947 passed = true;
nickjillings@1474 2948 break;
nickjillings@1474 2949 }
nickjillings@1474 2950 }
nickjillings@1474 2951 if (passed == false)
nickjillings@1474 2952 {
nickjillings@1474 2953 check_pass = false;
nickjillings@1340 2954 console.log("Continue listening to track-"+audioEngineContext.audioObjects.interfaceDOM.getPresentedId());
nickjillings@1340 2955 error_obj.push(audioEngineContext.audioObjects.interfaceDOM.getPresentedId());
nickjillings@1474 2956 }
nickjillings@1474 2957 }
nickjillings@1474 2958 if (check_pass == false)
nickjillings@1474 2959 {
nickjillings@1393 2960 var str_start = "You have not completely listened to fragments ";
nickjillings@1474 2961 for (var i=0; i<error_obj.length; i++)
nickjillings@1474 2962 {
nickjillings@1474 2963 str_start += error_obj[i];
nickjillings@1474 2964 if (i != error_obj.length-1)
nickjillings@1474 2965 {
nickjillings@1474 2966 str_start += ', ';
nickjillings@1474 2967 }
nickjillings@1474 2968 }
nickjillings@1474 2969 str_start += ". Please keep listening";
nickjillings@1474 2970 console.log("[ALERT]: "+str_start);
nickjillings@1367 2971 this.storeErrorNode("[ALERT]: "+str_start);
nickjillings@1474 2972 alert(str_start);
nickjillings@1474 2973 }
nickjillings@1474 2974 };
nickjillings@1399 2975 this.checkAllMoved = function()
nickjillings@1399 2976 {
nickjillings@1399 2977 var str = "You have not moved ";
nickjillings@1399 2978 var failed = [];
nickjillings@1340 2979 for (var ao of audioEngineContext.audioObjects)
nickjillings@1399 2980 {
nickjillings@1340 2981 if(ao.metric.wasMoved == false && ao.interfaceDOM.canMove() == true)
nickjillings@1399 2982 {
nickjillings@1340 2983 failed.push(ao.interfaceDOM.getPresentedId());
nickjillings@1399 2984 }
nickjillings@1399 2985 }
nickjillings@1399 2986 if (failed.length == 0)
nickjillings@1399 2987 {
nickjillings@1399 2988 return true;
nickjillings@1399 2989 } else if (failed.length == 1)
nickjillings@1399 2990 {
nickjillings@1399 2991 str += 'track '+failed[0];
nickjillings@1399 2992 } else {
nickjillings@1399 2993 str += 'tracks ';
nickjillings@1399 2994 for (var i=0; i<failed.length-1; i++)
nickjillings@1399 2995 {
nickjillings@1399 2996 str += failed[i]+', ';
nickjillings@1399 2997 }
nickjillings@1399 2998 str += 'and '+failed[i];
nickjillings@1399 2999 }
nickjillings@1399 3000 str +='.';
nickjillings@1399 3001 alert(str);
nickjillings@1399 3002 console.log(str);
nickjillings@1367 3003 this.storeErrorNode(str);
nickjillings@1399 3004 return false;
nickjillings@1399 3005 };
nickjillings@1399 3006 this.checkAllPlayed = function()
nickjillings@1399 3007 {
nickjillings@1399 3008 var str = "You have not played ";
nickjillings@1399 3009 var failed = [];
nickjillings@1340 3010 for (var ao of audioEngineContext.audioObjects)
nickjillings@1399 3011 {
nickjillings@1340 3012 if(ao.metric.wasListenedTo == false)
nickjillings@1399 3013 {
nickjillings@1340 3014 failed.push(ao.interfaceDOM.getPresentedId());
nickjillings@1399 3015 }
nickjillings@1399 3016 }
nickjillings@1399 3017 if (failed.length == 0)
nickjillings@1399 3018 {
nickjillings@1399 3019 return true;
nickjillings@1399 3020 } else if (failed.length == 1)
nickjillings@1399 3021 {
nickjillings@1399 3022 str += 'track '+failed[0];
nickjillings@1399 3023 } else {
nickjillings@1399 3024 str += 'tracks ';
nickjillings@1399 3025 for (var i=0; i<failed.length-1; i++)
nickjillings@1399 3026 {
nickjillings@1399 3027 str += failed[i]+', ';
nickjillings@1399 3028 }
nickjillings@1399 3029 str += 'and '+failed[i];
nickjillings@1399 3030 }
nickjillings@1399 3031 str +='.';
nickjillings@1399 3032 alert(str);
nickjillings@1399 3033 console.log(str);
nickjillings@1367 3034 this.storeErrorNode(str);
nickjillings@1399 3035 return false;
nickjillings@1399 3036 };
nickjillings@1367 3037
nickjillings@1367 3038 this.storeErrorNode = function(errorMessage)
nickjillings@1367 3039 {
nickjillings@1367 3040 var time = audioEngineContext.timer.getTestTime();
nickjillings@1367 3041 var node = storage.document.createElement('error');
nickjillings@1367 3042 node.setAttribute('time',time);
nickjillings@1367 3043 node.textContent = errorMessage;
nickjillings@1367 3044 testState.currentStore.XMLDOM.appendChild(node);
nickjillings@1367 3045 };
nickjillings@1324 3046 }
nickjillings@1324 3047
nickjillings@1324 3048 function Storage()
nickjillings@1324 3049 {
nickjillings@1324 3050 // Holds results in XML format until ready for collection
nickjillings@1324 3051 this.globalPreTest = null;
nickjillings@1324 3052 this.globalPostTest = null;
nickjillings@1324 3053 this.testPages = [];
nickjillings@1324 3054 this.document = document.implementation.createDocument(null,"waetresult");
nickjillings@1324 3055 this.root = this.document.children[0];
nickjillings@1324 3056 this.state = 0;
nickjillings@1324 3057
nickjillings@1324 3058 this.initialise = function()
nickjillings@1324 3059 {
nickjillings@1342 3060 if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);}
nickjillings@1342 3061 if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);}
nickjillings@1324 3062 };
nickjillings@1324 3063
nickjillings@1324 3064 this.createTestPageStore = function(specification)
nickjillings@1324 3065 {
nickjillings@1324 3066 var store = new this.pageNode(this,specification);
nickjillings@1324 3067 this.testPages.push(store);
nickjillings@1324 3068 return this.testPages[this.testPages.length-1];
nickjillings@1324 3069 };
nickjillings@1324 3070
nickjillings@1324 3071 this.surveyNode = function(parent,root,specification)
nickjillings@1324 3072 {
nickjillings@1324 3073 this.specification = specification;
nickjillings@1324 3074 this.parent = parent;
nickjillings@1324 3075 this.XMLDOM = this.parent.document.createElement('survey');
nickjillings@1324 3076 this.XMLDOM.setAttribute('location',this.specification.location);
nickjillings@1324 3077 for (var optNode of this.specification.options)
nickjillings@1324 3078 {
nickjillings@1324 3079 if (optNode.type != 'statement')
nickjillings@1324 3080 {
nickjillings@1324 3081 var node = this.parent.document.createElement('surveyresult');
nickjillings@1324 3082 node.id = optNode.id;
nickjillings@1324 3083 node.setAttribute('type',optNode.type);
nickjillings@1324 3084 this.XMLDOM.appendChild(node);
nickjillings@1324 3085 }
nickjillings@1324 3086 }
nickjillings@1324 3087 root.appendChild(this.XMLDOM);
nickjillings@1324 3088
nickjillings@1324 3089 this.postResult = function(node)
nickjillings@1324 3090 {
nickjillings@1324 3091 // From popup: node is the popupOption node containing both spec. and results
nickjillings@1324 3092 // ID is the position
nickjillings@1324 3093 if (node.specification.type == 'statement'){return;}
nickjillings@1324 3094 var surveyresult = this.parent.document.getElementById(node.specification.id);
nickjillings@1324 3095 switch(node.specification.type)
nickjillings@1324 3096 {
nickjillings@1324 3097 case "number":
nickjillings@1324 3098 case "question":
nickjillings@1324 3099 var child = this.parent.document.createElement('response');
nickjillings@1324 3100 child.textContent = node.response;
nickjillings@1324 3101 surveyresult.appendChild(child);
nickjillings@1324 3102 break;
nickjillings@1324 3103 case "radio":
nickjillings@1324 3104 var child = this.parent.document.createElement('response');
nickjillings@1324 3105 child.setAttribute('name',node.response.name);
nickjillings@1324 3106 child.textContent = node.response.text;
nickjillings@1324 3107 surveyresult.appendChild(child);
nickjillings@1324 3108 break;
nickjillings@1324 3109 case "checkbox":
nickjillings@1324 3110 for (var i=0; i<node.response.length; i++)
nickjillings@1324 3111 {
nickjillings@1324 3112 var checkNode = this.parent.document.createElement('response');
nickjillings@1347 3113 checkNode.setAttribute('name',node.response[i].name);
nickjillings@1347 3114 checkNode.setAttribute('checked',node.response[i].checked);
nickjillings@1326 3115 surveyresult.appendChild(checkNode);
nickjillings@1324 3116 }
nickjillings@1324 3117 break;
nickjillings@1324 3118 }
nickjillings@1324 3119 };
nickjillings@1324 3120 };
nickjillings@1324 3121
nickjillings@1324 3122 this.pageNode = function(parent,specification)
nickjillings@1324 3123 {
nickjillings@1324 3124 // Create one store per test page
nickjillings@1324 3125 this.specification = specification;
nickjillings@1324 3126 this.parent = parent;
nickjillings@1324 3127 this.XMLDOM = this.parent.document.createElement('page');
nickjillings@1324 3128 this.XMLDOM.setAttribute('id',specification.id);
nickjillings@1324 3129 this.XMLDOM.setAttribute('presentedId',specification.presentedId);
nickjillings@1345 3130 if (specification.preTest != undefined){this.preTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.preTest);}
nickjillings@1345 3131 if (specification.postTest != undefined){this.postTest = new this.parent.surveyNode(this.parent,this.XMLDOM,this.specification.postTest);}
nickjillings@1324 3132
nickjillings@1324 3133 // Add any page metrics
nickjillings@1324 3134 var page_metric = this.parent.document.createElement('metric');
nickjillings@1324 3135 this.XMLDOM.appendChild(page_metric);
nickjillings@1324 3136
nickjillings@1324 3137 // Add the audioelement
nickjillings@1324 3138 for (var element of this.specification.audioElements)
nickjillings@1324 3139 {
nickjillings@1324 3140 var aeNode = this.parent.document.createElement('audioelement');
nickjillings@1324 3141 aeNode.id = element.id;
nickjillings@1324 3142 aeNode.setAttribute('type',element.type);
nickjillings@1324 3143 aeNode.setAttribute('url', element.url);
nickjillings@1324 3144 aeNode.setAttribute('gain', element.gain);
nickjillings@1324 3145 if (element.type == 'anchor' || element.type == 'reference')
nickjillings@1324 3146 {
nickjillings@1324 3147 if (element.marker > 0)
nickjillings@1324 3148 {
nickjillings@1324 3149 aeNode.setAttribute('marker',element.marker);
nickjillings@1324 3150 }
nickjillings@1324 3151 }
nickjillings@1324 3152 var ae_metric = this.parent.document.createElement('metric');
nickjillings@1324 3153 aeNode.appendChild(ae_metric);
nickjillings@1324 3154 this.XMLDOM.appendChild(aeNode);
nickjillings@1324 3155 }
nickjillings@1324 3156
nickjillings@1324 3157 this.parent.root.appendChild(this.XMLDOM);
nickjillings@1324 3158 };
nickjillings@1324 3159 this.finish = function()
nickjillings@1324 3160 {
nickjillings@1324 3161 if (this.state == 0)
nickjillings@1324 3162 {
nickjillings@1324 3163 var projectDocument = specification.projectXML;
nickjillings@1324 3164 projectDocument.setAttribute('file-name',url);
nickjillings@1324 3165 this.root.appendChild(projectDocument);
nickjillings@1324 3166 this.root.appendChild(returnDateNode());
nickjillings@1324 3167 this.root.appendChild(interfaceContext.returnNavigator());
nickjillings@1324 3168 }
nickjillings@1324 3169 this.state = 1;
nickjillings@1324 3170 return this.root;
nickjillings@1324 3171 };
nickjillings@1324 3172 }