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