n@1143: // Once this is loaded and parsed, begin execution n@1143: loadInterface(); n@1143: n@1143: function loadInterface() { n@1143: // Use this to do any one-time page / element construction. For instance, placing any stationary text objects, n@1143: // holding div's, or setting up any nodes which are present for the entire test sequence n@1143: n@1143: // The injection point into the HTML page n@1143: interfaceContext.insertPoint = document.getElementById("topLevelBody"); n@1143: var testContent = document.createElement('div'); n@1143: testContent.id = 'testContent'; n@1143: n@1143: // Create the top div for the Title element n@1143: var titleAttr = specification.title; n@1143: var title = document.createElement('div'); n@1143: title.className = "title"; n@1143: title.align = "center"; n@1143: var titleSpan = document.createElement('span'); n@1143: n@1143: // Set title to that defined in XML, else set to default n@1143: if (titleAttr != undefined) { n@1143: titleSpan.textContent = titleAttr; n@1143: } else { n@1143: titleSpan.textContent = 'Listening test'; n@1143: } n@1143: // Insert the titleSpan element into the title div element. n@1143: title.appendChild(titleSpan); n@1143: n@1143: var pagetitle = document.createElement('div'); n@1143: pagetitle.className = "pageTitle"; n@1143: pagetitle.align = "center"; n@1143: var titleSpan = document.createElement('span'); n@1143: titleSpan.id = "pageTitle"; n@1143: pagetitle.appendChild(titleSpan); n@1143: n@1143: // Create Interface buttons! n@1143: var interfaceButtons = document.createElement('div'); n@1143: interfaceButtons.id = 'interface-buttons'; n@1143: interfaceButtons.style.height = '25px'; n@1143: n@1143: // Create playback start/stop points n@1143: var playback = document.createElement("button"); n@1143: playback.innerHTML = 'Stop'; n@1143: playback.id = 'playback-button'; n@1143: playback.style.float = 'left'; n@1143: // onclick function. Check if it is playing or not, call the correct function in the n@1143: // audioEngine, change the button text to reflect the next state. n@1143: playback.onclick = function() { n@1143: if (audioEngineContext.status == 1) { n@1143: audioEngineContext.stop(); n@1143: this.innerHTML = 'Stop'; n@1143: var time = audioEngineContext.timer.getTestTime(); n@1143: console.log('Stopped at ' + time); // DEBUG/SAFETY n@1143: } n@1143: }; n@1143: // Create Submit (save) button n@1143: var submit = document.createElement("button"); n@1143: submit.innerHTML = 'Submit'; n@1143: submit.onclick = buttonSubmitClick; n@1143: submit.id = 'submit-button'; n@1143: submit.style.float = 'left'; n@1143: // Append the interface buttons into the interfaceButtons object. n@1143: interfaceButtons.appendChild(playback); n@1143: interfaceButtons.appendChild(submit); n@1143: n@1143: // Create a slider box n@1143: var sliderBox = document.createElement('div'); n@1143: sliderBox.style.width = "100%"; n@1143: sliderBox.style.height = window.innerHeight - 200+12 + 'px'; n@1143: sliderBox.style.marginBottom = '10px'; n@1143: sliderBox.id = 'slider'; n@1143: var scaleHolder = document.createElement('div'); n@1143: scaleHolder.id = "scale-holder"; n@1143: scaleHolder.style.marginLeft = "107px"; n@1143: sliderBox.appendChild(scaleHolder); n@1143: var scaleText = document.createElement('div'); n@1143: scaleText.id = "scale-text-holder"; n@1143: scaleText.style.height = "25px"; n@1143: scaleText.style.width = "100%"; n@1143: scaleHolder.appendChild(scaleText); n@1143: var scaleCanvas = document.createElement('canvas'); n@1143: scaleCanvas.id = "scale-canvas"; n@1143: scaleCanvas.style.marginLeft = "100px"; n@1143: scaleHolder.appendChild(scaleCanvas); n@1143: var sliderObjectHolder = document.createElement('div'); n@1143: sliderObjectHolder.id = 'slider-holder'; n@1143: sliderObjectHolder.align = "center"; n@1143: sliderBox.appendChild(sliderObjectHolder); n@1143: n@1143: // Global parent for the comment boxes on the page n@1143: var feedbackHolder = document.createElement('div'); n@1143: feedbackHolder.id = 'feedbackHolder'; n@1143: n@1143: testContent.style.zIndex = 1; n@1143: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema n@1143: n@1143: // Inject into HTML n@1143: testContent.appendChild(title); // Insert the title n@1143: testContent.appendChild(pagetitle); n@1143: testContent.appendChild(interfaceButtons); n@1143: testContent.appendChild(sliderBox); n@1143: testContent.appendChild(feedbackHolder); n@1143: interfaceContext.insertPoint.appendChild(testContent); n@1143: n@1143: // Load the full interface n@1143: testState.initialise(); n@1143: testState.advanceState(); n@1143: }; n@1143: n@1143: function loadTest(page) n@1143: { n@1143: // Called each time a new test page is to be build. The page specification node is the only item passed in n@1143: var id = page.id; n@1143: n@1143: var feedbackHolder = document.getElementById('feedbackHolder'); n@1143: var interfaceObj = page.interfaces; n@1143: if (interfaceObj.length > 1) n@1143: { n@1143: console.log("WARNING - This interface only supports one node per page. Using first interface node"); n@1143: } n@1143: interfaceObj = interfaceObj[0]; n@1143: if(interfaceObj.title != null) n@1143: { n@1143: document.getElementById("pageTitle").textContent = interfaceObj.title; n@1143: } n@1143: n@1143: // Delete outside reference n@1143: var outsideReferenceHolder = document.getElementById('outside-reference'); n@1143: if (outsideReferenceHolder != null) { n@1143: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); n@1143: } n@1143: n@1143: var sliderBox = document.getElementById('slider-holder'); n@1143: feedbackHolder.innerHTML = null; n@1143: sliderBox.innerHTML = null; n@1143: n@1143: var commentBoxPrefix = "Comment on track"; n@1143: if (interfaceObj.commentBoxPrefix != undefined) { n@1143: commentBoxPrefix = interfaceObj.commentBoxPrefix; n@1143: } n@1143: var loopPlayback = page.loop; n@1143: n@1143: $(page.commentQuestions).each(function(index,element) { n@1143: var node = interfaceContext.createCommentQuestion(element); n@1143: feedbackHolder.appendChild(node.holder); n@1143: }); n@1143: n@1143: // Find all the audioElements from the audioHolder n@1143: var label = 0; n@1143: $(page.audioElements).each(function(index,element){ n@1143: // Find URL of track n@1143: // In this jQuery loop, variable 'this' holds the current audioElement. n@1143: n@1143: var audioObject = audioEngineContext.newTrack(element); n@1143: if (element.type == 'outside-reference') n@1143: { n@1143: // Construct outside reference; n@1143: var orNode = new outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons')); n@1143: audioObject.bindInterface(orNode); n@1143: } else { n@1143: var node = interfaceContext.createCommentBox(audioObject); n@1143: n@1143: // Create a slider per track n@1143: var sliderObj = new sliderObject(audioObject,label); n@1143: n@1143: if (typeof page.initialPosition === "number") n@1143: { n@1143: // Set the values n@1143: sliderObj.slider.value = page.initalPosition; n@1143: } else { n@1143: // Distribute it randomnly n@1143: sliderObj.slider.value = Math.random(); n@1143: } n@1143: sliderBox.appendChild(sliderObj.holder); n@1143: audioObject.bindInterface(sliderObj); n@1143: label += 1; n@1143: } n@1143: n@1143: }); n@1143: n@1143: // Auto-align n@1143: resizeWindow(null); n@1143: } n@1143: n@1143: function sliderObject(audioObject,label) n@1143: { n@1143: // An example node, you can make this however you want for each audioElement. n@1143: // However, every audioObject (audioEngineContext.audioObject) MUST have an interface object with the following n@1143: // You attach them by calling audioObject.bindInterface( ) n@1143: this.parent = audioObject; n@1143: n@1143: this.holder = document.createElement('div'); n@1143: this.title = document.createElement('div'); n@1143: this.slider = document.createElement('input'); n@1143: this.play = document.createElement('button'); n@1143: n@1143: this.holder.className = 'track-slider'; n@1143: this.holder.style.width = window.innerWidth-200 + 'px'; n@1143: this.holder.appendChild(this.title); n@1143: this.holder.appendChild(this.slider); n@1143: this.holder.appendChild(this.play); n@1143: this.holder.setAttribute('trackIndex',audioObject.id); n@1143: this.title.textContent = label; n@1143: this.title.className = 'track-slider-title'; n@1143: n@1143: this.slider.type = "range"; n@1143: this.slider.className = "track-slider-range track-slider-not-moved"; n@1143: this.slider.min = "0"; n@1143: this.slider.max = "1"; n@1143: this.slider.step = "0.01"; n@1143: this.slider.style.width = window.innerWidth-420 + 'px'; n@1143: this.slider.onchange = function() n@1143: { n@1143: var time = audioEngineContext.timer.getTestTime(); n@1143: var id = Number(this.parentNode.getAttribute('trackIndex')); n@1143: audioEngineContext.audioObjects[id].metric.moved(time,this.value); n@1143: console.log('slider '+id+' moved to '+this.value+' ('+time+')'); n@1143: $(this).removeClass('track-slider-not-moved'); n@1143: }; n@1143: n@1143: this.play.className = 'track-slider-button'; n@1143: this.play.textContent = "Loading..."; n@1143: this.play.value = audioObject.id; n@1143: this.play.disabled = true; n@1143: this.play.onclick = function(event) n@1143: { n@1143: var id = Number(event.currentTarget.value); n@1143: //audioEngineContext.metric.sliderPlayed(id); n@1143: audioEngineContext.play(id); n@1143: $(".track-slider").removeClass('track-slider-playing'); n@1143: $(event.currentTarget.parentElement).addClass('track-slider-playing'); n@1143: var outsideReference = document.getElementById('outside-reference'); n@1143: if (outsideReference != null) { n@1143: $(outsideReference).removeClass('track-slider-playing'); n@1143: } n@1143: }; n@1144: this.resize = function(event) n@1144: { n@1144: this.holder.style.width = window.innerWidth-200 + 'px'; n@1144: this.slider.style.width = window.innerWidth-420 + 'px'; n@1144: }; n@1143: this.enable = function() n@1143: { n@1143: // This is used to tell the interface object that playback of this node is ready n@1143: this.play.disabled = false; n@1143: this.play.textContent = "Play"; n@1143: $(this.slider).removeClass('track-slider-disabled'); n@1143: }; n@1143: this.updateLoading = function(progress) n@1143: { n@1143: // progress is a value from 0 to 100 indicating the current download state of media files n@1143: }; n@1143: this.getValue = function() n@1143: { n@1143: // Return the current value of the object. If there is no value, return 0 n@1143: return this.slider.value; n@1143: }; n@1143: this.getPresentedId = function() n@1143: { n@1143: // Return the presented ID of the object. For instance, the APE has sliders starting from 0. Whilst AB has alphabetical scale n@1143: return this.title.textContent; n@1143: }; n@1143: this.canMove = function() n@1143: { n@1143: // Return either true or false if the interface object can be moved. AB / Reference cannot, whilst sliders can and therefore have a continuous scale. n@1143: // These are checked primarily if the interface check option 'fragmentMoved' is enabled. n@1143: return true; n@1143: }; n@1143: this.exportXMLDOM = function(audioObject) { n@1143: // Called by the audioObject holding this element to export the interface node. n@1143: // If there is no value node (such as outside reference), return null n@1143: // If there are multiple value nodes (such as multiple scale / 2D scales), return an array of nodes with each value node having an 'interfaceName' attribute n@1143: // Use storage.document.createElement('value'); to generate the XML node. n@1147: var node = storage.document.createElement('value'); n@1147: node.textContent = this.slider.value; n@1147: return node; n@1143: }; n@1143: }; n@1143: n@1143: function resizeWindow(event) n@1143: { n@1143: // Called on every window resize event, use this to scale your page properly n@1143: n@1143: var numObj = document.getElementsByClassName('track-slider').length; n@1143: var totalHeight = (numObj * 125)-25; n@1143: document.getElementById('scale-holder').style.width = window.innerWidth-220 + 'px'; n@1143: var canvas = document.getElementById('scale-canvas'); n@1143: canvas.width = window.innerWidth-420; n@1143: canvas.height = totalHeight; n@1144: for (var i in audioEngineContext.audioObjects) n@1144: { n@1144: if (audioEngineContext.audioObjects[i].specification.type != 'outside-reference'){ n@1144: audioEngineContext.audioObjects[i].interfaceDOM.resize(event); n@1144: } n@1144: } n@1143: drawScale(); n@1143: } n@1143: n@1143: function drawScale() n@1143: { n@1143: var interfaceObj = testState.currentStateMap.interfaces[0]; n@1143: var scales = testState.currentStateMap.interfaces[0].scales; n@1143: scales = scales.sort(function(a,b) { n@1143: return a.position - b.position; n@1143: }); n@1143: var canvas = document.getElementById('scale-canvas'); n@1143: var ctx = canvas.getContext("2d"); n@1143: var height = canvas.height; n@1143: var width = canvas.width; n@1143: var textHolder = document.getElementById('scale-text-holder'); n@1143: textHolder.innerHTML = null; n@1143: ctx.fillStyle = "#000000"; n@1143: ctx.setLineDash([1,4]); n@1143: for (var scale of scales) n@1143: { n@1143: var posPercent = scale.position / 100.0; n@1143: var posPix = Math.round(width * posPercent); n@1143: if(posPix<=0){posPix=1;} n@1143: if(posPix>=width){posPix=width-1;} n@1143: ctx.moveTo(posPix,0); n@1143: ctx.lineTo(posPix,height); n@1143: ctx.stroke(); n@1143: n@1143: var text = document.createElement('div'); n@1143: text.align = "center"; n@1143: var textC = document.createElement('span'); n@1143: textC.textContent = scale.text; n@1143: text.appendChild(textC); n@1143: text.className = "scale-text"; n@1143: textHolder.appendChild(text); n@1143: text.style.width = Math.ceil($(text).width())+'px'; n@1143: text.style.left = (posPix+100-($(text).width()/2)) +'px'; n@1143: } n@1143: } n@1143: n@1143: function buttonSubmitClick() // TODO: Only when all songs have been played! n@1143: { n@1143: var checks = []; n@1143: checks = checks.concat(testState.currentStateMap.interfaces[0].options); n@1143: checks = checks.concat(specification.interfaces.options); n@1143: var canContinue = true; n@1143: n@1143: // Check that the anchor and reference objects are correctly placed n@1143: if (interfaceContext.checkHiddenAnchor() == false) {return;} n@1143: if (interfaceContext.checkHiddenReference() == false) {return;} n@1143: n@1143: for (var i=0; i saves n@1143: // Get the current information in store (remember to appendChild your data to it) n@1143: // pageSpecification is the current page node configuration n@1143: // To create new XML nodes, use storage.document.createElement(); n@1143: }