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@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@52: nicholas@214: // Bindings for interfaceContext n@428: interfaceContext.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 maxRanking) n@399: { n@399: maxRanking = ranking; n@383: } n@383: } n@399: if (minRanking > minScale || maxRanking < maxScale) n@399: { n@399: state = false; n@399: str += 'On axis "'+this.interfaceSliders[i].interfaceObject.title+'" you have not used the full width of the scale. '; nicholas@231: } nicholas@231: } n@399: if (state != true) n@399: { n@399: alert(str); n@399: console.log(str); n@399: } n@399: return state; 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: nicholas@391: // Bindings for slider interfaces nicholas@391: Interface.prototype.interfaceSliders = []; nicholas@391: 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: nicholas@391: interfaceContext.interfaceSliders = []; nicholas@391: 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: n@435: // Delete outside reference n@435: var outsideReferenceHolder = document.getElementById('outside-reference'); n@435: if (outsideReferenceHolder != null) { n@435: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); n@435: } n@435: 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: b@439: var commentBoxPrefix = "Comment on fragment"; 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@427: // Check if an outside reference n@453: if (element.type == 'outside-reference') n@427: { n@453: // Construct outside reference; n@453: var outsideReferenceHolder = document.createElement('div'); n@453: outsideReferenceHolder.id = 'outside-reference'; n@453: outsideReferenceHolder.className = 'outside-reference'; n@453: outsideReferenceHolderspan = document.createElement('span'); n@453: outsideReferenceHolderspan.textContent = 'Reference'; n@453: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); n@453: n@453: var audioObject = audioEngineContext.newTrack(element); n@453: n@453: outsideReferenceHolder.onclick = function(event) n@453: { n@453: audioEngineContext.play(audioEngineContext.audioObjects.length-1); n@453: $('.track-slider').removeClass('track-slider-playing'); n@453: $('.comment-div').removeClass('comment-box-playing'); n@453: if (event.currentTarget.nodeName == 'DIV') { n@453: $(event.currentTarget).addClass('track-slider-playing'); n@453: } else { n@453: $(event.currentTarget.parentElement).addClass('track-slider-playing'); n@453: } n@453: }; n@453: n@453: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); n@427: return; n@427: } n@427: 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: // Create a slider per track n@454: var sliderNode = new sliderObject(audioObject,interfaceObj); n@454: audioObject.bindInterface(sliderNode); n@34: }); n@454: n@454: // Initialse the interfaceSlider object metrics n@454: n@359: $('.track-slider').mousedown(function(event) { n@360: interfaceContext.selectObject($(this)[0]); n@358: }); n@441: $('.track-slider').on('touchstart',null,function(event) { n@441: interfaceContext.selectObject($(this)[0]); n@441: }); 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@441: n@441: $('.slider').on('touchmove',null,function(event) { n@441: event.preventDefault(); n@441: var obj = interfaceContext.getSelectedObject(); n@441: if (obj == null) {return;} n@441: var move = event.originalEvent.targetTouches[0].clientX - 6; n@441: $(obj).css("left",move + "px"); n@441: interfaceContext.moveObject(); n@441: }); n@360: n@360: $(document).mouseup(function(event){ n@360: event.preventDefault(); n@360: var obj = interfaceContext.getSelectedObject(); n@360: if (obj == null) {return;} nicholas@392: var interfaceID = obj.parentElement.getAttribute("interfaceid"); nicholas@392: var trackID = obj.getAttribute("trackindex"); 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); nicholas@392: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].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@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@441: $('.slider').on('touchend',null,function(event){ n@441: var obj = interfaceContext.getSelectedObject(); n@441: if (obj == null) {return;} n@441: var interfaceID = obj.parentElement.getAttribute("interfaceid"); n@441: var trackID = obj.getAttribute("trackindex"); n@441: if (interfaceContext.hasSelectedObjectMoved() == true) n@441: { n@441: var l = $(obj).css("left"); n@441: var id = obj.getAttribute('trackIndex'); n@441: var time = audioEngineContext.timer.getTestTime(); n@441: var rate = convSliderPosToRate(obj); n@441: audioEngineContext.audioObjects[id].metric.moved(time,rate); n@441: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate); n@441: console.log("slider "+id+" moved to "+rate+' ('+time+')'); n@441: } n@441: interfaceContext.releaseObject(); n@441: }); n@441: n@358: n@453: if (audioHolderObject.showElementComments) { 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: n@153: nicholas@271: //testWaitIndicator(); n@39: } n@39: nicholas@391: function interfaceSliderHolder(interfaceObject) nicholas@391: { nicholas@391: this.sliders = []; nicholas@392: this.metrics = []; nicholas@391: this.id = document.getElementsByClassName("sliderCanvasDiv").length; nicholas@391: this.name = interfaceObject.name; nicholas@391: this.interfaceObject = interfaceObject; nicholas@391: this.sliderDOM = document.createElement('div'); nicholas@391: this.sliderDOM.className = 'sliderCanvasDiv'; nicholas@391: this.sliderDOM.id = 'sliderCanvasHolder-'+this.id; nicholas@391: nicholas@391: var pagetitle = document.createElement('div'); nicholas@391: pagetitle.className = "pageTitle"; nicholas@391: pagetitle.align = "center"; nicholas@391: var titleSpan = document.createElement('span'); nicholas@391: titleSpan.id = "pageTitle-"+this.id; nicholas@391: if (interfaceObject.title != undefined && typeof interfaceObject.title == "string") nicholas@391: { nicholas@391: titleSpan.textContent = interfaceObject.title; nicholas@414: } else { nicholas@414: titleSpan.textContent = "Axis "+String(this.id+1); nicholas@391: } nicholas@391: pagetitle.appendChild(titleSpan); nicholas@391: this.sliderDOM.appendChild(pagetitle); nicholas@391: nicholas@391: // Create the slider box to hold the slider elements nicholas@391: this.canvas = document.createElement('div'); nicholas@391: if (this.name != undefined) nicholas@391: this.canvas.id = 'slider-'+this.name; nicholas@391: else nicholas@391: this.canvas.id = 'slider-'+this.id; nicholas@392: this.canvas.setAttribute("interfaceid",this.id); nicholas@391: this.canvas.className = 'slider'; nicholas@391: this.canvas.align = "left"; nicholas@391: this.canvas.addEventListener('dragover',function(event){ nicholas@391: event.preventDefault(); nicholas@391: event.dataTransfer.effectAllowed = 'none'; nicholas@391: event.dataTransfer.dropEffect = 'copy'; nicholas@391: return false; nicholas@391: },false); nicholas@391: var sliderMargin = document.createAttribute('marginsize'); nicholas@391: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nicholas@391: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@391: var w = (Number(sliderMargin.nodeValue)+8)*2; nicholas@391: this.canvas.style.width = window.innerWidth - w +"px"; nicholas@391: this.canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nicholas@391: this.canvas.setAttributeNode(sliderMargin); nicholas@391: this.sliderDOM.appendChild(this.canvas); nicholas@391: nicholas@391: // Create the div to hold any scale objects nicholas@391: this.scale = document.createElement('div'); nicholas@391: this.scale.className = 'sliderScale'; nicholas@391: this.scale.id = 'sliderScaleHolder-'+this.id; nicholas@391: this.scale.align = 'left'; nicholas@391: this.sliderDOM.appendChild(this.scale); nicholas@391: var positionScale = this.canvas.style.width.substr(0,this.canvas.style.width.length-2); nicholas@391: var offset = Number(this.canvas.attributes['marginsize'].value); n@453: for (var scaleObj of interfaceObject.scales) nicholas@391: { nicholas@391: var value = document.createAttribute('value'); n@453: var position = Number(scaleObj.position)*0.01; nicholas@391: value.nodeValue = position; nicholas@391: var pixelPosition = (position*positionScale)+offset; nicholas@391: var scaleDOM = document.createElement('span'); n@453: scaleDOM.textContent = scaleObj.text; nicholas@391: this.scale.appendChild(scaleDOM); nicholas@391: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; nicholas@391: scaleDOM.setAttributeNode(value); nicholas@391: } nicholas@391: nicholas@391: var dest = document.getElementById("slider-holder"); nicholas@391: dest.appendChild(this.sliderDOM); nicholas@391: nicholas@391: this.createSliderObject = function(audioObject) n@383: { n@383: var trackObj = document.createElement('div'); n@383: trackObj.className = 'track-slider track-slider-disabled track-slider-'+audioObject.id; nicholas@391: trackObj.id = 'track-slider-'+this.id+'-'+audioObject.id; n@383: trackObj.setAttribute('trackIndex',audioObject.id); n@383: trackObj.innerHTML = ''+audioObject.id+''; nicholas@391: if (this.name != undefined) { nicholas@391: trackObj.setAttribute('interface-name',this.name); n@383: } else { nicholas@391: trackObj.setAttribute('interface-name',this.id); n@383: } nicholas@391: var offset = Number(this.canvas.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'; nicholas@391: this.canvas.appendChild(trackObj); nicholas@391: this.sliders.push(trackObj); nicholas@392: this.metrics.push(new metricTracker(this)); n@454: this.metrics[this.metrics.length-1].initialise(convSliderPosToRate(trackObj)); nicholas@391: return trackObj; nicholas@391: }; nicholas@391: nicholas@391: this.resize = function(event) nicholas@391: { nicholas@391: var holdValues = []; nicholas@391: for (var index = 0; index < this.sliders.length; index++) nicholas@391: { nicholas@391: holdValues.push(convSliderPosToRate(this.sliders[index])); nicholas@391: } nicholas@391: var width = event.target.innerWidth; nicholas@391: var sliderDiv = this.canvas; nicholas@391: var sliderScaleDiv = this.scale; nicholas@391: var marginsize = Number(sliderDiv.attributes['marginsize'].value); nicholas@391: var w = (marginsize+8)*2; nicholas@391: sliderDiv.style.width = width - w + 'px'; nicholas@391: var width = width - w; nicholas@391: // Move sliders into new position nicholas@391: for (var index = 0; index < this.sliders.length; index++) nicholas@391: { nicholas@391: var pos = holdValues[index]; nicholas@391: var pix = pos * width; nicholas@391: this.sliders[index].style.left = pix+marginsize+'px'; nicholas@391: } nicholas@391: nicholas@391: // Move scale labels nicholas@391: for (var index = 0; index < this.scale.children.length; index++) nicholas@391: { nicholas@391: var scaleObj = this.scale.children[index]; nicholas@391: var position = Number(scaleObj.attributes['value'].value); nicholas@391: var pixelPosition = (position*width)+marginsize; nicholas@391: scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px'; nicholas@391: } n@394: }; nicholas@391: } nicholas@391: nicholas@391: function sliderObject(audioObject,interfaceObjects) { nicholas@391: // Create a new slider object; nicholas@391: this.parent = audioObject; nicholas@391: this.trackSliderObjects = []; nicholas@391: for (var i=0; i saves n@453: // Get the current information in store (remember to appendChild your data to it) n@453: // pageSpecification is the current page node configuration n@453: // To create new XML nodes, use storage.document.createElement(); n@453: n@396: if (interfaceContext.interfaceSliders.length == 1) n@396: { n@396: // If there is only one axis, there only needs to be one metric return n@396: return; n@396: } nicholas@392: var audioelements = store.getElementsByTagName("audioelement"); nicholas@392: for (var i=0; i