nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: n@32: /* n@32: * n@32: * WARNING!!! n@32: * n@32: * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL n@32: * n@32: * WARNING!!! n@32: * n@32: */ n@32: n@32: nicholas@2: // Once this is loaded and parsed, begin execution nicholas@2: loadInterface(projectXML); nicholas@2: nicholas@2: function loadInterface(xmlDoc) { 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 nicholas@2: var insertPoint = document.getElementById("topLevelBody"); n@22: var testContent = document.createElement('div'); n@22: testContent.id = 'testContent'; nicholas@2: nicholas@7: nicholas@2: // Decode parts of the xmlDoc that are needed nicholas@2: // xmlDoc MUST already be parsed by jQuery! nicholas@2: var xmlSetup = xmlDoc.find('setup'); nicholas@2: // Should put in an error function here incase of malprocessed or malformed XML nicholas@2: n@34: // Extract the different test XML DOM trees n@34: testXMLSetups = xmlDoc.find('audioHolder'); n@34: nicholas@2: // Create the top div for the Title element nicholas@2: var titleAttr = xmlSetup[0].attributes['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@25: titleSpan.innerHTML = titleAttr.value; nicholas@2: } else { n@25: titleSpan.innerHTML = 'APE Tool'; nicholas@2: } nicholas@2: // Insert the titleSpan element into the title div element. nicholas@2: title.appendChild(titleSpan); nicholas@2: nicholas@7: // Store the return URL path in global projectReturn nicholas@7: projectReturn = xmlSetup[0].attributes['projectReturn'].value; nicholas@7: nicholas@7: // Create Interface buttons! nicholas@7: var interfaceButtons = document.createElement('div'); nicholas@7: interfaceButtons.id = 'interface-buttons'; nicholas@7: nicholas@7: // MANUAL DOWNLOAD POINT nicholas@7: // If project return is null, this MUST be specified as the location to create the download link nicholas@7: var downloadPoint = document.createElement('div'); nicholas@7: downloadPoint.id = 'download-point'; nicholas@7: nicholas@7: // Create playback start/stop points nicholas@7: var playback = document.createElement("button"); n@25: playback.innerHTML = 'Start'; 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() { nicholas@7: if (audioEngineContext.status == 0) { nicholas@7: audioEngineContext.play(); n@25: this.innerHTML = 'Stop'; nicholas@7: } else { nicholas@7: audioEngineContext.stop(); n@25: this.innerHTML = 'Start'; nicholas@7: } n@16: }; nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); n@25: submit.innerHTML = 'Submit'; nicholas@7: submit.onclick = function() { n@15: // TODO: Update this for postTest tags nicholas@7: createProjectSave(projectReturn) n@16: }; n@16: // Append the interface buttons into the interfaceButtons object. nicholas@7: interfaceButtons.appendChild(playback); nicholas@7: interfaceButtons.appendChild(submit); nicholas@7: interfaceButtons.appendChild(downloadPoint); nicholas@7: nicholas@2: // Now create the slider and HTML5 canvas boxes nicholas@2: n@16: // Create the div box to center align nicholas@2: var sliderBox = document.createElement('div'); nicholas@2: sliderBox.className = 'sliderCanvasDiv'; n@16: sliderBox.id = 'sliderCanvasHolder'; nicholas@2: sliderBox.align = 'center'; nicholas@2: n@16: // Create the slider box to hold the slider elements nicholas@5: var canvas = document.createElement('div'); nicholas@2: canvas.id = 'slider'; n@16: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@5: canvas.style.width = width - 100 +"px"; nicholas@5: canvas.align = "left"; nicholas@2: sliderBox.appendChild(canvas); n@16: 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@22: // Create pre and post test questions n@22: nicholas@2: // Inject into HTML nicholas@2: insertPoint.innerHTML = null; // Clear the current schema n@22: testContent.appendChild(title); // Insert the title n@22: testContent.appendChild(interfaceButtons); n@22: testContent.appendChild(sliderBox); n@22: testContent.appendChild(feedbackHolder); n@22: insertPoint.appendChild(testContent); n@22: n@36: var preTest = xmlSetup.find('PreTest'); n@36: var postTest = xmlSetup.find('PostTest'); n@22: preTest = preTest[0]; n@22: postTest = postTest[0]; n@22: if (preTest != undefined || postTest != undefined) n@22: { n@22: testContent.style.zIndex = 1; n@22: var blank = document.createElement('div'); n@33: blank.className = 'testHalt'; n@22: insertPoint.appendChild(blank); n@22: } n@22: n@22: // Create Pre-Test Box n@22: if (preTest != undefined && preTest.children.length >= 1) n@22: { n@22: n@22: var preTestHolder = document.createElement('div'); n@22: preTestHolder.id = 'preTestHolder'; n@33: preTestHolder.className = 'popupHolder'; n@22: preTestHolder.style.position = 'absolute'; n@22: preTestHolder.style.left = (window.innerWidth/2)-250 + 'px'; n@22: preTestHolder.style.top = (window.innerHeight/2)-125 + 'px'; n@22: // Parse the first box n@22: var preTestOption = document.createElement('div'); n@22: preTestOption.id = 'preTest'; n@22: preTestOption.style.marginTop = '25px'; n@22: preTestOption.align = "center"; n@22: var child = preTest.children[0]; n@22: if (child.nodeName == 'statement') n@22: { n@22: preTestOption.innerHTML = ''+child.innerHTML+''; n@22: } else if (child.nodeName == 'question') n@22: { n@23: var questionId = child.attributes['id'].value; n@22: var textHold = document.createElement('span'); n@22: textHold.innerHTML = child.innerHTML; n@23: textHold.id = questionId + 'response'; n@22: var textEnter = document.createElement('textarea'); n@22: preTestOption.appendChild(textHold); n@22: preTestOption.appendChild(textEnter); n@22: } n@22: var nextButton = document.createElement('button'); n@22: nextButton.id = 'preTestNext'; n@36: nextButton.value = '0'; n@33: nextButton.innerHTML = 'Next'; n@36: nextButton.onclick = preTestButtonClick; n@22: n@22: preTestHolder.appendChild(preTestOption); n@22: preTestHolder.appendChild(nextButton); n@22: insertPoint.appendChild(preTestHolder); n@22: } n@22: n@36: // Load the full interface n@36: loadTest(testXMLSetups[0]); nicholas@2: } nicholas@5: n@34: function loadTest(textXML) n@34: { n@34: // Used to load a specific test page n@34: n@34: n@34: var feedbackHolder = document.getElementById('feedbackHolder'); n@34: var canvas = document.getElementById('slider'); n@34: feedbackHolder.innerHTML = null; n@34: canvas.innerHTML = null; n@34: n@34: // Extract the hostURL attribute. If not set, create an empty string. n@34: var hostURL = textXML.attributes['hostURL']; n@34: if (hostURL == undefined) { n@34: hostURL = ""; n@34: } else { n@34: hostURL = hostURL.value; n@34: } n@34: // Extract the sampleRate. If set, convert the string to a Number. n@34: var hostFs = textXML.attributes['sampleRate']; n@34: if (hostFs != undefined) { n@34: hostFs = Number(hostFs.value); n@34: } n@34: n@34: /// CHECK FOR SAMPLE RATE COMPATIBILITY n@34: if (hostFs != undefined) { n@34: if (Number(hostFs) != audioContext.sampleRate) { n@34: var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; n@34: alert(errStr); n@34: return; n@34: } n@34: } n@34: // Find all the audioElements from the audioHolder n@34: var audioElements = $(textXML).find('audioElements'); n@34: 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@34: var trackURL = hostURL + this.attributes['url'].value; n@34: audioEngineContext.newTrack(trackURL); n@34: // Create document objects to hold the comment boxes n@34: var trackComment = document.createElement('div'); n@34: // Create a string next to each comment asking for a comment n@34: var trackString = document.createElement('span'); n@34: trackString.innerHTML = 'Comment on track '+index; n@34: // Create the HTML5 comment box 'textarea' n@34: var trackCommentBox = document.createElement('textarea'); n@34: trackCommentBox.rows = '4'; n@34: trackCommentBox.cols = '100'; n@34: trackCommentBox.name = 'trackComment'+index; n@34: trackCommentBox.className = 'trackComment'; n@34: var br = document.createElement('br'); n@34: // Add to the holder. n@34: trackComment.appendChild(trackString); n@34: trackComment.appendChild(br); n@34: trackComment.appendChild(trackCommentBox); n@34: feedbackHolder.appendChild(trackComment); n@34: n@34: // Create a slider per track n@34: n@34: var trackSliderObj = document.createElement('div'); n@34: trackSliderObj.className = 'track-slider'; n@34: trackSliderObj.id = 'track-slider-'+index; n@34: // Distribute it randomnly n@34: var w = window.innerWidth - 100; n@34: w = Math.random()*w; n@34: trackSliderObj.style.left = Math.floor(w)+50+'px'; n@34: trackSliderObj.innerHTML = ''+index+''; n@34: trackSliderObj.draggable = true; n@34: trackSliderObj.ondragend = dragEnd; n@34: n@34: // Onclick, switch playback to that track n@34: trackSliderObj.onclick = function() { n@34: // Get the track ID from the object ID n@34: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! n@34: audioEngineContext.selectedTrack(id); n@34: }; n@34: n@34: canvas.appendChild(trackSliderObj); n@34: }); n@34: } n@34: n@34: function preTestButtonClick() n@34: { n@34: // Called on click of pre-test button n@36: // Need to find and parse preTest again! n@36: var xmlSetup = projectXML.find('setup'); n@36: var preTest = xmlSetup.find('PreTest')[0]; n@36: var preTestOption = document.getElementById('preTest'); n@36: // Check if current state is a question! n@36: if (preTest.children[this.value].nodeName == 'question') { n@36: var questionId = preTest.children[this.value].attributes['id'].value; n@36: var questionHold = document.createElement('comment'); n@36: var questionResponse = document.getElementById(questionId + 'response'); n@36: questionHold.id = questionId; n@36: questionHold.innerHTML = questionResponse.value; n@36: preTestQuestions.appendChild(questionHold); n@36: } n@36: this.value++; n@36: if (this.value < preTest.children.length) n@36: { n@36: // More to process n@36: var child = preTest.children[this.value]; n@36: if (child.nodeName == 'statement') n@36: { n@36: preTestOption.innerHTML = ''+child.innerHTML+''; n@36: } else if (child.nodeName == 'question') n@36: { n@36: var textHold = document.createElement('span'); n@36: textHold.innerHTML = child.innerHTML; n@36: var textEnter = document.createElement('textarea'); n@36: textEnter.id = child.attributes['id'].value + 'response'; n@36: var br = document.createElement('br'); n@36: preTestOption.innerHTML = null; n@36: preTestOption.appendChild(textHold); n@36: preTestOption.appendChild(br); n@36: preTestOption.appendChild(textEnter); n@36: } n@36: } else { n@36: // Time to clear n@36: preTestHolder.style.zIndex = -1; n@36: preTestHolder.style.visibility = 'hidden'; n@36: var blank = document.getElementsByClassName('testHalt')[0]; n@36: blank.style.zIndex = -2; n@36: blank.style.visibility = 'hidden'; n@36: } n@36: } n@36: n@36: function showPopup() n@36: { n@36: n@36: } n@36: n@36: function hidePopup() n@36: { n@36: n@34: } n@34: nicholas@5: function dragEnd(ev) { nicholas@5: // Function call when a div has been dropped n@33: var slider = document.getElementById('slider'); nicholas@5: if (ev.x >= 50 && ev.x < window.innerWidth-50) { nicholas@5: this.style.left = (ev.x)+'px'; nicholas@5: } else { nicholas@5: if (ev.x<50) { nicholas@5: this.style.left = '50px'; nicholas@5: } else { nicholas@5: this.style.left = window.innerWidth-50 + 'px'; nicholas@5: } nicholas@5: } nicholas@5: } nicholas@7: nicholas@7: // Only other global function which must be defined in the interface class. Determines how to create the XML document. nicholas@7: function interfaceXMLSave(){ nicholas@7: // Create the XML string to be exported with results nicholas@7: var xmlDoc = document.createElement("BrowserEvaluationResult"); nicholas@7: var trackSliderObjects = document.getElementsByClassName('track-slider'); nicholas@7: var commentObjects = document.getElementsByClassName('trackComment'); nicholas@7: var rateMin = 50; nicholas@7: var rateMax = window.innerWidth-50; nicholas@7: for (var i=0; i