nicholas@813: /** nicholas@813: * core.js nicholas@813: * nicholas@813: * Main script to run, calls all other core functions and manages loading/store to backend. nicholas@813: * Also contains all global variables. nicholas@813: */ nicholas@813: nicholas@813: /* create the web audio API context and store in audioContext*/ nicholas@813: var audioContext; // Hold the browser web audio API nicholas@813: var projectXML; // Hold the parsed setup XML nicholas@813: var specification; nicholas@813: var interfaceContext; nicholas@813: var popup; // Hold the interfacePopup object nicholas@813: var testState; nicholas@813: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order nicholas@813: var audioEngineContext; // The custome AudioEngine object nicholas@813: var projectReturn; // Hold the URL for the return nicholas@813: nicholas@813: nicholas@813: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it nicholas@813: AudioBufferSourceNode.prototype.owner = undefined; nicholas@813: nicholas@813: window.onload = function() { nicholas@813: // Function called once the browser has loaded all files. nicholas@813: // This should perform any initial commands such as structure / loading documents nicholas@813: nicholas@813: // Create a web audio API context nicholas@813: // Fixed for cross-browser support nicholas@813: var AudioContext = window.AudioContext || window.webkitAudioContext; nicholas@813: audioContext = new AudioContext; nicholas@813: nicholas@813: // Create test state nicholas@813: testState = new stateMachine(); nicholas@813: nicholas@813: // Create the audio engine object nicholas@813: audioEngineContext = new AudioEngine(); nicholas@813: nicholas@813: // Create the popup interface object nicholas@813: popup = new interfacePopup(); nicholas@813: nicholas@813: // Create the specification object nicholas@813: specification = new Specification(); nicholas@813: nicholas@813: // Create the interface object nicholas@813: interfaceContext = new Interface(specification); nicholas@813: }; nicholas@813: nicholas@813: function interfacePopup() { nicholas@813: // Creates an object to manage the popup nicholas@813: this.popup = null; nicholas@813: this.popupContent = null; nicholas@813: this.popupTitle = null; nicholas@813: this.popupResponse = null; nicholas@813: this.buttonProceed = null; nicholas@813: this.buttonPrevious = null; nicholas@813: this.popupOptions = null; nicholas@813: this.currentIndex = null; nicholas@813: this.responses = null; nicholas@813: nicholas@813: this.createPopup = function(){ nicholas@813: // Create popup window interface nicholas@813: var insertPoint = document.getElementById("topLevelBody"); nicholas@813: var blank = document.createElement('div'); nicholas@813: blank.className = 'testHalt'; nicholas@813: nicholas@813: this.popup = document.createElement('div'); nicholas@813: this.popup.id = 'popupHolder'; nicholas@813: this.popup.className = 'popupHolder'; nicholas@813: this.popup.style.position = 'absolute'; nicholas@813: this.popup.style.left = (window.innerWidth/2)-250 + 'px'; nicholas@813: this.popup.style.top = (window.innerHeight/2)-125 + 'px'; nicholas@813: nicholas@813: this.popupContent = document.createElement('div'); nicholas@813: this.popupContent.id = 'popupContent'; nicholas@813: this.popupContent.style.marginTop = '20px'; nicholas@813: this.popupContent.align = 'center'; nicholas@813: this.popup.appendChild(this.popupContent); nicholas@813: nicholas@813: var titleHolder = document.createElement('div'); nicholas@813: titleHolder.id = 'popupTitleHolder'; nicholas@813: titleHolder.style.width = 'inherit'; nicholas@813: titleHolder.style.height = '25px'; nicholas@813: titleHolder.style.marginBottom = '5px'; nicholas@813: nicholas@813: this.popupTitle = document.createElement('span'); nicholas@813: this.popupTitle.id = 'popupTitle'; nicholas@813: titleHolder.appendChild(this.popupTitle); nicholas@813: this.popupContent.appendChild(titleHolder); nicholas@813: nicholas@813: this.popupResponse = document.createElement('div'); nicholas@813: this.popupResponse.id = 'popupResponse'; nicholas@813: this.popupResponse.style.width = 'inherit'; nicholas@813: this.popupResponse.style.minHeight = '170px'; nicholas@813: this.popupResponse.style.maxHeight = '320px'; nicholas@813: this.popupResponse.style.overflow = 'auto'; nicholas@813: this.popupContent.appendChild(this.popupResponse); nicholas@813: nicholas@813: var buttonHolder = document.createElement('div'); nicholas@813: buttonHolder.id='buttonHolder'; nicholas@813: buttonHolder.width = 'inherit'; nicholas@813: buttonHolder.style.height= '30px'; nicholas@813: buttonHolder.align = 'left'; nicholas@813: this.popupContent.appendChild(buttonHolder); nicholas@813: nicholas@813: this.buttonProceed = document.createElement('button'); nicholas@813: this.buttonProceed.className = 'popupButton'; nicholas@813: this.buttonProceed.style.left = '390px'; nicholas@813: this.buttonProceed.style.top = '2px'; nicholas@813: this.buttonProceed.innerHTML = 'Next'; nicholas@813: this.buttonProceed.onclick = function(){popup.proceedClicked();}; nicholas@813: nicholas@813: this.buttonPrevious = document.createElement('button'); nicholas@813: this.buttonPrevious.className = 'popupButton'; nicholas@813: this.buttonPrevious.style.left = '10px'; nicholas@813: this.buttonPrevious.style.top = '2px'; nicholas@813: this.buttonPrevious.innerHTML = 'Back'; nicholas@813: this.buttonPrevious.onclick = function(){popup.previousClick();}; nicholas@813: nicholas@813: buttonHolder.appendChild(this.buttonPrevious); nicholas@813: buttonHolder.appendChild(this.buttonProceed); nicholas@813: nicholas@813: this.popup.style.zIndex = -1; nicholas@813: this.popup.style.visibility = 'hidden'; nicholas@813: blank.style.zIndex = -2; nicholas@813: blank.style.visibility = 'hidden'; nicholas@813: insertPoint.appendChild(this.popup); nicholas@813: insertPoint.appendChild(blank); nicholas@813: }; nicholas@813: nicholas@813: this.showPopup = function(){ nicholas@813: if (this.popup == null) { nicholas@813: this.createPopup(); nicholas@813: } nicholas@813: this.popup.style.zIndex = 3; nicholas@813: this.popup.style.visibility = 'visible'; nicholas@813: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@813: blank.style.zIndex = 2; nicholas@813: blank.style.visibility = 'visible'; nicholas@813: $(window).keypress(function(e){ nicholas@813: if (e.keyCode == 13 && popup.popup.style.visibility == 'visible') nicholas@813: { nicholas@813: // Enter key pressed nicholas@813: var textarea = $(popup.popupContent).find('textarea'); nicholas@813: if (textarea.length != 0) nicholas@813: { nicholas@813: if (textarea[0] == document.activeElement) nicholas@813: {return;} nicholas@813: } nicholas@813: popup.buttonProceed.onclick(); nicholas@813: } nicholas@813: }); nicholas@813: }; nicholas@813: nicholas@813: this.hidePopup = function(){ nicholas@813: this.popup.style.zIndex = -1; nicholas@813: this.popup.style.visibility = 'hidden'; nicholas@813: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@813: blank.style.zIndex = -2; nicholas@813: blank.style.visibility = 'hidden'; nicholas@813: this.buttonPrevious.style.visibility = 'inherit'; nicholas@813: }; nicholas@813: nicholas@813: this.postNode = function() { nicholas@813: // This will take the node from the popupOptions and display it nicholas@813: var node = this.popupOptions[this.currentIndex]; nicholas@813: this.popupResponse.innerHTML = null; nicholas@813: if (node.type == 'statement') { nicholas@813: this.popupTitle.textContent = null; nicholas@813: var statement = document.createElement('span'); nicholas@813: statement.textContent = node.statement; nicholas@813: this.popupResponse.appendChild(statement); nicholas@813: } else if (node.type == 'question') { nicholas@813: this.popupTitle.textContent = node.question; nicholas@813: var textArea = document.createElement('textarea'); nicholas@813: switch (node.boxsize) { nicholas@813: case 'small': nicholas@813: textArea.cols = "20"; nicholas@813: textArea.rows = "1"; nicholas@813: break; nicholas@813: case 'normal': nicholas@813: textArea.cols = "30"; nicholas@813: textArea.rows = "2"; nicholas@813: break; nicholas@813: case 'large': nicholas@813: textArea.cols = "40"; nicholas@813: textArea.rows = "5"; nicholas@813: break; nicholas@813: case 'huge': nicholas@813: textArea.cols = "50"; nicholas@813: textArea.rows = "10"; nicholas@813: break; nicholas@813: } nicholas@813: document.onkeydown=function(){ nicholas@813: if(window.event.keyCode=='13'){ // when you hit enter nicholas@813: window.event.preventDefault(); // don't make newline nicholas@813: popup.proceedClicked(); // go to the next window (or start the test or submit) nicholas@813: } nicholas@813: } nicholas@813: this.popupResponse.appendChild(textArea); nicholas@813: textArea.focus(); nicholas@813: } else if (node.type == 'checkbox') { nicholas@813: this.popupTitle.textContent = node.statement; nicholas@813: var optHold = this.popupResponse; nicholas@813: for (var i=0; i 0) nicholas@813: this.buttonPrevious.style.visibility = 'visible'; nicholas@813: else nicholas@813: this.buttonPrevious.style.visibility = 'hidden'; nicholas@813: }; nicholas@813: nicholas@813: this.initState = function(node) { nicholas@813: //Call this with your preTest and postTest nodes when needed to nicholas@813: // initialise the popup procedure. nicholas@813: this.popupOptions = node.options; nicholas@813: if (this.popupOptions.length > 0) { nicholas@813: if (node.type == 'pretest') { nicholas@813: this.responses = document.createElement('PreTest'); nicholas@813: } else if (node.type == 'posttest') { nicholas@813: this.responses = document.createElement('PostTest'); nicholas@813: } else { nicholas@813: console.log ('WARNING - popup node neither pre or post!'); nicholas@813: this.responses = document.createElement('responses'); nicholas@813: } nicholas@813: this.currentIndex = 0; nicholas@813: this.showPopup(); nicholas@813: this.postNode(); nicholas@813: } else { nicholas@813: advanceState(); nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.proceedClicked = function() { nicholas@813: // Each time the popup button is clicked! nicholas@813: var node = this.popupOptions[this.currentIndex]; nicholas@813: if (node.type == 'question') { nicholas@813: // Must extract the question data nicholas@813: var textArea = $(popup.popupContent).find('textarea')[0]; nicholas@813: if (node.mandatory == true && textArea.value.length == 0) { nicholas@813: alert('This question is mandatory'); nicholas@813: return; nicholas@813: } else { nicholas@813: // Save the text content nicholas@813: var hold = document.createElement('comment'); nicholas@813: hold.id = node.id; nicholas@813: hold.innerHTML = textArea.value; nicholas@813: console.log("Question: "+ node.question); nicholas@813: console.log("Question Response: "+ textArea.value); nicholas@813: this.responses.appendChild(hold); nicholas@813: } nicholas@813: } else if (node.type == 'checkbox') { nicholas@813: // Must extract checkbox data nicholas@813: var optHold = this.popupResponse; nicholas@813: var hold = document.createElement('checkbox'); nicholas@813: console.log("Checkbox: "+ node.statement); nicholas@813: hold.id = node.id; nicholas@813: for (var i=0; i node.max && node.max != null) { nicholas@813: alert('Number is above the maximum value of '+node.max); nicholas@813: return; nicholas@813: } nicholas@813: var hold = document.createElement('number'); nicholas@813: hold.id = node.id; nicholas@813: hold.textContent = input.value; nicholas@813: this.responses.appendChild(hold); nicholas@813: } nicholas@813: this.currentIndex++; nicholas@813: if (this.currentIndex < this.popupOptions.length) { nicholas@813: this.postNode(); nicholas@813: } else { nicholas@813: // Reached the end of the popupOptions nicholas@813: this.hidePopup(); nicholas@813: if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { nicholas@813: testState.stateResults[testState.stateIndex] = this.responses; nicholas@813: } else { nicholas@813: testState.stateResults[testState.stateIndex].appendChild(this.responses); nicholas@813: } nicholas@813: advanceState(); nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.previousClick = function() { nicholas@813: // Triggered when the 'Back' button is clicked in the survey nicholas@813: if (this.currentIndex > 0) { nicholas@813: this.currentIndex--; nicholas@813: var node = this.popupOptions[this.currentIndex]; nicholas@813: if (node.type != 'statement') { nicholas@813: var prevResp = this.responses.childNodes[this.responses.childElementCount-1]; nicholas@813: this.responses.removeChild(prevResp); nicholas@813: } nicholas@813: this.postNode(); nicholas@813: if (node.type == 'question') { nicholas@813: this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent; nicholas@813: } else if (node.type == 'checkbox') { nicholas@813: var options = this.popupContent.getElementsByTagName('input'); nicholas@813: var savedOptions = prevResp.getElementsByTagName('option'); nicholas@813: for (var i=0; i 0) { nicholas@813: if(this.stateIndex != null) { nicholas@813: console.log('NOTE - State already initialise'); nicholas@813: } nicholas@813: this.stateIndex = -1; nicholas@813: var that = this; nicholas@813: var aH_pId = 0; nicholas@813: for (var id=0; id= this.stateMap.length) { nicholas@813: console.log('Test Completed'); nicholas@813: createProjectSave(specification.projectReturn); nicholas@813: } else { nicholas@813: this.currentStateMap = this.stateMap[this.stateIndex]; nicholas@813: if (this.currentStateMap.type == "audioHolder") { nicholas@813: console.log('Loading test page'); nicholas@813: loadTest(this.currentStateMap); nicholas@813: this.initialiseInnerState(this.currentStateMap); nicholas@813: } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { nicholas@813: if (this.currentStateMap.options.length >= 1) { nicholas@813: popup.initState(this.currentStateMap); nicholas@813: } else { nicholas@813: this.advanceState(); nicholas@813: } nicholas@813: } else { nicholas@813: this.advanceState(); nicholas@813: } nicholas@813: } nicholas@813: } else { nicholas@813: this.advanceInnerState(); nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.testPageCompleted = function(store, testXML, testId) { nicholas@813: // Function called each time a test page has been completed nicholas@813: // Can be used to over-rule default behaviour nicholas@813: nicholas@813: pageXMLSave(store, testXML); nicholas@813: }; nicholas@813: nicholas@813: this.initialiseInnerState = function(node) { nicholas@813: // Parses the received testXML for pre and post test options nicholas@813: this.currentStateMap = []; nicholas@813: var preTest = node.preTest; nicholas@813: var postTest = node.postTest; nicholas@813: if (preTest == undefined) {preTest = document.createElement("preTest");} nicholas@813: if (postTest == undefined){postTest= document.createElement("postTest");} nicholas@813: this.currentStateMap.push(preTest); nicholas@813: this.currentStateMap.push(node); nicholas@813: this.currentStateMap.push(postTest); nicholas@813: this.currentIndex = -1; nicholas@813: this.advanceInnerState(); nicholas@813: }; nicholas@813: nicholas@813: this.advanceInnerState = function() { nicholas@813: this.currentIndex++; nicholas@813: if (this.currentIndex >= this.currentStateMap.length) { nicholas@813: this.currentIndex = null; nicholas@813: this.currentStateMap = this.stateMap[this.stateIndex]; nicholas@813: this.advanceState(); nicholas@813: } else { nicholas@813: if (this.currentStateMap[this.currentIndex].type == "audioHolder") { nicholas@813: console.log("Loading test page"+this.currentTestId); nicholas@813: } else if (this.currentStateMap[this.currentIndex].type == "pretest") { nicholas@813: popup.initState(this.currentStateMap[this.currentIndex]); nicholas@813: } else if (this.currentStateMap[this.currentIndex].type == "posttest") { nicholas@813: popup.initState(this.currentStateMap[this.currentIndex]); nicholas@813: } else { nicholas@813: this.advanceInnerState(); nicholas@813: } nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.previousState = function(){}; nicholas@813: } nicholas@813: nicholas@813: function testEnded(testId) nicholas@813: { nicholas@813: pageXMLSave(testId); nicholas@813: if (testXMLSetups.length-1 > testId) nicholas@813: { nicholas@813: // Yes we have another test to perform nicholas@813: testId = (Number(testId)+1); nicholas@813: currentState = 'testRun-'+testId; nicholas@813: loadTest(testId); nicholas@813: } else { nicholas@813: console.log('Testing Completed!'); nicholas@813: currentState = 'postTest'; nicholas@813: // Check for any post tests nicholas@813: var xmlSetup = projectXML.find('setup'); nicholas@813: var postTest = xmlSetup.find('PostTest')[0]; nicholas@813: popup.initState(postTest); nicholas@813: } nicholas@813: } nicholas@813: nicholas@813: function loadProjectSpec(url) { nicholas@813: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data nicholas@813: // If url is null, request client to upload project XML document nicholas@813: var r = new XMLHttpRequest(); nicholas@813: r.open('GET',url,true); nicholas@813: r.onload = function() { nicholas@813: loadProjectSpecCallback(r.response); nicholas@813: }; nicholas@813: r.send(); nicholas@813: }; nicholas@813: nicholas@813: function loadProjectSpecCallback(response) { nicholas@813: // Function called after asynchronous download of XML project specification nicholas@813: //var decode = $.parseXML(response); nicholas@813: //projectXML = $(decode); nicholas@813: nicholas@813: var parse = new DOMParser(); nicholas@813: projectXML = parse.parseFromString(response,'text/xml'); nicholas@813: nicholas@813: // Build the specification nicholas@813: specification.decode(); nicholas@813: nicholas@813: testState.stateMap.push(specification.preTest); nicholas@813: nicholas@813: $(specification.audioHolders).each(function(index,elem){ nicholas@813: testState.stateMap.push(elem); nicholas@813: }); nicholas@813: nicholas@813: testState.stateMap.push(specification.postTest); nicholas@813: nicholas@813: // Obtain the metrics enabled nicholas@813: $(specification.metrics).each(function(index,node){ nicholas@813: var enabled = node.textContent; nicholas@813: switch(node.enabled) nicholas@813: { nicholas@813: case 'testTimer': nicholas@813: sessionMetrics.prototype.enableTestTimer = true; nicholas@813: break; nicholas@813: case 'elementTimer': nicholas@813: sessionMetrics.prototype.enableElementTimer = true; nicholas@813: break; nicholas@813: case 'elementTracker': nicholas@813: sessionMetrics.prototype.enableElementTracker = true; nicholas@813: break; nicholas@813: case 'elementListenTracker': nicholas@813: sessionMetrics.prototype.enableElementListenTracker = true; nicholas@813: break; nicholas@813: case 'elementInitialPosition': nicholas@813: sessionMetrics.prototype.enableElementInitialPosition = true; nicholas@813: break; nicholas@813: case 'elementFlagListenedTo': nicholas@813: sessionMetrics.prototype.enableFlagListenedTo = true; nicholas@813: break; nicholas@813: case 'elementFlagMoved': nicholas@813: sessionMetrics.prototype.enableFlagMoved = true; nicholas@813: break; nicholas@813: case 'elementFlagComments': nicholas@813: sessionMetrics.prototype.enableFlagComments = true; nicholas@813: break; nicholas@813: } nicholas@813: }); nicholas@813: nicholas@813: nicholas@813: nicholas@813: // Detect the interface to use and load the relevant javascripts. nicholas@813: var interfaceJS = document.createElement('script'); nicholas@813: interfaceJS.setAttribute("type","text/javascript"); nicholas@813: if (specification.interfaceType == 'APE') { nicholas@813: interfaceJS.setAttribute("src","ape.js"); nicholas@813: nicholas@813: // APE comes with a css file nicholas@813: var css = document.createElement('link'); nicholas@813: css.rel = 'stylesheet'; nicholas@813: css.type = 'text/css'; nicholas@813: css.href = 'ape.css'; nicholas@813: nicholas@813: document.getElementsByTagName("head")[0].appendChild(css); nicholas@813: } else if (specification.interfaceType == "MUSHRA") nicholas@813: { nicholas@813: interfaceJS.setAttribute("src","mushra.js"); nicholas@813: nicholas@813: // MUSHRA comes with a css file nicholas@813: var css = document.createElement('link'); nicholas@813: css.rel = 'stylesheet'; nicholas@813: css.type = 'text/css'; nicholas@813: css.href = 'mushra.css'; nicholas@813: nicholas@813: document.getElementsByTagName("head")[0].appendChild(css); nicholas@813: } nicholas@813: document.getElementsByTagName("head")[0].appendChild(interfaceJS); nicholas@813: nicholas@813: // Define window callbacks for interface nicholas@813: window.onresize = function(event){interfaceContext.resizeWindow(event);}; nicholas@813: } nicholas@813: nicholas@813: function createProjectSave(destURL) { nicholas@813: // Save the data from interface into XML and send to destURL nicholas@813: // If destURL is null then download XML in client nicholas@813: // Now time to render file locally nicholas@813: var xmlDoc = interfaceXMLSave(); nicholas@813: var parent = document.createElement("div"); nicholas@813: parent.appendChild(xmlDoc); nicholas@813: var file = [parent.innerHTML]; nicholas@813: if (destURL == "null" || destURL == undefined) { nicholas@813: var bb = new Blob(file,{type : 'application/xml'}); nicholas@813: var dnlk = window.URL.createObjectURL(bb); nicholas@813: var a = document.createElement("a"); nicholas@813: a.hidden = ''; nicholas@813: a.href = dnlk; nicholas@813: a.download = "save.xml"; nicholas@813: a.textContent = "Save File"; nicholas@813: nicholas@813: popup.showPopup(); nicholas@813: popup.popupContent.innerHTML = null; nicholas@813: popup.popupContent.appendChild(a); nicholas@813: } else { nicholas@813: var xmlhttp = new XMLHttpRequest; nicholas@813: xmlhttp.open("POST",destURL,true); nicholas@813: xmlhttp.setRequestHeader('Content-Type', 'text/xml'); nicholas@813: xmlhttp.onerror = function(){ nicholas@813: console.log('Error saving file to server! Presenting download locally'); nicholas@813: createProjectSave(null); nicholas@813: }; nicholas@813: xmlhttp.onreadystatechange = function() { nicholas@813: console.log(xmlhttp.status); nicholas@813: if (xmlhttp.status != 200 && xmlhttp.readyState == 4) { nicholas@813: createProjectSave(null); nicholas@813: } else { nicholas@813: if (xmlhttp.responseXML == null) nicholas@813: { nicholas@813: return createProjectSave(null); nicholas@813: } nicholas@813: var response = xmlhttp.responseXML.childNodes[0]; nicholas@813: if (response.getAttribute('state') == "OK") nicholas@813: { nicholas@813: var file = response.getElementsByTagName('file')[0]; nicholas@813: console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B'); nicholas@813: popup.showPopup(); nicholas@813: popup.popupContent.innerHTML = null; nicholas@813: popup.popupContent.textContent = "Thank you!"; nicholas@813: } else { nicholas@813: var message = response.getElementsByTagName('message')[0]; nicholas@813: errorSessionDump(message.textContent); nicholas@813: } nicholas@813: } nicholas@813: }; nicholas@813: xmlhttp.send(file); nicholas@813: } nicholas@813: } nicholas@813: nicholas@813: function errorSessionDump(msg){ nicholas@813: // Create the partial interface XML save nicholas@813: // Include error node with message on why the dump occured nicholas@813: var xmlDoc = interfaceXMLSave(); nicholas@813: var err = document.createElement('error'); nicholas@813: err.textContent = msg; nicholas@813: xmlDoc.appendChild(err); nicholas@813: var parent = document.createElement("div"); nicholas@813: parent.appendChild(xmlDoc); nicholas@813: var file = [parent.innerHTML]; nicholas@813: var bb = new Blob(file,{type : 'application/xml'}); nicholas@813: var dnlk = window.URL.createObjectURL(bb); nicholas@813: var a = document.createElement("a"); nicholas@813: a.hidden = ''; nicholas@813: a.href = dnlk; nicholas@813: a.download = "save.xml"; nicholas@813: a.textContent = "Save File"; nicholas@813: nicholas@813: popup.showPopup(); nicholas@813: popup.popupContent.innerHTML = "ERROR : "+msg; nicholas@813: popup.popupContent.appendChild(a); nicholas@813: } nicholas@813: nicholas@813: // Only other global function which must be defined in the interface class. Determines how to create the XML document. nicholas@813: function interfaceXMLSave(){ nicholas@813: // Create the XML string to be exported with results nicholas@813: var xmlDoc = document.createElement("BrowserEvaluationResult"); nicholas@813: var projectDocument = specification.projectXML; nicholas@813: projectDocument.setAttribute('file-name',url); nicholas@813: xmlDoc.appendChild(projectDocument); nicholas@813: xmlDoc.appendChild(returnDateNode()); nicholas@813: xmlDoc.appendChild(interfaceContext.returnNavigator()); nicholas@813: for (var i=0; i nicholas@813: // DD/MM/YY nicholas@813: // nicholas@813: // nicholas@813: var dateTime = new Date(); nicholas@813: var year = document.createAttribute('year'); nicholas@813: var month = document.createAttribute('month'); nicholas@813: var day = document.createAttribute('day'); nicholas@813: var hour = document.createAttribute('hour'); nicholas@813: var minute = document.createAttribute('minute'); nicholas@813: var secs = document.createAttribute('secs'); nicholas@813: nicholas@813: year.nodeValue = dateTime.getFullYear(); nicholas@813: month.nodeValue = dateTime.getMonth()+1; nicholas@813: day.nodeValue = dateTime.getDate(); nicholas@813: hour.nodeValue = dateTime.getHours(); nicholas@813: minute.nodeValue = dateTime.getMinutes(); nicholas@813: secs.nodeValue = dateTime.getSeconds(); nicholas@813: nicholas@813: var hold = document.createElement("datetime"); nicholas@813: var date = document.createElement("date"); nicholas@813: date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; nicholas@813: var time = document.createElement("time"); nicholas@813: time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; nicholas@813: nicholas@813: date.setAttributeNode(year); nicholas@813: date.setAttributeNode(month); nicholas@813: date.setAttributeNode(day); nicholas@813: time.setAttributeNode(hour); nicholas@813: time.setAttributeNode(minute); nicholas@813: time.setAttributeNode(secs); nicholas@813: nicholas@813: hold.appendChild(date); nicholas@813: hold.appendChild(time); nicholas@813: return hold nicholas@813: nicholas@813: } nicholas@813: nicholas@813: function Specification() { nicholas@813: // Handles the decoding of the project specification XML into a simple JavaScript Object. nicholas@813: nicholas@813: this.interfaceType = null; nicholas@813: this.commonInterface = null; nicholas@813: this.projectReturn = null; nicholas@813: this.randomiseOrder = null; nicholas@813: this.collectMetrics = null; nicholas@813: this.testPages = null; nicholas@813: this.preTest = null; nicholas@813: this.postTest = null; nicholas@813: this.metrics =[]; nicholas@813: nicholas@813: this.audioHolders = []; nicholas@813: nicholas@813: this.decode = function() { nicholas@813: // projectXML - DOM Parsed document nicholas@813: this.projectXML = projectXML.childNodes[0]; nicholas@813: var setupNode = projectXML.getElementsByTagName('setup')[0]; nicholas@813: this.interfaceType = setupNode.getAttribute('interface'); nicholas@813: this.projectReturn = setupNode.getAttribute('projectReturn'); nicholas@813: this.testPages = setupNode.getAttribute('testPages'); nicholas@813: if (setupNode.getAttribute('randomiseOrder') == "true") { nicholas@813: this.randomiseOrder = true; nicholas@813: } else {this.randomiseOrder = false;} nicholas@813: if (setupNode.getAttribute('collectMetrics') == "true") { nicholas@813: this.collectMetrics = true; nicholas@813: } else {this.collectMetrics = false;} nicholas@813: if (isNaN(Number(this.testPages)) || this.testPages == undefined) nicholas@813: { nicholas@813: this.testPages = null; nicholas@813: } else { nicholas@813: this.testPages = Number(this.testPages); nicholas@813: if (this.testPages == 0) {this.testPages = null;} nicholas@813: } nicholas@813: var metricCollection = setupNode.getElementsByTagName('Metric'); nicholas@813: nicholas@813: this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest')); nicholas@813: this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest')); nicholas@813: nicholas@813: if (metricCollection.length > 0) { nicholas@813: metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); nicholas@813: for (var i=0; i 0) { nicholas@813: commonInterfaceNode = commonInterfaceNode[0]; nicholas@813: } else { nicholas@813: commonInterfaceNode = undefined; nicholas@813: } nicholas@813: nicholas@813: this.commonInterface = new function() { nicholas@813: this.OptionNode = function(child) { nicholas@813: this.type = child.nodeName; nicholas@813: if (this.type == 'option') nicholas@813: { nicholas@813: this.name = child.getAttribute('name'); nicholas@813: } nicholas@813: else if (this.type == 'check') { nicholas@813: this.check = child.getAttribute('name'); nicholas@813: if (this.check == 'scalerange') { nicholas@813: this.min = child.getAttribute('min'); nicholas@813: this.max = child.getAttribute('max'); nicholas@813: if (this.min == null) {this.min = 1;} nicholas@813: else if (Number(this.min) > 1 && this.min != null) { nicholas@813: this.min = Number(this.min)/100; nicholas@813: } else { nicholas@813: this.min = Number(this.min); nicholas@813: } nicholas@813: if (this.max == null) {this.max = 0;} nicholas@813: else if (Number(this.max) > 1 && this.max != null) { nicholas@813: this.max = Number(this.max)/100; nicholas@813: } else { nicholas@813: this.max = Number(this.max); nicholas@813: } nicholas@813: } nicholas@813: } else if (this.type == 'anchor' || this.type == 'reference') { nicholas@813: Console.log("WARNING: Anchor and Reference tags in the node are depricated"); nicholas@813: } nicholas@813: }; nicholas@813: this.options = []; nicholas@813: if (commonInterfaceNode != undefined) { nicholas@813: var child = commonInterfaceNode.firstElementChild; nicholas@813: while (child != undefined) { nicholas@813: this.options.push(new this.OptionNode(child)); nicholas@813: child = child.nextElementSibling; nicholas@813: } nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: var audioHolders = projectXML.getElementsByTagName('audioHolder'); nicholas@813: for (var i=0; i audioHolders.length) nicholas@813: { nicholas@813: console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!'); nicholas@813: this.testPages = audioHolders.length; nicholas@813: } nicholas@813: var aH = this.audioHolders; nicholas@813: this.audioHolders = []; nicholas@813: for (var i=0; i 1) nicholas@813: { this.marker /= 100.0;} nicholas@813: if (this.marker >= 0 && this.marker <= 1) nicholas@813: { nicholas@813: this.enforce = true; nicholas@813: return; nicholas@813: } else { nicholas@813: console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!"); nicholas@813: console.log("ERROR - Marker not enforced!"); nicholas@813: } nicholas@813: } else { nicholas@813: console.log("ERROR - Marker of audioElement "+this.id+" is not a number!"); nicholas@813: console.log("ERROR - Marker not enforced!"); nicholas@813: } nicholas@813: } nicholas@813: } nicholas@813: this.marker = false; nicholas@813: this.enforce = false; nicholas@813: }; nicholas@813: nicholas@813: this.commentQuestionNode = function(xml) { nicholas@813: this.childOption = function(element) { nicholas@813: this.type = 'option'; nicholas@813: this.name = element.getAttribute('name'); nicholas@813: this.text = element.textContent; nicholas@813: }; nicholas@813: this.id = xml.id; nicholas@813: if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;} nicholas@813: else {this.mandatory = false;} nicholas@813: this.type = xml.getAttribute('type'); nicholas@813: if (this.type == undefined) {this.type = 'text';} nicholas@813: switch (this.type) { nicholas@813: case 'text': nicholas@813: this.question = xml.textContent; nicholas@813: break; nicholas@813: case 'radio': nicholas@813: var child = xml.firstElementChild; nicholas@813: this.options = []; nicholas@813: while (child != undefined) { nicholas@813: if (child.nodeName == 'statement' && this.statement == undefined) { nicholas@813: this.statement = child.textContent; nicholas@813: } else if (child.nodeName == 'option') { nicholas@813: this.options.push(new this.childOption(child)); nicholas@813: } nicholas@813: child = child.nextElementSibling; nicholas@813: } nicholas@813: break; nicholas@813: case 'checkbox': nicholas@813: var child = xml.firstElementChild; nicholas@813: this.options = []; nicholas@813: while (child != undefined) { nicholas@813: if (child.nodeName == 'statement' && this.statement == undefined) { nicholas@813: this.statement = child.textContent; nicholas@813: } else if (child.nodeName == 'option') { nicholas@813: this.options.push(new this.childOption(child)); nicholas@813: } nicholas@813: child = child.nextElementSibling; nicholas@813: } nicholas@813: break; nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.id = xml.id; nicholas@813: this.hostURL = xml.getAttribute('hostURL'); nicholas@813: this.sampleRate = xml.getAttribute('sampleRate'); nicholas@813: if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;} nicholas@813: else {this.randomiseOrder = false;} nicholas@813: this.repeatCount = xml.getAttribute('repeatCount'); nicholas@813: if (xml.getAttribute('loop') == 'true') {this.loop = true;} nicholas@813: else {this.loop == false;} nicholas@813: if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;} nicholas@813: else {this.elementComments = false;} nicholas@813: nicholas@813: this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest')); nicholas@813: this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest')); nicholas@813: nicholas@813: this.interfaces = []; nicholas@813: var interfaceDOM = xml.getElementsByTagName('interface'); nicholas@813: for (var i=0; i tag. nicholas@813: this.interfaceObjects = []; nicholas@813: this.interfaceObject = function(){}; nicholas@813: nicholas@813: this.resizeWindow = function(event) nicholas@813: { nicholas@813: for(var i=0; i= 600) nicholas@813: { nicholas@813: boxwidth = 600; nicholas@813: } nicholas@813: else if (boxwidth < 400) nicholas@813: { nicholas@813: boxwidth = 400; nicholas@813: } nicholas@813: this.trackComment.style.width = boxwidth+"px"; nicholas@813: this.trackCommentBox.style.width = boxwidth-6+"px"; nicholas@813: }; nicholas@813: this.resize(); nicholas@813: }; nicholas@813: nicholas@813: this.commentQuestions = []; nicholas@813: nicholas@813: this.commentBox = function(commentQuestion) { nicholas@813: this.specification = commentQuestion; nicholas@813: // Create document objects to hold the comment boxes nicholas@813: this.holder = document.createElement('div'); nicholas@813: this.holder.className = 'comment-div'; nicholas@813: // Create a string next to each comment asking for a comment nicholas@813: this.string = document.createElement('span'); nicholas@813: this.string.innerHTML = commentQuestion.question; nicholas@813: // Create the HTML5 comment box 'textarea' nicholas@813: this.textArea = document.createElement('textarea'); nicholas@813: this.textArea.rows = '4'; nicholas@813: this.textArea.cols = '100'; nicholas@813: this.textArea.className = 'trackComment'; nicholas@813: var br = document.createElement('br'); nicholas@813: // Add to the holder. nicholas@813: this.holder.appendChild(this.string); nicholas@813: this.holder.appendChild(br); nicholas@813: this.holder.appendChild(this.textArea); nicholas@813: nicholas@813: this.exportXMLDOM = function() { nicholas@813: var root = document.createElement('comment'); nicholas@813: root.id = this.specification.id; nicholas@813: root.setAttribute('type',this.specification.type); nicholas@813: root.textContent = this.textArea.value; nicholas@813: console.log("Question: "+this.string.textContent); nicholas@813: console.log("Response: "+root.textContent); nicholas@813: return root; nicholas@813: }; nicholas@813: this.resize = function() nicholas@813: { nicholas@813: var boxwidth = (window.innerWidth-100)/2; nicholas@813: if (boxwidth >= 600) nicholas@813: { nicholas@813: boxwidth = 600; nicholas@813: } nicholas@813: else if (boxwidth < 400) nicholas@813: { nicholas@813: boxwidth = 400; nicholas@813: } nicholas@813: this.holder.style.width = boxwidth+"px"; nicholas@813: this.textArea.style.width = boxwidth-6+"px"; nicholas@813: }; nicholas@813: this.resize(); nicholas@813: }; nicholas@813: nicholas@813: this.radioBox = function(commentQuestion) { nicholas@813: this.specification = commentQuestion; nicholas@813: // Create document objects to hold the comment boxes nicholas@813: this.holder = document.createElement('div'); nicholas@813: this.holder.className = 'comment-div'; nicholas@813: // Create a string next to each comment asking for a comment nicholas@813: this.string = document.createElement('span'); nicholas@813: this.string.innerHTML = commentQuestion.statement; nicholas@813: var br = document.createElement('br'); nicholas@813: // Add to the holder. nicholas@813: this.holder.appendChild(this.string); nicholas@813: this.holder.appendChild(br); nicholas@813: this.options = []; nicholas@813: this.inputs = document.createElement('div'); nicholas@813: this.span = document.createElement('div'); nicholas@813: this.inputs.align = 'center'; nicholas@813: this.inputs.style.marginLeft = '12px'; nicholas@813: this.span.style.marginLeft = '12px'; nicholas@813: this.span.align = 'center'; nicholas@813: this.span.style.marginTop = '15px'; nicholas@813: nicholas@813: var optCount = commentQuestion.options.length; nicholas@813: for (var i=0; i= this.options.length) { nicholas@813: break; nicholas@813: } nicholas@813: } nicholas@813: if (i >= this.options.length) { nicholas@813: response.textContent = 'null'; nicholas@813: } else { nicholas@813: response.textContent = this.options[i].getAttribute('setvalue'); nicholas@813: response.setAttribute('number',i); nicholas@813: } nicholas@813: console.log('Comment: '+question.textContent); nicholas@813: console.log('Response: '+response.textContent); nicholas@813: root.appendChild(question); nicholas@813: root.appendChild(response); nicholas@813: return root; nicholas@813: }; nicholas@813: this.resize = function() nicholas@813: { nicholas@813: var boxwidth = (window.innerWidth-100)/2; nicholas@813: if (boxwidth >= 600) nicholas@813: { nicholas@813: boxwidth = 600; nicholas@813: } nicholas@813: else if (boxwidth < 400) nicholas@813: { nicholas@813: boxwidth = 400; nicholas@813: } nicholas@813: this.holder.style.width = boxwidth+"px"; nicholas@813: var text = this.holder.children[2]; nicholas@813: var options = this.holder.children[3]; nicholas@813: var optCount = options.children.length; nicholas@813: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nicholas@813: var options = options.firstChild; nicholas@813: var text = text.firstChild; nicholas@813: options.style.marginRight = spanMargin; nicholas@813: options.style.marginLeft = spanMargin; nicholas@813: text.style.marginRight = spanMargin; nicholas@813: text.style.marginLeft = spanMargin; nicholas@813: while(options.nextSibling != undefined) nicholas@813: { nicholas@813: options = options.nextSibling; nicholas@813: text = text.nextSibling; nicholas@813: options.style.marginRight = spanMargin; nicholas@813: options.style.marginLeft = spanMargin; nicholas@813: text.style.marginRight = spanMargin; nicholas@813: text.style.marginLeft = spanMargin; nicholas@813: } nicholas@813: }; nicholas@813: this.resize(); nicholas@813: }; nicholas@813: nicholas@813: this.checkboxBox = function(commentQuestion) { nicholas@813: this.specification = commentQuestion; nicholas@813: // Create document objects to hold the comment boxes nicholas@813: this.holder = document.createElement('div'); nicholas@813: this.holder.className = 'comment-div'; nicholas@813: // Create a string next to each comment asking for a comment nicholas@813: this.string = document.createElement('span'); nicholas@813: this.string.innerHTML = commentQuestion.statement; nicholas@813: var br = document.createElement('br'); nicholas@813: // Add to the holder. nicholas@813: this.holder.appendChild(this.string); nicholas@813: this.holder.appendChild(br); nicholas@813: this.options = []; nicholas@813: this.inputs = document.createElement('div'); nicholas@813: this.span = document.createElement('div'); nicholas@813: this.inputs.align = 'center'; nicholas@813: this.inputs.style.marginLeft = '12px'; nicholas@813: this.span.style.marginLeft = '12px'; nicholas@813: this.span.align = 'center'; nicholas@813: this.span.style.marginTop = '15px'; nicholas@813: nicholas@813: var optCount = commentQuestion.options.length; nicholas@813: for (var i=0; i= 600) nicholas@813: { nicholas@813: boxwidth = 600; nicholas@813: } nicholas@813: else if (boxwidth < 400) nicholas@813: { nicholas@813: boxwidth = 400; nicholas@813: } nicholas@813: this.holder.style.width = boxwidth+"px"; nicholas@813: var text = this.holder.children[2]; nicholas@813: var options = this.holder.children[3]; nicholas@813: var optCount = options.children.length; nicholas@813: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nicholas@813: var options = options.firstChild; nicholas@813: var text = text.firstChild; nicholas@813: options.style.marginRight = spanMargin; nicholas@813: options.style.marginLeft = spanMargin; nicholas@813: text.style.marginRight = spanMargin; nicholas@813: text.style.marginLeft = spanMargin; nicholas@813: while(options.nextSibling != undefined) nicholas@813: { nicholas@813: options = options.nextSibling; nicholas@813: text = text.nextSibling; nicholas@813: options.style.marginRight = spanMargin; nicholas@813: options.style.marginLeft = spanMargin; nicholas@813: text.style.marginRight = spanMargin; nicholas@813: text.style.marginLeft = spanMargin; nicholas@813: } nicholas@813: }; nicholas@813: this.resize(); nicholas@813: }; nicholas@813: nicholas@813: this.createCommentBox = function(audioObject) { nicholas@813: var node = new this.elementCommentBox(audioObject); nicholas@813: this.commentBoxes.push(node); nicholas@813: audioObject.commentDOM = node; nicholas@813: return node; nicholas@813: }; nicholas@813: nicholas@813: this.sortCommentBoxes = function() { nicholas@813: var holder = []; nicholas@813: while (this.commentBoxes.length > 0) { nicholas@813: var node = this.commentBoxes.pop(0); nicholas@813: holder[node.id] = node; nicholas@813: } nicholas@813: this.commentBoxes = holder; nicholas@813: }; nicholas@813: nicholas@813: this.showCommentBoxes = function(inject, sort) { nicholas@813: if (sort) {interfaceContext.sortCommentBoxes();} nicholas@813: for (var i=0; i 0) { nicholas@813: var time = this.playbackObject.getCurrentPosition(); nicholas@813: if (time > 0) { nicholas@813: var width = 490; nicholas@813: var pix = Math.floor(time/this.timePerPixel); nicholas@813: this.scrubberHead.style.left = pix+'px'; nicholas@813: if (this.maxTime > 60.0) { nicholas@813: var secs = time%60; nicholas@813: var mins = Math.floor((time-secs)/60); nicholas@813: secs = secs.toString(); nicholas@813: secs = secs.substr(0,2); nicholas@813: mins = mins.toString(); nicholas@813: this.curTimeSpan.textContent = mins+':'+secs; nicholas@813: } else { nicholas@813: time = time.toString(); nicholas@813: this.curTimeSpan.textContent = time.substr(0,4); nicholas@813: } nicholas@813: } else { nicholas@813: this.scrubberHead.style.left = '0px'; nicholas@813: if (this.maxTime < 60) { nicholas@813: this.curTimeSpan.textContent = '0.00'; nicholas@813: } else { nicholas@813: this.curTimeSpan.textContent = '00:00'; nicholas@813: } nicholas@813: } nicholas@813: } nicholas@813: }; nicholas@813: nicholas@813: this.interval = undefined; nicholas@813: nicholas@813: this.start = function() { nicholas@813: if (this.playbackObject != undefined && this.interval == undefined) { nicholas@813: if (this.maxTime < 60) { nicholas@813: this.interval = setInterval(function(){interfaceContext.playhead.update();},10); nicholas@813: } else { nicholas@813: this.interval = setInterval(function(){interfaceContext.playhead.update();},100); nicholas@813: } nicholas@813: } nicholas@813: }; nicholas@813: this.stop = function() { nicholas@813: clearInterval(this.interval); nicholas@813: this.interval = undefined; nicholas@813: if (this.maxTime < 60) { nicholas@813: this.curTimeSpan.textContent = '0.00'; nicholas@813: } else { nicholas@813: this.curTimeSpan.textContent = '00:00'; nicholas@813: } nicholas@813: }; nicholas@813: }; nicholas@813: nicholas@813: // Global Checkers nicholas@813: // These functions will help enforce the checkers nicholas@813: this.checkHiddenAnchor = function() nicholas@813: { nicholas@813: var audioHolder = testState.currentStateMap[testState.currentIndex]; nicholas@813: if (audioHolder.anchorId != null) nicholas@813: { nicholas@813: var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId]; nicholas@813: if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) nicholas@813: { nicholas@813: // Anchor is not set below nicholas@813: console.log('Anchor node not below marker value'); nicholas@813: alert('Please keep listening'); nicholas@813: return false; nicholas@813: } nicholas@813: } nicholas@813: return true; nicholas@813: }; nicholas@813: nicholas@813: this.checkHiddenReference = function() nicholas@813: { nicholas@813: var audioHolder = testState.currentStateMap[testState.currentIndex]; nicholas@813: if (audioHolder.referenceId != null) nicholas@813: { nicholas@813: var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId]; nicholas@813: if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) nicholas@813: { nicholas@813: // Anchor is not set below nicholas@813: console.log('Reference node not above marker value'); nicholas@813: alert('Please keep listening'); nicholas@813: return false; nicholas@813: } nicholas@813: } nicholas@813: return true; nicholas@813: }; nicholas@813: }