nickjillings@1316: // Once this is loaded and parsed, begin execution nickjillings@1316: loadInterface(); nickjillings@1316: nickjillings@1316: function loadInterface() { nickjillings@1316: // Use this to do any one-time page / element construction. For instance, placing any stationary text objects, nickjillings@1316: // holding div's, or setting up any nodes which are present for the entire test sequence nickjillings@1316: nickjillings@1316: // The injection point into the HTML page nickjillings@1316: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nickjillings@1316: var testContent = document.createElement('div'); nickjillings@1316: testContent.id = 'testContent'; nickjillings@1316: nickjillings@1316: // Create the top div for the Title element nickjillings@1316: var titleAttr = specification.title; nickjillings@1316: var title = document.createElement('div'); nickjillings@1316: title.className = "title"; nickjillings@1316: title.align = "center"; nickjillings@1316: var titleSpan = document.createElement('span'); nickjillings@1316: nickjillings@1316: // Set title to that defined in XML, else set to default nickjillings@1316: if (titleAttr != undefined) { nickjillings@1316: titleSpan.textContent = titleAttr; nickjillings@1316: } else { nickjillings@1316: titleSpan.textContent = 'Listening test'; nickjillings@1316: } nickjillings@1316: // Insert the titleSpan element into the title div element. nickjillings@1316: title.appendChild(titleSpan); nickjillings@1316: nickjillings@1316: var pagetitle = document.createElement('div'); nickjillings@1316: pagetitle.className = "pageTitle"; nickjillings@1316: pagetitle.align = "center"; nickjillings@1316: var titleSpan = document.createElement('span'); nickjillings@1316: titleSpan.id = "pageTitle"; nickjillings@1316: pagetitle.appendChild(titleSpan); nickjillings@1316: nickjillings@1316: // Create Interface buttons! nickjillings@1316: var interfaceButtons = document.createElement('div'); nickjillings@1316: interfaceButtons.id = 'interface-buttons'; nickjillings@1316: interfaceButtons.style.height = '25px'; nickjillings@1316: nickjillings@1316: // Create playback start/stop points nickjillings@1316: var playback = document.createElement("button"); nickjillings@1316: playback.innerHTML = 'Stop'; nickjillings@1316: playback.id = 'playback-button'; nickjillings@1316: playback.style.float = 'left'; nickjillings@1316: // onclick function. Check if it is playing or not, call the correct function in the nickjillings@1316: // audioEngine, change the button text to reflect the next state. nickjillings@1316: playback.onclick = function() { nickjillings@1316: if (audioEngineContext.status == 1) { nickjillings@1316: audioEngineContext.stop(); nickjillings@1316: this.innerHTML = 'Stop'; nickjillings@1316: var time = audioEngineContext.timer.getTestTime(); nickjillings@1316: console.log('Stopped at ' + time); // DEBUG/SAFETY nickjillings@1316: } nickjillings@1316: }; nickjillings@1316: // Create Submit (save) button nickjillings@1316: var submit = document.createElement("button"); nickjillings@1316: submit.innerHTML = 'Submit'; nickjillings@1316: submit.onclick = buttonSubmitClick; nickjillings@1316: submit.id = 'submit-button'; nickjillings@1316: submit.style.float = 'left'; nickjillings@1316: // Append the interface buttons into the interfaceButtons object. nickjillings@1316: interfaceButtons.appendChild(playback); nickjillings@1316: interfaceButtons.appendChild(submit); nickjillings@1316: nickjillings@1316: // Create a slider box nickjillings@1316: var sliderBox = document.createElement('div'); nickjillings@1316: sliderBox.style.width = "100%"; nickjillings@1316: sliderBox.style.height = window.innerHeight - 200+12 + 'px'; nickjillings@1316: sliderBox.style.marginBottom = '10px'; nickjillings@1316: sliderBox.id = 'slider'; nickjillings@1316: var scaleHolder = document.createElement('div'); nickjillings@1316: scaleHolder.id = "scale-holder"; nickjillings@1316: scaleHolder.style.marginLeft = "107px"; nickjillings@1316: sliderBox.appendChild(scaleHolder); nickjillings@1316: var scaleText = document.createElement('div'); nickjillings@1316: scaleText.id = "scale-text-holder"; nickjillings@1316: scaleText.style.height = "25px"; nickjillings@1316: scaleText.style.width = "100%"; nickjillings@1316: scaleHolder.appendChild(scaleText); nickjillings@1316: var scaleCanvas = document.createElement('canvas'); nickjillings@1316: scaleCanvas.id = "scale-canvas"; nickjillings@1316: scaleCanvas.style.marginLeft = "150px"; nickjillings@1316: scaleHolder.appendChild(scaleCanvas); nickjillings@1316: var sliderObjectHolder = document.createElement('div'); nickjillings@1316: sliderObjectHolder.id = 'slider-holder'; nickjillings@1316: sliderObjectHolder.align = "center"; nickjillings@1316: sliderBox.appendChild(sliderObjectHolder); nickjillings@1316: nickjillings@1316: // Global parent for the comment boxes on the page nickjillings@1316: var feedbackHolder = document.createElement('div'); nickjillings@1316: feedbackHolder.id = 'feedbackHolder'; nickjillings@1316: nickjillings@1316: testContent.style.zIndex = 1; nickjillings@1316: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nickjillings@1316: nickjillings@1316: // Inject into HTML nickjillings@1316: testContent.appendChild(title); // Insert the title nickjillings@1316: testContent.appendChild(pagetitle); nickjillings@1316: testContent.appendChild(interfaceButtons); nickjillings@1316: testContent.appendChild(sliderBox); nickjillings@1316: testContent.appendChild(feedbackHolder); nickjillings@1316: interfaceContext.insertPoint.appendChild(testContent); nickjillings@1316: nickjillings@1316: // Load the full interface nickjillings@1316: testState.initialise(); nickjillings@1316: testState.advanceState(); nickjillings@1316: }; nickjillings@1316: nickjillings@1316: function loadTest(page) nickjillings@1316: { nickjillings@1316: // Called each time a new test page is to be build. The page specification node is the only item passed in nickjillings@1316: var id = page.id; nickjillings@1316: nickjillings@1316: var feedbackHolder = document.getElementById('feedbackHolder'); nickjillings@1316: feedbackHolder.innerHTML = null; nickjillings@1316: var interfaceObj = page.interfaces; nickjillings@1316: if (interfaceObj.length > 1) nickjillings@1316: { nickjillings@1316: console.log("WARNING - This interface only supports one node per page. Using first interface node"); nickjillings@1316: } nickjillings@1316: interfaceObj = interfaceObj[0]; nickjillings@1316: if(interfaceObj.title != null) nickjillings@1316: { nickjillings@1316: document.getElementById("pageTitle").textContent = interfaceObj.title; nickjillings@1316: } nickjillings@1316: nickjillings@1316: var interfaceOptions = specification.interfaces.options.concat(interfaceObj.options); nickjillings@1316: for (var option of interfaceOptions) nickjillings@1316: { nickjillings@1316: if (option.type == "show") nickjillings@1316: { nickjillings@1316: switch(option.name) { nickjillings@1316: case "playhead": nickjillings@1316: var playbackHolder = document.getElementById('playback-holder'); nickjillings@1316: if (playbackHolder == null) nickjillings@1316: { nickjillings@1316: playbackHolder = document.createElement('div'); nickjillings@1316: playbackHolder.style.width = "100%"; nickjillings@1316: playbackHolder.align = 'center'; nickjillings@1316: playbackHolder.appendChild(interfaceContext.playhead.object); nickjillings@1316: feedbackHolder.appendChild(playbackHolder); nickjillings@1316: } nickjillings@1316: break; nickjillings@1316: case "page-count": nickjillings@1316: var pagecountHolder = document.getElementById('page-count'); nickjillings@1316: if (pagecountHolder == null) nickjillings@1316: { nickjillings@1316: pagecountHolder = document.createElement('div'); nickjillings@1316: pagecountHolder.id = 'page-count'; nickjillings@1316: } nickjillings@1316: pagecountHolder.innerHTML = 'Page '+(page.presentedId+1)+' of '+specification.pages.length+''; nickjillings@1316: var inject = document.getElementById('interface-buttons'); nickjillings@1316: inject.appendChild(pagecountHolder); nickjillings@1316: break; nickjillings@1316: case "volume": nickjillings@1316: if (document.getElementById('master-volume-holder') == null) nickjillings@1316: { nickjillings@1316: feedbackHolder.appendChild(interfaceContext.volume.object); nickjillings@1316: } nickjillings@1316: break; nickjillings@1316: } nickjillings@1316: } nickjillings@1316: } nickjillings@1316: nickjillings@1316: // Delete outside reference nickjillings@1316: var outsideReferenceHolder = document.getElementById('outside-reference'); nickjillings@1316: if (outsideReferenceHolder != null) { nickjillings@1316: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); nickjillings@1316: } nickjillings@1316: nickjillings@1316: var sliderBox = document.getElementById('slider-holder'); nickjillings@1316: sliderBox.innerHTML = null; nickjillings@1316: nickjillings@1316: var commentBoxPrefix = "Comment on track"; nickjillings@1316: if (interfaceObj.commentBoxPrefix != undefined) { nickjillings@1316: commentBoxPrefix = interfaceObj.commentBoxPrefix; nickjillings@1316: } nickjillings@1316: var loopPlayback = page.loop; nickjillings@1316: nickjillings@1316: $(page.commentQuestions).each(function(index,element) { nickjillings@1316: var node = interfaceContext.createCommentQuestion(element); nickjillings@1316: feedbackHolder.appendChild(node.holder); nickjillings@1316: }); nickjillings@1316: nickjillings@1316: // Find all the audioElements from the audioHolder nickjillings@1316: var label = 0; nickjillings@1316: var interfaceScales = testState.currentStateMap.interfaces[0].scales; nickjillings@1316: $(page.audioElements).each(function(index,element){ nickjillings@1316: // Find URL of track nickjillings@1316: // In this jQuery loop, variable 'this' holds the current audioElement. nickjillings@1316: nickjillings@1316: var audioObject = audioEngineContext.newTrack(element); nickjillings@1316: if (element.type == 'outside-reference') nickjillings@1316: { nickjillings@1316: // Construct outside reference; nickjillings@1316: var orNode = new outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons')); nickjillings@1316: audioObject.bindInterface(orNode); nickjillings@1316: } else { nickjillings@1316: // Create a slider per track nickjillings@1316: var sliderObj = new discreteObject(audioObject,label,interfaceScales); nickjillings@1316: sliderBox.appendChild(sliderObj.holder); nickjillings@1316: audioObject.bindInterface(sliderObj); nickjillings@1316: interfaceContext.createCommentBox(audioObject); nickjillings@1316: label += 1; nickjillings@1316: } nickjillings@1316: nickjillings@1316: }); nickjillings@1316: nickjillings@1316: if (page.showElementComments) nickjillings@1316: { nickjillings@1316: interfaceContext.showCommentBoxes(feedbackHolder,true); nickjillings@1316: } nickjillings@1316: nickjillings@1316: // Auto-align nickjillings@1316: resizeWindow(null); nickjillings@1316: } nickjillings@1316: nickjillings@1316: function discreteObject(audioObject,label,interfaceScales) nickjillings@1316: { nickjillings@1316: // An example node, you can make this however you want for each audioElement. nickjillings@1316: // However, every audioObject (audioEngineContext.audioObject) MUST have an interface object with the following nickjillings@1316: // You attach them by calling audioObject.bindInterface( ) nickjillings@1316: if (interfaceScales == null || interfaceScales.length == 0) nickjillings@1316: { nickjillings@1316: console.log("WARNING: The discrete radio's are built depending on the number of scale points specified! Ensure you have some specified. Defaulting to 5 for now!"); nickjillings@1316: numOptions = 5; nickjillings@1316: } nickjillings@1316: this.parent = audioObject; nickjillings@1316: nickjillings@1316: this.holder = document.createElement('div'); nickjillings@1316: this.title = document.createElement('div'); nickjillings@1316: this.discreteHolder = document.createElement('div'); nickjillings@1316: this.discretes = []; nickjillings@1316: this.play = document.createElement('button'); nickjillings@1316: nickjillings@1316: this.holder.className = 'track-slider'; nickjillings@1316: this.holder.style.width = window.innerWidth-200 + 'px'; nickjillings@1316: this.holder.appendChild(this.title); nickjillings@1316: this.holder.appendChild(this.discreteHolder); nickjillings@1316: this.holder.appendChild(this.play); nickjillings@1316: this.holder.setAttribute('trackIndex',audioObject.id); nickjillings@1316: this.title.textContent = label; nickjillings@1316: this.title.className = 'track-slider-title'; nickjillings@1316: nickjillings@1316: this.discreteHolder.className = "track-slider-range"; nickjillings@1316: this.discreteHolder.style.width = window.innerWidth-500 + 'px'; nickjillings@1316: for (var i=0; i node. nickjillings@1316: // If there is no value node (such as outside reference), return null nickjillings@1316: // 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 nickjillings@1316: // Use storage.document.createElement('value'); to generate the XML node. nickjillings@1316: var node = storage.document.createElement('value'); nickjillings@1316: node.textContent = this.getValue(); nickjillings@1316: return node; nickjillings@1316: }; nickjillings@1316: }; nickjillings@1316: nickjillings@1316: function resizeWindow(event) nickjillings@1316: { nickjillings@1316: // Called on every window resize event, use this to scale your page properly nickjillings@1316: var numObj = document.getElementsByClassName('track-slider').length; nickjillings@1316: var totalHeight = (numObj * 66)-30; nickjillings@1316: document.getElementById('scale-holder').style.width = window.innerWidth-220 + 'px'; nickjillings@1316: var canvas = document.getElementById('scale-canvas'); nickjillings@1316: canvas.width = window.innerWidth-520; nickjillings@1316: canvas.height = totalHeight; nickjillings@1316: for (var i in audioEngineContext.audioObjects) nickjillings@1316: { nickjillings@1316: if (audioEngineContext.audioObjects[i].specification.type != 'outside-reference'){ nickjillings@1316: audioEngineContext.audioObjects[i].interfaceDOM.resize(event); nickjillings@1316: } nickjillings@1316: } nickjillings@1316: document.getElementById('slider-holder').style.height = totalHeight + 'px'; nickjillings@1316: document.getElementById('slider').style.height = totalHeight + 70 + 'px'; nickjillings@1316: drawScale(); nickjillings@1316: } nickjillings@1316: nickjillings@1316: function drawScale() nickjillings@1316: { nickjillings@1316: var interfaceObj = testState.currentStateMap.interfaces[0]; nickjillings@1316: var scales = testState.currentStateMap.interfaces[0].scales; nickjillings@1316: scales = scales.sort(function(a,b) { nickjillings@1316: return a.position - b.position; nickjillings@1316: }); nickjillings@1316: var canvas = document.getElementById('scale-canvas'); nickjillings@1316: var ctx = canvas.getContext("2d"); nickjillings@1316: var height = canvas.height; nickjillings@1316: var width = canvas.width; nickjillings@1316: var textHolder = document.getElementById('scale-text-holder'); nickjillings@1316: textHolder.innerHTML = null; nickjillings@1316: ctx.fillStyle = "#000000"; nickjillings@1316: ctx.setLineDash([1,4]); nickjillings@1316: for (var scale of scales) nickjillings@1316: { nickjillings@1316: var posPercent = scale.position / 100.0; nickjillings@1316: var posPix = Math.round(width * posPercent); nickjillings@1316: if(posPix<=0){posPix=1;} nickjillings@1316: if(posPix>=width){posPix=width-1;} nickjillings@1316: ctx.moveTo(posPix,0); nickjillings@1316: ctx.lineTo(posPix,height); nickjillings@1316: ctx.stroke(); nickjillings@1316: nickjillings@1316: var text = document.createElement('div'); nickjillings@1316: text.align = "center"; nickjillings@1316: var textC = document.createElement('span'); nickjillings@1316: textC.textContent = scale.text; nickjillings@1316: text.appendChild(textC); nickjillings@1316: text.className = "scale-text"; nickjillings@1316: textHolder.appendChild(text); nickjillings@1316: text.style.width = $(text.children[0]).width()+'px'; nickjillings@1316: text.style.left = (posPix+150-($(text).width()/2)) +'px'; nickjillings@1316: } nickjillings@1316: } nickjillings@1316: nickjillings@1316: function buttonSubmitClick() // TODO: Only when all songs have been played! nickjillings@1316: { nickjillings@1316: var checks = []; nickjillings@1316: checks = checks.concat(testState.currentStateMap.interfaces[0].options); nickjillings@1316: checks = checks.concat(specification.interfaces.options); nickjillings@1316: var canContinue = true; nickjillings@1316: nickjillings@1316: // Check that the anchor and reference objects are correctly placed nickjillings@1316: if (interfaceContext.checkHiddenAnchor() == false) {return;} nickjillings@1316: if (interfaceContext.checkHiddenReference() == false) {return;} nickjillings@1316: nickjillings@1316: for (var i=0; i saves nickjillings@1316: // Get the current information in store (remember to appendChild your data to it) nickjillings@1316: // pageSpecification is the current page node configuration nickjillings@1316: // To create new XML nodes, use storage.document.createElement(); nickjillings@1316: }