nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: n@32: nicholas@2: // Once this is loaded and parsed, begin execution n@181: loadInterface(); nicholas@2: n@358: var clicking = -1; n@358: n@181: function loadInterface() { nicholas@2: n@16: // Get the dimensions of the screen available to the page nicholas@2: var width = window.innerWidth; nicholas@2: var height = window.innerHeight; nicholas@2: nicholas@2: // The injection point into the HTML page n@182: interfaceContext.insertPoint = document.getElementById("topLevelBody"); n@22: var testContent = document.createElement('div'); nicholas@135: n@22: testContent.id = 'testContent'; n@176: n@34: n@52: // Create APE specific metric functions n@52: audioEngineContext.metric.initialiseTest = function() n@52: { n@52: }; n@52: n@52: audioEngineContext.metric.sliderMoved = function() n@52: { n@52: var id = this.data; n@52: this.data = -1; n@52: var position = convSliderPosToRate(id); b@115: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id n@52: if (audioEngineContext.timer.testStarted) n@52: { n@52: audioEngineContext.audioObjects[id].metric.moved(time,position); n@52: } n@52: }; n@52: n@52: audioEngineContext.metric.sliderPlayed = function(id) n@52: { n@52: var time = audioEngineContext.timer.getTestTime(); n@52: if (audioEngineContext.timer.testStarted) n@52: { n@52: if (this.lastClicked >= 0) n@52: { n@52: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); n@52: } n@52: this.lastClicked = id; n@52: audioEngineContext.audioObjects[id].metric.listening(time); n@52: } b@115: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id n@52: }; n@52: nicholas@214: // Bindings for interfaceContext nicholas@214: Interface.prototype.checkAllPlayed = function() nicholas@214: { nicholas@214: hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@214: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@214: { nicholas@214: str = ""; nicholas@214: if (hasBeenPlayed.length > 1) { nicholas@214: for (var i=0; i 1) { nicholas@214: var str = ""; nicholas@214: for (var i=0; i 1) { nicholas@214: var str = ""; nicholas@214: for (var i=0; i maxRanking) { maxRanking = ranking;} n@383: } n@383: } n@383: if (minRanking > minScale || maxRanking < maxScale) { n@383: alert('Please use the full width of the scale'); n@383: return false; nicholas@231: } nicholas@231: } n@383: return true; nicholas@231: }; nicholas@231: n@360: Interface.prototype.objectSelected = null; n@360: Interface.prototype.objectMoved = false; n@360: Interface.prototype.selectObject = function(object) n@360: { n@360: if (this.objectSelected == null) n@360: { n@360: this.objectSelected = object; n@360: this.objectMoved = false; n@360: } n@360: }; n@360: Interface.prototype.moveObject = function() n@360: { n@360: if (this.objectMoved == false) n@360: { n@360: this.objectMoved = true; n@360: } n@360: }; n@360: Interface.prototype.releaseObject = function() n@360: { n@360: this.objectSelected = null; n@360: this.objectMoved = false; n@360: }; n@360: Interface.prototype.getSelectedObject = function() n@360: { n@360: return this.objectSelected; n@360: }; n@360: Interface.prototype.hasSelectedObjectMoved = function() n@360: { n@360: return this.objectMoved; n@360: }; n@360: n@177: // Bindings for audioObjects n@177: nicholas@2: // Create the top div for the Title element n@181: var titleAttr = specification.title; nicholas@2: var title = document.createElement('div'); nicholas@2: title.className = "title"; nicholas@2: title.align = "center"; nicholas@2: var titleSpan = document.createElement('span'); nicholas@2: nicholas@2: // Set title to that defined in XML, else set to default nicholas@2: if (titleAttr != undefined) { n@181: titleSpan.textContent = titleAttr; nicholas@2: } else { n@181: titleSpan.textContent = 'Listening test'; nicholas@2: } nicholas@2: // Insert the titleSpan element into the title div element. nicholas@2: title.appendChild(titleSpan); nicholas@2: nicholas@7: // Create Interface buttons! nicholas@7: var interfaceButtons = document.createElement('div'); nicholas@7: interfaceButtons.id = 'interface-buttons'; nicholas@7: nicholas@7: // Create playback start/stop points nicholas@7: var playback = document.createElement("button"); b@101: playback.innerHTML = 'Stop'; n@49: playback.id = 'playback-button'; n@16: // onclick function. Check if it is playing or not, call the correct function in the n@16: // audioEngine, change the button text to reflect the next state. nicholas@7: playback.onclick = function() { b@101: if (audioEngineContext.status == 1) { b@101: audioEngineContext.stop(); n@25: this.innerHTML = 'Stop'; b@115: var time = audioEngineContext.timer.getTestTime(); b@115: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@7: } n@16: }; nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); n@25: submit.innerHTML = 'Submit'; n@43: submit.onclick = buttonSubmitClick; n@49: submit.id = 'submit-button'; n@16: // Append the interface buttons into the interfaceButtons object. nicholas@7: interfaceButtons.appendChild(playback); nicholas@7: interfaceButtons.appendChild(submit); nicholas@7: n@383: var sliderHolder = document.createElement("div"); n@383: sliderHolder.id = "slider-holder"; nicholas@2: n@47: n@16: // Global parent for the comment boxes on the page nicholas@3: var feedbackHolder = document.createElement('div'); n@34: feedbackHolder.id = 'feedbackHolder'; nicholas@2: n@38: testContent.style.zIndex = 1; n@182: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema n@38: n@38: // Inject into HTML n@38: testContent.appendChild(title); // Insert the title n@38: testContent.appendChild(interfaceButtons); n@383: testContent.appendChild(sliderHolder); n@38: testContent.appendChild(feedbackHolder); n@182: interfaceContext.insertPoint.appendChild(testContent); n@22: n@36: // Load the full interface nicholas@129: testState.initialise(); nicholas@129: testState.advanceState(); nicholas@135: nicholas@2: } nicholas@5: n@181: function loadTest(audioHolderObject) n@34: { n@383: var width = window.innerWidth; n@383: var height = window.innerHeight; n@181: var id = audioHolderObject.id; n@34: n@34: var feedbackHolder = document.getElementById('feedbackHolder'); n@383: var sliderHolder = document.getElementById('slider-holder'); n@34: feedbackHolder.innerHTML = null; n@383: sliderHolder.innerHTML = null; n@47: nicholas@256: var interfaceObj = audioHolderObject.interfaces; nicholas@256: for (var k=0; k'; nicholas@274: var inject = document.getElementById('interface-buttons'); nicholas@274: inject.appendChild(pagecountHolder); nicholas@256: } nicholas@256: } nicholas@256: } nicholas@256: n@184: var commentBoxPrefix = "Comment on track"; n@45: n@181: var commentShow = audioHolderObject.elementComments; nicholas@66: n@181: var loopPlayback = audioHolderObject.loop; n@181: n@46: currentTestHolder = document.createElement('audioHolder'); n@181: currentTestHolder.id = audioHolderObject.id; n@181: currentTestHolder.repeatCount = audioHolderObject.repeatCount; n@46: n@45: // Find all the audioElements from the audioHolder n@181: $(audioHolderObject.audioElements).each(function(index,element){ n@34: // Find URL of track n@34: // In this jQuery loop, variable 'this' holds the current audioElement. n@34: n@34: // Now load each audio sample. First create the new track by passing the full URL n@181: var trackURL = audioHolderObject.hostURL + element.url; n@182: var audioObject = audioEngineContext.newTrack(element); nicholas@66: n@205: var node = interfaceContext.createCommentBox(audioObject); n@34: n@34: // Create a slider per track n@383: audioObject.interfaceDOM = new sliderObject(audioObject,interfaceObj); n@379: if (audioObject.state == 1) n@379: { n@379: audioObject.interfaceDOM.enable(); n@379: } b@102: n@34: }); n@358: n@359: $('.track-slider').mousedown(function(event) { n@360: interfaceContext.selectObject($(this)[0]); n@358: }); n@358: n@359: $('.track-slider').mousemove(function(event) { n@359: event.preventDefault(); n@359: }); n@359: n@383: $('.slider').mousemove(function(event) { n@358: event.preventDefault(); n@360: var obj = interfaceContext.getSelectedObject(); n@360: if (obj == null) {return;} n@360: $(obj).css("left",event.clientX + "px"); n@360: interfaceContext.moveObject(); n@360: }); n@360: n@360: $(document).mouseup(function(event){ n@360: event.preventDefault(); n@360: var obj = interfaceContext.getSelectedObject(); n@360: if (obj == null) {return;} n@360: if (interfaceContext.hasSelectedObjectMoved() == true) n@358: { n@360: var l = $(obj).css("left"); n@360: var id = obj.getAttribute('trackIndex'); n@360: var time = audioEngineContext.timer.getTestTime(); n@360: var rate = convSliderPosToRate(obj); n@360: audioEngineContext.audioObjects[id].metric.moved(time,rate); n@360: console.log("slider "+id+" moved to "+rate+' ('+time+')'); n@360: } else { n@360: var id = Number(obj.attributes['trackIndex'].value); n@360: //audioEngineContext.metric.sliderPlayed(id); n@360: audioEngineContext.play(id); n@360: // Currently playing track red, rest green n@360: n@383: n@360: $('.track-slider').removeClass('track-slider-playing'); n@383: var name = ".track-slider-"+obj.getAttribute("trackindex"); n@383: $(name).addClass('track-slider-playing'); n@360: $('.comment-div').removeClass('comment-box-playing'); n@360: $('#comment-div-'+id).addClass('comment-box-playing'); n@360: var outsideReference = document.getElementById('outside-reference'); n@360: if (outsideReference != undefined) n@360: $(outsideReference).removeClass('track-slider-playing'); n@358: } n@360: interfaceContext.releaseObject(); n@358: }); n@358: n@358: n@182: if (commentShow) { n@182: interfaceContext.showCommentBoxes(feedbackHolder,true); n@182: } n@39: n@181: $(audioHolderObject.commentQuestions).each(function(index,element) { n@193: var node = interfaceContext.createCommentQuestion(element); n@193: feedbackHolder.appendChild(node.holder); nicholas@65: }); n@153: nicholas@236: // Construct outside reference; nicholas@236: if (audioHolderObject.outsideReference != null) { nicholas@236: var outsideReferenceHolder = document.createElement('div'); nicholas@236: outsideReferenceHolder.id = 'outside-reference'; nicholas@236: outsideReferenceHolderspan = document.createElement('span'); nicholas@236: outsideReferenceHolderspan.textContent = 'Reference'; nicholas@236: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nicholas@236: nicholas@236: var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference); nicholas@236: n@299: outsideReferenceHolder.onclick = function(event) nicholas@236: { nicholas@236: audioEngineContext.play(audioEngineContext.audioObjects.length-1); nicholas@236: $('.track-slider').removeClass('track-slider-playing'); nicholas@236: $('.comment-div').removeClass('comment-box-playing'); n@299: if (event.currentTarget.nodeName == 'DIV') { n@299: $(event.currentTarget).addClass('track-slider-playing'); nicholas@236: } else { n@299: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nicholas@236: } nicholas@236: }; nicholas@236: nicholas@236: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); nicholas@236: } nicholas@236: n@153: nicholas@271: //testWaitIndicator(); n@39: } n@39: n@383: function sliderObject(audioObject,interfaceObjects) { n@183: // Create a new slider object; n@183: this.parent = audioObject; n@383: this.trackSliderObjects = []; n@383: for (var i=0; i'; n@383: if (interfaceObjects[i].name != undefined) { n@383: trackObj.setAttribute('interface-name',interfaceObjects[i].name); n@383: } else { n@383: trackObj.setAttribute('interface-name',i); n@383: } n@383: this.trackSliderObjects.push(trackObj); n@383: var slider = document.getElementById("slider-"+trackObj.getAttribute("interface-name")); n@383: var offset = Number(slider.attributes['marginsize'].value); n@383: // Distribute it randomnly n@383: var w = window.innerWidth - (offset+8)*2; n@383: w = Math.random()*w; n@383: w = Math.floor(w+(offset+8)); n@383: trackObj.style.left = w+'px'; n@383: slider.appendChild(trackObj); n@383: } n@183: n@183: // Onclick, switch playback to that track n@183: nicholas@271: this.enable = function() { nicholas@271: if (this.parent.state == 1) nicholas@271: { n@383: $(this.trackSliderObjects).each(function(i,trackObj){ n@383: $(trackObj).removeClass('track-slider-disabled'); n@383: }); nicholas@271: } nicholas@271: }; nicholas@271: nicholas@219: this.exportXMLDOM = function(audioObject) { n@183: // Called by the audioObject holding this element. Must be present n@383: var obj = []; n@383: $(this.trackSliderObjects).each(function(i,trackObj){ n@383: var node = document.createElement('value'); n@383: node.setAttribute("name",trackObj.getAttribute("interface-name")); n@383: node.textContent = convSliderPosToRate(trackObj); n@383: obj.push(node); n@383: }); n@383: n@383: return obj; n@183: }; nicholas@219: this.getValue = function() { nicholas@219: return convSliderPosToRate(this.trackSliderObj); nicholas@220: }; n@183: } nicholas@119: n@296: function buttonSubmitClick() n@40: { nicholas@232: var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options; nicholas@214: var canContinue = true; nicholas@219: nicholas@219: // Check that the anchor and reference objects are correctly placed nicholas@235: if (interfaceContext.checkHiddenAnchor() == false) {return;} nicholas@235: if (interfaceContext.checkHiddenReference() == false) {return;} nicholas@219: nicholas@214: for (var i=0; i