annotate ape.js @ 36:d848d24fdb7c Dev_main

Shuffled project.xml so metrics and pre/post are part of setup tag. Split pre-post button interface to separate functions
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 12:48:07 +0100
parents 8fdb7c1af7d9
children 8a78c6d38c5f
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@36 124 var preTest = xmlSetup.find('PreTest');
n@36 125 var postTest = xmlSetup.find('PostTest');
n@22 126 preTest = preTest[0];
n@22 127 postTest = postTest[0];
n@22 128 if (preTest != undefined || postTest != undefined)
n@22 129 {
n@22 130 testContent.style.zIndex = 1;
n@22 131 var blank = document.createElement('div');
n@33 132 blank.className = 'testHalt';
n@22 133 insertPoint.appendChild(blank);
n@22 134 }
n@22 135
n@22 136 // Create Pre-Test Box
n@22 137 if (preTest != undefined && preTest.children.length >= 1)
n@22 138 {
n@22 139
n@22 140 var preTestHolder = document.createElement('div');
n@22 141 preTestHolder.id = 'preTestHolder';
n@33 142 preTestHolder.className = 'popupHolder';
n@22 143 preTestHolder.style.position = 'absolute';
n@22 144 preTestHolder.style.left = (window.innerWidth/2)-250 + 'px';
n@22 145 preTestHolder.style.top = (window.innerHeight/2)-125 + 'px';
n@22 146 // Parse the first box
n@22 147 var preTestOption = document.createElement('div');
n@22 148 preTestOption.id = 'preTest';
n@22 149 preTestOption.style.marginTop = '25px';
n@22 150 preTestOption.align = "center";
n@22 151 var child = preTest.children[0];
n@22 152 if (child.nodeName == 'statement')
n@22 153 {
n@22 154 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@22 155 } else if (child.nodeName == 'question')
n@22 156 {
n@23 157 var questionId = child.attributes['id'].value;
n@22 158 var textHold = document.createElement('span');
n@22 159 textHold.innerHTML = child.innerHTML;
n@23 160 textHold.id = questionId + 'response';
n@22 161 var textEnter = document.createElement('textarea');
n@22 162 preTestOption.appendChild(textHold);
n@22 163 preTestOption.appendChild(textEnter);
n@22 164 }
n@22 165 var nextButton = document.createElement('button');
n@22 166 nextButton.id = 'preTestNext';
n@36 167 nextButton.value = '0';
n@33 168 nextButton.innerHTML = 'Next';
n@36 169 nextButton.onclick = preTestButtonClick;
n@22 170
n@22 171 preTestHolder.appendChild(preTestOption);
n@22 172 preTestHolder.appendChild(nextButton);
n@22 173 insertPoint.appendChild(preTestHolder);
n@22 174 }
n@22 175
n@36 176 // Load the full interface
n@36 177 loadTest(testXMLSetups[0]);
nicholas@2 178 }
nicholas@5 179
n@34 180 function loadTest(textXML)
n@34 181 {
n@34 182 // Used to load a specific test page
n@34 183
n@34 184
n@34 185 var feedbackHolder = document.getElementById('feedbackHolder');
n@34 186 var canvas = document.getElementById('slider');
n@34 187 feedbackHolder.innerHTML = null;
n@34 188 canvas.innerHTML = null;
n@34 189
n@34 190 // Extract the hostURL attribute. If not set, create an empty string.
n@34 191 var hostURL = textXML.attributes['hostURL'];
n@34 192 if (hostURL == undefined) {
n@34 193 hostURL = "";
n@34 194 } else {
n@34 195 hostURL = hostURL.value;
n@34 196 }
n@34 197 // Extract the sampleRate. If set, convert the string to a Number.
n@34 198 var hostFs = textXML.attributes['sampleRate'];
n@34 199 if (hostFs != undefined) {
n@34 200 hostFs = Number(hostFs.value);
n@34 201 }
n@34 202
n@34 203 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@34 204 if (hostFs != undefined) {
n@34 205 if (Number(hostFs) != audioContext.sampleRate) {
n@34 206 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 207 alert(errStr);
n@34 208 return;
n@34 209 }
n@34 210 }
n@34 211 // Find all the audioElements from the audioHolder
n@34 212 var audioElements = $(textXML).find('audioElements');
n@34 213 audioElements.each(function(index,element){
n@34 214 // Find URL of track
n@34 215 // In this jQuery loop, variable 'this' holds the current audioElement.
n@34 216
n@34 217 // Now load each audio sample. First create the new track by passing the full URL
n@34 218 var trackURL = hostURL + this.attributes['url'].value;
n@34 219 audioEngineContext.newTrack(trackURL);
n@34 220 // Create document objects to hold the comment boxes
n@34 221 var trackComment = document.createElement('div');
n@34 222 // Create a string next to each comment asking for a comment
n@34 223 var trackString = document.createElement('span');
n@34 224 trackString.innerHTML = 'Comment on track '+index;
n@34 225 // Create the HTML5 comment box 'textarea'
n@34 226 var trackCommentBox = document.createElement('textarea');
n@34 227 trackCommentBox.rows = '4';
n@34 228 trackCommentBox.cols = '100';
n@34 229 trackCommentBox.name = 'trackComment'+index;
n@34 230 trackCommentBox.className = 'trackComment';
n@34 231 var br = document.createElement('br');
n@34 232 // Add to the holder.
n@34 233 trackComment.appendChild(trackString);
n@34 234 trackComment.appendChild(br);
n@34 235 trackComment.appendChild(trackCommentBox);
n@34 236 feedbackHolder.appendChild(trackComment);
n@34 237
n@34 238 // Create a slider per track
n@34 239
n@34 240 var trackSliderObj = document.createElement('div');
n@34 241 trackSliderObj.className = 'track-slider';
n@34 242 trackSliderObj.id = 'track-slider-'+index;
n@34 243 // Distribute it randomnly
n@34 244 var w = window.innerWidth - 100;
n@34 245 w = Math.random()*w;
n@34 246 trackSliderObj.style.left = Math.floor(w)+50+'px';
n@34 247 trackSliderObj.innerHTML = '<span>'+index+'</span>';
n@34 248 trackSliderObj.draggable = true;
n@34 249 trackSliderObj.ondragend = dragEnd;
n@34 250
n@34 251 // Onclick, switch playback to that track
n@34 252 trackSliderObj.onclick = function() {
n@34 253 // Get the track ID from the object ID
n@34 254 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@34 255 audioEngineContext.selectedTrack(id);
n@34 256 };
n@34 257
n@34 258 canvas.appendChild(trackSliderObj);
n@34 259 });
n@34 260 }
n@34 261
n@34 262 function preTestButtonClick()
n@34 263 {
n@34 264 // Called on click of pre-test button
n@36 265 // Need to find and parse preTest again!
n@36 266 var xmlSetup = projectXML.find('setup');
n@36 267 var preTest = xmlSetup.find('PreTest')[0];
n@36 268 var preTestOption = document.getElementById('preTest');
n@36 269 // Check if current state is a question!
n@36 270 if (preTest.children[this.value].nodeName == 'question') {
n@36 271 var questionId = preTest.children[this.value].attributes['id'].value;
n@36 272 var questionHold = document.createElement('comment');
n@36 273 var questionResponse = document.getElementById(questionId + 'response');
n@36 274 questionHold.id = questionId;
n@36 275 questionHold.innerHTML = questionResponse.value;
n@36 276 preTestQuestions.appendChild(questionHold);
n@36 277 }
n@36 278 this.value++;
n@36 279 if (this.value < preTest.children.length)
n@36 280 {
n@36 281 // More to process
n@36 282 var child = preTest.children[this.value];
n@36 283 if (child.nodeName == 'statement')
n@36 284 {
n@36 285 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@36 286 } else if (child.nodeName == 'question')
n@36 287 {
n@36 288 var textHold = document.createElement('span');
n@36 289 textHold.innerHTML = child.innerHTML;
n@36 290 var textEnter = document.createElement('textarea');
n@36 291 textEnter.id = child.attributes['id'].value + 'response';
n@36 292 var br = document.createElement('br');
n@36 293 preTestOption.innerHTML = null;
n@36 294 preTestOption.appendChild(textHold);
n@36 295 preTestOption.appendChild(br);
n@36 296 preTestOption.appendChild(textEnter);
n@36 297 }
n@36 298 } else {
n@36 299 // Time to clear
n@36 300 preTestHolder.style.zIndex = -1;
n@36 301 preTestHolder.style.visibility = 'hidden';
n@36 302 var blank = document.getElementsByClassName('testHalt')[0];
n@36 303 blank.style.zIndex = -2;
n@36 304 blank.style.visibility = 'hidden';
n@36 305 }
n@36 306 }
n@36 307
n@36 308 function showPopup()
n@36 309 {
n@36 310
n@36 311 }
n@36 312
n@36 313 function hidePopup()
n@36 314 {
n@36 315
n@34 316 }
n@34 317
nicholas@5 318 function dragEnd(ev) {
nicholas@5 319 // Function call when a div has been dropped
n@33 320 var slider = document.getElementById('slider');
nicholas@5 321 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
nicholas@5 322 this.style.left = (ev.x)+'px';
nicholas@5 323 } else {
nicholas@5 324 if (ev.x<50) {
nicholas@5 325 this.style.left = '50px';
nicholas@5 326 } else {
nicholas@5 327 this.style.left = window.innerWidth-50 + 'px';
nicholas@5 328 }
nicholas@5 329 }
nicholas@5 330 }
nicholas@7 331
nicholas@7 332 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 333 function interfaceXMLSave(){
nicholas@7 334 // Create the XML string to be exported with results
nicholas@7 335 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@7 336 var trackSliderObjects = document.getElementsByClassName('track-slider');
nicholas@7 337 var commentObjects = document.getElementsByClassName('trackComment');
nicholas@7 338 var rateMin = 50;
nicholas@7 339 var rateMax = window.innerWidth-50;
nicholas@7 340 for (var i=0; i<trackSliderObjects.length; i++)
nicholas@7 341 {
n@24 342 var trackObj = document.createElement("audioElement");
nicholas@7 343 trackObj.id = i;
n@24 344 trackObj.url = audioEngineContext.audioObjects[i].url;
nicholas@7 345 var slider = document.createElement("Rating");
nicholas@7 346 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
nicholas@7 347 rate = (rate-rateMin)/rateMax;
n@25 348 slider.innerHTML = Math.floor(rate*100);
nicholas@7 349 var comment = document.createElement("Comment");
n@25 350 comment.innerHTML = commentObjects[i].value;
nicholas@7 351 trackObj.appendChild(slider);
nicholas@7 352 trackObj.appendChild(comment);
nicholas@7 353 xmlDoc.appendChild(trackObj);
nicholas@7 354 }
nicholas@7 355
n@23 356 // Append Pre/Post Questions
n@23 357 xmlDoc.appendChild(preTestQuestions);
n@23 358 xmlDoc.appendChild(postTestQuestions);
n@23 359
nicholas@7 360 return xmlDoc;
nicholas@7 361 }
nicholas@7 362