nicholas@858: /** nicholas@858: * ape.js nicholas@858: * Create the APE interface nicholas@858: */ nicholas@858: nicholas@858: // preTest - In preTest state nicholas@858: // testRun-ID - In test running, test Id number at the end 'testRun-2' nicholas@858: // testRunPost-ID - Post test of test ID nicholas@858: // testRunPre-ID - Pre-test of test ID nicholas@858: // postTest - End of test, final submission! nicholas@858: nicholas@858: nicholas@858: // Once this is loaded and parsed, begin execution nicholas@858: loadInterface(); nicholas@858: nicholas@858: function loadInterface() { nicholas@858: nicholas@858: // Get the dimensions of the screen available to the page nicholas@858: var width = window.innerWidth; nicholas@858: var height = window.innerHeight; nicholas@858: nicholas@858: // The injection point into the HTML page nicholas@858: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nicholas@858: var testContent = document.createElement('div'); nicholas@858: nicholas@858: testContent.id = 'testContent'; nicholas@858: nicholas@858: nicholas@858: // Create APE specific metric functions nicholas@858: audioEngineContext.metric.initialiseTest = function() nicholas@858: { nicholas@858: }; nicholas@858: nicholas@858: audioEngineContext.metric.sliderMoved = function() nicholas@858: { nicholas@858: var id = this.data; nicholas@858: this.data = -1; nicholas@858: var position = convSliderPosToRate(id); nicholas@858: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id nicholas@858: if (audioEngineContext.timer.testStarted) nicholas@858: { nicholas@858: audioEngineContext.audioObjects[id].metric.moved(time,position); nicholas@858: } nicholas@858: }; nicholas@858: nicholas@858: audioEngineContext.metric.sliderPlayed = function(id) nicholas@858: { nicholas@858: var time = audioEngineContext.timer.getTestTime(); nicholas@858: if (audioEngineContext.timer.testStarted) nicholas@858: { nicholas@858: if (this.lastClicked >= 0) nicholas@858: { nicholas@858: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); nicholas@858: } nicholas@858: this.lastClicked = id; nicholas@858: audioEngineContext.audioObjects[id].metric.listening(time); nicholas@858: } nicholas@858: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id nicholas@858: }; nicholas@858: nicholas@858: // Bindings for interfaceContext nicholas@858: Interface.prototype.checkAllPlayed = function() nicholas@858: { nicholas@858: hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@858: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@858: { nicholas@858: str = ""; nicholas@858: if (hasBeenPlayed.length > 1) { nicholas@858: for (var i=0; i 1) { nicholas@858: var str = ""; nicholas@858: for (var i=0; i 1) { nicholas@858: var str = ""; nicholas@858: for (var i=0; i maxRanking) { maxRanking = ranking;} nicholas@858: } nicholas@858: } nicholas@858: if (minRanking > minScale || maxRanking < maxScale) { nicholas@858: alert('Please use the full width of the scale'); nicholas@858: return false; nicholas@858: } else { nicholas@858: return true; nicholas@858: } nicholas@858: }; nicholas@858: nicholas@858: // Bindings for audioObjects nicholas@858: nicholas@858: // Create the top div for the Title element nicholas@858: var titleAttr = specification.title; nicholas@858: var title = document.createElement('div'); nicholas@858: title.className = "title"; nicholas@858: title.align = "center"; nicholas@858: var titleSpan = document.createElement('span'); nicholas@858: nicholas@858: // Set title to that defined in XML, else set to default nicholas@858: if (titleAttr != undefined) { nicholas@858: titleSpan.textContent = titleAttr; nicholas@858: } else { nicholas@858: titleSpan.textContent = 'Listening test'; nicholas@858: } nicholas@858: // Insert the titleSpan element into the title div element. nicholas@858: title.appendChild(titleSpan); nicholas@858: nicholas@858: var pagetitle = document.createElement('div'); nicholas@858: pagetitle.className = "pageTitle"; nicholas@858: pagetitle.align = "center"; nicholas@858: var titleSpan = document.createElement('span'); nicholas@858: titleSpan.id = "pageTitle"; nicholas@858: pagetitle.appendChild(titleSpan); nicholas@858: nicholas@858: // Create Interface buttons! nicholas@858: var interfaceButtons = document.createElement('div'); nicholas@858: interfaceButtons.id = 'interface-buttons'; nicholas@858: nicholas@858: // Create playback start/stop points nicholas@858: var playback = document.createElement("button"); nicholas@858: playback.innerHTML = 'Stop'; nicholas@858: playback.id = 'playback-button'; nicholas@858: // onclick function. Check if it is playing or not, call the correct function in the nicholas@858: // audioEngine, change the button text to reflect the next state. nicholas@858: playback.onclick = function() { nicholas@858: if (audioEngineContext.status == 1) { nicholas@858: audioEngineContext.stop(); nicholas@858: this.innerHTML = 'Stop'; nicholas@858: var time = audioEngineContext.timer.getTestTime(); nicholas@858: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@858: } nicholas@858: }; nicholas@858: // Create Submit (save) button nicholas@858: var submit = document.createElement("button"); nicholas@858: submit.innerHTML = 'Submit'; nicholas@858: submit.onclick = buttonSubmitClick; nicholas@858: submit.id = 'submit-button'; nicholas@858: // Append the interface buttons into the interfaceButtons object. nicholas@858: interfaceButtons.appendChild(playback); nicholas@858: interfaceButtons.appendChild(submit); nicholas@858: nicholas@858: // Now create the slider and HTML5 canvas boxes nicholas@858: nicholas@858: // Create the div box to center align nicholas@858: var sliderBox = document.createElement('div'); nicholas@858: sliderBox.className = 'sliderCanvasDiv'; nicholas@858: sliderBox.id = 'sliderCanvasHolder'; nicholas@858: nicholas@858: // Create the slider box to hold the slider elements nicholas@858: var canvas = document.createElement('div'); nicholas@858: canvas.id = 'slider'; nicholas@858: canvas.align = "left"; nicholas@858: canvas.addEventListener('dragover',function(event){ nicholas@858: event.preventDefault(); nicholas@858: return false; nicholas@858: },false); nicholas@858: var sliderMargin = document.createAttribute('marginsize'); nicholas@858: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nicholas@858: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@858: var w = (Number(sliderMargin.nodeValue)+8)*2; nicholas@858: canvas.style.width = width - w +"px"; nicholas@858: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nicholas@858: canvas.setAttributeNode(sliderMargin); nicholas@858: sliderBox.appendChild(canvas); nicholas@858: nicholas@858: // Create the div to hold any scale objects nicholas@858: var scale = document.createElement('div'); nicholas@858: scale.className = 'sliderScale'; nicholas@858: scale.id = 'sliderScaleHolder'; nicholas@858: scale.align = 'left'; nicholas@858: sliderBox.appendChild(scale); nicholas@858: nicholas@858: // Global parent for the comment boxes on the page nicholas@858: var feedbackHolder = document.createElement('div'); nicholas@858: feedbackHolder.id = 'feedbackHolder'; nicholas@858: nicholas@858: testContent.style.zIndex = 1; nicholas@858: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nicholas@858: nicholas@858: // Inject into HTML nicholas@858: testContent.appendChild(title); // Insert the title nicholas@858: testContent.appendChild(pagetitle); nicholas@858: testContent.appendChild(interfaceButtons); nicholas@858: testContent.appendChild(sliderBox); nicholas@858: testContent.appendChild(feedbackHolder); nicholas@858: interfaceContext.insertPoint.appendChild(testContent); nicholas@858: nicholas@858: // Load the full interface nicholas@858: testState.initialise(); nicholas@858: testState.advanceState(); nicholas@858: nicholas@858: } nicholas@858: nicholas@858: function loadTest(audioHolderObject) nicholas@858: { nicholas@858: nicholas@858: // Reset audioEngineContext.Metric globals for new test nicholas@858: audioEngineContext.newTestPage(); nicholas@858: nicholas@858: var id = audioHolderObject.id; nicholas@858: nicholas@858: var feedbackHolder = document.getElementById('feedbackHolder'); nicholas@858: var canvas = document.getElementById('slider'); nicholas@858: feedbackHolder.innerHTML = null; nicholas@858: canvas.innerHTML = null; nicholas@858: nicholas@858: var interfaceObj = audioHolderObject.interfaces; nicholas@858: for (var k=0; k'; nicholas@858: this.trackSliderObj.draggable = true; nicholas@858: this.trackSliderObj.ondragend = dragEnd; nicholas@858: nicholas@858: // Onclick, switch playback to that track nicholas@858: this.trackSliderObj.onclick = function() { nicholas@858: // Start the test on first click, that way timings are more accurate. nicholas@858: audioEngineContext.play(); nicholas@858: if (audioEngineContext.audioObjectsReady) { nicholas@858: // Cannot continue to issue play command until audioObjects reported as ready! nicholas@858: // Get the track ID from the object ID nicholas@858: var element; nicholas@858: if (event.srcElement.nodeName == "SPAN") { nicholas@858: element = event.srcElement.parentNode; nicholas@858: } else { nicholas@858: element = event.srcElement; nicholas@858: } nicholas@858: var id = Number(element.attributes['trackIndex'].value); nicholas@858: //audioEngineContext.metric.sliderPlayed(id); nicholas@858: audioEngineContext.play(id); nicholas@858: // Currently playing track red, rest green nicholas@858: nicholas@858: //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; nicholas@858: $('.track-slider').removeClass('track-slider-playing'); nicholas@858: $(element).addClass('track-slider-playing'); nicholas@858: $('.comment-div').removeClass('comment-box-playing'); nicholas@858: $('#comment-div-'+id).addClass('comment-box-playing'); nicholas@858: var outsideReference = document.getElementById('outside-reference'); nicholas@858: if (outsideReference != undefined) nicholas@858: $(outsideReference).removeClass('track-slider-playing'); nicholas@858: } nicholas@858: }; nicholas@858: nicholas@858: this.exportXMLDOM = function(audioObject) { nicholas@858: // Called by the audioObject holding this element. Must be present nicholas@858: var node = document.createElement('value'); nicholas@858: node.textContent = convSliderPosToRate(this.trackSliderObj); nicholas@858: return node; nicholas@858: }; nicholas@858: this.getValue = function() { nicholas@858: return convSliderPosToRate(this.trackSliderObj); nicholas@858: }; nicholas@858: } nicholas@858: nicholas@858: function dragEnd(ev) { nicholas@858: // Function call when a div has been dropped nicholas@858: var slider = document.getElementById('slider'); nicholas@858: var marginSize = Number(slider.attributes['marginsize'].value); nicholas@858: var w = slider.style.width; nicholas@858: w = Number(w.substr(0,w.length-2)); nicholas@858: var x = ev.x; nicholas@858: if (x >= marginSize && x < w+marginSize) { nicholas@858: this.style.left = (x)+'px'; nicholas@858: } else { nicholas@858: if (x