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 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: 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: 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;} 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@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: 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@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); nicholas@391: for (var index=0; index'; 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)); nicholas@392: this.metrics[this.metrics.length-1].initialPosition = 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: } nicholas@391: } 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 nicholas@392: // Get the current information in store (remember to appendChild your data to it) nicholas@392: var audioelements = store.getElementsByTagName("audioelement"); nicholas@392: for (var i=0; i