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