annotate ape.js @ 33:c61a92659541 Dev_main

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