annotate ape.js @ 34:8fdb7c1af7d9 Dev_main

Separated interface loading to seperate test pages.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 12:18:51 +0100
parents c61a92659541
children d848d24fdb7c
rev   line source
nicholas@2 1 /**
nicholas@2 2 * ape.js
nicholas@2 3 * Create the APE interface
nicholas@2 4 */
nicholas@2 5
n@32 6 /*
n@32 7 *
n@32 8 * WARNING!!!
n@32 9 *
n@32 10 * YOU ARE VIEWING THE DEV VERSION. THERE IS NO GUARANTEE THIS WILL BE FULLY FUNCTIONAL
n@32 11 *
n@32 12 * WARNING!!!
n@32 13 *
n@32 14 */
n@32 15
n@32 16
nicholas@2 17 // Once this is loaded and parsed, begin execution
nicholas@2 18 loadInterface(projectXML);
nicholas@2 19
nicholas@2 20 function loadInterface(xmlDoc) {
nicholas@2 21
n@16 22 // Get the dimensions of the screen available to the page
nicholas@2 23 var width = window.innerWidth;
nicholas@2 24 var height = window.innerHeight;
nicholas@2 25
nicholas@2 26 // The injection point into the HTML page
nicholas@2 27 var insertPoint = document.getElementById("topLevelBody");
n@22 28 var testContent = document.createElement('div');
n@22 29 testContent.id = 'testContent';
nicholas@2 30
nicholas@7 31
nicholas@2 32 // Decode parts of the xmlDoc that are needed
nicholas@2 33 // xmlDoc MUST already be parsed by jQuery!
nicholas@2 34 var xmlSetup = xmlDoc.find('setup');
nicholas@2 35 // Should put in an error function here incase of malprocessed or malformed XML
nicholas@2 36
n@34 37 // Extract the different test XML DOM trees
n@34 38 testXMLSetups = xmlDoc.find('audioHolder');
n@34 39
nicholas@2 40 // Create the top div for the Title element
nicholas@2 41 var titleAttr = xmlSetup[0].attributes['title'];
nicholas@2 42 var title = document.createElement('div');
nicholas@2 43 title.className = "title";
nicholas@2 44 title.align = "center";
nicholas@2 45 var titleSpan = document.createElement('span');
nicholas@2 46
nicholas@2 47 // Set title to that defined in XML, else set to default
nicholas@2 48 if (titleAttr != undefined) {
n@25 49 titleSpan.innerHTML = titleAttr.value;
nicholas@2 50 } else {
n@25 51 titleSpan.innerHTML = 'APE Tool';
nicholas@2 52 }
nicholas@2 53 // Insert the titleSpan element into the title div element.
nicholas@2 54 title.appendChild(titleSpan);
nicholas@2 55
nicholas@7 56 // Store the return URL path in global projectReturn
nicholas@7 57 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
nicholas@7 58
nicholas@7 59 // Create Interface buttons!
nicholas@7 60 var interfaceButtons = document.createElement('div');
nicholas@7 61 interfaceButtons.id = 'interface-buttons';
nicholas@7 62
nicholas@7 63 // MANUAL DOWNLOAD POINT
nicholas@7 64 // If project return is null, this MUST be specified as the location to create the download link
nicholas@7 65 var downloadPoint = document.createElement('div');
nicholas@7 66 downloadPoint.id = 'download-point';
nicholas@7 67
nicholas@7 68 // Create playback start/stop points
nicholas@7 69 var playback = document.createElement("button");
n@25 70 playback.innerHTML = 'Start';
n@16 71 // onclick function. Check if it is playing or not, call the correct function in the
n@16 72 // audioEngine, change the button text to reflect the next state.
nicholas@7 73 playback.onclick = function() {
nicholas@7 74 if (audioEngineContext.status == 0) {
nicholas@7 75 audioEngineContext.play();
n@25 76 this.innerHTML = 'Stop';
nicholas@7 77 } else {
nicholas@7 78 audioEngineContext.stop();
n@25 79 this.innerHTML = 'Start';
nicholas@7 80 }
n@16 81 };
nicholas@7 82 // Create Submit (save) button
nicholas@7 83 var submit = document.createElement("button");
n@25 84 submit.innerHTML = 'Submit';
nicholas@7 85 submit.onclick = function() {
n@15 86 // TODO: Update this for postTest tags
nicholas@7 87 createProjectSave(projectReturn)
n@16 88 };
n@16 89 // Append the interface buttons into the interfaceButtons object.
nicholas@7 90 interfaceButtons.appendChild(playback);
nicholas@7 91 interfaceButtons.appendChild(submit);
nicholas@7 92 interfaceButtons.appendChild(downloadPoint);
nicholas@7 93
nicholas@2 94 // Now create the slider and HTML5 canvas boxes
nicholas@2 95
n@16 96 // Create the div box to center align
nicholas@2 97 var sliderBox = document.createElement('div');
nicholas@2 98 sliderBox.className = 'sliderCanvasDiv';
n@16 99 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 100 sliderBox.align = 'center';
nicholas@2 101
n@16 102 // Create the slider box to hold the slider elements
nicholas@5 103 var canvas = document.createElement('div');
nicholas@2 104 canvas.id = 'slider';
n@16 105 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@5 106 canvas.style.width = width - 100 +"px";
nicholas@5 107 canvas.align = "left";
nicholas@2 108 sliderBox.appendChild(canvas);
n@16 109
n@16 110 // Global parent for the comment boxes on the page
nicholas@3 111 var feedbackHolder = document.createElement('div');
n@34 112 feedbackHolder.id = 'feedbackHolder';
nicholas@2 113
n@22 114 // Create pre and post test questions
n@22 115
nicholas@2 116 // Inject into HTML
nicholas@2 117 insertPoint.innerHTML = null; // Clear the current schema
n@22 118 testContent.appendChild(title); // Insert the title
n@22 119 testContent.appendChild(interfaceButtons);
n@22 120 testContent.appendChild(sliderBox);
n@22 121 testContent.appendChild(feedbackHolder);
n@22 122 insertPoint.appendChild(testContent);
n@22 123
n@34 124 loadTest(testXMLSetups[0]);
n@34 125
n@22 126 var preTest = xmlDoc.find('PreTest');
n@22 127 var postTest = xmlDoc.find('PostTest');
n@22 128 preTest = preTest[0];
n@22 129 postTest = postTest[0];
n@22 130 if (preTest != undefined || postTest != undefined)
n@22 131 {
n@22 132 testContent.style.zIndex = 1;
n@22 133 var blank = document.createElement('div');
n@33 134 blank.className = 'testHalt';
n@22 135 insertPoint.appendChild(blank);
n@22 136 }
n@22 137
n@22 138 // Create Pre-Test Box
n@22 139 if (preTest != undefined && preTest.children.length >= 1)
n@22 140 {
n@22 141
n@22 142 var preTestHolder = document.createElement('div');
n@22 143 preTestHolder.id = 'preTestHolder';
n@33 144 preTestHolder.className = 'popupHolder';
n@22 145 preTestHolder.style.position = 'absolute';
n@22 146 preTestHolder.style.left = (window.innerWidth/2)-250 + 'px';
n@22 147 preTestHolder.style.top = (window.innerHeight/2)-125 + 'px';
n@22 148 // Parse the first box
n@22 149 var preTestOption = document.createElement('div');
n@22 150 preTestOption.id = 'preTest';
n@22 151 preTestOption.style.marginTop = '25px';
n@22 152 preTestOption.align = "center";
n@22 153 var child = preTest.children[0];
n@22 154 if (child.nodeName == 'statement')
n@22 155 {
n@22 156 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@22 157 } else if (child.nodeName == 'question')
n@22 158 {
n@23 159 var questionId = child.attributes['id'].value;
n@22 160 var textHold = document.createElement('span');
n@22 161 textHold.innerHTML = child.innerHTML;
n@23 162 textHold.id = questionId + 'response';
n@22 163 var textEnter = document.createElement('textarea');
n@22 164 preTestOption.appendChild(textHold);
n@22 165 preTestOption.appendChild(textEnter);
n@22 166 }
n@22 167 var nextButton = document.createElement('button');
n@22 168 nextButton.id = 'preTestNext';
n@22 169 nextButton.value = '1';
n@33 170 nextButton.innerHTML = 'Next';
n@22 171 nextButton.onclick = function() {
n@22 172 // Need to find and parse preTest again!
n@22 173 var preTest = projectXML.find('PreTest')[0];
n@23 174 // Check if current state is a question!
n@23 175 if (preTest.children[this.value-1].nodeName == 'question') {
n@23 176 var questionId = preTest.children[this.value-1].attributes['id'].value;
n@23 177 var questionHold = document.createElement('comment');
n@23 178 var questionResponse = document.getElementById(questionId + 'response');
n@23 179 questionHold.id = questionId;
n@23 180 questionHold.innerHTML = questionResponse.value;
n@23 181 preTestQuestions.appendChild(questionHold);
n@23 182 }
n@22 183 if (this.value < preTest.children.length)
n@22 184 {
n@22 185 // More to process
n@22 186 var child = preTest.children[this.value];
n@22 187 if (child.nodeName == 'statement')
n@22 188 {
n@22 189 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@22 190 } else if (child.nodeName == 'question')
n@22 191 {
n@22 192 var textHold = document.createElement('span');
n@22 193 textHold.innerHTML = child.innerHTML;
n@22 194 var textEnter = document.createElement('textarea');
n@23 195 textEnter.id = child.attributes['id'].value + 'response';
n@33 196 var br = document.createElement('br');
n@23 197 preTestOption.innerHTML = null;
n@22 198 preTestOption.appendChild(textHold);
n@33 199 preTestOption.appendChild(br);
n@22 200 preTestOption.appendChild(textEnter);
n@22 201 }
n@22 202 } else {
n@22 203 // Time to clear
n@22 204 preTestHolder.style.zIndex = -1;
n@22 205 preTestHolder.style.visibility = 'hidden';
n@33 206 var blank = document.getElementsByClassName('testHalt')[0];
n@22 207 blank.style.zIndex = -2;
n@22 208 blank.style.visibility = 'hidden';
n@22 209 }
n@22 210 this.value++;
n@22 211 };
n@22 212
n@22 213 preTestHolder.appendChild(preTestOption);
n@22 214 preTestHolder.appendChild(nextButton);
n@22 215 insertPoint.appendChild(preTestHolder);
n@22 216 }
n@22 217
nicholas@2 218 }
nicholas@5 219
n@34 220 function loadTest(textXML)
n@34 221 {
n@34 222 // Used to load a specific test page
n@34 223
n@34 224
n@34 225 var feedbackHolder = document.getElementById('feedbackHolder');
n@34 226 var canvas = document.getElementById('slider');
n@34 227 feedbackHolder.innerHTML = null;
n@34 228 canvas.innerHTML = null;
n@34 229
n@34 230 // Extract the hostURL attribute. If not set, create an empty string.
n@34 231 var hostURL = textXML.attributes['hostURL'];
n@34 232 if (hostURL == undefined) {
n@34 233 hostURL = "";
n@34 234 } else {
n@34 235 hostURL = hostURL.value;
n@34 236 }
n@34 237 // Extract the sampleRate. If set, convert the string to a Number.
n@34 238 var hostFs = textXML.attributes['sampleRate'];
n@34 239 if (hostFs != undefined) {
n@34 240 hostFs = Number(hostFs.value);
n@34 241 }
n@34 242
n@34 243 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@34 244 if (hostFs != undefined) {
n@34 245 if (Number(hostFs) != audioContext.sampleRate) {
n@34 246 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 247 alert(errStr);
n@34 248 return;
n@34 249 }
n@34 250 }
n@34 251 // Find all the audioElements from the audioHolder
n@34 252 var audioElements = $(textXML).find('audioElements');
n@34 253 audioElements.each(function(index,element){
n@34 254 // Find URL of track
n@34 255 // In this jQuery loop, variable 'this' holds the current audioElement.
n@34 256
n@34 257 // Now load each audio sample. First create the new track by passing the full URL
n@34 258 var trackURL = hostURL + this.attributes['url'].value;
n@34 259 audioEngineContext.newTrack(trackURL);
n@34 260 // Create document objects to hold the comment boxes
n@34 261 var trackComment = document.createElement('div');
n@34 262 // Create a string next to each comment asking for a comment
n@34 263 var trackString = document.createElement('span');
n@34 264 trackString.innerHTML = 'Comment on track '+index;
n@34 265 // Create the HTML5 comment box 'textarea'
n@34 266 var trackCommentBox = document.createElement('textarea');
n@34 267 trackCommentBox.rows = '4';
n@34 268 trackCommentBox.cols = '100';
n@34 269 trackCommentBox.name = 'trackComment'+index;
n@34 270 trackCommentBox.className = 'trackComment';
n@34 271 var br = document.createElement('br');
n@34 272 // Add to the holder.
n@34 273 trackComment.appendChild(trackString);
n@34 274 trackComment.appendChild(br);
n@34 275 trackComment.appendChild(trackCommentBox);
n@34 276 feedbackHolder.appendChild(trackComment);
n@34 277
n@34 278 // Create a slider per track
n@34 279
n@34 280 var trackSliderObj = document.createElement('div');
n@34 281 trackSliderObj.className = 'track-slider';
n@34 282 trackSliderObj.id = 'track-slider-'+index;
n@34 283 // Distribute it randomnly
n@34 284 var w = window.innerWidth - 100;
n@34 285 w = Math.random()*w;
n@34 286 trackSliderObj.style.left = Math.floor(w)+50+'px';
n@34 287 trackSliderObj.innerHTML = '<span>'+index+'</span>';
n@34 288 trackSliderObj.draggable = true;
n@34 289 trackSliderObj.ondragend = dragEnd;
n@34 290
n@34 291 // Onclick, switch playback to that track
n@34 292 trackSliderObj.onclick = function() {
n@34 293 // Get the track ID from the object ID
n@34 294 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@34 295 audioEngineContext.selectedTrack(id);
n@34 296 };
n@34 297
n@34 298 canvas.appendChild(trackSliderObj);
n@34 299 });
n@34 300 }
n@34 301
n@34 302 function preTestButtonClick()
n@34 303 {
n@34 304 // Called on click of pre-test button
n@34 305 }
n@34 306
nicholas@5 307 function dragEnd(ev) {
nicholas@5 308 // Function call when a div has been dropped
n@33 309 var slider = document.getElementById('slider');
nicholas@5 310 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
nicholas@5 311 this.style.left = (ev.x)+'px';
nicholas@5 312 } else {
nicholas@5 313 if (ev.x<50) {
nicholas@5 314 this.style.left = '50px';
nicholas@5 315 } else {
nicholas@5 316 this.style.left = window.innerWidth-50 + 'px';
nicholas@5 317 }
nicholas@5 318 }
nicholas@5 319 }
nicholas@7 320
nicholas@7 321 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 322 function interfaceXMLSave(){
nicholas@7 323 // Create the XML string to be exported with results
nicholas@7 324 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@7 325 var trackSliderObjects = document.getElementsByClassName('track-slider');
nicholas@7 326 var commentObjects = document.getElementsByClassName('trackComment');
nicholas@7 327 var rateMin = 50;
nicholas@7 328 var rateMax = window.innerWidth-50;
nicholas@7 329 for (var i=0; i<trackSliderObjects.length; i++)
nicholas@7 330 {
n@24 331 var trackObj = document.createElement("audioElement");
nicholas@7 332 trackObj.id = i;
n@24 333 trackObj.url = audioEngineContext.audioObjects[i].url;
nicholas@7 334 var slider = document.createElement("Rating");
nicholas@7 335 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
nicholas@7 336 rate = (rate-rateMin)/rateMax;
n@25 337 slider.innerHTML = Math.floor(rate*100);
nicholas@7 338 var comment = document.createElement("Comment");
n@25 339 comment.innerHTML = commentObjects[i].value;
nicholas@7 340 trackObj.appendChild(slider);
nicholas@7 341 trackObj.appendChild(comment);
nicholas@7 342 xmlDoc.appendChild(trackObj);
nicholas@7 343 }
nicholas@7 344
n@23 345 // Append Pre/Post Questions
n@23 346 xmlDoc.appendChild(preTestQuestions);
n@23 347 xmlDoc.appendChild(postTestQuestions);
n@23 348
nicholas@7 349 return xmlDoc;
nicholas@7 350 }
nicholas@7 351