annotate ape.js @ 656:0a401224660b

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