giuliomoro@15: /**
giuliomoro@15: * core.js
giuliomoro@15: *
giuliomoro@15: * Main script to run, calls all other core functions and manages loading/store to backend.
giuliomoro@15: * Also contains all global variables.
giuliomoro@15: */
giuliomoro@15:
giuliomoro@15: /* create the web audio API context and store in audioContext*/
giuliomoro@15: var audioContext; // Hold the browser web audio API
giuliomoro@15: var projectXML; // Hold the parsed setup XML
giuliomoro@15: var schemaXSD; // Hold the parsed schema XSD
giuliomoro@15: var specification;
giuliomoro@15: var interfaceContext;
giuliomoro@15: var storage;
giuliomoro@15: var popup; // Hold the interfacePopup object
giuliomoro@15: var testState;
giuliomoro@15: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
giuliomoro@15: var audioEngineContext; // The custome AudioEngine object
giuliomoro@15: var gReturnURL;
giuliomoro@16: var gSaveFilenamePrefix;
giuliomoro@15:
giuliomoro@15:
giuliomoro@15: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
giuliomoro@15: AudioBufferSourceNode.prototype.owner = undefined;
giuliomoro@15: // Add a prototype to the bufferSourceNode to hold when the object was given a play command
giuliomoro@15: AudioBufferSourceNode.prototype.playbackStartTime = undefined;
giuliomoro@15: // Add a prototype to the bufferNode to hold the desired LINEAR gain
giuliomoro@15: AudioBuffer.prototype.playbackGain = undefined;
giuliomoro@15: // Add a prototype to the bufferNode to hold the computed LUFS loudness
giuliomoro@15: AudioBuffer.prototype.lufs = undefined;
giuliomoro@15:
giuliomoro@15: // Convert relative URLs into absolutes
giuliomoro@15: function escapeHTML(s) {
giuliomoro@15: return s.split('&').join('&').split('<').join('<').split('"').join('"');
giuliomoro@15: }
giuliomoro@15: function qualifyURL(url) {
giuliomoro@15: var el= document.createElement('div');
giuliomoro@15: el.innerHTML= 'x';
giuliomoro@15: return el.firstChild.href;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // Firefox does not have an XMLDocument.prototype.getElementsByName
giuliomoro@15: // and there is no searchAll style command, this custom function will
giuliomoro@15: // search all children recusrively for the name. Used for XSD where all
giuliomoro@15: // element nodes must have a name and therefore can pull the schema node
giuliomoro@15: XMLDocument.prototype.getAllElementsByName = function(name)
giuliomoro@15: {
giuliomoro@15: name = String(name);
giuliomoro@15: var selected = this.documentElement.getAllElementsByName(name);
giuliomoro@15: return selected;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: Element.prototype.getAllElementsByName = function(name)
giuliomoro@15: {
giuliomoro@15: name = String(name);
giuliomoro@15: var selected = [];
giuliomoro@15: var node = this.firstElementChild;
giuliomoro@15: while(node != null)
giuliomoro@15: {
giuliomoro@15: if (node.getAttribute('name') == name)
giuliomoro@15: {
giuliomoro@15: selected.push(node);
giuliomoro@15: }
giuliomoro@15: if (node.childElementCount > 0)
giuliomoro@15: {
giuliomoro@15: selected = selected.concat(node.getAllElementsByName(name));
giuliomoro@15: }
giuliomoro@15: node = node.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: return selected;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: XMLDocument.prototype.getAllElementsByTagName = function(name)
giuliomoro@15: {
giuliomoro@15: name = String(name);
giuliomoro@15: var selected = this.documentElement.getAllElementsByTagName(name);
giuliomoro@15: return selected;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: Element.prototype.getAllElementsByTagName = function(name)
giuliomoro@15: {
giuliomoro@15: name = String(name);
giuliomoro@15: var selected = [];
giuliomoro@15: var node = this.firstElementChild;
giuliomoro@15: while(node != null)
giuliomoro@15: {
giuliomoro@15: if (node.nodeName == name)
giuliomoro@15: {
giuliomoro@15: selected.push(node);
giuliomoro@15: }
giuliomoro@15: if (node.childElementCount > 0)
giuliomoro@15: {
giuliomoro@15: selected = selected.concat(node.getAllElementsByTagName(name));
giuliomoro@15: }
giuliomoro@15: node = node.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: return selected;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // Firefox does not have an XMLDocument.prototype.getElementsByName
giuliomoro@15: if (typeof XMLDocument.prototype.getElementsByName != "function") {
giuliomoro@15: XMLDocument.prototype.getElementsByName = function(name)
giuliomoro@15: {
giuliomoro@15: name = String(name);
giuliomoro@15: var node = this.documentElement.firstElementChild;
giuliomoro@15: var selected = [];
giuliomoro@15: while(node != null)
giuliomoro@15: {
giuliomoro@15: if (node.getAttribute('name') == name)
giuliomoro@15: {
giuliomoro@15: selected.push(node);
giuliomoro@15: }
giuliomoro@15: node = node.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: return selected;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: window.onload = function() {
giuliomoro@15: // Function called once the browser has loaded all files.
giuliomoro@15: // This should perform any initial commands such as structure / loading documents
giuliomoro@15:
giuliomoro@15: // Create a web audio API context
giuliomoro@15: // Fixed for cross-browser support
giuliomoro@15: var AudioContext = window.AudioContext || window.webkitAudioContext;
giuliomoro@15: audioContext = new AudioContext;
giuliomoro@15:
giuliomoro@15: // Create test state
giuliomoro@15: testState = new stateMachine();
giuliomoro@15:
giuliomoro@15: // Create the popup interface object
giuliomoro@15: popup = new interfacePopup();
giuliomoro@15:
giuliomoro@15: // Create the specification object
giuliomoro@15: specification = new Specification();
giuliomoro@15:
giuliomoro@15: // Create the interface object
giuliomoro@15: interfaceContext = new Interface(specification);
giuliomoro@15:
giuliomoro@15: // Create the storage object
giuliomoro@15: storage = new Storage();
giuliomoro@15: // Define window callbacks for interface
giuliomoro@15: window.onresize = function(event){interfaceContext.resizeWindow(event);};
giuliomoro@15:
giuliomoro@15: if (window.location.search.length != 0)
giuliomoro@15: {
giuliomoro@15: var search = window.location.search.split('?')[1];
giuliomoro@15: // Now split the requests into pairs
giuliomoro@20: var allowEarlyExit = false;
giuliomoro@20: var trainingURL = '';
giuliomoro@20:
giuliomoro@15: var searchQueries = search.split('&');
giuliomoro@15: for (var i in searchQueries)
giuliomoro@15: {
giuliomoro@15: // Split each key-value pair
giuliomoro@15: searchQueries[i] = searchQueries[i].split('=');
giuliomoro@15: var key = searchQueries[i][0];
giuliomoro@15: var value = decodeURIComponent(searchQueries[i][1]);
giuliomoro@15: switch(key) {
giuliomoro@15: case "url":
giuliomoro@15: url = value;
giuliomoro@15: break;
giuliomoro@15: case "returnURL":
giuliomoro@15: gReturnURL = value;
giuliomoro@15: break;
giuliomoro@16: case "saveFilenamePrefix":
giuliomoro@16: gSaveFilenamePrefix = value;
giuliomoro@16: break;
giuliomoro@17: case "allowEarlyExit":
giuliomoro@20: allowEarlyExit = value;
giuliomoro@20: break;
giuliomoro@20: case "trainingURL":
giuliomoro@20: trainingURL = value;
giuliomoro@17: break;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: loadProjectSpec(url);
giuliomoro@15: window.onbeforeunload = function() {
giuliomoro@15: return "Please only leave this page once you have completed the tests. Are you sure you have completed all testing?";
giuliomoro@15: };
giuliomoro@20: if(allowEarlyExit === "true" || allowEarlyExit === "show"){
giuliomoro@17: window.onbeforeunload = undefined;
giuliomoro@17: var msg = document.createElement('div');
giuliomoro@20: if(allowEarlyExit === "show"){
giuliomoro@20: msg.innerHTML = 'You can exit this training and go back to the test at any time by clicking here.';
giuliomoro@20: } else if (allowEarlyExit === "true"){
giuliomoro@20: msg.innerHTML = 'You can close this training window at any time.';
giuliomoro@20: }
giuliomoro@17: msg.id = 'earlyExitBox';
giuliomoro@20: msg.className = 'bottomBox';
giuliomoro@20: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@20: }
giuliomoro@20: if(trainingURL.length > 0){
giuliomoro@20: var msg = document.createElement('div');
giuliomoro@20: msg.innerHTML = 'You can open the training page in a new tab by clicking here.';
giuliomoro@20: msg.id = 'trainingUrlBox';
giuliomoro@20: msg.className = 'bottomBox';
giuliomoro@17: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@17: }
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: function loadProjectSpec(url) {
giuliomoro@15: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
giuliomoro@15: // If url is null, request client to upload project XML document
giuliomoro@15: var xmlhttp = new XMLHttpRequest();
giuliomoro@15: xmlhttp.open("GET",'xml/test-schema.xsd',true);
giuliomoro@15: xmlhttp.onload = function()
giuliomoro@15: {
giuliomoro@15: schemaXSD = xmlhttp.response;
giuliomoro@15: var parse = new DOMParser();
giuliomoro@15: specification.schema = parse.parseFromString(xmlhttp.response,'text/xml');
giuliomoro@15: var r = new XMLHttpRequest();
giuliomoro@15: r.open('GET',url,true);
giuliomoro@15: r.onload = function() {
giuliomoro@15: loadProjectSpecCallback(r.response);
giuliomoro@15: };
giuliomoro@15: r.onerror = function() {
giuliomoro@15: document.getElementsByTagName('body')[0].innerHTML = null;
giuliomoro@15: var msg = document.createElement("h3");
giuliomoro@15: msg.textContent = "FATAL ERROR";
giuliomoro@15: var span = document.createElement("p");
giuliomoro@15: span.textContent = "There was an error when loading your XML file. Please check your path in the URL. After the path to this page, there should be '?url=path/to/your/file.xml'. Check the spelling of your filename as well. If you are still having issues, check the log of the python server or your webserver distribution for 404 codes for your file.";
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(span);
giuliomoro@15: }
giuliomoro@15: r.send();
giuliomoro@15: };
giuliomoro@15: xmlhttp.send();
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: function loadProjectSpecCallback(response) {
giuliomoro@15: // Function called after asynchronous download of XML project specification
giuliomoro@15: //var decode = $.parseXML(response);
giuliomoro@15: //projectXML = $(decode);
giuliomoro@15:
giuliomoro@15: // Check if XML is new or a resumption
giuliomoro@15: var parse = new DOMParser();
giuliomoro@15: var responseDocument = parse.parseFromString(response,'text/xml');
giuliomoro@15: var errorNode = responseDocument.getElementsByTagName('parsererror');
giuliomoro@15: if (errorNode.length >= 1)
giuliomoro@15: {
giuliomoro@15: var msg = document.createElement("h3");
giuliomoro@15: msg.textContent = "FATAL ERROR";
giuliomoro@15: var span = document.createElement("span");
giuliomoro@15: span.textContent = "The XML parser returned the following errors when decoding your XML file";
giuliomoro@15: document.getElementsByTagName('body')[0].innerHTML = null;
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(span);
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(errorNode[0]);
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: if (responseDocument == undefined || responseDocument.firstChild == undefined) {
giuliomoro@15: var msg = document.createElement("h3");
giuliomoro@15: msg.textContent = "FATAL ERROR";
giuliomoro@15: var span = document.createElement("span");
giuliomoro@15: span.textContent = "The project XML was not decoded properly, try refreshing your browser and clearing caches. If the problem persists, contact the test creator.";
giuliomoro@15: document.getElementsByTagName('body')[0].innerHTML = null;
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(span);
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: if (responseDocument.firstChild.nodeName == "waet") {
giuliomoro@15: // document is a specification
giuliomoro@15:
giuliomoro@15: // Perform XML schema validation
giuliomoro@15: var Module = {
giuliomoro@15: xml: response,
giuliomoro@15: schema: schemaXSD,
giuliomoro@15: arguments:["--noout", "--schema", 'test-schema.xsd','document.xml']
giuliomoro@15: };
giuliomoro@15: projectXML = responseDocument;
giuliomoro@15: var xmllint = validateXML(Module);
giuliomoro@15: console.log(xmllint);
giuliomoro@15: if(xmllint != 'document.xml validates\n')
giuliomoro@15: {
giuliomoro@15: document.getElementsByTagName('body')[0].innerHTML = null;
giuliomoro@15: var msg = document.createElement("h3");
giuliomoro@15: msg.textContent = "FATAL ERROR";
giuliomoro@15: var span = document.createElement("h4");
giuliomoro@15: span.textContent = "The XML validator returned the following errors when decoding your XML file";
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(msg);
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(span);
giuliomoro@15: xmllint = xmllint.split('\n');
giuliomoro@15: for (var i in xmllint)
giuliomoro@15: {
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(document.createElement('br'));
giuliomoro@15: var span = document.createElement("span");
giuliomoro@15: span.textContent = xmllint[i];
giuliomoro@15: document.getElementsByTagName('body')[0].appendChild(span);
giuliomoro@15: }
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: // Build the specification
giuliomoro@15: specification.decode(projectXML);
giuliomoro@15: // Generate the session-key
giuliomoro@15: storage.initialise();
giuliomoro@15:
giuliomoro@15: } else if (responseDocument.firstChild.nodeName == "waetresult") {
giuliomoro@15: // document is a result
giuliomoro@15: projectXML = document.implementation.createDocument(null,"waet");
giuliomoro@15: projectXML.firstChild.appendChild(responseDocument.getElementsByTagName('waet')[0].getElementsByTagName("setup")[0].cloneNode(true));
giuliomoro@15: var child = responseDocument.firstChild.firstChild;
giuliomoro@15: while (child != null) {
giuliomoro@15: if (child.nodeName == "survey") {
giuliomoro@15: // One of the global survey elements
giuliomoro@15: if (child.getAttribute("state") == "complete") {
giuliomoro@15: // We need to remove this survey from
giuliomoro@15: var location = child.getAttribute("location");
giuliomoro@15: var globalSurveys = projectXML.getElementsByTagName("setup")[0].getElementsByTagName("survey")[0];
giuliomoro@15: while(globalSurveys != null) {
giuliomoro@15: if (location == "pre" || location == "before") {
giuliomoro@15: if (globalSurveys.getAttribute("location") == "pre" || globalSurveys.getAttribute("location") == "before") {
giuliomoro@15: projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys);
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: } else {
giuliomoro@15: if (globalSurveys.getAttribute("location") == "post" || globalSurveys.getAttribute("location") == "after") {
giuliomoro@15: projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys);
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: globalSurveys = globalSurveys.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: } else {
giuliomoro@15: // We need to complete this, so it must be regenerated by store
giuliomoro@15: var copy = child;
giuliomoro@15: child = child.previousElementSibling;
giuliomoro@15: responseDocument.firstChild.removeChild(copy);
giuliomoro@15: }
giuliomoro@15: } else if (child.nodeName == "page") {
giuliomoro@15: if (child.getAttribute("state") == "empty") {
giuliomoro@15: // We need to complete this page
giuliomoro@15: projectXML.firstChild.appendChild(responseDocument.getElementById(child.getAttribute("ref")).cloneNode(true));
giuliomoro@15: var copy = child;
giuliomoro@15: child = child.previousElementSibling;
giuliomoro@15: responseDocument.firstChild.removeChild(copy);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: child = child.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: // Build the specification
giuliomoro@15: specification.decode(projectXML);
giuliomoro@15: // Use the original
giuliomoro@15: storage.initialise(responseDocument);
giuliomoro@15: }
giuliomoro@15: /// CHECK FOR SAMPLE RATE COMPATIBILITY
giuliomoro@15: if (specification.sampleRate != undefined) {
giuliomoro@15: if (Number(specification.sampleRate) != audioContext.sampleRate) {
giuliomoro@15: 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.';
giuliomoro@15: alert(errStr);
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // Detect the interface to use and load the relevant javascripts.
giuliomoro@15: var interfaceJS = document.createElement('script');
giuliomoro@15: interfaceJS.setAttribute("type","text/javascript");
giuliomoro@15: switch(specification.interface)
giuliomoro@15: {
giuliomoro@15: case "APE":
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/ape.js");
giuliomoro@15:
giuliomoro@15: // APE comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/ape.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15:
giuliomoro@15: case "MUSHRA":
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/mushra.js");
giuliomoro@15:
giuliomoro@15: // MUSHRA comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/mushra.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15:
giuliomoro@15: case "AB":
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/AB.js");
giuliomoro@15:
giuliomoro@15: // AB comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/AB.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15:
giuliomoro@15: case "ABX":
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/ABX.js");
giuliomoro@15:
giuliomoro@15: // AB comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/ABX.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15:
giuliomoro@15: case "Bipolar":
giuliomoro@15: case "ACR":
giuliomoro@15: case "DCR":
giuliomoro@15: case "CCR":
giuliomoro@15: case "ABC":
giuliomoro@15: // Above enumerate to horizontal sliders
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/horizontal-sliders.js");
giuliomoro@15:
giuliomoro@15: // horizontal-sliders comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/horizontal-sliders.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15: case "discrete":
giuliomoro@15: case "likert":
giuliomoro@15: // Above enumerate to horizontal discrete radios
giuliomoro@15: interfaceJS.setAttribute("src","interfaces/discrete.js");
giuliomoro@15:
giuliomoro@15: // horizontal-sliders comes with a css file
giuliomoro@15: var css = document.createElement('link');
giuliomoro@15: css.rel = 'stylesheet';
giuliomoro@15: css.type = 'text/css';
giuliomoro@15: css.href = 'interfaces/discrete.css';
giuliomoro@15:
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(css);
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: document.getElementsByTagName("head")[0].appendChild(interfaceJS);
giuliomoro@15:
giuliomoro@15: if (gReturnURL != undefined) {
giuliomoro@15: console.log("returnURL Overide from "+specification.returnURL+" to "+gReturnURL);
giuliomoro@15: specification.returnURL = gReturnURL;
giuliomoro@15: }
giuliomoro@16: if (gSaveFilenamePrefix != undefined){
giuliomoro@16: specification.saveFilenamePrefix = gSaveFilenamePrefix;
giuliomoro@16: }
giuliomoro@15:
giuliomoro@15: // Create the audio engine object
giuliomoro@15: audioEngineContext = new AudioEngine(specification);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function createProjectSave(destURL) {
giuliomoro@15: // Clear the window.onbeforeunload
giuliomoro@15: window.onbeforeunload = null;
giuliomoro@15: // Save the data from interface into XML and send to destURL
giuliomoro@15: // If destURL is null then download XML in client
giuliomoro@15: // Now time to render file locally
giuliomoro@15: var xmlDoc = interfaceXMLSave();
giuliomoro@15: var parent = document.createElement("div");
giuliomoro@15: parent.appendChild(xmlDoc);
giuliomoro@15: var file = [parent.innerHTML];
giuliomoro@15: if (destURL == "local") {
giuliomoro@15: var bb = new Blob(file,{type : 'application/xml'});
giuliomoro@15: var dnlk = window.URL.createObjectURL(bb);
giuliomoro@15: var a = document.createElement("a");
giuliomoro@15: a.hidden = '';
giuliomoro@15: a.href = dnlk;
giuliomoro@15: a.download = "save.xml";
giuliomoro@15: a.textContent = "Save File";
giuliomoro@15:
giuliomoro@15: popup.showPopup();
giuliomoro@15: popup.popupContent.innerHTML = "Please save the file below to give to your test supervisor
";
giuliomoro@15: popup.popupContent.appendChild(a);
giuliomoro@15: } else {
giuliomoro@16: var saveUrlSuffix = "";
giuliomoro@16: var saveFilenamePrefix = specification.saveFilenamePrefix;
giuliomoro@16: if(typeof(saveFilenamePrefix) === "string" && saveFilenamePrefix.length > 0){
giuliomoro@16: saveUrlSuffix = "&saveFilenamePrefix="+saveFilenamePrefix;
giuliomoro@16: }
giuliomoro@16: var projectReturn = "";
giuliomoro@16: if (typeof specification.projectReturn == "string") {
giuliomoro@16: if (specification.projectReturn.substr(0,4) == "http") {
giuliomoro@16: projectReturn = specification.projectReturn;
giuliomoro@16: }
giuliomoro@16: }
giuliomoro@16: var saveURL = projectReturn+"php/save.php?key="+storage.SessionKey.key+saveUrlSuffix;
giuliomoro@15: var xmlhttp = new XMLHttpRequest;
giuliomoro@16: xmlhttp.open("POST", saveURL, true);
giuliomoro@15: xmlhttp.setRequestHeader('Content-Type', 'text/xml');
giuliomoro@15: xmlhttp.onerror = function(){
giuliomoro@15: console.log('Error saving file to server! Presenting download locally');
giuliomoro@15: createProjectSave("local");
giuliomoro@15: };
giuliomoro@15: xmlhttp.onload = function() {
giuliomoro@15: console.log(xmlhttp);
giuliomoro@15: if (this.status >= 300) {
giuliomoro@15: console.log("WARNING - Could not update at this time");
giuliomoro@15: createProjectSave("local");
giuliomoro@15: } else {
giuliomoro@15: var parser = new DOMParser();
giuliomoro@15: var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
giuliomoro@15: var response = xmlDoc.getElementsByTagName('response')[0];
giuliomoro@15: if (response.getAttribute("state") == "OK") {
giuliomoro@15: window.onbeforeunload = undefined;
giuliomoro@15: var file = response.getElementsByTagName("file")[0];
giuliomoro@15: console.log("Save: OK, written "+file.getAttribute("bytes")+"B");
giuliomoro@16: if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) {
giuliomoro@15: window.location = specification.returnURL;
giuliomoro@15: } else {
giuliomoro@15: popup.popupContent.textContent = specification.exitText;
giuliomoro@15: }
giuliomoro@15: } else {
giuliomoro@15: var message = response.getElementsByTagName("message");
giuliomoro@15: console.log("Save: Error! "+message.textContent);
giuliomoro@15: createProjectSave("local");
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: xmlhttp.send(file);
giuliomoro@15: popup.showPopup();
giuliomoro@15: popup.popupContent.innerHTML = null;
giuliomoro@15: popup.popupContent.textContent = "Submitting. Please Wait";
giuliomoro@15: if(typeof(popup.hideNextButton) === "function"){
giuliomoro@15: popup.hideNextButton();
giuliomoro@15: }
giuliomoro@15: if(typeof(popup.hidePreviousButton) === "function"){
giuliomoro@15: popup.hidePreviousButton();
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function errorSessionDump(msg){
giuliomoro@15: // Create the partial interface XML save
giuliomoro@15: // Include error node with message on why the dump occured
giuliomoro@15: popup.showPopup();
giuliomoro@15: popup.popupContent.innerHTML = null;
giuliomoro@15: var err = document.createElement('error');
giuliomoro@15: var parent = document.createElement("div");
giuliomoro@15: if (typeof msg === "object")
giuliomoro@15: {
giuliomoro@15: err.appendChild(msg);
giuliomoro@15: popup.popupContent.appendChild(msg);
giuliomoro@15:
giuliomoro@15: } else {
giuliomoro@15: err.textContent = msg;
giuliomoro@15: popup.popupContent.innerHTML = "ERROR : "+msg;
giuliomoro@15: }
giuliomoro@15: var xmlDoc = interfaceXMLSave();
giuliomoro@15: xmlDoc.appendChild(err);
giuliomoro@15: parent.appendChild(xmlDoc);
giuliomoro@15: var file = [parent.innerHTML];
giuliomoro@15: var bb = new Blob(file,{type : 'application/xml'});
giuliomoro@15: var dnlk = window.URL.createObjectURL(bb);
giuliomoro@15: var a = document.createElement("a");
giuliomoro@15: a.hidden = '';
giuliomoro@15: a.href = dnlk;
giuliomoro@15: a.download = "save.xml";
giuliomoro@15: a.textContent = "Save File";
giuliomoro@15:
giuliomoro@15:
giuliomoro@15:
giuliomoro@15: popup.popupContent.appendChild(a);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // Only other global function which must be defined in the interface class. Determines how to create the XML document.
giuliomoro@15: function interfaceXMLSave(){
giuliomoro@15: // Create the XML string to be exported with results
giuliomoro@15: return storage.finish();
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function linearToDecibel(gain)
giuliomoro@15: {
giuliomoro@15: return 20.0*Math.log10(gain);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function decibelToLinear(gain)
giuliomoro@15: {
giuliomoro@15: return Math.pow(10,gain/20.0);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function secondsToSamples(time,fs) {
giuliomoro@15: return Math.round(time*fs);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function samplesToSeconds(samples,fs) {
giuliomoro@15: return samples / fs;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function randomString(length) {
giuliomoro@15: return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function randomiseOrder(input)
giuliomoro@15: {
giuliomoro@15: // This takes an array of information and randomises the order
giuliomoro@15: var N = input.length;
giuliomoro@15:
giuliomoro@15: var inputSequence = []; // For safety purposes: keep track of randomisation
giuliomoro@15: for (var counter = 0; counter < N; ++counter)
giuliomoro@15: inputSequence.push(counter) // Fill array
giuliomoro@15: var inputSequenceClone = inputSequence.slice(0);
giuliomoro@15:
giuliomoro@15: var holdArr = [];
giuliomoro@15: var outputSequence = [];
giuliomoro@15: for (var n=0; n array.length) {
giuliomoro@15: num = array.length;
giuliomoro@15: }
giuliomoro@15: var ret = [];
giuliomoro@15: while (num > 0) {
giuliomoro@15: var index = Math.floor(Math.random() * array.length);
giuliomoro@15: ret.push( array.splice(index,1)[0] );
giuliomoro@15: num--;
giuliomoro@15: }
giuliomoro@15: return ret;
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function interfacePopup() {
giuliomoro@15: // Creates an object to manage the popup
giuliomoro@15: this.popup = null;
giuliomoro@15: this.popupContent = null;
giuliomoro@15: this.popupTitle = null;
giuliomoro@15: this.popupResponse = null;
giuliomoro@15: this.buttonProceed = null;
giuliomoro@15: this.buttonPrevious = null;
giuliomoro@15: this.popupOptions = null;
giuliomoro@15: this.currentIndex = null;
giuliomoro@15: this.node = null;
giuliomoro@15: this.store = null;
giuliomoro@15: $(window).keypress(function(e){
giuliomoro@17: if (e.keyCode == 13 && e.shiftKey === true && popup.popup.style.visibility == 'visible')
giuliomoro@15: {
giuliomoro@15: console.log(e);
giuliomoro@15: popup.buttonProceed.onclick();
giuliomoro@15: e.preventDefault();
giuliomoro@15: }
giuliomoro@15: });
giuliomoro@15:
giuliomoro@15: this.createPopup = function(){
giuliomoro@15: // Create popup window interface
giuliomoro@15: var insertPoint = document.getElementById("topLevelBody");
giuliomoro@15:
giuliomoro@15: this.popup = document.getElementById('popupHolder');
giuliomoro@15: this.popup.style.left = (window.innerWidth/2)-250 + 'px';
giuliomoro@15: this.popup.style.top = (window.innerHeight/2)-125 + 'px';
giuliomoro@15:
giuliomoro@15: this.popupContent = document.getElementById('popupContent');
giuliomoro@15:
giuliomoro@15: this.popupTitle = document.getElementById('popupTitle');
giuliomoro@15:
giuliomoro@15: this.popupResponse = document.getElementById('popupResponse');
giuliomoro@15:
giuliomoro@15: this.buttonProceed = document.getElementById('popup-proceed');
giuliomoro@15: this.buttonProceed.onclick = function(){popup.proceedClicked();};
giuliomoro@15:
giuliomoro@15: this.buttonPrevious = document.getElementById('popup-previous');
giuliomoro@15: this.buttonPrevious.onclick = function(){popup.previousClick();};
giuliomoro@15:
giuliomoro@15: this.hidePopup();
giuliomoro@15: this.popup.style.visibility = 'hidden';
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.showPopup = function(){
giuliomoro@15: if (this.popup == null) {
giuliomoro@15: this.createPopup();
giuliomoro@15: }
giuliomoro@15: this.popup.style.visibility = 'visible';
giuliomoro@15: var blank = document.getElementsByClassName('testHalt')[0];
giuliomoro@15: blank.style.visibility = 'visible';
giuliomoro@15: this.popupResponse.style.left="0%";
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.hidePopup = function(){
giuliomoro@15: if (this.popup) {
giuliomoro@15: this.popup.style.visibility = 'hidden';
giuliomoro@15: var blank = document.getElementsByClassName('testHalt')[0];
giuliomoro@15: blank.style.visibility = 'hidden';
giuliomoro@15: this.buttonPrevious.style.visibility = 'inherit';
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.postNode = function() {
giuliomoro@15: // This will take the node from the popupOptions and display it
giuliomoro@15: var node = this.popupOptions[this.currentIndex];
giuliomoro@15: this.popupResponse.innerHTML = null;
giuliomoro@15: this.popupTitle.textContent = node.specification.statement;
giuliomoro@15: if (node.specification.type == 'question') {
giuliomoro@15: var textArea = document.createElement('textarea');
giuliomoro@15: switch (node.specification.boxsize) {
giuliomoro@15: case 'small':
giuliomoro@15: textArea.cols = "20";
giuliomoro@15: textArea.rows = "1";
giuliomoro@15: break;
giuliomoro@15: case 'normal':
giuliomoro@15: textArea.cols = "30";
giuliomoro@15: textArea.rows = "2";
giuliomoro@15: break;
giuliomoro@15: case 'large':
giuliomoro@15: textArea.cols = "40";
giuliomoro@15: textArea.rows = "5";
giuliomoro@15: break;
giuliomoro@15: case 'huge':
giuliomoro@15: textArea.cols = "50";
giuliomoro@15: textArea.rows = "10";
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: if (node.response == undefined) {
giuliomoro@15: node.response = "";
giuliomoro@15: } else {
giuliomoro@15: textArea.value = node.response;
giuliomoro@15: }
giuliomoro@15: this.popupResponse.appendChild(textArea);
giuliomoro@15: textArea.focus();
giuliomoro@15: this.popupResponse.style.textAlign="center";
giuliomoro@15: this.popupResponse.style.left="0%";
giuliomoro@15: } else if (node.specification.type == 'checkbox') {
giuliomoro@15: if (node.response == undefined) {
giuliomoro@15: node.response = Array(node.specification.options.length);
giuliomoro@15: }
giuliomoro@15: var index = 0;
giuliomoro@15: var max_w = 0;
giuliomoro@15: for (var option of node.specification.options) {
giuliomoro@15: var input = document.createElement('input');
giuliomoro@15: input.id = option.name;
giuliomoro@15: input.type = 'checkbox';
giuliomoro@15: var span = document.createElement('span');
giuliomoro@15: span.textContent = option.text;
giuliomoro@15: var hold = document.createElement('div');
giuliomoro@15: hold.setAttribute('name','option');
giuliomoro@15: hold.className = "popup-option-checbox";
giuliomoro@15: hold.appendChild(input);
giuliomoro@15: hold.appendChild(span);
giuliomoro@15: this.popupResponse.appendChild(hold);
giuliomoro@15: if (node.response[index] != undefined){
giuliomoro@15: if (node.response[index].checked == true) {
giuliomoro@15: input.checked = "true";
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: var w = $(hold).width();
giuliomoro@15: if (w > max_w)
giuliomoro@15: max_w = w;
giuliomoro@15: index++;
giuliomoro@15: }
giuliomoro@15: this.popupResponse.style.textAlign="";
giuliomoro@15: } else if (node.specification.type == 'radio') {
giuliomoro@15: if (node.response == undefined) {
giuliomoro@15: node.response = {name: "", text: ""};
giuliomoro@15: }
giuliomoro@15: var index = 0;
giuliomoro@15: var max_w = 0;
giuliomoro@15: for (var option of node.specification.options) {
giuliomoro@15: var input = document.createElement('input');
giuliomoro@15: input.id = option.name;
giuliomoro@15: input.type = 'radio';
giuliomoro@15: input.name = node.specification.id;
giuliomoro@15: var span = document.createElement('span');
giuliomoro@15: span.textContent = option.text;
giuliomoro@15: var hold = document.createElement('div');
giuliomoro@15: hold.setAttribute('name','option');
giuliomoro@15: hold.className = "popup-option-checbox";
giuliomoro@15: hold.appendChild(input);
giuliomoro@15: hold.appendChild(span);
giuliomoro@15: this.popupResponse.appendChild(hold);
giuliomoro@15: if (input.id == node.response.name) {
giuliomoro@15: input.checked = "true";
giuliomoro@15: }
giuliomoro@15: var w = $(hold).width();
giuliomoro@15: if (w > max_w)
giuliomoro@15: max_w = w;
giuliomoro@15: }
giuliomoro@15: this.popupResponse.style.textAlign="";
giuliomoro@15: } else if (node.specification.type == 'number') {
giuliomoro@15: var input = document.createElement('input');
giuliomoro@15: input.type = 'textarea';
giuliomoro@15: if (node.min != null) {input.min = node.specification.min;}
giuliomoro@15: if (node.max != null) {input.max = node.specification.max;}
giuliomoro@15: if (node.step != null) {input.step = node.specification.step;}
giuliomoro@15: if (node.response != undefined) {
giuliomoro@15: input.value = node.response;
giuliomoro@15: }
giuliomoro@15: this.popupResponse.appendChild(input);
giuliomoro@15: this.popupResponse.style.textAlign="center";
giuliomoro@15: this.popupResponse.style.left="0%";
giuliomoro@15: }
giuliomoro@15: if(this.currentIndex+1 == this.popupOptions.length) {
giuliomoro@15: if (this.node.location == "pre") {
giuliomoro@15: this.buttonProceed.textContent = 'Start';
giuliomoro@15: } else {
giuliomoro@15: this.buttonProceed.textContent = 'Submit';
giuliomoro@15: }
giuliomoro@15: } else {
giuliomoro@15: this.buttonProceed.textContent = 'Next';
giuliomoro@15: }
giuliomoro@15: if(this.currentIndex > 0)
giuliomoro@15: this.buttonPrevious.style.visibility = 'visible';
giuliomoro@15: else
giuliomoro@15: this.buttonPrevious.style.visibility = 'hidden';
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.initState = function(node,store) {
giuliomoro@15: //Call this with your preTest and postTest nodes when needed to
giuliomoro@15: // initialise the popup procedure.
giuliomoro@15: if (node.options.length > 0) {
giuliomoro@15: this.popupOptions = [];
giuliomoro@15: this.node = node;
giuliomoro@15: this.store = store;
giuliomoro@15: for (var opt of node.options)
giuliomoro@15: {
giuliomoro@15: this.popupOptions.push({
giuliomoro@15: specification: opt,
giuliomoro@15: response: null
giuliomoro@15: });
giuliomoro@15: }
giuliomoro@15: this.currentIndex = 0;
giuliomoro@15: this.showPopup();
giuliomoro@15: this.postNode();
giuliomoro@15: } else {
giuliomoro@15: advanceState();
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.proceedClicked = function() {
giuliomoro@15: // Each time the popup button is clicked!
giuliomoro@15: if (testState.stateIndex == 0 && specification.calibration) {
giuliomoro@15: interfaceContext.calibrationModuleObject.collect();
giuliomoro@15: advanceState();
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: var node = this.popupOptions[this.currentIndex];
giuliomoro@15: if (node.specification.type == 'question') {
giuliomoro@15: // Must extract the question data
giuliomoro@15: var textArea = $(popup.popupContent).find('textarea')[0];
giuliomoro@15: if (node.specification.mandatory == true && textArea.value.length == 0) {
giuliomoro@15: alert('This question is mandatory');
giuliomoro@15: return;
giuliomoro@15: } else {
giuliomoro@15: // Save the text content
giuliomoro@15: console.log("Question: "+ node.specification.statement);
giuliomoro@15: console.log("Question Response: "+ textArea.value);
giuliomoro@15: node.response = textArea.value;
giuliomoro@15: }
giuliomoro@15: } else if (node.specification.type == 'checkbox') {
giuliomoro@15: // Must extract checkbox data
giuliomoro@15: console.log("Checkbox: "+ node.specification.statement);
giuliomoro@15: var inputs = this.popupResponse.getElementsByTagName('input');
giuliomoro@15: node.response = [];
giuliomoro@15: for (var i=0; i node.max && node.max != null) {
giuliomoro@15: alert('Number is above the maximum value of '+node.max);
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: node.response = input.value;
giuliomoro@15: }
giuliomoro@15: this.currentIndex++;
giuliomoro@15: if (this.currentIndex < this.popupOptions.length) {
giuliomoro@15: this.postNode();
giuliomoro@15: } else {
giuliomoro@15: // Reached the end of the popupOptions
giuliomoro@15: this.hidePopup();
giuliomoro@15: for (var node of this.popupOptions)
giuliomoro@15: {
giuliomoro@15: this.store.postResult(node);
giuliomoro@15: }
giuliomoro@15: this.store.complete();
giuliomoro@15: advanceState();
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.previousClick = function() {
giuliomoro@15: // Triggered when the 'Back' button is clicked in the survey
giuliomoro@15: if (this.currentIndex > 0) {
giuliomoro@15: this.currentIndex--;
giuliomoro@15: this.postNode();
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.resize = function(event)
giuliomoro@15: {
giuliomoro@15: // Called on window resize;
giuliomoro@15: if (this.popup != null) {
giuliomoro@15: this.popup.style.left = (window.innerWidth/2)-250 + 'px';
giuliomoro@15: this.popup.style.top = (window.innerHeight/2)-125 + 'px';
giuliomoro@15: var blank = document.getElementsByClassName('testHalt')[0];
giuliomoro@15: blank.style.width = window.innerWidth;
giuliomoro@15: blank.style.height = window.innerHeight;
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.hideNextButton = function() {
giuliomoro@15: this.buttonProceed.style.visibility = "hidden";
giuliomoro@15: }
giuliomoro@15: this.hidePreviousButton = function() {
giuliomoro@15: this.buttonPrevious.style.visibility = "hidden";
giuliomoro@15: }
giuliomoro@15: this.showNextButton = function() {
giuliomoro@15: this.buttonProceed.style.visibility = "visible";
giuliomoro@15: }
giuliomoro@15: this.showPreviousButton = function() {
giuliomoro@15: this.buttonPrevious.style.visibility = "visible";
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function advanceState()
giuliomoro@15: {
giuliomoro@15: // Just for complete clarity
giuliomoro@15: testState.advanceState();
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function stateMachine()
giuliomoro@15: {
giuliomoro@15: // Object prototype for tracking and managing the test state
giuliomoro@15: this.stateMap = [];
giuliomoro@15: this.preTestSurvey = null;
giuliomoro@15: this.postTestSurvey = null;
giuliomoro@15: this.stateIndex = null;
giuliomoro@15: this.currentStateMap = null;
giuliomoro@15: this.currentStatePosition = null;
giuliomoro@15: this.currentStore = null;
giuliomoro@15: this.initialise = function(){
giuliomoro@15:
giuliomoro@15: // Get the data from Specification
giuliomoro@15: var pagePool = [];
giuliomoro@15: var pageInclude = [];
giuliomoro@15: for (var page of specification.pages)
giuliomoro@15: {
giuliomoro@15: if (page.alwaysInclude) {
giuliomoro@15: pageInclude.push(page);
giuliomoro@15: } else {
giuliomoro@15: pagePool.push(page);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // Find how many are left to get
giuliomoro@15: var numPages = specification.poolSize;
giuliomoro@15: if (numPages > pagePool.length) {
giuliomoro@15: console.log("WARNING - You have specified more pages in than you have created!!");
giuliomoro@15: numPages = specification.pages.length;
giuliomoro@15: }
giuliomoro@15: if (specification.poolSize == 0) {
giuliomoro@15: numPages = specification.pages.length;
giuliomoro@15: }
giuliomoro@15: numPages -= pageInclude.length;
giuliomoro@15:
giuliomoro@15: if (numPages > 0) {
giuliomoro@15: // Go find the rest of the pages from the pool
giuliomoro@15: var subarr = null;
giuliomoro@15: if (specification.randomiseOrder) {
giuliomoro@15: // Append a random sub-array
giuliomoro@15: subarr = randomSubArray(pagePool,numPages);
giuliomoro@15: } else {
giuliomoro@15: // Append the matching number
giuliomoro@15: subarr = pagePool.slice(0,numPages);
giuliomoro@15: }
giuliomoro@15: pageInclude = pageInclude.concat(subarr);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: // We now have our selected pages in pageInclude array
giuliomoro@15: if (specification.randomiseOrder)
giuliomoro@15: {
giuliomoro@15: pageInclude = randomiseOrder(pageInclude);
giuliomoro@15: }
giuliomoro@15: for (var i=0; i 0) {
giuliomoro@15: if(this.stateIndex != null) {
giuliomoro@15: console.log('NOTE - State already initialise');
giuliomoro@15: }
giuliomoro@15: this.stateIndex = -2;
giuliomoro@15: console.log('Starting test...');
giuliomoro@15: } else {
giuliomoro@15: console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.advanceState = function(){
giuliomoro@15: if (this.stateIndex == null) {
giuliomoro@15: this.initialise();
giuliomoro@15: }
giuliomoro@15: storage.update();
giuliomoro@15: if (this.stateIndex == -2) {
giuliomoro@15: this.stateIndex++;
giuliomoro@15: if (this.preTestSurvey != null)
giuliomoro@15: {
giuliomoro@15: popup.initState(this.preTestSurvey,storage.globalPreTest);
giuliomoro@15: } else {
giuliomoro@15: this.advanceState();
giuliomoro@15: }
giuliomoro@15: } else if (this.stateIndex == -1) {
giuliomoro@15: this.stateIndex++;
giuliomoro@15: if (specification.calibration) {
giuliomoro@15: popup.showPopup();
giuliomoro@15: popup.popupTitle.textContent = "Calibration. Set the levels so all tones are of equal amplitude. Move your mouse over the sliders to hear the tones. The red slider is the reference tone";
giuliomoro@15: interfaceContext.calibrationModuleObject = new interfaceContext.calibrationModule();
giuliomoro@15: interfaceContext.calibrationModuleObject.build(popup.popupResponse);
giuliomoro@15: popup.hidePreviousButton();
giuliomoro@15: } else {
giuliomoro@15: this.advanceState();
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: else if (this.stateIndex == this.stateMap.length)
giuliomoro@15: {
giuliomoro@15: // All test pages complete, post test
giuliomoro@15: console.log('Ending test ...');
giuliomoro@15: this.stateIndex++;
giuliomoro@15: if (this.postTestSurvey == null) {
giuliomoro@15: this.advanceState();
giuliomoro@15: } else {
giuliomoro@15: popup.initState(this.postTestSurvey,storage.globalPostTest);
giuliomoro@15: }
giuliomoro@15: } else if (this.stateIndex > this.stateMap.length)
giuliomoro@15: {
giuliomoro@15: createProjectSave(specification.projectReturn);
giuliomoro@15: }
giuliomoro@15: else
giuliomoro@15: {
giuliomoro@15: popup.hidePopup();
giuliomoro@15: if (this.currentStateMap == null)
giuliomoro@15: {
giuliomoro@15: this.currentStateMap = this.stateMap[this.stateIndex];
giuliomoro@15: if (this.currentStateMap.randomiseOrder)
giuliomoro@15: {
giuliomoro@15: this.currentStateMap.audioElements = randomiseOrder(this.currentStateMap.audioElements);
giuliomoro@15: }
giuliomoro@15: this.currentStore = storage.testPages[this.stateIndex];
giuliomoro@15: if (this.currentStateMap.preTest != null)
giuliomoro@15: {
giuliomoro@15: this.currentStatePosition = 'pre';
giuliomoro@15: popup.initState(this.currentStateMap.preTest,storage.testPages[this.stateIndex].preTest);
giuliomoro@15: } else {
giuliomoro@15: this.currentStatePosition = 'test';
giuliomoro@15: }
giuliomoro@15: interfaceContext.newPage(this.currentStateMap,storage.testPages[this.stateIndex]);
giuliomoro@15: return;
giuliomoro@15: }
giuliomoro@15: switch(this.currentStatePosition)
giuliomoro@15: {
giuliomoro@15: case 'pre':
giuliomoro@15: this.currentStatePosition = 'test';
giuliomoro@15: break;
giuliomoro@15: case 'test':
giuliomoro@15: this.currentStatePosition = 'post';
giuliomoro@15: // Save the data
giuliomoro@15: this.testPageCompleted();
giuliomoro@15: if (this.currentStateMap.postTest == null)
giuliomoro@15: {
giuliomoro@15: this.advanceState();
giuliomoro@15: return;
giuliomoro@15: } else {
giuliomoro@15: popup.initState(this.currentStateMap.postTest,storage.testPages[this.stateIndex].postTest);
giuliomoro@15: }
giuliomoro@15: break;
giuliomoro@15: case 'post':
giuliomoro@15: this.stateIndex++;
giuliomoro@15: this.currentStateMap = null;
giuliomoro@15: this.advanceState();
giuliomoro@15: break;
giuliomoro@15: };
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.testPageCompleted = function() {
giuliomoro@15: // Function called each time a test page has been completed
giuliomoro@15: var storePoint = storage.testPages[this.stateIndex];
giuliomoro@15: // First get the test metric
giuliomoro@15:
giuliomoro@15: var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0];
giuliomoro@15: if (audioEngineContext.metric.enableTestTimer)
giuliomoro@15: {
giuliomoro@15: var testTime = storePoint.parent.document.createElement('metricresult');
giuliomoro@15: testTime.id = 'testTime';
giuliomoro@15: testTime.textContent = audioEngineContext.timer.testDuration;
giuliomoro@15: metric.appendChild(testTime);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: var audioObjects = audioEngineContext.audioObjects;
giuliomoro@15: for (var ao of audioEngineContext.audioObjects)
giuliomoro@15: {
giuliomoro@15: ao.exportXMLDOM();
giuliomoro@15: }
giuliomoro@15: for (var element of interfaceContext.commentQuestions)
giuliomoro@15: {
giuliomoro@15: element.exportXMLDOM(storePoint);
giuliomoro@15: }
giuliomoro@15: pageXMLSave(storePoint.XMLDOM, this.currentStateMap);
giuliomoro@15: storePoint.complete();
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.getCurrentTestPage = function() {
giuliomoro@15: if (this.stateIndex >= 0 && this.stateIndex< this.stateMap.length) {
giuliomoro@15: return this.currentStateMap;
giuliomoro@15: } else {
giuliomoro@15: return null;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: this.getCurrentTestPageStore = function() {
giuliomoro@15: if (this.stateIndex >= 0 && this.stateIndex< this.stateMap.length) {
giuliomoro@15: return this.currentStore;
giuliomoro@15: } else {
giuliomoro@15: return null;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function AudioEngine(specification) {
giuliomoro@15:
giuliomoro@15: // Create two output paths, the main outputGain and fooGain.
giuliomoro@15: // Output gain is default to 1 and any items for playback route here
giuliomoro@15: // Foo gain is used for analysis to ensure paths get processed, but are not heard
giuliomoro@15: // because web audio will optimise and any route which does not go to the destination gets ignored.
giuliomoro@15: this.outputGain = audioContext.createGain();
giuliomoro@15: this.fooGain = audioContext.createGain();
giuliomoro@15: this.fooGain.gain = 0;
giuliomoro@15:
giuliomoro@15: // Use this to detect playback state: 0 - stopped, 1 - playing
giuliomoro@15: this.status = 0;
giuliomoro@15:
giuliomoro@15: // Connect both gains to output
giuliomoro@15: this.outputGain.connect(audioContext.destination);
giuliomoro@15: this.fooGain.connect(audioContext.destination);
giuliomoro@15:
giuliomoro@15: // Create the timer Object
giuliomoro@15: this.timer = new timer();
giuliomoro@15: // Create session metrics
giuliomoro@15: this.metric = new sessionMetrics(this,specification);
giuliomoro@15:
giuliomoro@15: this.loopPlayback = false;
giuliomoro@15:
giuliomoro@15: this.pageStore = null;
giuliomoro@15:
giuliomoro@15: // Create store for new audioObjects
giuliomoro@15: this.audioObjects = [];
giuliomoro@15:
giuliomoro@15: this.buffers = [];
giuliomoro@15: this.bufferObj = function()
giuliomoro@15: {
giuliomoro@15: this.url = null;
giuliomoro@15: this.buffer = null;
giuliomoro@15: this.xmlRequest = new XMLHttpRequest();
giuliomoro@15: this.xmlRequest.parent = this;
giuliomoro@15: this.users = [];
giuliomoro@15: this.progress = 0;
giuliomoro@15: this.status = 0;
giuliomoro@15: this.ready = function()
giuliomoro@15: {
giuliomoro@15: if (this.status >= 2)
giuliomoro@15: {
giuliomoro@15: this.status = 3;
giuliomoro@15: }
giuliomoro@15: for (var i=0; i 0) {this.wasMoved = true;}
giuliomoro@15: this.movementTracker[this.movementTracker.length] = [time, position];
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.startListening = function(time)
giuliomoro@15: {
giuliomoro@15: if (this.listenHold == false)
giuliomoro@15: {
giuliomoro@15: this.wasListenedTo = true;
giuliomoro@15: this.listenStart = time;
giuliomoro@15: this.listenHold = true;
giuliomoro@15:
giuliomoro@15: var evnt = document.createElement('event');
giuliomoro@15: var testTime = document.createElement('testTime');
giuliomoro@15: testTime.setAttribute('start',time);
giuliomoro@15: var bufferTime = document.createElement('bufferTime');
giuliomoro@15: bufferTime.setAttribute('start',this.parent.getCurrentPosition());
giuliomoro@15: evnt.appendChild(testTime);
giuliomoro@15: evnt.appendChild(bufferTime);
giuliomoro@15: this.listenTracker.push(evnt);
giuliomoro@15:
giuliomoro@15: console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.stopListening = function(time,bufferStopTime)
giuliomoro@15: {
giuliomoro@15: if (this.listenHold == true)
giuliomoro@15: {
giuliomoro@15: var diff = time - this.listenStart;
giuliomoro@15: this.listenedTimer += (diff);
giuliomoro@15: this.listenStart = 0;
giuliomoro@15: this.listenHold = false;
giuliomoro@15:
giuliomoro@15: var evnt = this.listenTracker[this.listenTracker.length-1];
giuliomoro@15: var testTime = evnt.getElementsByTagName('testTime')[0];
giuliomoro@15: var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
giuliomoro@15: testTime.setAttribute('stop',time);
giuliomoro@15: if (bufferStopTime == undefined) {
giuliomoro@15: bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
giuliomoro@15: } else {
giuliomoro@15: bufferTime.setAttribute('stop',bufferStopTime);
giuliomoro@15: }
giuliomoro@15: console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.exportXMLDOM = function() {
giuliomoro@15: var storeDOM = [];
giuliomoro@15: if (audioEngineContext.metric.enableElementTimer) {
giuliomoro@15: var mElementTimer = storage.document.createElement('metricresult');
giuliomoro@15: mElementTimer.setAttribute('name','enableElementTimer');
giuliomoro@15: mElementTimer.textContent = this.listenedTimer;
giuliomoro@15: storeDOM.push(mElementTimer);
giuliomoro@15: }
giuliomoro@15: if (audioEngineContext.metric.enableElementTracker) {
giuliomoro@15: var elementTrackerFull = storage.document.createElement('metricresult');
giuliomoro@15: elementTrackerFull.setAttribute('name','elementTrackerFull');
giuliomoro@15: for (var k=0; k tag.
giuliomoro@15: this.interfaceObjects = [];
giuliomoro@15: this.interfaceObject = function(){};
giuliomoro@15:
giuliomoro@15: this.resizeWindow = function(event)
giuliomoro@15: {
giuliomoro@15: popup.resize(event);
giuliomoro@15: for(var i=0; i
giuliomoro@15: // DD/MM/YY
giuliomoro@15: //
giuliomoro@15: //
giuliomoro@15: var dateTime = new Date();
giuliomoro@15: var hold = storage.document.createElement("datetime");
giuliomoro@15: var date = storage.document.createElement("date");
giuliomoro@15: var time = storage.document.createElement("time");
giuliomoro@15: date.setAttribute('year',dateTime.getFullYear());
giuliomoro@15: date.setAttribute('month',dateTime.getMonth()+1);
giuliomoro@15: date.setAttribute('day',dateTime.getDate());
giuliomoro@15: time.setAttribute('hour',dateTime.getHours());
giuliomoro@15: time.setAttribute('minute',dateTime.getMinutes());
giuliomoro@15: time.setAttribute('secs',dateTime.getSeconds());
giuliomoro@15:
giuliomoro@15: hold.appendChild(date);
giuliomoro@15: hold.appendChild(time);
giuliomoro@15: return hold;
giuliomoro@15:
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.commentBoxes = new function() {
giuliomoro@15: this.boxes = [];
giuliomoro@15: this.injectPoint = null;
giuliomoro@15: this.elementCommentBox = function(audioObject) {
giuliomoro@15: var element = audioObject.specification;
giuliomoro@15: this.audioObject = audioObject;
giuliomoro@15: this.id = audioObject.id;
giuliomoro@15: var audioHolderObject = audioObject.specification.parent;
giuliomoro@15: // Create document objects to hold the comment boxes
giuliomoro@15: this.trackComment = document.createElement('div');
giuliomoro@15: this.trackComment.className = 'comment-div';
giuliomoro@15: this.trackComment.id = 'comment-div-'+audioObject.id;
giuliomoro@15: // Create a string next to each comment asking for a comment
giuliomoro@15: this.trackString = document.createElement('span');
giuliomoro@15: this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.interfaceDOM.getPresentedId();
giuliomoro@15: // Create the HTML5 comment box 'textarea'
giuliomoro@15: this.trackCommentBox = document.createElement('textarea');
giuliomoro@15: this.trackCommentBox.rows = '4';
giuliomoro@15: this.trackCommentBox.cols = '100';
giuliomoro@15: this.trackCommentBox.name = 'trackComment'+audioObject.id;
giuliomoro@15: this.trackCommentBox.className = 'trackComment';
giuliomoro@15: var br = document.createElement('br');
giuliomoro@15: // Add to the holder.
giuliomoro@15: this.trackComment.appendChild(this.trackString);
giuliomoro@15: this.trackComment.appendChild(br);
giuliomoro@15: this.trackComment.appendChild(this.trackCommentBox);
giuliomoro@15:
giuliomoro@15: this.exportXMLDOM = function() {
giuliomoro@15: var root = document.createElement('comment');
giuliomoro@15: var question = document.createElement('question');
giuliomoro@15: question.textContent = this.trackString.textContent;
giuliomoro@15: var response = document.createElement('response');
giuliomoro@15: response.textContent = this.trackCommentBox.value;
giuliomoro@15: console.log("Comment frag-"+this.id+": "+response.textContent);
giuliomoro@15: root.appendChild(question);
giuliomoro@15: root.appendChild(response);
giuliomoro@15: return root;
giuliomoro@15: };
giuliomoro@15: this.resize = function()
giuliomoro@15: {
giuliomoro@15: var boxwidth = (window.innerWidth-100)/2;
giuliomoro@15: if (boxwidth >= 600)
giuliomoro@15: {
giuliomoro@15: boxwidth = 600;
giuliomoro@15: }
giuliomoro@15: else if (boxwidth < 400)
giuliomoro@15: {
giuliomoro@15: boxwidth = 400;
giuliomoro@15: }
giuliomoro@15: this.trackComment.style.width = boxwidth+"px";
giuliomoro@15: this.trackCommentBox.style.width = boxwidth-6+"px";
giuliomoro@15: };
giuliomoro@15: this.resize();
giuliomoro@15: };
giuliomoro@15: this.createCommentBox = function(audioObject) {
giuliomoro@15: var node = new this.elementCommentBox(audioObject);
giuliomoro@15: this.boxes.push(node);
giuliomoro@15: audioObject.commentDOM = node;
giuliomoro@15: return node;
giuliomoro@15: };
giuliomoro@15: this.sortCommentBoxes = function() {
giuliomoro@15: this.boxes.sort(function(a,b){return a.id - b.id;});
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.showCommentBoxes = function(inject, sort) {
giuliomoro@15: this.injectPoint = inject;
giuliomoro@15: if (sort) {this.sortCommentBoxes();}
giuliomoro@15: for (var box of this.boxes) {
giuliomoro@15: inject.appendChild(box.trackComment);
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.deleteCommentBoxes = function() {
giuliomoro@15: if (this.injectPoint != null) {
giuliomoro@15: for (var box of this.boxes) {
giuliomoro@15: this.injectPoint.removeChild(box.trackComment);
giuliomoro@15: }
giuliomoro@15: this.injectPoint = null;
giuliomoro@15: }
giuliomoro@15: this.boxes = [];
giuliomoro@15: };
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.commentQuestions = [];
giuliomoro@15:
giuliomoro@15: this.commentBox = function(commentQuestion) {
giuliomoro@15: this.specification = commentQuestion;
giuliomoro@15: // Create document objects to hold the comment boxes
giuliomoro@15: this.holder = document.createElement('div');
giuliomoro@15: this.holder.className = 'comment-div';
giuliomoro@15: // Create a string next to each comment asking for a comment
giuliomoro@15: this.string = document.createElement('span');
giuliomoro@15: this.string.innerHTML = commentQuestion.statement;
giuliomoro@15: // Create the HTML5 comment box 'textarea'
giuliomoro@15: this.textArea = document.createElement('textarea');
giuliomoro@15: this.textArea.rows = '4';
giuliomoro@15: this.textArea.cols = '100';
giuliomoro@15: this.textArea.className = 'trackComment';
giuliomoro@15: var br = document.createElement('br');
giuliomoro@15: // Add to the holder.
giuliomoro@15: this.holder.appendChild(this.string);
giuliomoro@15: this.holder.appendChild(br);
giuliomoro@15: this.holder.appendChild(this.textArea);
giuliomoro@15:
giuliomoro@15: this.exportXMLDOM = function(storePoint) {
giuliomoro@15: var root = storePoint.parent.document.createElement('comment');
giuliomoro@15: root.id = this.specification.id;
giuliomoro@15: root.setAttribute('type',this.specification.type);
giuliomoro@15: console.log("Question: "+this.string.textContent);
giuliomoro@15: console.log("Response: "+root.textContent);
giuliomoro@15: var question = storePoint.parent.document.createElement('question');
giuliomoro@15: question.textContent = this.string.textContent;
giuliomoro@15: var response = storePoint.parent.document.createElement('response');
giuliomoro@15: response.textContent = this.textArea.value;
giuliomoro@15: root.appendChild(question);
giuliomoro@15: root.appendChild(response);
giuliomoro@15: storePoint.XMLDOM.appendChild(root);
giuliomoro@15: return root;
giuliomoro@15: };
giuliomoro@15: this.resize = function()
giuliomoro@15: {
giuliomoro@15: var boxwidth = (window.innerWidth-100)/2;
giuliomoro@15: if (boxwidth >= 600)
giuliomoro@15: {
giuliomoro@15: boxwidth = 600;
giuliomoro@15: }
giuliomoro@15: else if (boxwidth < 400)
giuliomoro@15: {
giuliomoro@15: boxwidth = 400;
giuliomoro@15: }
giuliomoro@15: this.holder.style.width = boxwidth+"px";
giuliomoro@15: this.textArea.style.width = boxwidth-6+"px";
giuliomoro@15: };
giuliomoro@15: this.resize();
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.radioBox = function(commentQuestion) {
giuliomoro@15: this.specification = commentQuestion;
giuliomoro@15: // Create document objects to hold the comment boxes
giuliomoro@15: this.holder = document.createElement('div');
giuliomoro@15: this.holder.className = 'comment-div';
giuliomoro@15: // Create a string next to each comment asking for a comment
giuliomoro@15: this.string = document.createElement('span');
giuliomoro@15: this.string.innerHTML = commentQuestion.statement;
giuliomoro@15: var br = document.createElement('br');
giuliomoro@15: // Add to the holder.
giuliomoro@15: this.holder.appendChild(this.string);
giuliomoro@15: this.holder.appendChild(br);
giuliomoro@15: this.options = [];
giuliomoro@15: this.inputs = document.createElement('div');
giuliomoro@15: this.span = document.createElement('div');
giuliomoro@15: this.inputs.align = 'center';
giuliomoro@15: this.inputs.style.marginLeft = '12px';
giuliomoro@15: this.inputs.className = "comment-radio-inputs-holder";
giuliomoro@15: this.span.style.marginLeft = '12px';
giuliomoro@15: this.span.align = 'center';
giuliomoro@15: this.span.style.marginTop = '15px';
giuliomoro@15: this.span.className = "comment-radio-span-holder";
giuliomoro@15:
giuliomoro@15: var optCount = commentQuestion.options.length;
giuliomoro@15: for (var optNode of commentQuestion.options)
giuliomoro@15: {
giuliomoro@15: var div = document.createElement('div');
giuliomoro@15: div.style.width = '80px';
giuliomoro@15: div.style.float = 'left';
giuliomoro@15: var input = document.createElement('input');
giuliomoro@15: input.type = 'radio';
giuliomoro@15: input.name = commentQuestion.id;
giuliomoro@15: input.setAttribute('setvalue',optNode.name);
giuliomoro@15: input.className = 'comment-radio';
giuliomoro@15: div.appendChild(input);
giuliomoro@15: this.inputs.appendChild(div);
giuliomoro@15:
giuliomoro@15:
giuliomoro@15: div = document.createElement('div');
giuliomoro@15: div.style.width = '80px';
giuliomoro@15: div.style.float = 'left';
giuliomoro@15: div.align = 'center';
giuliomoro@15: var span = document.createElement('span');
giuliomoro@15: span.textContent = optNode.text;
giuliomoro@15: span.className = 'comment-radio-span';
giuliomoro@15: div.appendChild(span);
giuliomoro@15: this.span.appendChild(div);
giuliomoro@15: this.options.push(input);
giuliomoro@15: }
giuliomoro@15: this.holder.appendChild(this.span);
giuliomoro@15: this.holder.appendChild(this.inputs);
giuliomoro@15:
giuliomoro@15: this.exportXMLDOM = function(storePoint) {
giuliomoro@15: var root = storePoint.parent.document.createElement('comment');
giuliomoro@15: root.id = this.specification.id;
giuliomoro@15: root.setAttribute('type',this.specification.type);
giuliomoro@15: var question = document.createElement('question');
giuliomoro@15: question.textContent = this.string.textContent;
giuliomoro@15: var response = document.createElement('response');
giuliomoro@15: var i=0;
giuliomoro@15: while(this.options[i].checked == false) {
giuliomoro@15: i++;
giuliomoro@15: if (i >= this.options.length) {
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: if (i >= this.options.length) {
giuliomoro@15: response.textContent = 'null';
giuliomoro@15: } else {
giuliomoro@15: response.textContent = this.options[i].getAttribute('setvalue');
giuliomoro@15: response.setAttribute('number',i);
giuliomoro@15: }
giuliomoro@15: console.log('Comment: '+question.textContent);
giuliomoro@15: console.log('Response: '+response.textContent);
giuliomoro@15: root.appendChild(question);
giuliomoro@15: root.appendChild(response);
giuliomoro@15: storePoint.XMLDOM.appendChild(root);
giuliomoro@15: return root;
giuliomoro@15: };
giuliomoro@15: this.resize = function()
giuliomoro@15: {
giuliomoro@15: var boxwidth = (window.innerWidth-100)/2;
giuliomoro@15: if (boxwidth >= 600)
giuliomoro@15: {
giuliomoro@15: boxwidth = 600;
giuliomoro@15: }
giuliomoro@15: else if (boxwidth < 400)
giuliomoro@15: {
giuliomoro@15: boxwidth = 400;
giuliomoro@15: }
giuliomoro@15: this.holder.style.width = boxwidth+"px";
giuliomoro@15: var text = this.holder.getElementsByClassName("comment-radio-span-holder")[0];
giuliomoro@15: var options = this.holder.getElementsByClassName("comment-radio-inputs-holder")[0];
giuliomoro@15: var optCount = options.childElementCount;
giuliomoro@15: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
giuliomoro@15: var options = options.firstChild;
giuliomoro@15: var text = text.firstChild;
giuliomoro@15: options.style.marginRight = spanMargin;
giuliomoro@15: options.style.marginLeft = spanMargin;
giuliomoro@15: text.style.marginRight = spanMargin;
giuliomoro@15: text.style.marginLeft = spanMargin;
giuliomoro@15: while(options.nextSibling != undefined)
giuliomoro@15: {
giuliomoro@15: options = options.nextSibling;
giuliomoro@15: text = text.nextSibling;
giuliomoro@15: options.style.marginRight = spanMargin;
giuliomoro@15: options.style.marginLeft = spanMargin;
giuliomoro@15: text.style.marginRight = spanMargin;
giuliomoro@15: text.style.marginLeft = spanMargin;
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.resize();
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.checkboxBox = function(commentQuestion) {
giuliomoro@15: this.specification = commentQuestion;
giuliomoro@15: // Create document objects to hold the comment boxes
giuliomoro@15: this.holder = document.createElement('div');
giuliomoro@15: this.holder.className = 'comment-div';
giuliomoro@15: // Create a string next to each comment asking for a comment
giuliomoro@15: this.string = document.createElement('span');
giuliomoro@15: this.string.innerHTML = commentQuestion.statement;
giuliomoro@15: var br = document.createElement('br');
giuliomoro@15: // Add to the holder.
giuliomoro@15: this.holder.appendChild(this.string);
giuliomoro@15: this.holder.appendChild(br);
giuliomoro@15: this.options = [];
giuliomoro@15: this.inputs = document.createElement('div');
giuliomoro@15: this.span = document.createElement('div');
giuliomoro@15: this.inputs.align = 'center';
giuliomoro@15: this.inputs.style.marginLeft = '12px';
giuliomoro@15: this.inputs.className = "comment-checkbox-inputs-holder";
giuliomoro@15: this.span.style.marginLeft = '12px';
giuliomoro@15: this.span.align = 'center';
giuliomoro@15: this.span.style.marginTop = '15px';
giuliomoro@15: this.span.className = "comment-checkbox-span-holder";
giuliomoro@15:
giuliomoro@15: var optCount = commentQuestion.options.length;
giuliomoro@15: for (var i=0; i= 600)
giuliomoro@15: {
giuliomoro@15: boxwidth = 600;
giuliomoro@15: }
giuliomoro@15: else if (boxwidth < 400)
giuliomoro@15: {
giuliomoro@15: boxwidth = 400;
giuliomoro@15: }
giuliomoro@15: this.holder.style.width = boxwidth+"px";
giuliomoro@15: var text = this.holder.getElementsByClassName("comment-checkbox-span-holder")[0];
giuliomoro@15: var options = this.holder.getElementsByClassName("comment-checkbox-inputs-holder")[0];
giuliomoro@15: var optCount = options.childElementCount;
giuliomoro@15: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
giuliomoro@15: var options = options.firstChild;
giuliomoro@15: var text = text.firstChild;
giuliomoro@15: options.style.marginRight = spanMargin;
giuliomoro@15: options.style.marginLeft = spanMargin;
giuliomoro@15: text.style.marginRight = spanMargin;
giuliomoro@15: text.style.marginLeft = spanMargin;
giuliomoro@15: while(options.nextSibling != undefined)
giuliomoro@15: {
giuliomoro@15: options = options.nextSibling;
giuliomoro@15: text = text.nextSibling;
giuliomoro@15: options.style.marginRight = spanMargin;
giuliomoro@15: options.style.marginLeft = spanMargin;
giuliomoro@15: text.style.marginRight = spanMargin;
giuliomoro@15: text.style.marginLeft = spanMargin;
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.resize();
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.createCommentQuestion = function(element) {
giuliomoro@15: var node;
giuliomoro@15: if (element.type == 'question') {
giuliomoro@15: node = new this.commentBox(element);
giuliomoro@15: } else if (element.type == 'radio') {
giuliomoro@15: node = new this.radioBox(element);
giuliomoro@15: } else if (element.type == 'checkbox') {
giuliomoro@15: node = new this.checkboxBox(element);
giuliomoro@15: }
giuliomoro@15: this.commentQuestions.push(node);
giuliomoro@15: return node;
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.deleteCommentQuestions = function()
giuliomoro@15: {
giuliomoro@15: this.commentQuestions = [];
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.outsideReferenceDOM = function(audioObject,index,inject)
giuliomoro@15: {
giuliomoro@15: this.parent = audioObject;
giuliomoro@15: this.outsideReferenceHolder = document.createElement('button');
giuliomoro@15: this.outsideReferenceHolder.id = 'outside-reference';
giuliomoro@15: this.outsideReferenceHolder.className = 'outside-reference';
giuliomoro@15: this.outsideReferenceHolder.setAttribute('track-id',index);
giuliomoro@15: this.outsideReferenceHolder.textContent = "Play Reference";
giuliomoro@15: this.outsideReferenceHolder.disabled = true;
giuliomoro@15:
giuliomoro@15: this.outsideReferenceHolder.onclick = function(event)
giuliomoro@15: {
giuliomoro@15: audioEngineContext.play(event.currentTarget.getAttribute('track-id'));
giuliomoro@15: };
giuliomoro@15: inject.appendChild(this.outsideReferenceHolder);
giuliomoro@15: this.enable = function()
giuliomoro@15: {
giuliomoro@15: if (this.parent.state == 1)
giuliomoro@15: {
giuliomoro@15: this.outsideReferenceHolder.disabled = false;
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.updateLoading = function(progress)
giuliomoro@15: {
giuliomoro@15: if (progress != 100)
giuliomoro@15: {
giuliomoro@15: progress = String(progress);
giuliomoro@15: progress = progress.split('.')[0];
giuliomoro@15: this.outsideReferenceHolder.textContent = progress+'%';
giuliomoro@15: } else {
giuliomoro@15: this.outsideReferenceHolder.textContent = "Play Reference";
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.startPlayback = function()
giuliomoro@15: {
giuliomoro@15: // Called when playback has begun
giuliomoro@15: $('.track-slider').removeClass('track-slider-playing');
giuliomoro@15: $('.comment-div').removeClass('comment-box-playing');
giuliomoro@15: this.outsideReferenceHolder.style.backgroundColor = "#FDD";
giuliomoro@15: };
giuliomoro@15: this.stopPlayback = function()
giuliomoro@15: {
giuliomoro@15: // Called when playback has stopped. This gets called even if playback never started!
giuliomoro@15: this.outsideReferenceHolder.style.backgroundColor = "";
giuliomoro@15: };
giuliomoro@15: this.exportXMLDOM = function(audioObject)
giuliomoro@15: {
giuliomoro@15: return null;
giuliomoro@15: };
giuliomoro@15: this.getValue = function()
giuliomoro@15: {
giuliomoro@15: return 0;
giuliomoro@15: };
giuliomoro@15: this.getPresentedId = function()
giuliomoro@15: {
giuliomoro@15: return 'Reference';
giuliomoro@15: };
giuliomoro@15: this.canMove = function()
giuliomoro@15: {
giuliomoro@15: return false;
giuliomoro@15: };
giuliomoro@15: this.error = function() {
giuliomoro@15: // audioObject has an error!!
giuliomoro@15: this.outsideReferenceHolder.textContent = "Error";
giuliomoro@15: this.outsideReferenceHolder.style.backgroundColor = "#F00";
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.playhead = new function()
giuliomoro@15: {
giuliomoro@15: this.object = document.createElement('div');
giuliomoro@15: this.object.className = 'playhead';
giuliomoro@15: this.object.align = 'left';
giuliomoro@15: var curTime = document.createElement('div');
giuliomoro@15: curTime.style.width = '50px';
giuliomoro@15: this.curTimeSpan = document.createElement('span');
giuliomoro@15: this.curTimeSpan.textContent = '00:00';
giuliomoro@15: curTime.appendChild(this.curTimeSpan);
giuliomoro@15: this.object.appendChild(curTime);
giuliomoro@15: this.scrubberTrack = document.createElement('div');
giuliomoro@15: this.scrubberTrack.className = 'playhead-scrub-track';
giuliomoro@15:
giuliomoro@15: this.scrubberHead = document.createElement('div');
giuliomoro@15: this.scrubberHead.id = 'playhead-scrubber';
giuliomoro@15: this.scrubberTrack.appendChild(this.scrubberHead);
giuliomoro@15: this.object.appendChild(this.scrubberTrack);
giuliomoro@15:
giuliomoro@15: this.timePerPixel = 0;
giuliomoro@15: this.maxTime = 0;
giuliomoro@15:
giuliomoro@15: this.playbackObject;
giuliomoro@15:
giuliomoro@15: this.setTimePerPixel = function(audioObject) {
giuliomoro@15: //maxTime must be in seconds
giuliomoro@15: this.playbackObject = audioObject;
giuliomoro@15: this.maxTime = audioObject.buffer.buffer.duration;
giuliomoro@15: var width = 490; //500 - 10, 5 each side of the tracker head
giuliomoro@15: this.timePerPixel = this.maxTime/490;
giuliomoro@15: if (this.maxTime < 60) {
giuliomoro@15: this.curTimeSpan.textContent = '0.00';
giuliomoro@15: } else {
giuliomoro@15: this.curTimeSpan.textContent = '00:00';
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.update = function() {
giuliomoro@15: // Update the playhead position, startPlay must be called
giuliomoro@15: if (this.timePerPixel > 0) {
giuliomoro@15: var time = this.playbackObject.getCurrentPosition();
giuliomoro@15: if (time > 0 && time < this.maxTime) {
giuliomoro@15: var width = 490;
giuliomoro@15: var pix = Math.floor(time/this.timePerPixel);
giuliomoro@15: this.scrubberHead.style.left = pix+'px';
giuliomoro@15: if (this.maxTime > 60.0) {
giuliomoro@15: var secs = time%60;
giuliomoro@15: var mins = Math.floor((time-secs)/60);
giuliomoro@15: secs = secs.toString();
giuliomoro@15: secs = secs.substr(0,2);
giuliomoro@15: mins = mins.toString();
giuliomoro@15: this.curTimeSpan.textContent = mins+':'+secs;
giuliomoro@15: } else {
giuliomoro@15: time = time.toString();
giuliomoro@15: this.curTimeSpan.textContent = time.substr(0,4);
giuliomoro@15: }
giuliomoro@15: } else {
giuliomoro@15: this.scrubberHead.style.left = '0px';
giuliomoro@15: if (this.maxTime < 60) {
giuliomoro@15: this.curTimeSpan.textContent = '0.00';
giuliomoro@15: } else {
giuliomoro@15: this.curTimeSpan.textContent = '00:00';
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.interval = undefined;
giuliomoro@15:
giuliomoro@15: this.start = function() {
giuliomoro@15: if (this.playbackObject != undefined && this.interval == undefined) {
giuliomoro@15: if (this.maxTime < 60) {
giuliomoro@15: this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
giuliomoro@15: } else {
giuliomoro@15: this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.stop = function() {
giuliomoro@15: clearInterval(this.interval);
giuliomoro@15: this.interval = undefined;
giuliomoro@15: this.scrubberHead.style.left = '0px';
giuliomoro@15: if (this.maxTime < 60) {
giuliomoro@15: this.curTimeSpan.textContent = '0.00';
giuliomoro@15: } else {
giuliomoro@15: this.curTimeSpan.textContent = '00:00';
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.volume = new function()
giuliomoro@15: {
giuliomoro@15: // An in-built volume module which can be viewed on page
giuliomoro@15: // Includes trackers on page-by-page data
giuliomoro@15: // Volume does NOT reset to 0dB on each page load
giuliomoro@15: this.valueLin = 1.0;
giuliomoro@15: this.valueDB = 0.0;
giuliomoro@15: this.object = document.createElement('div');
giuliomoro@15: this.object.id = 'master-volume-holder';
giuliomoro@15: this.slider = document.createElement('input');
giuliomoro@15: this.slider.id = 'master-volume-control';
giuliomoro@15: this.slider.type = 'range';
giuliomoro@15: this.valueText = document.createElement('span');
giuliomoro@15: this.valueText.id = 'master-volume-feedback';
giuliomoro@15: this.valueText.textContent = '0dB';
giuliomoro@15:
giuliomoro@15: this.slider.min = -60;
giuliomoro@15: this.slider.max = 12;
giuliomoro@15: this.slider.value = 0;
giuliomoro@15: this.slider.step = 1;
giuliomoro@15: this.slider.onmousemove = function(event)
giuliomoro@15: {
giuliomoro@15: interfaceContext.volume.valueDB = event.currentTarget.value;
giuliomoro@15: interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB);
giuliomoro@15: interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB+'dB';
giuliomoro@15: audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin;
giuliomoro@15: }
giuliomoro@15: this.slider.onmouseup = function(event)
giuliomoro@15: {
giuliomoro@15: var storePoint = testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].getAllElementsByName('volumeTracker');
giuliomoro@15: if (storePoint.length == 0)
giuliomoro@15: {
giuliomoro@15: storePoint = storage.document.createElement('metricresult');
giuliomoro@15: storePoint.setAttribute('name','volumeTracker');
giuliomoro@15: testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].appendChild(storePoint);
giuliomoro@15: }
giuliomoro@15: else {
giuliomoro@15: storePoint = storePoint[0];
giuliomoro@15: }
giuliomoro@15: var node = storage.document.createElement('movement');
giuliomoro@15: node.setAttribute('test-time',audioEngineContext.timer.getTestTime());
giuliomoro@15: node.setAttribute('volume',interfaceContext.volume.valueDB);
giuliomoro@15: node.setAttribute('format','dBFS');
giuliomoro@15: storePoint.appendChild(node);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: var title = document.createElement('div');
giuliomoro@15: title.innerHTML = 'Master Volume Control';
giuliomoro@15: title.style.fontSize = '0.75em';
giuliomoro@15: title.style.width = "100%";
giuliomoro@15: title.align = 'center';
giuliomoro@15: this.object.appendChild(title);
giuliomoro@15:
giuliomoro@15: this.object.appendChild(this.slider);
giuliomoro@15: this.object.appendChild(this.valueText);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.calibrationModuleObject = null;
giuliomoro@15: this.calibrationModule = function() {
giuliomoro@15: // This creates an on-page calibration module
giuliomoro@15: this.storeDOM = storage.document.createElement("calibration");
giuliomoro@15: storage.root.appendChild(this.storeDOM);
giuliomoro@15: // The calibration is a fixed state module
giuliomoro@15: this.calibrationNodes = [];
giuliomoro@15: this.holder = null;
giuliomoro@15: this.build = function(inject) {
giuliomoro@15: var f0 = 62.5;
giuliomoro@15: this.holder = document.createElement("div");
giuliomoro@15: this.holder.className = "calibration-holder";
giuliomoro@15: this.calibrationNodes = [];
giuliomoro@15: while(f0 < 20000) {
giuliomoro@15: var obj = {
giuliomoro@15: root: document.createElement("div"),
giuliomoro@15: input: document.createElement("input"),
giuliomoro@15: oscillator: audioContext.createOscillator(),
giuliomoro@15: gain: audioContext.createGain(),
giuliomoro@15: f: f0,
giuliomoro@15: parent: this,
giuliomoro@15: handleEvent: function(event) {
giuliomoro@15: switch(event.type) {
giuliomoro@15: case "mouseenter":
giuliomoro@15: this.oscillator.start(0);
giuliomoro@15: break;
giuliomoro@15: case "mouseleave":
giuliomoro@15: this.oscillator.stop(0);
giuliomoro@15: this.oscillator = audioContext.createOscillator();
giuliomoro@15: this.oscillator.connect(this.gain);
giuliomoro@15: this.oscillator.frequency.value = this.f;
giuliomoro@15: break;
giuliomoro@15: case "mousemove":
giuliomoro@15: var value = Math.pow(10,this.input.value/20);
giuliomoro@15: if (this.f == 1000) {
giuliomoro@15: audioEngineContext.outputGain.gain.value = value;
giuliomoro@15: interfaceContext.volume.slider.value = this.input.value;
giuliomoro@15: } else {
giuliomoro@15: this.gain.gain.value = value
giuliomoro@15: }
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: },
giuliomoro@15: disconnect: function() {
giuliomoro@15: this.gain.disconnect();
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: obj.root.className = "calibration-slider";
giuliomoro@15: obj.root.appendChild(obj.input);
giuliomoro@15: obj.oscillator.connect(obj.gain);
giuliomoro@15: obj.gain.connect(audioEngineContext.outputGain);
giuliomoro@15: obj.gain.gain.value = Math.random()*2;
giuliomoro@15: obj.input.value = obj.gain.gain.value;
giuliomoro@15: obj.input.setAttribute('orient','vertical');
giuliomoro@15: obj.input.type = "range";
giuliomoro@15: obj.input.min = -6;
giuliomoro@15: obj.input.max = 6;
giuliomoro@15: obj.input.step = 0.25;
giuliomoro@15: if (f0 != 1000) {
giuliomoro@15: obj.input.value = (Math.random()*12)-6;
giuliomoro@15: } else {
giuliomoro@15: obj.input.value = 0;
giuliomoro@15: obj.root.style.backgroundColor="rgb(255,125,125)";
giuliomoro@15: }
giuliomoro@15: obj.input.addEventListener("mousemove",obj);
giuliomoro@15: obj.input.addEventListener("mouseenter",obj);
giuliomoro@15: obj.input.addEventListener("mouseleave",obj);
giuliomoro@15: obj.gain.gain.value = Math.pow(10,obj.input.value/20);
giuliomoro@15: obj.oscillator.frequency.value = f0;
giuliomoro@15: this.calibrationNodes.push(obj);
giuliomoro@15: this.holder.appendChild(obj.root);
giuliomoro@15: f0 *= 2;
giuliomoro@15: }
giuliomoro@15: inject.appendChild(this.holder);
giuliomoro@15: }
giuliomoro@15: this.collect = function() {
giuliomoro@15: for (var obj of this.calibrationNodes) {
giuliomoro@15: var node = storage.document.createElement("calibrationresult");
giuliomoro@15: node.setAttribute("frequency",obj.f);
giuliomoro@15: node.setAttribute("range-min",obj.input.min);
giuliomoro@15: node.setAttribute("range-max",obj.input.max);
giuliomoro@15: node.setAttribute("gain-lin",obj.gain.gain.value);
giuliomoro@15: this.storeDOM.appendChild(node);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15:
giuliomoro@15: // Global Checkers
giuliomoro@15: // These functions will help enforce the checkers
giuliomoro@15: this.checkHiddenAnchor = function()
giuliomoro@15: {
giuliomoro@15: for (var ao of audioEngineContext.audioObjects)
giuliomoro@15: {
giuliomoro@15: if (ao.specification.type == "anchor")
giuliomoro@15: {
giuliomoro@15: if (ao.interfaceDOM.getValue() > (ao.specification.marker/100) && ao.specification.marker > 0) {
giuliomoro@15: // Anchor is not set below
giuliomoro@15: console.log('Anchor node not below marker value');
giuliomoro@15: alert('Please keep listening');
giuliomoro@15: this.storeErrorNode('Anchor node not below marker value');
giuliomoro@15: return false;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: return true;
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.checkHiddenReference = function()
giuliomoro@15: {
giuliomoro@15: for (var ao of audioEngineContext.audioObjects)
giuliomoro@15: {
giuliomoro@15: if (ao.specification.type == "reference")
giuliomoro@15: {
giuliomoro@15: if (ao.interfaceDOM.getValue() < (ao.specification.marker/100) && ao.specification.marker > 0) {
giuliomoro@15: // Anchor is not set below
giuliomoro@15: console.log('Reference node not above marker value');
giuliomoro@15: this.storeErrorNode('Reference node not above marker value');
giuliomoro@15: alert('Please keep listening');
giuliomoro@15: return false;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: return true;
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.checkFragmentsFullyPlayed = function ()
giuliomoro@15: {
giuliomoro@15: // Checks the entire file has been played back
giuliomoro@15: // NOTE ! This will return true IF playback is Looped!!!
giuliomoro@15: if (audioEngineContext.loopPlayback)
giuliomoro@15: {
giuliomoro@15: console.log("WARNING - Looped source: Cannot check fragments are fully played");
giuliomoro@15: return true;
giuliomoro@15: }
giuliomoro@15: var check_pass = true;
giuliomoro@15: var error_obj = [];
giuliomoro@15: for (var i = 0; i= time)
giuliomoro@15: {
giuliomoro@15: passed = true;
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: if (passed == false)
giuliomoro@15: {
giuliomoro@15: check_pass = false;
giuliomoro@15: console.log("Continue listening to track-"+object.interfaceDOM.getPresentedId());
giuliomoro@15: error_obj.push(object.interfaceDOM.getPresentedId());
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: if (check_pass == false)
giuliomoro@15: {
giuliomoro@15: var str_start = "You have not completely listened to fragments ";
giuliomoro@15: for (var i=0; i maxRanking) {maxRanking = rank;}
giuliomoro@15: }
giuliomoro@15: if (minRanking*100 > min) {
giuliomoro@15: str += "At least one fragment must be below the "+min+" mark.";
giuliomoro@15: state = false;
giuliomoro@15: }
giuliomoro@15: if (maxRanking*100 < max) {
giuliomoro@15: if(isAb){ // if it is AB or ABX let's phrase it differently
giuliomoro@15: str += "You must select a fragment before continuing";
giuliomoro@15: } else{
giuliomoro@15: str += "At least one fragment must be above the "+max+" mark."
giuliomoro@15: }
giuliomoro@15: state = false;
giuliomoro@15: }
giuliomoro@15: if (!state) {
giuliomoro@15: console.log(str);
giuliomoro@15: this.storeErrorNode(str);
giuliomoro@15: alert(str);
giuliomoro@15: }
giuliomoro@15: return state;
giuliomoro@15: }
giuliomoro@15: this.storeErrorNode = function(errorMessage)
giuliomoro@15: {
giuliomoro@15: var time = audioEngineContext.timer.getTestTime();
giuliomoro@15: var node = storage.document.createElement('error');
giuliomoro@15: node.setAttribute('time',time);
giuliomoro@15: node.textContent = errorMessage;
giuliomoro@15: testState.currentStore.XMLDOM.appendChild(node);
giuliomoro@15: };
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: function Storage()
giuliomoro@15: {
giuliomoro@15: // Holds results in XML format until ready for collection
giuliomoro@15: this.globalPreTest = null;
giuliomoro@15: this.globalPostTest = null;
giuliomoro@15: this.testPages = [];
giuliomoro@15: this.document = null;
giuliomoro@15: this.root = null;
giuliomoro@15: this.state = 0;
giuliomoro@15:
giuliomoro@15: this.initialise = function(existingStore)
giuliomoro@15: {
giuliomoro@15: if (existingStore == undefined) {
giuliomoro@15: // We need to get the sessionKey
giuliomoro@15: this.SessionKey.generateKey();
giuliomoro@15: this.document = document.implementation.createDocument(null,"waetresult");
giuliomoro@15: this.root = this.document.childNodes[0];
giuliomoro@15: var projectDocument = specification.projectXML;
giuliomoro@15: projectDocument.setAttribute('file-name',url);
giuliomoro@15: projectDocument.setAttribute('url',qualifyURL(url));
giuliomoro@15: this.root.appendChild(projectDocument);
giuliomoro@15: this.root.appendChild(interfaceContext.returnDateNode());
giuliomoro@15: this.root.appendChild(interfaceContext.returnNavigator());
giuliomoro@15: } else {
giuliomoro@15: this.document = existingStore;
giuliomoro@15: this.root = existingStore.firstChild;
giuliomoro@15: this.SessionKey.key = this.root.getAttribute("key");
giuliomoro@15: }
giuliomoro@15: if (specification.preTest != undefined){this.globalPreTest = new this.surveyNode(this,this.root,specification.preTest);}
giuliomoro@15: if (specification.postTest != undefined){this.globalPostTest = new this.surveyNode(this,this.root,specification.postTest);}
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.SessionKey = {
giuliomoro@15: key: null,
giuliomoro@15: request: new XMLHttpRequest(),
giuliomoro@15: parent: this,
giuliomoro@15: handleEvent: function() {
giuliomoro@15: var parse = new DOMParser();
giuliomoro@15: var xml = parse.parseFromString(this.request.response,"text/xml");
giuliomoro@15: var shouldGenerateKey = true;
giuliomoro@15: if(xml.getAllElementsByTagName("state").length > 0){
giuliomoro@15: if (xml.getAllElementsByTagName("state")[0].textContent == "OK") {
giuliomoro@15: this.key = xml.getAllElementsByTagName("key")[0].textContent;
giuliomoro@15: this.parent.root.setAttribute("key",this.key);
giuliomoro@15: this.parent.root.setAttribute("state","empty");
giuliomoro@15: shouldGenerateKey = false;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: if(shouldGenerateKey === true){
giuliomoro@15: this.generateKey();
giuliomoro@15: }
giuliomoro@15: },
giuliomoro@15: generateKey: function() {
giuliomoro@15: var temp_key = randomString(32);
giuliomoro@15: var returnURL = "";
giuliomoro@15: if (typeof specification.projectReturn == "string") {
giuliomoro@15: if (specification.projectReturn.substr(0,4) == "http") {
giuliomoro@15: returnURL = specification.projectReturn;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: this.request.open("GET",returnURL+"php/keygen.php?key="+temp_key,true);
giuliomoro@15: this.request.addEventListener("load",this);
giuliomoro@15: this.request.send();
giuliomoro@15: },
giuliomoro@15: update: function() {
giuliomoro@15: this.parent.root.setAttribute("state","update");
giuliomoro@15: var xmlhttp = new XMLHttpRequest();
giuliomoro@15: var returnURL = "";
giuliomoro@15: if (typeof specification.projectReturn == "string") {
giuliomoro@15: if (specification.projectReturn.substr(0,4) == "http") {
giuliomoro@15: returnURL = specification.projectReturn;
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: xmlhttp.open("POST",returnURL+"php/save.php?key="+this.key);
giuliomoro@15: xmlhttp.setRequestHeader('Content-Type', 'text/xml');
giuliomoro@15: xmlhttp.onerror = function(){
giuliomoro@15: console.log('Error updating file to server!');
giuliomoro@15: };
giuliomoro@15: var hold = document.createElement("div");
giuliomoro@15: var clone = this.parent.root.cloneNode(true);
giuliomoro@15: hold.appendChild(clone);
giuliomoro@15: xmlhttp.onload = function() {
giuliomoro@15: if (this.status >= 300) {
giuliomoro@15: console.log("WARNING - Could not update at this time");
giuliomoro@15: } else {
giuliomoro@15: var parser = new DOMParser();
giuliomoro@15: var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
giuliomoro@15: var response = xmlDoc.getElementsByTagName('response')[0];
giuliomoro@15: if (response.getAttribute("state") == "OK") {
giuliomoro@15: var file = response.getElementsByTagName("file")[0];
giuliomoro@15: console.log("Intermediate save: OK, written "+file.getAttribute("bytes")+"B");
giuliomoro@15: } else {
giuliomoro@15: var message = response.getElementsByTagName("message");
giuliomoro@15: console.log("Intermediate save: Error! "+message.textContent);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: xmlhttp.send([hold.innerHTML]);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.createTestPageStore = function(specification)
giuliomoro@15: {
giuliomoro@15: var store = new this.pageNode(this,specification);
giuliomoro@15: this.testPages.push(store);
giuliomoro@15: return this.testPages[this.testPages.length-1];
giuliomoro@15: };
giuliomoro@15:
giuliomoro@15: this.surveyNode = function(parent,root,specification)
giuliomoro@15: {
giuliomoro@15: this.specification = specification;
giuliomoro@15: this.parent = parent;
giuliomoro@15: this.state = "empty";
giuliomoro@15: this.XMLDOM = this.parent.document.createElement('survey');
giuliomoro@15: this.XMLDOM.setAttribute('location',this.specification.location);
giuliomoro@15: this.XMLDOM.setAttribute("state",this.state);
giuliomoro@15: for (var optNode of this.specification.options)
giuliomoro@15: {
giuliomoro@15: if (optNode.type != 'statement')
giuliomoro@15: {
giuliomoro@15: var node = this.parent.document.createElement('surveyresult');
giuliomoro@15: node.setAttribute("ref",optNode.id);
giuliomoro@15: node.setAttribute('type',optNode.type);
giuliomoro@15: this.XMLDOM.appendChild(node);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: root.appendChild(this.XMLDOM);
giuliomoro@15:
giuliomoro@15: this.postResult = function(node)
giuliomoro@15: {
giuliomoro@15: // From popup: node is the popupOption node containing both spec. and results
giuliomoro@15: // ID is the position
giuliomoro@15: if (node.specification.type == 'statement'){return;}
giuliomoro@15: var surveyresult = this.XMLDOM.firstChild;
giuliomoro@15: while(surveyresult != null) {
giuliomoro@15: if (surveyresult.getAttribute("ref") == node.specification.id)
giuliomoro@15: {
giuliomoro@15: break;
giuliomoro@15: }
giuliomoro@15: surveyresult = surveyresult.nextElementSibling;
giuliomoro@15: }
giuliomoro@15: switch(node.specification.type)
giuliomoro@15: {
giuliomoro@15: case "number":
giuliomoro@15: case "question":
giuliomoro@15: var child = this.parent.document.createElement('response');
giuliomoro@15: child.textContent = node.response;
giuliomoro@15: surveyresult.appendChild(child);
giuliomoro@15: break;
giuliomoro@15: case "radio":
giuliomoro@15: var child = this.parent.document.createElement('response');
giuliomoro@15: child.setAttribute('name',node.response.name);
giuliomoro@15: child.textContent = node.response.text;
giuliomoro@15: surveyresult.appendChild(child);
giuliomoro@15: break;
giuliomoro@15: case "checkbox":
giuliomoro@15: for (var i=0; i 0)
giuliomoro@15: {
giuliomoro@15: aeNode.setAttribute('marker',element.marker);
giuliomoro@15: }
giuliomoro@15: }
giuliomoro@15: var ae_metric = this.parent.document.createElement('metric');
giuliomoro@15: aeNode.appendChild(ae_metric);
giuliomoro@15: this.XMLDOM.appendChild(aeNode);
giuliomoro@15: }
giuliomoro@15:
giuliomoro@15: this.parent.root.appendChild(this.XMLDOM);
giuliomoro@15:
giuliomoro@15: this.complete = function() {
giuliomoro@15: this.state = "complete";
giuliomoro@15: this.XMLDOM.setAttribute("state","complete");
giuliomoro@15: }
giuliomoro@15: };
giuliomoro@15: this.update = function() {
giuliomoro@15: this.SessionKey.update();
giuliomoro@15: }
giuliomoro@15: this.finish = function()
giuliomoro@15: {
giuliomoro@15: if (this.state == 0)
giuliomoro@15: {
giuliomoro@15: this.update();
giuliomoro@15: }
giuliomoro@15: this.state = 1;
giuliomoro@15: return this.root;
giuliomoro@15: };
giuliomoro@15: }