annotate core.js @ 1378:589d9860a974

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