BrechtDeMan@873: /** BrechtDeMan@873: * core.js BrechtDeMan@873: * BrechtDeMan@873: * Main script to run, calls all other core functions and manages loading/store to backend. BrechtDeMan@873: * Also contains all global variables. BrechtDeMan@873: */ BrechtDeMan@873: BrechtDeMan@873: /* create the web audio API context and store in audioContext*/ BrechtDeMan@873: var audioContext; // Hold the browser web audio API BrechtDeMan@873: var projectXML; // Hold the parsed setup XML BrechtDeMan@873: var specification; BrechtDeMan@873: var interfaceContext; BrechtDeMan@873: var popup; // Hold the interfacePopup object BrechtDeMan@873: var testState; BrechtDeMan@873: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order BrechtDeMan@873: var audioEngineContext; // The custome AudioEngine object BrechtDeMan@873: var projectReturn; // Hold the URL for the return BrechtDeMan@873: BrechtDeMan@873: BrechtDeMan@873: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it BrechtDeMan@873: AudioBufferSourceNode.prototype.owner = undefined; BrechtDeMan@873: BrechtDeMan@873: window.onload = function() { BrechtDeMan@873: // Function called once the browser has loaded all files. BrechtDeMan@873: // This should perform any initial commands such as structure / loading documents BrechtDeMan@873: BrechtDeMan@873: // Create a web audio API context BrechtDeMan@873: // Fixed for cross-browser support BrechtDeMan@873: var AudioContext = window.AudioContext || window.webkitAudioContext; BrechtDeMan@873: audioContext = new AudioContext; BrechtDeMan@873: BrechtDeMan@873: // Create test state BrechtDeMan@873: testState = new stateMachine(); BrechtDeMan@873: BrechtDeMan@873: // Create the audio engine object BrechtDeMan@873: audioEngineContext = new AudioEngine(); BrechtDeMan@873: BrechtDeMan@873: // Create the popup interface object BrechtDeMan@873: popup = new interfacePopup(); BrechtDeMan@873: BrechtDeMan@873: // Create the specification object BrechtDeMan@873: specification = new Specification(); BrechtDeMan@873: BrechtDeMan@873: // Create the interface object BrechtDeMan@873: interfaceContext = new Interface(specification); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: function interfacePopup() { BrechtDeMan@873: // Creates an object to manage the popup BrechtDeMan@873: this.popup = null; BrechtDeMan@873: this.popupContent = null; BrechtDeMan@873: this.buttonProceed = null; BrechtDeMan@873: this.buttonPrevious = null; BrechtDeMan@873: this.popupOptions = null; BrechtDeMan@873: this.currentIndex = null; BrechtDeMan@873: this.responses = null; BrechtDeMan@873: BrechtDeMan@873: this.createPopup = function(){ BrechtDeMan@873: // Create popup window interface BrechtDeMan@873: var insertPoint = document.getElementById("topLevelBody"); BrechtDeMan@873: var blank = document.createElement('div'); BrechtDeMan@873: blank.className = 'testHalt'; BrechtDeMan@873: BrechtDeMan@873: this.popup = document.createElement('div'); BrechtDeMan@873: this.popup.id = 'popupHolder'; BrechtDeMan@873: this.popup.className = 'popupHolder'; BrechtDeMan@873: this.popup.style.position = 'absolute'; BrechtDeMan@873: this.popup.style.left = (window.innerWidth/2)-250 + 'px'; BrechtDeMan@873: this.popup.style.top = (window.innerHeight/2)-125 + 'px'; BrechtDeMan@873: BrechtDeMan@873: this.popupContent = document.createElement('div'); BrechtDeMan@873: this.popupContent.id = 'popupContent'; BrechtDeMan@873: this.popupContent.style.marginTop = '25px'; BrechtDeMan@873: this.popupContent.align = 'center'; BrechtDeMan@873: this.popup.appendChild(this.popupContent); BrechtDeMan@873: BrechtDeMan@873: this.buttonProceed = document.createElement('button'); BrechtDeMan@873: this.buttonProceed.className = 'popupButton'; BrechtDeMan@873: this.buttonProceed.style.left = '440px'; BrechtDeMan@873: this.buttonProceed.style.top = '215px'; BrechtDeMan@873: this.buttonProceed.innerHTML = 'Next'; BrechtDeMan@873: this.buttonProceed.onclick = function(){popup.proceedClicked();}; BrechtDeMan@873: BrechtDeMan@873: this.buttonPrevious = document.createElement('button'); BrechtDeMan@873: this.buttonPrevious.className = 'popupButton'; BrechtDeMan@873: this.buttonPrevious.style.left = '10px'; BrechtDeMan@873: this.buttonPrevious.style.top = '215px'; BrechtDeMan@873: this.buttonPrevious.innerHTML = 'Back'; BrechtDeMan@873: this.buttonPrevious.onclick = function(){popup.previousClick();}; BrechtDeMan@873: BrechtDeMan@873: this.popup.style.zIndex = -1; BrechtDeMan@873: this.popup.style.visibility = 'hidden'; BrechtDeMan@873: blank.style.zIndex = -2; BrechtDeMan@873: blank.style.visibility = 'hidden'; BrechtDeMan@873: insertPoint.appendChild(this.popup); BrechtDeMan@873: insertPoint.appendChild(blank); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.showPopup = function(){ BrechtDeMan@873: if (this.popup == null) { BrechtDeMan@873: this.createPopup(); BrechtDeMan@873: } BrechtDeMan@873: this.popup.style.zIndex = 3; BrechtDeMan@873: this.popup.style.visibility = 'visible'; BrechtDeMan@873: var blank = document.getElementsByClassName('testHalt')[0]; BrechtDeMan@873: blank.style.zIndex = 2; BrechtDeMan@873: blank.style.visibility = 'visible'; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.hidePopup = function(){ BrechtDeMan@873: this.popup.style.zIndex = -1; BrechtDeMan@873: this.popup.style.visibility = 'hidden'; BrechtDeMan@873: var blank = document.getElementsByClassName('testHalt')[0]; BrechtDeMan@873: blank.style.zIndex = -2; BrechtDeMan@873: blank.style.visibility = 'hidden'; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.postNode = function() { BrechtDeMan@873: // This will take the node from the popupOptions and display it BrechtDeMan@873: var node = this.popupOptions[this.currentIndex]; BrechtDeMan@873: this.popupContent.innerHTML = null; BrechtDeMan@873: if (node.type == 'statement') { BrechtDeMan@873: var span = document.createElement('span'); BrechtDeMan@873: span.textContent = node.statement; BrechtDeMan@873: this.popupContent.appendChild(span); BrechtDeMan@873: } else if (node.type == 'question') { BrechtDeMan@873: var span = document.createElement('span'); BrechtDeMan@873: span.textContent = node.question; BrechtDeMan@873: var textArea = document.createElement('textarea'); BrechtDeMan@873: switch (node.boxsize) { BrechtDeMan@873: case 'small': BrechtDeMan@873: textArea.cols = "20"; BrechtDeMan@873: textArea.rows = "1"; BrechtDeMan@873: break; BrechtDeMan@873: case 'normal': BrechtDeMan@873: textArea.cols = "30"; BrechtDeMan@873: textArea.rows = "2"; BrechtDeMan@873: break; BrechtDeMan@873: case 'large': BrechtDeMan@873: textArea.cols = "40"; BrechtDeMan@873: textArea.rows = "5"; BrechtDeMan@873: break; BrechtDeMan@873: case 'huge': BrechtDeMan@873: textArea.cols = "50"; BrechtDeMan@873: textArea.rows = "10"; BrechtDeMan@873: break; BrechtDeMan@873: } BrechtDeMan@873: var br = document.createElement('br'); BrechtDeMan@873: this.popupContent.appendChild(span); BrechtDeMan@873: this.popupContent.appendChild(br); BrechtDeMan@873: this.popupContent.appendChild(textArea); BrechtDeMan@873: this.popupContent.childNodes[2].focus(); BrechtDeMan@873: } else if (node.type == 'checkbox') { BrechtDeMan@873: var span = document.createElement('span'); BrechtDeMan@873: span.textContent = node.statement; BrechtDeMan@873: this.popupContent.appendChild(span); BrechtDeMan@873: var optHold = document.createElement('div'); BrechtDeMan@873: optHold.id = 'option-holder'; BrechtDeMan@873: optHold.align = 'left'; BrechtDeMan@873: for (var i=0; i 0) BrechtDeMan@873: this.popupContent.appendChild(this.buttonPrevious); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.initState = function(node) { BrechtDeMan@873: //Call this with your preTest and postTest nodes when needed to BrechtDeMan@873: // initialise the popup procedure. BrechtDeMan@873: this.popupOptions = node.options; BrechtDeMan@873: if (this.popupOptions.length > 0) { BrechtDeMan@873: if (node.type == 'pretest') { BrechtDeMan@873: this.responses = document.createElement('PreTest'); BrechtDeMan@873: } else if (node.type == 'posttest') { BrechtDeMan@873: this.responses = document.createElement('PostTest'); BrechtDeMan@873: } else { BrechtDeMan@873: console.log ('WARNING - popup node neither pre or post!'); BrechtDeMan@873: this.responses = document.createElement('responses'); BrechtDeMan@873: } BrechtDeMan@873: this.currentIndex = 0; BrechtDeMan@873: this.showPopup(); BrechtDeMan@873: this.postNode(); BrechtDeMan@873: } else { BrechtDeMan@873: advanceState(); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.proceedClicked = function() { BrechtDeMan@873: // Each time the popup button is clicked! BrechtDeMan@873: var node = this.popupOptions[this.currentIndex]; BrechtDeMan@873: if (node.type == 'question') { BrechtDeMan@873: // Must extract the question data BrechtDeMan@873: var textArea = $(popup.popupContent).find('textarea')[0]; BrechtDeMan@873: if (node.mandatory == true && textArea.value.length == 0) { BrechtDeMan@873: alert('This question is mandatory'); BrechtDeMan@873: return; BrechtDeMan@873: } else { BrechtDeMan@873: // Save the text content BrechtDeMan@873: var hold = document.createElement('comment'); BrechtDeMan@873: hold.id = node.id; BrechtDeMan@873: hold.innerHTML = textArea.value; BrechtDeMan@873: console.log("Question: "+ node.question); BrechtDeMan@873: console.log("Question Response: "+ textArea.value); BrechtDeMan@873: this.responses.appendChild(hold); BrechtDeMan@873: } BrechtDeMan@873: } else if (node.type == 'checkbox') { BrechtDeMan@873: // Must extract checkbox data BrechtDeMan@873: var optHold = document.getElementById('option-holder'); BrechtDeMan@873: var hold = document.createElement('checkbox'); BrechtDeMan@873: console.log("Checkbox: "+ node.statement); BrechtDeMan@873: hold.id = node.id; BrechtDeMan@873: for (var i=0; i node.max && node.max != null) { BrechtDeMan@873: alert('Number is above the maximum value of '+node.max); BrechtDeMan@873: return; BrechtDeMan@873: } BrechtDeMan@873: var hold = document.createElement('number'); BrechtDeMan@873: hold.id = node.id; BrechtDeMan@873: hold.textContent = input.value; BrechtDeMan@873: this.responses.appendChild(hold); BrechtDeMan@873: } BrechtDeMan@873: this.currentIndex++; BrechtDeMan@873: if (this.currentIndex < this.popupOptions.length) { BrechtDeMan@873: this.postNode(); BrechtDeMan@873: } else { BrechtDeMan@873: // Reached the end of the popupOptions BrechtDeMan@873: this.hidePopup(); BrechtDeMan@873: if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { BrechtDeMan@873: testState.stateResults[testState.stateIndex] = this.responses; BrechtDeMan@873: } else { BrechtDeMan@873: testState.stateResults[testState.stateIndex].appendChild(this.responses); BrechtDeMan@873: } BrechtDeMan@873: advanceState(); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.previousClick = function() { BrechtDeMan@873: // Triggered when the 'Back' button is clicked in the survey BrechtDeMan@873: if (this.currentIndex > 0) { BrechtDeMan@873: this.currentIndex--; BrechtDeMan@873: var node = this.popupOptions[this.currentIndex]; BrechtDeMan@873: if (node.type != 'statement') { BrechtDeMan@873: var prevResp = this.responses.childNodes[this.responses.childElementCount-1]; BrechtDeMan@873: this.responses.removeChild(prevResp); BrechtDeMan@873: } BrechtDeMan@873: this.postNode(); BrechtDeMan@873: if (node.type == 'question') { BrechtDeMan@873: this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent; BrechtDeMan@873: } else if (node.type == 'checkbox') { BrechtDeMan@873: var options = this.popupContent.getElementsByTagName('input'); BrechtDeMan@873: var savedOptions = prevResp.getElementsByTagName('option'); BrechtDeMan@873: for (var i=0; i 0) { BrechtDeMan@873: if(this.stateIndex != null) { BrechtDeMan@873: console.log('NOTE - State already initialise'); BrechtDeMan@873: } BrechtDeMan@873: this.stateIndex = -1; BrechtDeMan@873: var that = this; BrechtDeMan@873: for (var id=0; id= this.stateMap.length) { BrechtDeMan@873: console.log('Test Completed'); BrechtDeMan@873: createProjectSave(specification.projectReturn); BrechtDeMan@873: } else { BrechtDeMan@873: this.currentStateMap = this.stateMap[this.stateIndex]; BrechtDeMan@873: if (this.currentStateMap.type == "audioHolder") { BrechtDeMan@873: console.log('Loading test page'); BrechtDeMan@873: loadTest(this.currentStateMap); BrechtDeMan@873: this.initialiseInnerState(this.currentStateMap); BrechtDeMan@873: } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { BrechtDeMan@873: if (this.currentStateMap.options.length >= 1) { BrechtDeMan@873: popup.initState(this.currentStateMap); BrechtDeMan@873: } else { BrechtDeMan@873: this.advanceState(); BrechtDeMan@873: } BrechtDeMan@873: } else { BrechtDeMan@873: this.advanceState(); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: } else { BrechtDeMan@873: this.advanceInnerState(); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.testPageCompleted = function(store, testXML, testId) { BrechtDeMan@873: // Function called each time a test page has been completed BrechtDeMan@873: // Can be used to over-rule default behaviour BrechtDeMan@873: BrechtDeMan@873: pageXMLSave(store, testXML); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.initialiseInnerState = function(node) { BrechtDeMan@873: // Parses the received testXML for pre and post test options BrechtDeMan@873: this.currentStateMap = []; BrechtDeMan@873: var preTest = node.preTest; BrechtDeMan@873: var postTest = node.postTest; BrechtDeMan@873: if (preTest == undefined) {preTest = document.createElement("preTest");} BrechtDeMan@873: if (postTest == undefined){postTest= document.createElement("postTest");} BrechtDeMan@873: this.currentStateMap.push(preTest); BrechtDeMan@873: this.currentStateMap.push(node); BrechtDeMan@873: this.currentStateMap.push(postTest); BrechtDeMan@873: this.currentIndex = -1; BrechtDeMan@873: this.advanceInnerState(); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.advanceInnerState = function() { BrechtDeMan@873: this.currentIndex++; BrechtDeMan@873: if (this.currentIndex >= this.currentStateMap.length) { BrechtDeMan@873: this.currentIndex = null; BrechtDeMan@873: this.currentStateMap = this.stateMap[this.stateIndex]; BrechtDeMan@873: this.advanceState(); BrechtDeMan@873: } else { BrechtDeMan@873: if (this.currentStateMap[this.currentIndex].type == "audioHolder") { BrechtDeMan@873: console.log("Loading test page"+this.currentTestId); BrechtDeMan@873: } else if (this.currentStateMap[this.currentIndex].type == "pretest") { BrechtDeMan@873: popup.initState(this.currentStateMap[this.currentIndex]); BrechtDeMan@873: } else if (this.currentStateMap[this.currentIndex].type == "posttest") { BrechtDeMan@873: popup.initState(this.currentStateMap[this.currentIndex]); BrechtDeMan@873: } else { BrechtDeMan@873: this.advanceInnerState(); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.previousState = function(){}; BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: function testEnded(testId) BrechtDeMan@873: { BrechtDeMan@873: pageXMLSave(testId); BrechtDeMan@873: if (testXMLSetups.length-1 > testId) BrechtDeMan@873: { BrechtDeMan@873: // Yes we have another test to perform BrechtDeMan@873: testId = (Number(testId)+1); BrechtDeMan@873: currentState = 'testRun-'+testId; BrechtDeMan@873: loadTest(testId); BrechtDeMan@873: } else { BrechtDeMan@873: console.log('Testing Completed!'); BrechtDeMan@873: currentState = 'postTest'; BrechtDeMan@873: // Check for any post tests BrechtDeMan@873: var xmlSetup = projectXML.find('setup'); BrechtDeMan@873: var postTest = xmlSetup.find('PostTest')[0]; BrechtDeMan@873: popup.initState(postTest); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: function loadProjectSpec(url) { BrechtDeMan@873: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data BrechtDeMan@873: // If url is null, request client to upload project XML document BrechtDeMan@873: var r = new XMLHttpRequest(); BrechtDeMan@873: r.open('GET',url,true); BrechtDeMan@873: r.onload = function() { BrechtDeMan@873: loadProjectSpecCallback(r.response); BrechtDeMan@873: }; BrechtDeMan@873: r.send(); BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: function loadProjectSpecCallback(response) { BrechtDeMan@873: // Function called after asynchronous download of XML project specification BrechtDeMan@873: //var decode = $.parseXML(response); BrechtDeMan@873: //projectXML = $(decode); BrechtDeMan@873: BrechtDeMan@873: var parse = new DOMParser(); BrechtDeMan@873: projectXML = parse.parseFromString(response,'text/xml'); BrechtDeMan@873: BrechtDeMan@873: // Build the specification BrechtDeMan@873: specification.decode(); BrechtDeMan@873: BrechtDeMan@873: testState.stateMap.push(specification.preTest); BrechtDeMan@873: BrechtDeMan@873: // New check if we need to randomise the test order BrechtDeMan@873: if (specification.randomiseOrder) BrechtDeMan@873: { BrechtDeMan@873: specification.audioHolders = randomiseOrder(specification.audioHolders); BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: $(specification.audioHolders).each(function(index,elem){ BrechtDeMan@873: testState.stateMap.push(elem); BrechtDeMan@873: }); BrechtDeMan@873: BrechtDeMan@873: testState.stateMap.push(specification.postTest); BrechtDeMan@873: BrechtDeMan@873: // Obtain the metrics enabled BrechtDeMan@873: $(specification.metrics).each(function(index,node){ BrechtDeMan@873: var enabled = node.textContent; BrechtDeMan@873: switch(node.enabled) BrechtDeMan@873: { BrechtDeMan@873: case 'testTimer': BrechtDeMan@873: sessionMetrics.prototype.enableTestTimer = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementTimer': BrechtDeMan@873: sessionMetrics.prototype.enableElementTimer = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementTracker': BrechtDeMan@873: sessionMetrics.prototype.enableElementTracker = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementListenTracker': BrechtDeMan@873: sessionMetrics.prototype.enableElementListenTracker = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementInitialPosition': BrechtDeMan@873: sessionMetrics.prototype.enableElementInitialPosition = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementFlagListenedTo': BrechtDeMan@873: sessionMetrics.prototype.enableFlagListenedTo = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementFlagMoved': BrechtDeMan@873: sessionMetrics.prototype.enableFlagMoved = true; BrechtDeMan@873: break; BrechtDeMan@873: case 'elementFlagComments': BrechtDeMan@873: sessionMetrics.prototype.enableFlagComments = true; BrechtDeMan@873: break; BrechtDeMan@873: } BrechtDeMan@873: }); BrechtDeMan@873: BrechtDeMan@873: BrechtDeMan@873: BrechtDeMan@873: // Detect the interface to use and load the relevant javascripts. BrechtDeMan@873: var interfaceJS = document.createElement('script'); BrechtDeMan@873: interfaceJS.setAttribute("type","text/javascript"); BrechtDeMan@873: if (specification.interfaceType == 'APE') { BrechtDeMan@873: interfaceJS.setAttribute("src","ape.js"); BrechtDeMan@873: BrechtDeMan@873: // APE comes with a css file BrechtDeMan@873: var css = document.createElement('link'); BrechtDeMan@873: css.rel = 'stylesheet'; BrechtDeMan@873: css.type = 'text/css'; BrechtDeMan@873: css.href = 'ape.css'; BrechtDeMan@873: BrechtDeMan@873: document.getElementsByTagName("head")[0].appendChild(css); BrechtDeMan@873: } BrechtDeMan@873: document.getElementsByTagName("head")[0].appendChild(interfaceJS); BrechtDeMan@873: BrechtDeMan@873: // Define window callbacks for interface BrechtDeMan@873: window.onresize = function(event){resizeWindow(event);}; BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: function createProjectSave(destURL) { BrechtDeMan@873: // Save the data from interface into XML and send to destURL BrechtDeMan@873: // If destURL is null then download XML in client BrechtDeMan@873: // Now time to render file locally BrechtDeMan@873: var xmlDoc = interfaceXMLSave(); BrechtDeMan@873: var parent = document.createElement("div"); BrechtDeMan@873: parent.appendChild(xmlDoc); BrechtDeMan@873: var file = [parent.innerHTML]; BrechtDeMan@873: if (destURL == "null" || destURL == undefined) { BrechtDeMan@873: var bb = new Blob(file,{type : 'application/xml'}); BrechtDeMan@873: var dnlk = window.URL.createObjectURL(bb); BrechtDeMan@873: var a = document.createElement("a"); BrechtDeMan@873: a.hidden = ''; BrechtDeMan@873: a.href = dnlk; BrechtDeMan@873: a.download = "save.xml"; BrechtDeMan@873: a.textContent = "Save File"; BrechtDeMan@873: BrechtDeMan@873: popup.showPopup(); BrechtDeMan@873: popup.popupContent.innerHTML = null; BrechtDeMan@873: popup.popupContent.appendChild(a); BrechtDeMan@873: } else { BrechtDeMan@873: var xmlhttp = new XMLHttpRequest; BrechtDeMan@873: xmlhttp.open("POST",destURL,true); BrechtDeMan@873: xmlhttp.setRequestHeader('Content-Type', 'text/xml'); BrechtDeMan@873: xmlhttp.onerror = function(){ BrechtDeMan@873: console.log('Error saving file to server! Presenting download locally'); BrechtDeMan@873: createProjectSave(null); BrechtDeMan@873: }; BrechtDeMan@873: xmlhttp.onreadystatechange = function() { BrechtDeMan@873: console.log(xmlhttp.status); BrechtDeMan@873: if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { BrechtDeMan@873: createProjectSave(null); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: xmlhttp.send(file); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: // Only other global function which must be defined in the interface class. Determines how to create the XML document. BrechtDeMan@873: function interfaceXMLSave(){ BrechtDeMan@873: // Create the XML string to be exported with results BrechtDeMan@873: var xmlDoc = document.createElement("BrowserEvaluationResult"); BrechtDeMan@873: xmlDoc.appendChild(returnDateNode()); BrechtDeMan@873: for (var i=0; i BrechtDeMan@873: // DD/MM/YY BrechtDeMan@873: // BrechtDeMan@873: // BrechtDeMan@873: var dateTime = new Date(); BrechtDeMan@873: var year = document.createAttribute('year'); BrechtDeMan@873: var month = document.createAttribute('month'); BrechtDeMan@873: var day = document.createAttribute('day'); BrechtDeMan@873: var hour = document.createAttribute('hour'); BrechtDeMan@873: var minute = document.createAttribute('minute'); BrechtDeMan@873: var secs = document.createAttribute('secs'); BrechtDeMan@873: BrechtDeMan@873: year.nodeValue = dateTime.getFullYear(); BrechtDeMan@873: month.nodeValue = dateTime.getMonth()+1; BrechtDeMan@873: day.nodeValue = dateTime.getDate(); BrechtDeMan@873: hour.nodeValue = dateTime.getHours(); BrechtDeMan@873: minute.nodeValue = dateTime.getMinutes(); BrechtDeMan@873: secs.nodeValue = dateTime.getSeconds(); BrechtDeMan@873: BrechtDeMan@873: var hold = document.createElement("datetime"); BrechtDeMan@873: var date = document.createElement("date"); BrechtDeMan@873: date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; BrechtDeMan@873: var time = document.createElement("time"); BrechtDeMan@873: time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; BrechtDeMan@873: BrechtDeMan@873: date.setAttributeNode(year); BrechtDeMan@873: date.setAttributeNode(month); BrechtDeMan@873: date.setAttributeNode(day); BrechtDeMan@873: time.setAttributeNode(hour); BrechtDeMan@873: time.setAttributeNode(minute); BrechtDeMan@873: time.setAttributeNode(secs); BrechtDeMan@873: BrechtDeMan@873: hold.appendChild(date); BrechtDeMan@873: hold.appendChild(time); BrechtDeMan@873: return hold BrechtDeMan@873: BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: function testWaitIndicator() { BrechtDeMan@873: if (audioEngineContext.checkAllReady() == false) { BrechtDeMan@873: var hold = document.createElement("div"); BrechtDeMan@873: hold.id = "testWaitIndicator"; BrechtDeMan@873: hold.className = "indicator-box"; BrechtDeMan@873: hold.style.zIndex = 3; BrechtDeMan@873: var span = document.createElement("span"); BrechtDeMan@873: span.textContent = "Please wait! Elements still loading"; BrechtDeMan@873: hold.appendChild(span); BrechtDeMan@873: var blank = document.createElement('div'); BrechtDeMan@873: blank.className = 'testHalt'; BrechtDeMan@873: blank.id = "testHaltBlank"; BrechtDeMan@873: var body = document.getElementsByTagName('body')[0]; BrechtDeMan@873: body.appendChild(hold); BrechtDeMan@873: body.appendChild(blank); BrechtDeMan@873: testWaitTimerIntervalHolder = setInterval(function(){ BrechtDeMan@873: var ready = audioEngineContext.checkAllReady(); BrechtDeMan@873: if (ready) { BrechtDeMan@873: var elem = document.getElementById('testWaitIndicator'); BrechtDeMan@873: var blank = document.getElementById('testHaltBlank'); BrechtDeMan@873: var body = document.getElementsByTagName('body')[0]; BrechtDeMan@873: body.removeChild(elem); BrechtDeMan@873: body.removeChild(blank); BrechtDeMan@873: clearInterval(testWaitTimerIntervalHolder); BrechtDeMan@873: } BrechtDeMan@873: },500,false); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: var testWaitTimerIntervalHolder = null; BrechtDeMan@873: BrechtDeMan@873: function Specification() { BrechtDeMan@873: // Handles the decoding of the project specification XML into a simple JavaScript Object. BrechtDeMan@873: BrechtDeMan@873: this.interfaceType; BrechtDeMan@873: this.commonInterface; BrechtDeMan@873: this.projectReturn; BrechtDeMan@873: this.randomiseOrder; BrechtDeMan@873: this.collectMetrics; BrechtDeMan@873: this.preTest; BrechtDeMan@873: this.postTest; BrechtDeMan@873: this.metrics =[]; BrechtDeMan@873: BrechtDeMan@873: this.audioHolders = []; BrechtDeMan@873: BrechtDeMan@873: this.decode = function() { BrechtDeMan@873: // projectXML - DOM Parsed document BrechtDeMan@873: var setupNode = projectXML.getElementsByTagName('setup')[0]; BrechtDeMan@873: this.interfaceType = setupNode.getAttribute('interface'); BrechtDeMan@873: this.projectReturn = setupNode.getAttribute('projectReturn'); BrechtDeMan@873: if (setupNode.getAttribute('randomiseOrder') == "true") { BrechtDeMan@873: this.randomiseOrder = true; BrechtDeMan@873: } else {this.randomiseOrder = false;} BrechtDeMan@873: if (setupNode.getAttribute('collectMetrics') == "true") { BrechtDeMan@873: this.collectMetrics = true; BrechtDeMan@873: } else {this.collectMetrics = false;} BrechtDeMan@873: var metricCollection = setupNode.getElementsByTagName('Metric'); BrechtDeMan@873: BrechtDeMan@873: this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest')); BrechtDeMan@873: this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest')); BrechtDeMan@873: BrechtDeMan@873: if (metricCollection.length > 0) { BrechtDeMan@873: metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); BrechtDeMan@873: for (var i=0; i 0) { BrechtDeMan@873: commonInterfaceNode = commonInterfaceNode[0]; BrechtDeMan@873: } else { BrechtDeMan@873: commonInterfaceNode = undefined; BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: this.commonInterface = new function() { BrechtDeMan@873: this.OptionNode = function(child) { BrechtDeMan@873: this.type = child.nodeName; BrechtDeMan@873: if (this.type == 'check') { BrechtDeMan@873: this.check = child.getAttribute('name'); BrechtDeMan@873: } else if (this.type == 'anchor' || this.type == 'reference') { BrechtDeMan@873: this.value = Number(child.textContent); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: this.options = []; BrechtDeMan@873: if (commonInterfaceNode != undefined) { BrechtDeMan@873: var child = commonInterfaceNode.firstElementChild; BrechtDeMan@873: while (child != undefined) { BrechtDeMan@873: this.options.push(new this.OptionNode(child)); BrechtDeMan@873: child = child.nextElementSibling; BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: var audioHolders = projectXML.getElementsByTagName('audioHolder'); BrechtDeMan@873: for (var i=0; i 1 && anchor < 100) {anchor /= 100.0;} BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: if (typeof(reference) == 'number') { BrechtDeMan@873: if (reference > 1 && reference < 100) {reference /= 100.0;} BrechtDeMan@873: } BrechtDeMan@873: BrechtDeMan@873: this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest')); BrechtDeMan@873: this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest')); BrechtDeMan@873: BrechtDeMan@873: this.interfaces = []; BrechtDeMan@873: var interfaceDOM = xml.getElementsByTagName('interface'); BrechtDeMan@873: for (var i=0; i 1) { BrechtDeMan@873: console.log('Error - cannot have more than one anchor!'); BrechtDeMan@873: console.log('Each anchor node will be a normal mode to continue the test'); BrechtDeMan@873: for (var i=0; i 1) { BrechtDeMan@873: console.log('Error - cannot have more than one anchor!'); BrechtDeMan@873: console.log('Each anchor node will be a normal mode to continue the test'); BrechtDeMan@873: for (var i=0; i tag. BrechtDeMan@873: this.interfaceObjects = []; BrechtDeMan@873: this.interfaceObject = function(){}; BrechtDeMan@873: BrechtDeMan@873: this.commentBoxes = []; BrechtDeMan@873: this.elementCommentBox = function(audioObject) { BrechtDeMan@873: var element = audioObject.specification; BrechtDeMan@873: this.audioObject = audioObject; BrechtDeMan@873: this.id = audioObject.id; BrechtDeMan@873: var audioHolderObject = audioObject.specification.parent; BrechtDeMan@873: // Create document objects to hold the comment boxes BrechtDeMan@873: this.trackComment = document.createElement('div'); BrechtDeMan@873: this.trackComment.className = 'comment-div'; BrechtDeMan@873: this.trackComment.id = 'comment-div-'+audioObject.id; BrechtDeMan@873: // Create a string next to each comment asking for a comment BrechtDeMan@873: this.trackString = document.createElement('span'); BrechtDeMan@873: this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id; BrechtDeMan@873: // Create the HTML5 comment box 'textarea' BrechtDeMan@873: this.trackCommentBox = document.createElement('textarea'); BrechtDeMan@873: this.trackCommentBox.rows = '4'; BrechtDeMan@873: this.trackCommentBox.cols = '100'; BrechtDeMan@873: this.trackCommentBox.name = 'trackComment'+audioObject.id; BrechtDeMan@873: this.trackCommentBox.className = 'trackComment'; BrechtDeMan@873: var br = document.createElement('br'); BrechtDeMan@873: // Add to the holder. BrechtDeMan@873: this.trackComment.appendChild(this.trackString); BrechtDeMan@873: this.trackComment.appendChild(br); BrechtDeMan@873: this.trackComment.appendChild(this.trackCommentBox); BrechtDeMan@873: BrechtDeMan@873: this.exportXMLDOM = function() { BrechtDeMan@873: var root = document.createElement('comment'); BrechtDeMan@873: if (this.audioObject.specification.parent.elementComments) { BrechtDeMan@873: var question = document.createElement('question'); BrechtDeMan@873: question.textContent = this.trackString.textContent; BrechtDeMan@873: var response = document.createElement('response'); BrechtDeMan@873: response.textContent = this.trackCommentBox.value; BrechtDeMan@873: root.appendChild(question); BrechtDeMan@873: root.appendChild(response); BrechtDeMan@873: } BrechtDeMan@873: return root; BrechtDeMan@873: }; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.commentQuestions = []; BrechtDeMan@873: BrechtDeMan@873: this.commentBox = function(commentQuestion) { BrechtDeMan@873: this.specification = commentQuestion; BrechtDeMan@873: // Create document objects to hold the comment boxes BrechtDeMan@873: this.holder = document.createElement('div'); BrechtDeMan@873: this.holder.className = 'comment-div'; BrechtDeMan@873: // Create a string next to each comment asking for a comment BrechtDeMan@873: this.string = document.createElement('span'); BrechtDeMan@873: this.string.innerHTML = commentQuestion.question; BrechtDeMan@873: // Create the HTML5 comment box 'textarea' BrechtDeMan@873: this.textArea = document.createElement('textarea'); BrechtDeMan@873: this.textArea.rows = '4'; BrechtDeMan@873: this.textArea.cols = '100'; BrechtDeMan@873: this.textArea.className = 'trackComment'; BrechtDeMan@873: var br = document.createElement('br'); BrechtDeMan@873: // Add to the holder. BrechtDeMan@873: this.holder.appendChild(this.string); BrechtDeMan@873: this.holder.appendChild(br); BrechtDeMan@873: this.holder.appendChild(this.textArea); BrechtDeMan@873: BrechtDeMan@873: this.exportXMLDOM = function() { BrechtDeMan@873: var root = document.createElement('comment'); BrechtDeMan@873: root.id = this.specification.id; BrechtDeMan@873: root.setAttribute('type',this.specification.type); BrechtDeMan@873: root.textContent = this.textArea.value; BrechtDeMan@873: return root; BrechtDeMan@873: }; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.radioBox = function(commentQuestion) { BrechtDeMan@873: this.specification = commentQuestion; BrechtDeMan@873: // Create document objects to hold the comment boxes BrechtDeMan@873: this.holder = document.createElement('div'); BrechtDeMan@873: this.holder.className = 'comment-div'; BrechtDeMan@873: // Create a string next to each comment asking for a comment BrechtDeMan@873: this.string = document.createElement('span'); BrechtDeMan@873: this.string.innerHTML = commentQuestion.statement; BrechtDeMan@873: var br = document.createElement('br'); BrechtDeMan@873: // Add to the holder. BrechtDeMan@873: this.holder.appendChild(this.string); BrechtDeMan@873: this.holder.appendChild(br); BrechtDeMan@873: this.options = []; BrechtDeMan@873: this.inputs = document.createElement('div'); BrechtDeMan@873: this.span = document.createElement('div'); BrechtDeMan@873: this.inputs.align = 'center'; BrechtDeMan@873: this.inputs.style.marginLeft = '12px'; BrechtDeMan@873: this.span.style.marginLeft = '12px'; BrechtDeMan@873: this.span.align = 'center'; BrechtDeMan@873: this.span.style.marginTop = '15px'; BrechtDeMan@873: BrechtDeMan@873: var optCount = commentQuestion.options.length; BrechtDeMan@873: var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; BrechtDeMan@873: console.log(spanMargin); BrechtDeMan@873: for (var i=0; i= this.options.length) { BrechtDeMan@873: break; BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: if (i >= this.options.length) { BrechtDeMan@873: response.textContent = 'null'; BrechtDeMan@873: } else { BrechtDeMan@873: response.textContent = this.options[i].getAttribute('setvalue'); BrechtDeMan@873: response.setAttribute('number',i); BrechtDeMan@873: } BrechtDeMan@873: console.log('Comment: '+question.textContent); BrechtDeMan@873: console.log('Response: '+response.textContent); BrechtDeMan@873: root.appendChild(question); BrechtDeMan@873: root.appendChild(response); BrechtDeMan@873: return root; BrechtDeMan@873: }; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.checkboxBox = function(commentQuestion) { BrechtDeMan@873: this.specification = commentQuestion; BrechtDeMan@873: // Create document objects to hold the comment boxes BrechtDeMan@873: this.holder = document.createElement('div'); BrechtDeMan@873: this.holder.className = 'comment-div'; BrechtDeMan@873: // Create a string next to each comment asking for a comment BrechtDeMan@873: this.string = document.createElement('span'); BrechtDeMan@873: this.string.innerHTML = commentQuestion.statement; BrechtDeMan@873: var br = document.createElement('br'); BrechtDeMan@873: // Add to the holder. BrechtDeMan@873: this.holder.appendChild(this.string); BrechtDeMan@873: this.holder.appendChild(br); BrechtDeMan@873: this.options = []; BrechtDeMan@873: this.inputs = document.createElement('div'); BrechtDeMan@873: this.span = document.createElement('div'); BrechtDeMan@873: this.inputs.align = 'center'; BrechtDeMan@873: this.inputs.style.marginLeft = '12px'; BrechtDeMan@873: this.span.style.marginLeft = '12px'; BrechtDeMan@873: this.span.align = 'center'; BrechtDeMan@873: this.span.style.marginTop = '15px'; BrechtDeMan@873: BrechtDeMan@873: var optCount = commentQuestion.options.length; BrechtDeMan@873: var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px'; BrechtDeMan@873: console.log(spanMargin); BrechtDeMan@873: for (var i=0; i 0) { BrechtDeMan@873: var node = this.commentBoxes.pop(0); BrechtDeMan@873: holder[node.id] = node; BrechtDeMan@873: } BrechtDeMan@873: this.commentBoxes = holder; BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.showCommentBoxes = function(inject, sort) { BrechtDeMan@873: if (sort) {interfaceContext.sortCommentBoxes();} BrechtDeMan@873: for (var i=0; i 0) { BrechtDeMan@873: var time = this.playbackObject.getCurrentPosition(); BrechtDeMan@873: var width = 490; BrechtDeMan@873: var pix = Math.floor(time/this.timePerPixel); BrechtDeMan@873: this.scrubberHead.style.left = pix+'px'; BrechtDeMan@873: if (this.maxTime > 60.0) { BrechtDeMan@873: var secs = time%60; BrechtDeMan@873: var mins = Math.floor((time-secs)/60); BrechtDeMan@873: secs = secs.toString(); BrechtDeMan@873: secs = secs.substr(0,2); BrechtDeMan@873: mins = mins.toString(); BrechtDeMan@873: this.curTimeSpan.textContent = mins+':'+secs; BrechtDeMan@873: } else { BrechtDeMan@873: time = time.toString(); BrechtDeMan@873: this.curTimeSpan.textContent = time.substr(0,4); BrechtDeMan@873: } BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: BrechtDeMan@873: this.interval = undefined; BrechtDeMan@873: BrechtDeMan@873: this.start = function() { BrechtDeMan@873: if (this.playbackObject != undefined && this.interval == undefined) { BrechtDeMan@873: this.interval = setInterval(function(){interfaceContext.playhead.update();},100); BrechtDeMan@873: } BrechtDeMan@873: }; BrechtDeMan@873: this.stop = function() { BrechtDeMan@873: clearInterval(this.interval); BrechtDeMan@873: this.interval = undefined; BrechtDeMan@873: }; BrechtDeMan@873: }; BrechtDeMan@873: } BrechtDeMan@873: