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