annotate core.js @ 2101:974bc78e6250

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