annotate ape.js @ 32:2b9bcac771a8 Dev_main

Added dev-main branch warning at top of files
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 10:25:52 +0100
parents 8337908b1a2a
children c61a92659541
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 // Set background to grey #ddd
nicholas@2 27 document.getElementsByTagName('body')[0].style.backgroundColor = '#ddd';
nicholas@2 28
nicholas@2 29 // The injection point into the HTML page
nicholas@2 30 var insertPoint = document.getElementById("topLevelBody");
n@22 31 var testContent = document.createElement('div');
n@22 32 testContent.id = 'testContent';
nicholas@2 33
nicholas@7 34
nicholas@2 35 // Decode parts of the xmlDoc that are needed
nicholas@2 36 // xmlDoc MUST already be parsed by jQuery!
nicholas@2 37 var xmlSetup = xmlDoc.find('setup');
nicholas@2 38 // Should put in an error function here incase of malprocessed or malformed XML
nicholas@2 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.style.height = 150 + "px";
n@16 108 canvas.style.marginBottom = "25px";
nicholas@2 109 canvas.style.backgroundColor = '#eee';
nicholas@5 110 canvas.align = "left";
nicholas@2 111 sliderBox.appendChild(canvas);
n@16 112
n@16 113 // Global parent for the comment boxes on the page
nicholas@3 114 var feedbackHolder = document.createElement('div');
n@16 115 // Find the parent audioHolder object.
n@16 116 var audioHolder = xmlDoc.find('audioHolder');
n@16 117 audioHolder = audioHolder[0]; // Remove from one field array
n@16 118 // Extract the hostURL attribute. If not set, create an empty string.
n@16 119 var hostURL = audioHolder.attributes['hostURL'];
nicholas@7 120 if (hostURL == undefined) {
nicholas@7 121 hostURL = "";
nicholas@7 122 } else {
nicholas@7 123 hostURL = hostURL.value;
nicholas@7 124 }
n@16 125 // Extract the sampleRate. If set, convert the string to a Number.
n@16 126 var hostFs = audioHolder.attributes['sampleRate'];
n@15 127 if (hostFs != undefined) {
n@15 128 hostFs = Number(hostFs.value);
nicholas@7 129 }
nicholas@7 130
nicholas@7 131 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@15 132 if (hostFs != undefined) {
nicholas@7 133 if (Number(hostFs) != audioContext.sampleRate) {
nicholas@7 134 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nicholas@7 135 alert(errStr);
nicholas@7 136 return;
nicholas@7 137 }
nicholas@7 138 }
n@16 139 // Find all the audioElements from the audioHolder
n@16 140 var audioElements = $(audioHolder).find('audioElements');
n@16 141 audioElements.each(function(index,element){
nicholas@7 142 // Find URL of track
n@16 143 // In this jQuery loop, variable 'this' holds the current audioElement.
n@16 144
n@16 145 // Now load each audio sample. First create the new track by passing the full URL
nicholas@7 146 var trackURL = hostURL + this.attributes['url'].value;
nicholas@7 147 audioEngineContext.newTrack(trackURL);
n@16 148 // Create document objects to hold the comment boxes
n@16 149 var trackComment = document.createElement('div');
n@16 150 // Create a string next to each comment asking for a comment
n@16 151 var trackString = document.createElement('span');
n@25 152 trackString.innerHTML = 'Comment on track '+index;
n@16 153 // Create the HTML5 comment box 'textarea'
n@16 154 var trackCommentBox = document.createElement('textarea');
n@16 155 trackCommentBox.rows = '4';
n@16 156 trackCommentBox.cols = '100';
n@16 157 trackCommentBox.name = 'trackComment'+index;
n@16 158 trackCommentBox.className = 'trackComment';
n@16 159 // Add to the holder.
n@16 160 trackComment.appendChild(trackString);
n@16 161 trackComment.appendChild(trackCommentBox);
n@16 162 feedbackHolder.appendChild(trackComment);
n@16 163
nicholas@5 164 // Create a slider per track
nicholas@5 165
nicholas@5 166 var trackSliderObj = document.createElement('div');
nicholas@5 167 trackSliderObj.className = 'track-slider';
nicholas@5 168 trackSliderObj.id = 'track-slider-'+index;
nicholas@5 169 trackSliderObj.style.position = 'absolute';
nicholas@5 170 // Distribute it randomnly
nicholas@5 171 var w = window.innerWidth - 100;
nicholas@5 172 w = Math.random()*w;
nicholas@5 173 trackSliderObj.style.left = Math.floor(w)+50+'px';
nicholas@5 174 trackSliderObj.style.height = "150px";
nicholas@5 175 trackSliderObj.style.width = "10px";
nicholas@5 176 trackSliderObj.style.backgroundColor = 'rgb(100,200,100)';
nicholas@5 177 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nicholas@5 178 trackSliderObj.style.float = "left";
nicholas@5 179 trackSliderObj.draggable = true;
nicholas@5 180 trackSliderObj.ondragend = dragEnd;
nicholas@8 181
nicholas@8 182 // Onclick, switch playback to that track
nicholas@8 183 trackSliderObj.onclick = function() {
nicholas@8 184 // Get the track ID from the object ID
nicholas@8 185 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
nicholas@8 186 audioEngineContext.selectedTrack(id);
n@15 187 };
nicholas@8 188
nicholas@5 189 canvas.appendChild(trackSliderObj);
n@15 190 });
nicholas@3 191
nicholas@2 192
n@22 193 // Create pre and post test questions
n@22 194
nicholas@2 195 // Inject into HTML
nicholas@2 196 insertPoint.innerHTML = null; // Clear the current schema
n@22 197 testContent.appendChild(title); // Insert the title
n@22 198 testContent.appendChild(interfaceButtons);
n@22 199 testContent.appendChild(sliderBox);
n@22 200 testContent.appendChild(feedbackHolder);
n@22 201 insertPoint.appendChild(testContent);
n@22 202
n@22 203 var preTest = xmlDoc.find('PreTest');
n@22 204 var postTest = xmlDoc.find('PostTest');
n@22 205 preTest = preTest[0];
n@22 206 postTest = postTest[0];
n@22 207 if (preTest != undefined || postTest != undefined)
n@22 208 {
n@22 209 testContent.style.zIndex = 1;
n@22 210 var blank = document.createElement('div');
n@22 211 blank.id = 'testHalt';
n@22 212 blank.style.zIndex = 2;
n@22 213 blank.style.width = window.innerWidth + 'px';
n@22 214 blank.style.height = window.innerHeight + 'px';
n@22 215 blank.style.position = 'absolute';
n@22 216 blank.style.top = '0';
n@22 217 blank.style.left = '0';
n@22 218 insertPoint.appendChild(blank);
n@22 219 }
n@22 220
n@22 221 // Create Pre-Test Box
n@22 222 if (preTest != undefined && preTest.children.length >= 1)
n@22 223 {
n@22 224
n@22 225 var preTestHolder = document.createElement('div');
n@22 226 preTestHolder.id = 'preTestHolder';
n@22 227 preTestHolder.style.zIndex = 2;
n@22 228 preTestHolder.style.width = '500px';
n@22 229 preTestHolder.style.height = '250px';
n@22 230 preTestHolder.style.backgroundColor = '#fff';
n@22 231 preTestHolder.style.position = 'absolute';
n@22 232 preTestHolder.style.left = (window.innerWidth/2)-250 + 'px';
n@22 233 preTestHolder.style.top = (window.innerHeight/2)-125 + 'px';
n@22 234 // Parse the first box
n@22 235 var preTestOption = document.createElement('div');
n@22 236 preTestOption.id = 'preTest';
n@22 237 preTestOption.style.marginTop = '25px';
n@22 238 preTestOption.align = "center";
n@22 239 var child = preTest.children[0];
n@22 240 if (child.nodeName == 'statement')
n@22 241 {
n@22 242 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@22 243 } else if (child.nodeName == 'question')
n@22 244 {
n@23 245 var questionId = child.attributes['id'].value;
n@22 246 var textHold = document.createElement('span');
n@22 247 textHold.innerHTML = child.innerHTML;
n@23 248 textHold.id = questionId + 'response';
n@22 249 var textEnter = document.createElement('textarea');
n@22 250 preTestOption.appendChild(textHold);
n@22 251 preTestOption.appendChild(textEnter);
n@22 252 }
n@22 253 var nextButton = document.createElement('button');
n@22 254 nextButton.id = 'preTestNext';
n@22 255 nextButton.value = '1';
n@22 256 nextButton.innerHTML = 'next';
n@22 257 nextButton.style.position = 'relative';
n@22 258 nextButton.style.left = '450px';
n@22 259 nextButton.style.top = '175px';
n@22 260 nextButton.onclick = function() {
n@22 261 // Need to find and parse preTest again!
n@22 262 var preTest = projectXML.find('PreTest')[0];
n@23 263 // Check if current state is a question!
n@23 264 if (preTest.children[this.value-1].nodeName == 'question') {
n@23 265 var questionId = preTest.children[this.value-1].attributes['id'].value;
n@23 266 var questionHold = document.createElement('comment');
n@23 267 var questionResponse = document.getElementById(questionId + 'response');
n@23 268 questionHold.id = questionId;
n@23 269 questionHold.innerHTML = questionResponse.value;
n@23 270 preTestQuestions.appendChild(questionHold);
n@23 271 }
n@22 272 if (this.value < preTest.children.length)
n@22 273 {
n@22 274 // More to process
n@22 275 var child = preTest.children[this.value];
n@22 276 if (child.nodeName == 'statement')
n@22 277 {
n@22 278 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@22 279 } else if (child.nodeName == 'question')
n@22 280 {
n@22 281 var textHold = document.createElement('span');
n@22 282 textHold.innerHTML = child.innerHTML;
n@22 283 var textEnter = document.createElement('textarea');
n@23 284 textEnter.id = child.attributes['id'].value + 'response';
n@23 285 preTestOption.innerHTML = null;
n@22 286 preTestOption.appendChild(textHold);
n@22 287 preTestOption.appendChild(textEnter);
n@22 288 }
n@22 289 } else {
n@22 290 // Time to clear
n@22 291 preTestHolder.style.zIndex = -1;
n@22 292 preTestHolder.style.visibility = 'hidden';
n@22 293 var blank = document.getElementById('testHalt');
n@22 294 blank.style.zIndex = -2;
n@22 295 blank.style.visibility = 'hidden';
n@22 296 }
n@22 297 this.value++;
n@22 298 };
n@22 299
n@22 300 preTestHolder.appendChild(preTestOption);
n@22 301 preTestHolder.appendChild(nextButton);
n@22 302 insertPoint.appendChild(preTestHolder);
n@22 303 }
n@22 304
nicholas@2 305 }
nicholas@5 306
nicholas@5 307 function dragEnd(ev) {
nicholas@5 308 // Function call when a div has been dropped
nicholas@5 309 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
nicholas@5 310 this.style.left = (ev.x)+'px';
nicholas@5 311 } else {
nicholas@5 312 if (ev.x<50) {
nicholas@5 313 this.style.left = '50px';
nicholas@5 314 } else {
nicholas@5 315 this.style.left = window.innerWidth-50 + 'px';
nicholas@5 316 }
nicholas@5 317 }
nicholas@5 318 }
nicholas@7 319
nicholas@7 320 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 321 function interfaceXMLSave(){
nicholas@7 322 // Create the XML string to be exported with results
nicholas@7 323 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@7 324 var trackSliderObjects = document.getElementsByClassName('track-slider');
nicholas@7 325 var commentObjects = document.getElementsByClassName('trackComment');
nicholas@7 326 var rateMin = 50;
nicholas@7 327 var rateMax = window.innerWidth-50;
nicholas@7 328 for (var i=0; i<trackSliderObjects.length; i++)
nicholas@7 329 {
n@24 330 var trackObj = document.createElement("audioElement");
nicholas@7 331 trackObj.id = i;
n@24 332 trackObj.url = audioEngineContext.audioObjects[i].url;
nicholas@7 333 var slider = document.createElement("Rating");
nicholas@7 334 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
nicholas@7 335 rate = (rate-rateMin)/rateMax;
n@25 336 slider.innerHTML = Math.floor(rate*100);
nicholas@7 337 var comment = document.createElement("Comment");
n@25 338 comment.innerHTML = commentObjects[i].value;
nicholas@7 339 trackObj.appendChild(slider);
nicholas@7 340 trackObj.appendChild(comment);
nicholas@7 341 xmlDoc.appendChild(trackObj);
nicholas@7 342 }
nicholas@7 343
n@23 344 // Append Pre/Post Questions
n@23 345 xmlDoc.appendChild(preTestQuestions);
n@23 346 xmlDoc.appendChild(postTestQuestions);
n@23 347
nicholas@7 348 return xmlDoc;
nicholas@7 349 }
nicholas@7 350