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