annotate ape.js @ 657:1e64848f5940

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