nicholas@755: /** nicholas@755: * ape.js nicholas@755: * Create the APE interface nicholas@755: */ nicholas@755: nicholas@755: nicholas@755: // Once this is loaded and parsed, begin execution nicholas@755: loadInterface(); nicholas@755: nicholas@755: function loadInterface() { nicholas@755: nicholas@755: // Get the dimensions of the screen available to the page nicholas@755: var width = window.innerWidth; nicholas@755: var height = window.innerHeight; nicholas@755: nicholas@755: // The injection point into the HTML page nicholas@755: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nicholas@755: var testContent = document.createElement('div'); nicholas@755: nicholas@755: testContent.id = 'testContent'; nicholas@755: nicholas@755: // Bindings for interfaceContext nicholas@755: Interface.prototype.checkAllPlayed = function() nicholas@755: { nicholas@755: hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@755: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@755: { nicholas@755: str = ""; nicholas@755: if (hasBeenPlayed.length > 1) { nicholas@755: for (var i=0; i 1) { nicholas@755: var str = ""; nicholas@755: for (var i=0; i maxRanking) nicholas@755: { nicholas@755: maxRanking = ranking; nicholas@755: } nicholas@755: } nicholas@755: if (minRanking > minScale || maxRanking < maxScale) nicholas@755: { nicholas@755: state = false; nicholas@755: str += 'On axis "'+this.interfaceSliders[i].interfaceObject.title+'" you have not used the full width of the scale. '; nicholas@755: } nicholas@755: } nicholas@755: if (state != true) nicholas@755: { nicholas@755: alert(str); nicholas@755: console.log(str); nicholas@755: } nicholas@755: return state; nicholas@755: }; nicholas@755: nicholas@755: Interface.prototype.objectSelected = null; nicholas@755: Interface.prototype.objectMoved = false; nicholas@755: Interface.prototype.selectObject = function(object) nicholas@755: { nicholas@755: if (this.objectSelected == null) nicholas@755: { nicholas@755: this.objectSelected = object; nicholas@755: this.objectMoved = false; nicholas@755: } nicholas@755: }; nicholas@755: Interface.prototype.moveObject = function() nicholas@755: { nicholas@755: if (this.objectMoved == false) nicholas@755: { nicholas@755: this.objectMoved = true; nicholas@755: } nicholas@755: }; nicholas@755: Interface.prototype.releaseObject = function() nicholas@755: { nicholas@755: this.objectSelected = null; nicholas@755: this.objectMoved = false; nicholas@755: }; nicholas@755: Interface.prototype.getSelectedObject = function() nicholas@755: { nicholas@755: return this.objectSelected; nicholas@755: }; nicholas@755: Interface.prototype.hasSelectedObjectMoved = function() nicholas@755: { nicholas@755: return this.objectMoved; nicholas@755: }; nicholas@755: nicholas@755: // Bindings for slider interfaces nicholas@755: Interface.prototype.interfaceSliders = []; nicholas@755: nicholas@755: // Bindings for audioObjects nicholas@755: nicholas@755: // Create the top div for the Title element nicholas@755: var titleAttr = specification.title; nicholas@755: var title = document.createElement('div'); nicholas@755: title.className = "title"; nicholas@755: title.align = "center"; nicholas@755: var titleSpan = document.createElement('span'); nicholas@755: nicholas@755: // Set title to that defined in XML, else set to default nicholas@755: if (titleAttr != undefined) { nicholas@755: titleSpan.textContent = titleAttr; nicholas@755: } else { nicholas@755: titleSpan.textContent = 'Listening test'; nicholas@755: } nicholas@755: // Insert the titleSpan element into the title div element. nicholas@755: title.appendChild(titleSpan); nicholas@755: nicholas@755: // Create Interface buttons! nicholas@755: var interfaceButtons = document.createElement('div'); nicholas@755: interfaceButtons.id = 'interface-buttons'; nicholas@755: nicholas@755: // Create playback start/stop points nicholas@755: var playback = document.createElement("button"); nicholas@755: playback.innerHTML = 'Stop'; nicholas@755: playback.id = 'playback-button'; nicholas@755: // onclick function. Check if it is playing or not, call the correct function in the nicholas@755: // audioEngine, change the button text to reflect the next state. nicholas@755: playback.onclick = function() { nicholas@755: if (audioEngineContext.status == 1) { nicholas@755: audioEngineContext.stop(); nicholas@755: this.innerHTML = 'Stop'; nicholas@755: var time = audioEngineContext.timer.getTestTime(); nicholas@755: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@755: } nicholas@755: }; nicholas@755: // Create Submit (save) button nicholas@755: var submit = document.createElement("button"); nicholas@755: submit.innerHTML = 'Submit'; nicholas@755: submit.onclick = buttonSubmitClick; nicholas@755: submit.id = 'submit-button'; nicholas@755: // Append the interface buttons into the interfaceButtons object. nicholas@755: interfaceButtons.appendChild(playback); nicholas@755: interfaceButtons.appendChild(submit); nicholas@755: nicholas@755: var sliderHolder = document.createElement("div"); nicholas@755: sliderHolder.id = "slider-holder"; nicholas@755: nicholas@755: nicholas@755: // Global parent for the comment boxes on the page nicholas@755: var feedbackHolder = document.createElement('div'); nicholas@755: feedbackHolder.id = 'feedbackHolder'; nicholas@755: nicholas@755: testContent.style.zIndex = 1; nicholas@755: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nicholas@755: nicholas@755: // Inject into HTML nicholas@755: testContent.appendChild(title); // Insert the title nicholas@755: testContent.appendChild(interfaceButtons); nicholas@755: testContent.appendChild(sliderHolder); nicholas@755: testContent.appendChild(feedbackHolder); nicholas@755: interfaceContext.insertPoint.appendChild(testContent); nicholas@755: nicholas@755: // Load the full interface nicholas@755: testState.initialise(); nicholas@755: testState.advanceState(); nicholas@755: nicholas@755: } nicholas@755: nicholas@755: function loadTest(audioHolderObject) nicholas@755: { nicholas@755: var width = window.innerWidth; nicholas@755: var height = window.innerHeight; nicholas@755: var id = audioHolderObject.id; nicholas@755: nicholas@755: interfaceContext.interfaceSliders = []; nicholas@755: nicholas@755: var feedbackHolder = document.getElementById('feedbackHolder'); nicholas@755: var sliderHolder = document.getElementById('slider-holder'); nicholas@755: feedbackHolder.innerHTML = null; nicholas@755: sliderHolder.innerHTML = null; nicholas@755: nicholas@755: var interfaceObj = audioHolderObject.interfaces; nicholas@755: for (var k=0; k'; nicholas@755: var inject = document.getElementById('interface-buttons'); nicholas@755: inject.appendChild(pagecountHolder); nicholas@755: } nicholas@755: } nicholas@755: } nicholas@755: nicholas@755: var commentBoxPrefix = "Comment on track"; nicholas@755: nicholas@755: var commentShow = audioHolderObject.elementComments; nicholas@755: nicholas@755: var loopPlayback = audioHolderObject.loop; nicholas@755: nicholas@755: currentTestHolder = document.createElement('audioHolder'); nicholas@755: currentTestHolder.id = audioHolderObject.id; nicholas@755: currentTestHolder.repeatCount = audioHolderObject.repeatCount; nicholas@755: nicholas@755: // Find all the audioElements from the audioHolder nicholas@755: $(audioHolderObject.audioElements).each(function(index,element){ nicholas@755: // Find URL of track nicholas@755: // In this jQuery loop, variable 'this' holds the current audioElement. nicholas@755: nicholas@755: // Now load each audio sample. First create the new track by passing the full URL nicholas@755: var trackURL = audioHolderObject.hostURL + element.url; nicholas@755: var audioObject = audioEngineContext.newTrack(element); nicholas@755: nicholas@755: var node = interfaceContext.createCommentBox(audioObject); nicholas@755: nicholas@755: // Create a slider per track nicholas@755: audioObject.interfaceDOM = new sliderObject(audioObject,interfaceObj); nicholas@755: audioObject.metric.initialPosition = convSliderPosToRate(audioObject.interfaceDOM.trackSliderObjects[0]); nicholas@755: if (audioObject.state == 1) nicholas@755: { nicholas@755: audioObject.interfaceDOM.enable(); nicholas@755: } nicholas@755: nicholas@755: }); nicholas@755: nicholas@755: $('.track-slider').mousedown(function(event) { nicholas@755: interfaceContext.selectObject($(this)[0]); nicholas@755: }); nicholas@755: nicholas@755: $('.track-slider').mousemove(function(event) { nicholas@755: event.preventDefault(); nicholas@755: }); nicholas@755: nicholas@755: $('.slider').mousemove(function(event) { nicholas@755: event.preventDefault(); nicholas@755: var obj = interfaceContext.getSelectedObject(); nicholas@755: if (obj == null) {return;} nicholas@755: $(obj).css("left",event.clientX + "px"); nicholas@755: interfaceContext.moveObject(); nicholas@755: }); nicholas@755: nicholas@755: $(document).mouseup(function(event){ nicholas@755: event.preventDefault(); nicholas@755: var obj = interfaceContext.getSelectedObject(); nicholas@755: if (obj == null) {return;} nicholas@755: var interfaceID = obj.parentElement.getAttribute("interfaceid"); nicholas@755: var trackID = obj.getAttribute("trackindex"); nicholas@755: if (interfaceContext.hasSelectedObjectMoved() == true) nicholas@755: { nicholas@755: var l = $(obj).css("left"); nicholas@755: var id = obj.getAttribute('trackIndex'); nicholas@755: var time = audioEngineContext.timer.getTestTime(); nicholas@755: var rate = convSliderPosToRate(obj); nicholas@755: audioEngineContext.audioObjects[id].metric.moved(time,rate); nicholas@755: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate); nicholas@755: console.log("slider "+id+" moved to "+rate+' ('+time+')'); nicholas@755: } else { nicholas@755: var id = Number(obj.attributes['trackIndex'].value); nicholas@755: //audioEngineContext.metric.sliderPlayed(id); nicholas@755: audioEngineContext.play(id); nicholas@755: // Currently playing track red, rest green nicholas@755: nicholas@755: $('.track-slider').removeClass('track-slider-playing'); nicholas@755: var name = ".track-slider-"+obj.getAttribute("trackindex"); nicholas@755: $(name).addClass('track-slider-playing'); nicholas@755: $('.comment-div').removeClass('comment-box-playing'); nicholas@755: $('#comment-div-'+id).addClass('comment-box-playing'); nicholas@755: var outsideReference = document.getElementById('outside-reference'); nicholas@755: if (outsideReference != undefined) nicholas@755: $(outsideReference).removeClass('track-slider-playing'); nicholas@755: } nicholas@755: interfaceContext.releaseObject(); nicholas@755: }); nicholas@755: nicholas@755: nicholas@755: if (commentShow) { nicholas@755: interfaceContext.showCommentBoxes(feedbackHolder,true); nicholas@755: } nicholas@755: nicholas@755: $(audioHolderObject.commentQuestions).each(function(index,element) { nicholas@755: var node = interfaceContext.createCommentQuestion(element); nicholas@755: feedbackHolder.appendChild(node.holder); nicholas@755: }); nicholas@755: nicholas@755: // Construct outside reference; nicholas@755: if (audioHolderObject.outsideReference != null) { nicholas@755: var outsideReferenceHolder = document.createElement('div'); nicholas@755: outsideReferenceHolder.id = 'outside-reference'; nicholas@755: outsideReferenceHolder.className = 'outside-reference'; nicholas@755: outsideReferenceHolderspan = document.createElement('span'); nicholas@755: outsideReferenceHolderspan.textContent = 'Reference'; nicholas@755: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nicholas@755: nicholas@755: var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference); nicholas@755: nicholas@755: outsideReferenceHolder.onclick = function(event) nicholas@755: { nicholas@755: audioEngineContext.play(audioEngineContext.audioObjects.length-1); nicholas@755: $('.track-slider').removeClass('track-slider-playing'); nicholas@755: $('.comment-div').removeClass('comment-box-playing'); nicholas@755: if (event.currentTarget.nodeName == 'DIV') { nicholas@755: $(event.currentTarget).addClass('track-slider-playing'); nicholas@755: } else { nicholas@755: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nicholas@755: } nicholas@755: }; nicholas@755: nicholas@755: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); nicholas@755: } nicholas@755: nicholas@755: nicholas@755: //testWaitIndicator(); nicholas@755: } nicholas@755: nicholas@755: function interfaceSliderHolder(interfaceObject) nicholas@755: { nicholas@755: this.sliders = []; nicholas@755: this.metrics = []; nicholas@755: this.id = document.getElementsByClassName("sliderCanvasDiv").length; nicholas@755: this.name = interfaceObject.name; nicholas@755: this.interfaceObject = interfaceObject; nicholas@755: this.sliderDOM = document.createElement('div'); nicholas@755: this.sliderDOM.className = 'sliderCanvasDiv'; nicholas@755: this.sliderDOM.id = 'sliderCanvasHolder-'+this.id; nicholas@755: nicholas@755: var pagetitle = document.createElement('div'); nicholas@755: pagetitle.className = "pageTitle"; nicholas@755: pagetitle.align = "center"; nicholas@755: var titleSpan = document.createElement('span'); nicholas@755: titleSpan.id = "pageTitle-"+this.id; nicholas@755: if (interfaceObject.title != undefined && typeof interfaceObject.title == "string") nicholas@755: { nicholas@755: titleSpan.textContent = interfaceObject.title; nicholas@755: } else { nicholas@755: titleSpan.textContent = "Axis "+String(this.id+1); nicholas@755: } nicholas@755: pagetitle.appendChild(titleSpan); nicholas@755: this.sliderDOM.appendChild(pagetitle); nicholas@755: nicholas@755: // Create the slider box to hold the slider elements nicholas@755: this.canvas = document.createElement('div'); nicholas@755: if (this.name != undefined) nicholas@755: this.canvas.id = 'slider-'+this.name; nicholas@755: else nicholas@755: this.canvas.id = 'slider-'+this.id; nicholas@755: this.canvas.setAttribute("interfaceid",this.id); nicholas@755: this.canvas.className = 'slider'; nicholas@755: this.canvas.align = "left"; nicholas@755: this.canvas.addEventListener('dragover',function(event){ nicholas@755: event.preventDefault(); nicholas@755: event.dataTransfer.effectAllowed = 'none'; nicholas@755: event.dataTransfer.dropEffect = 'copy'; nicholas@755: return false; nicholas@755: },false); nicholas@755: var sliderMargin = document.createAttribute('marginsize'); nicholas@755: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nicholas@755: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@755: var w = (Number(sliderMargin.nodeValue)+8)*2; nicholas@755: this.canvas.style.width = window.innerWidth - w +"px"; nicholas@755: this.canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nicholas@755: this.canvas.setAttributeNode(sliderMargin); nicholas@755: this.sliderDOM.appendChild(this.canvas); nicholas@755: nicholas@755: // Create the div to hold any scale objects nicholas@755: this.scale = document.createElement('div'); nicholas@755: this.scale.className = 'sliderScale'; nicholas@755: this.scale.id = 'sliderScaleHolder-'+this.id; nicholas@755: this.scale.align = 'left'; nicholas@755: this.sliderDOM.appendChild(this.scale); nicholas@755: var positionScale = this.canvas.style.width.substr(0,this.canvas.style.width.length-2); nicholas@755: var offset = Number(this.canvas.attributes['marginsize'].value); nicholas@755: for (var index=0; index'; nicholas@755: if (this.name != undefined) { nicholas@755: trackObj.setAttribute('interface-name',this.name); nicholas@755: } else { nicholas@755: trackObj.setAttribute('interface-name',this.id); nicholas@755: } nicholas@755: var offset = Number(this.canvas.attributes['marginsize'].value); nicholas@755: // Distribute it randomnly nicholas@755: var w = window.innerWidth - (offset+8)*2; nicholas@755: w = Math.random()*w; nicholas@755: w = Math.floor(w+(offset+8)); nicholas@755: trackObj.style.left = w+'px'; nicholas@755: this.canvas.appendChild(trackObj); nicholas@755: this.sliders.push(trackObj); nicholas@755: this.metrics.push(new metricTracker(this)); nicholas@755: this.metrics[this.metrics.length-1].initialPosition = convSliderPosToRate(trackObj); nicholas@755: return trackObj; nicholas@755: }; nicholas@755: nicholas@755: this.resize = function(event) nicholas@755: { nicholas@755: var holdValues = []; nicholas@755: for (var index = 0; index < this.sliders.length; index++) nicholas@755: { nicholas@755: holdValues.push(convSliderPosToRate(this.sliders[index])); nicholas@755: } nicholas@755: var width = event.target.innerWidth; nicholas@755: var sliderDiv = this.canvas; nicholas@755: var sliderScaleDiv = this.scale; nicholas@755: var marginsize = Number(sliderDiv.attributes['marginsize'].value); nicholas@755: var w = (marginsize+8)*2; nicholas@755: sliderDiv.style.width = width - w + 'px'; nicholas@755: var width = width - w; nicholas@755: // Move sliders into new position nicholas@755: for (var index = 0; index < this.sliders.length; index++) nicholas@755: { nicholas@755: var pos = holdValues[index]; nicholas@755: var pix = pos * width; nicholas@755: this.sliders[index].style.left = pix+marginsize+'px'; nicholas@755: } nicholas@755: nicholas@755: // Move scale labels nicholas@755: for (var index = 0; index < this.scale.children.length; index++) nicholas@755: { nicholas@755: var scaleObj = this.scale.children[index]; nicholas@755: var position = Number(scaleObj.attributes['value'].value); nicholas@755: var pixelPosition = (position*width)+marginsize; nicholas@755: scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px'; nicholas@755: } nicholas@755: }; nicholas@755: } nicholas@755: nicholas@755: function sliderObject(audioObject,interfaceObjects) { nicholas@755: // Create a new slider object; nicholas@755: this.parent = audioObject; nicholas@755: this.trackSliderObjects = []; nicholas@755: for (var i=0; i saves nicholas@755: // Get the current information in store (remember to appendChild your data to it) nicholas@755: if (interfaceContext.interfaceSliders.length == 1) nicholas@755: { nicholas@755: // If there is only one axis, there only needs to be one metric return nicholas@755: return; nicholas@755: } nicholas@755: var audioelements = store.getElementsByTagName("audioelement"); nicholas@755: for (var i=0; i