annotate ape.js @ 661:97ade670d7c1

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