b@1608: /** b@1608: * ape.js b@1608: * Create the APE interface b@1608: */ b@1608: b@1608: // preTest - In preTest state b@1608: // testRun-ID - In test running, test Id number at the end 'testRun-2' b@1608: // testRunPost-ID - Post test of test ID b@1608: // testRunPre-ID - Pre-test of test ID b@1608: // postTest - End of test, final submission! b@1608: b@1608: b@1608: // Once this is loaded and parsed, begin execution b@1608: loadInterface(projectXML); b@1608: b@1608: function loadInterface(xmlDoc) { b@1608: b@1608: // Get the dimensions of the screen available to the page b@1608: var width = window.innerWidth; b@1608: var height = window.innerHeight; b@1608: b@1608: // The injection point into the HTML page b@1608: var insertPoint = document.getElementById("topLevelBody"); b@1608: var testContent = document.createElement('div'); b@1608: testContent.id = 'testContent'; b@1608: b@1608: b@1608: // Decode parts of the xmlDoc that are needed b@1608: // xmlDoc MUST already be parsed by jQuery! b@1608: var xmlSetup = xmlDoc.find('setup'); b@1608: // Should put in an error function here incase of malprocessed or malformed XML b@1608: nickjillings@1634: // Create pre and post test questions nickjillings@1634: nickjillings@1634: var preTest = xmlSetup.find('PreTest'); nickjillings@1634: var postTest = xmlSetup.find('PostTest'); nickjillings@1634: preTest = preTest[0]; nickjillings@1634: postTest = postTest[0]; nickjillings@1634: nickjillings@1634: if (preTest == undefined) {preTest = document.createElement("preTest");} nickjillings@1634: if (postTest == undefined){postTest= document.createElement("postTest");} nickjillings@1634: nickjillings@1634: testState.stateMap.push(preTest); nickjillings@1634: b@1608: // Extract the different test XML DOM trees b@1608: var audioHolders = xmlDoc.find('audioHolder'); nickjillings@1634: var testXMLSetups = []; b@1608: audioHolders.each(function(index,element) { b@1608: var repeatN = element.attributes['repeatCount'].value; b@1608: for (var r=0; r<=repeatN; r++) { nickjillings@1634: testXMLSetups.push(element); b@1608: } b@1608: }); b@1608: b@1608: // New check if we need to randomise the test order b@1608: var randomise = xmlSetup[0].attributes['randomiseOrder']; b@1608: if (randomise != undefined) { nickjillings@1616: if (randomise.value === 'true'){ nickjillings@1616: randomise = true; nickjillings@1616: } else { nickjillings@1616: randomise = false; nickjillings@1616: } b@1608: } else { b@1608: randomise = false; b@1608: } nickjillings@1616: b@1608: if (randomise) b@1608: { b@1608: testXMLSetups = randomiseOrder(testXMLSetups); b@1608: } nickjillings@1634: nickjillings@1634: $(testXMLSetups).each(function(index,elem){ nickjillings@1634: testState.stateMap.push(elem); nickjillings@1634: }) nickjillings@1634: nickjillings@1634: testState.stateMap.push(postTest); b@1608: b@1608: // Obtain the metrics enabled b@1608: var metricNode = xmlSetup.find('Metric'); b@1608: var metricNode = metricNode.find('metricEnable'); b@1608: metricNode.each(function(index,node){ b@1608: var enabled = node.textContent; b@1608: switch(enabled) b@1608: { b@1608: case 'testTimer': b@1608: sessionMetrics.prototype.enableTestTimer = true; b@1608: break; b@1608: case 'elementTimer': b@1608: sessionMetrics.prototype.enableElementTimer = true; b@1608: break; b@1608: case 'elementTracker': b@1608: sessionMetrics.prototype.enableElementTracker = true; b@1608: break; b@1608: case 'elementInitalPosition': b@1608: sessionMetrics.prototype.enableElementInitialPosition = true; b@1608: break; b@1608: case 'elementFlagListenedTo': b@1608: sessionMetrics.prototype.enableFlagListenedTo = true; b@1608: break; b@1608: case 'elementFlagMoved': b@1608: sessionMetrics.prototype.enableFlagMoved = true; b@1608: break; b@1608: case 'elementFlagComments': b@1608: sessionMetrics.prototype.enableFlagComments = true; b@1608: break; b@1608: } b@1608: }); b@1608: b@1608: // Create APE specific metric functions b@1608: audioEngineContext.metric.initialiseTest = function() b@1608: { b@1608: var sliders = document.getElementsByClassName('track-slider'); b@1608: for (var i=0; i= 0) b@1608: { b@1608: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); b@1608: } b@1608: this.lastClicked = id; b@1608: audioEngineContext.audioObjects[id].metric.listening(time); b@1608: } b@1606: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id b@1608: }; b@1608: b@1608: // Create the top div for the Title element b@1608: var titleAttr = xmlSetup[0].attributes['title']; b@1608: var title = document.createElement('div'); b@1608: title.className = "title"; b@1608: title.align = "center"; b@1608: var titleSpan = document.createElement('span'); b@1608: b@1608: // Set title to that defined in XML, else set to default b@1608: if (titleAttr != undefined) { b@1608: titleSpan.innerHTML = titleAttr.value; b@1608: } else { b@1608: titleSpan.innerHTML = 'Listening test'; b@1608: } b@1608: // Insert the titleSpan element into the title div element. b@1608: title.appendChild(titleSpan); b@1608: b@1608: var pagetitle = document.createElement('div'); b@1608: pagetitle.className = "pageTitle"; b@1608: pagetitle.align = "center"; b@1608: var titleSpan = document.createElement('span'); b@1608: titleSpan.id = "pageTitle"; b@1608: pagetitle.appendChild(titleSpan); b@1608: b@1608: // Store the return URL path in global projectReturn b@1608: projectReturn = xmlSetup[0].attributes['projectReturn'].value; b@1608: b@1608: // Create Interface buttons! b@1608: var interfaceButtons = document.createElement('div'); b@1608: interfaceButtons.id = 'interface-buttons'; b@1608: b@1608: // MANUAL DOWNLOAD POINT b@1608: // If project return is null, this MUST be specified as the location to create the download link b@1608: var downloadPoint = document.createElement('div'); b@1608: downloadPoint.id = 'download-point'; b@1608: b@1608: // Create playback start/stop points b@1608: var playback = document.createElement("button"); b@1609: playback.innerHTML = 'Stop'; b@1608: playback.id = 'playback-button'; b@1608: // onclick function. Check if it is playing or not, call the correct function in the b@1608: // audioEngine, change the button text to reflect the next state. b@1608: playback.onclick = function() { b@1609: if (audioEngineContext.status == 1) { b@1609: audioEngineContext.stop(); b@1608: this.innerHTML = 'Stop'; b@1606: var time = audioEngineContext.timer.getTestTime(); b@1606: console.log('Stopped at ' + time); // DEBUG/SAFETY b@1608: } b@1608: }; b@1608: // Create Submit (save) button b@1608: var submit = document.createElement("button"); b@1608: submit.innerHTML = 'Submit'; b@1608: submit.onclick = buttonSubmitClick; b@1608: submit.id = 'submit-button'; b@1608: // Append the interface buttons into the interfaceButtons object. b@1608: interfaceButtons.appendChild(playback); b@1608: interfaceButtons.appendChild(submit); b@1608: interfaceButtons.appendChild(downloadPoint); b@1608: b@1608: // Now create the slider and HTML5 canvas boxes b@1608: b@1608: // Create the div box to center align b@1608: var sliderBox = document.createElement('div'); b@1608: sliderBox.className = 'sliderCanvasDiv'; b@1608: sliderBox.id = 'sliderCanvasHolder'; b@1608: b@1608: // Create the slider box to hold the slider elements b@1608: var canvas = document.createElement('div'); b@1608: canvas.id = 'slider'; nickjillings@1633: canvas.align = "left"; nickjillings@1633: canvas.addEventListener('dragover',function(event){ nickjillings@1633: event.preventDefault(); nickjillings@1633: return false; nickjillings@1633: },false); nickjillings@1633: var sliderMargin = document.createAttribute('marginsize'); nickjillings@1633: sliderMargin.nodeValue = 42; // Set default margins to 42px either side b@1608: // Must have a known EXACT width, as this is used later to determine the ratings nickjillings@1633: var w = (Number(sliderMargin.nodeValue)+8)*2; nickjillings@1633: canvas.style.width = width - w +"px"; nickjillings@1633: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nickjillings@1633: canvas.setAttributeNode(sliderMargin); b@1608: sliderBox.appendChild(canvas); b@1608: b@1608: // Create the div to hold any scale objects b@1608: var scale = document.createElement('div'); b@1608: scale.className = 'sliderScale'; b@1608: scale.id = 'sliderScaleHolder'; b@1608: scale.align = 'left'; b@1608: sliderBox.appendChild(scale); b@1608: b@1608: // Global parent for the comment boxes on the page b@1608: var feedbackHolder = document.createElement('div'); b@1608: feedbackHolder.id = 'feedbackHolder'; b@1608: b@1608: testContent.style.zIndex = 1; b@1608: insertPoint.innerHTML = null; // Clear the current schema b@1608: b@1608: currentState = 'preTest'; b@1608: b@1608: // Inject into HTML b@1608: testContent.appendChild(title); // Insert the title b@1608: testContent.appendChild(pagetitle); b@1608: testContent.appendChild(interfaceButtons); b@1608: testContent.appendChild(sliderBox); b@1608: testContent.appendChild(feedbackHolder); b@1608: insertPoint.appendChild(testContent); b@1608: b@1608: // Load the full interface nickjillings@1634: testState.initialise(); nickjillings@1634: testState.advanceState(); b@1608: } b@1608: nickjillings@1634: function loadTest(textXML) b@1608: { nickjillings@1617: nickjillings@1617: // Reset audioEngineContext.Metric globals for new test nickjillings@1620: audioEngineContext.newTestPage(); nickjillings@1617: nickjillings@1634: var id = textXML.id; b@1608: b@1608: var feedbackHolder = document.getElementById('feedbackHolder'); b@1608: var canvas = document.getElementById('slider'); b@1608: feedbackHolder.innerHTML = null; b@1608: canvas.innerHTML = null; b@1608: b@1608: // Setup question title b@1608: var interfaceObj = $(textXML).find('interface'); b@1608: var titleNode = interfaceObj.find('title'); b@1608: if (titleNode[0] != undefined) b@1608: { b@1608: document.getElementById('pageTitle').textContent = titleNode[0].textContent; b@1608: } b@1608: var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); nickjillings@1633: var offset = Number(document.getElementById('slider').attributes['marginsize'].value); b@1608: var scale = document.getElementById('sliderScaleHolder'); b@1608: scale.innerHTML = null; b@1608: interfaceObj.find('scale').each(function(index,scaleObj){ nickjillings@1633: var value = document.createAttribute('value'); b@1608: var position = Number(scaleObj.attributes['position'].value)*0.01; nickjillings@1633: value.nodeValue = position; b@1608: var pixelPosition = (position*positionScale)+offset; b@1608: var scaleDOM = document.createElement('span'); b@1608: scaleDOM.textContent = scaleObj.textContent; b@1608: scale.appendChild(scaleDOM); b@1608: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; nickjillings@1633: scaleDOM.setAttributeNode(value); b@1608: }); b@1608: b@1608: // Extract the hostURL attribute. If not set, create an empty string. b@1608: var hostURL = textXML.attributes['hostURL']; b@1608: if (hostURL == undefined) { b@1608: hostURL = ""; b@1608: } else { b@1608: hostURL = hostURL.value; b@1608: } b@1608: // Extract the sampleRate. If set, convert the string to a Number. b@1608: var hostFs = textXML.attributes['sampleRate']; b@1608: if (hostFs != undefined) { b@1608: hostFs = Number(hostFs.value); b@1608: } b@1608: b@1608: /// CHECK FOR SAMPLE RATE COMPATIBILITY b@1608: if (hostFs != undefined) { b@1608: if (Number(hostFs) != audioContext.sampleRate) { b@1608: var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; b@1608: alert(errStr); b@1608: return; b@1608: } b@1608: } b@1608: b@1608: var commentShow = textXML.attributes['elementComments']; b@1608: if (commentShow != undefined) { b@1608: if (commentShow.value == 'false') {commentShow = false;} b@1608: else {commentShow = true;} b@1608: } else {commentShow = true;} b@1608: b@1608: var loopPlayback = textXML.attributes['loop']; b@1608: if (loopPlayback != undefined) b@1608: { b@1608: loopPlayback = loopPlayback.value; b@1608: if (loopPlayback == 'true') { b@1608: loopPlayback = true; b@1608: } else { b@1608: loopPlayback = false; b@1608: } b@1608: } else { b@1608: loopPlayback = false; b@1608: } b@1608: audioEngineContext.loopPlayback = loopPlayback; b@1608: // Create AudioEngine bindings for playback b@1608: if (loopPlayback) { b@1608: audioEngineContext.selectedTrack = function(id) { b@1608: for (var i=0; i'; b@1608: trackSliderObj.draggable = true; b@1608: trackSliderObj.ondragend = dragEnd; b@1608: trackSliderObj.ondragstart = function() b@1608: { b@1608: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! b@1608: audioEngineContext.metric.sliderMoveStart(id); b@1608: }; b@1608: b@1608: // Onclick, switch playback to that track b@1608: trackSliderObj.onclick = function() { nickjillings@1615: // Start the test on first click, that way timings are more accurate. nickjillings@1615: audioEngineContext.play(); b@1608: // Get the track ID from the object ID b@1608: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! nickjillings@1617: //audioEngineContext.metric.sliderPlayed(id); b@1608: audioEngineContext.selectedTrack(id); b@1608: // Currently playing track red, rest green b@1608: document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; b@1608: for (var i = 0; i<$(currentTrackOrder).length; i++) b@1608: { b@1610: if (i!=index) // Make all other sliders green b@1608: { b@1608: document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)"; b@1608: } b@1608: b@1608: } b@1608: }; b@1608: b@1608: canvas.appendChild(trackSliderObj); b@1610: b@1608: }); b@1608: b@1608: // Append any commentQuestion boxes b@1608: var commentQuestions = $(textXML).find('CommentQuestion'); b@1608: $(commentQuestions).each(function(index,element) { b@1608: // Create document objects to hold the comment boxes b@1608: var trackComment = document.createElement('div'); b@1608: trackComment.className = 'comment-div commentQuestion'; b@1608: trackComment.id = element.attributes['id'].value; b@1608: // Create a string next to each comment asking for a comment b@1608: var trackString = document.createElement('span'); b@1608: trackString.innerHTML = element.textContent; b@1608: // Create the HTML5 comment box 'textarea' b@1608: var trackCommentBox = document.createElement('textarea'); b@1608: trackCommentBox.rows = '4'; b@1608: trackCommentBox.cols = '100'; b@1608: trackCommentBox.name = 'commentQuestion'+index; b@1608: trackCommentBox.className = 'trackComment'; b@1608: var br = document.createElement('br'); b@1608: // Add to the holder. b@1608: trackComment.appendChild(trackString); b@1608: trackComment.appendChild(br); b@1608: trackComment.appendChild(trackCommentBox); b@1608: feedbackHolder.appendChild(trackComment); b@1608: }); b@1608: } b@1608: nickjillings@1625: b@1608: function dragEnd(ev) { b@1608: // Function call when a div has been dropped b@1608: var slider = document.getElementById('slider'); nickjillings@1633: var marginSize = Number(slider.attributes['marginsize'].value); b@1608: var w = slider.style.width; b@1608: w = Number(w.substr(0,w.length-2)); nickjillings@1638: var x = ev.x; nickjillings@1633: if (x >= marginSize && x < w+marginSize) { b@1608: this.style.left = (x)+'px'; b@1608: } else { nickjillings@1633: if (x 1) { nickjillings@1614: for (var i=0; i