annotate ape.js @ 37:8a78c6d38c5f Dev_main

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