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