annotate ape.js @ 25:8337908b1a2a

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