annotate ape.js @ 40:ee5ae5902574 Dev_main

Completed State Machine
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 15:04:53 +0100
parents 6653aaec3650
children 86d332f77174
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@38 16 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
n@38 17 // preTest - In preTest state
n@38 18 // testRun-ID - In test running, test Id number at the end 'testRun-2'
n@38 19 // testRunPost-ID - Post test of test ID
n@38 20 // testRunPre-ID - Pre-test of test ID
n@38 21 // postTest - End of test, final submission!
n@38 22
n@32 23
nicholas@2 24 // Once this is loaded and parsed, begin execution
nicholas@2 25 loadInterface(projectXML);
nicholas@2 26
nicholas@2 27 function loadInterface(xmlDoc) {
nicholas@2 28
n@16 29 // Get the dimensions of the screen available to the page
nicholas@2 30 var width = window.innerWidth;
nicholas@2 31 var height = window.innerHeight;
nicholas@2 32
nicholas@2 33 // The injection point into the HTML page
nicholas@2 34 var insertPoint = document.getElementById("topLevelBody");
n@22 35 var testContent = document.createElement('div');
n@22 36 testContent.id = 'testContent';
nicholas@2 37
nicholas@7 38
nicholas@2 39 // Decode parts of the xmlDoc that are needed
nicholas@2 40 // xmlDoc MUST already be parsed by jQuery!
nicholas@2 41 var xmlSetup = xmlDoc.find('setup');
nicholas@2 42 // Should put in an error function here incase of malprocessed or malformed XML
nicholas@2 43
n@34 44 // Extract the different test XML DOM trees
n@34 45 testXMLSetups = xmlDoc.find('audioHolder');
n@34 46
nicholas@2 47 // Create the top div for the Title element
nicholas@2 48 var titleAttr = xmlSetup[0].attributes['title'];
nicholas@2 49 var title = document.createElement('div');
nicholas@2 50 title.className = "title";
nicholas@2 51 title.align = "center";
nicholas@2 52 var titleSpan = document.createElement('span');
nicholas@2 53
nicholas@2 54 // Set title to that defined in XML, else set to default
nicholas@2 55 if (titleAttr != undefined) {
n@25 56 titleSpan.innerHTML = titleAttr.value;
nicholas@2 57 } else {
n@25 58 titleSpan.innerHTML = 'APE Tool';
nicholas@2 59 }
nicholas@2 60 // Insert the titleSpan element into the title div element.
nicholas@2 61 title.appendChild(titleSpan);
nicholas@2 62
nicholas@7 63 // Store the return URL path in global projectReturn
nicholas@7 64 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
nicholas@7 65
nicholas@7 66 // Create Interface buttons!
nicholas@7 67 var interfaceButtons = document.createElement('div');
nicholas@7 68 interfaceButtons.id = 'interface-buttons';
nicholas@7 69
nicholas@7 70 // MANUAL DOWNLOAD POINT
nicholas@7 71 // If project return is null, this MUST be specified as the location to create the download link
nicholas@7 72 var downloadPoint = document.createElement('div');
nicholas@7 73 downloadPoint.id = 'download-point';
nicholas@7 74
nicholas@7 75 // Create playback start/stop points
nicholas@7 76 var playback = document.createElement("button");
n@25 77 playback.innerHTML = 'Start';
n@16 78 // onclick function. Check if it is playing or not, call the correct function in the
n@16 79 // audioEngine, change the button text to reflect the next state.
nicholas@7 80 playback.onclick = function() {
nicholas@7 81 if (audioEngineContext.status == 0) {
nicholas@7 82 audioEngineContext.play();
n@25 83 this.innerHTML = 'Stop';
nicholas@7 84 } else {
nicholas@7 85 audioEngineContext.stop();
n@25 86 this.innerHTML = 'Start';
nicholas@7 87 }
n@16 88 };
nicholas@7 89 // Create Submit (save) button
nicholas@7 90 var submit = document.createElement("button");
n@25 91 submit.innerHTML = 'Submit';
nicholas@7 92 submit.onclick = function() {
n@15 93 // TODO: Update this for postTest tags
nicholas@7 94 createProjectSave(projectReturn)
n@16 95 };
n@16 96 // Append the interface buttons into the interfaceButtons object.
nicholas@7 97 interfaceButtons.appendChild(playback);
nicholas@7 98 interfaceButtons.appendChild(submit);
nicholas@7 99 interfaceButtons.appendChild(downloadPoint);
nicholas@7 100
nicholas@2 101 // Now create the slider and HTML5 canvas boxes
nicholas@2 102
n@16 103 // Create the div box to center align
nicholas@2 104 var sliderBox = document.createElement('div');
nicholas@2 105 sliderBox.className = 'sliderCanvasDiv';
n@16 106 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 107 sliderBox.align = 'center';
nicholas@2 108
n@16 109 // Create the slider box to hold the slider elements
nicholas@5 110 var canvas = document.createElement('div');
nicholas@2 111 canvas.id = 'slider';
n@16 112 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@5 113 canvas.style.width = width - 100 +"px";
nicholas@5 114 canvas.align = "left";
nicholas@2 115 sliderBox.appendChild(canvas);
n@16 116
n@16 117 // Global parent for the comment boxes on the page
nicholas@3 118 var feedbackHolder = document.createElement('div');
n@34 119 feedbackHolder.id = 'feedbackHolder';
nicholas@2 120
n@38 121 testContent.style.zIndex = 1;
n@38 122 insertPoint.innerHTML = null; // Clear the current schema
n@38 123
n@22 124 // Create pre and post test questions
n@38 125 var blank = document.createElement('div');
n@38 126 blank.className = 'testHalt';
n@22 127
n@38 128 var popupHolder = document.createElement('div');
n@38 129 popupHolder.id = 'popupHolder';
n@38 130 popupHolder.className = 'popupHolder';
n@38 131 popupHolder.style.position = 'absolute';
n@38 132 popupHolder.style.left = (window.innerWidth/2)-250 + 'px';
n@38 133 popupHolder.style.top = (window.innerHeight/2)-125 + 'px';
n@38 134 insertPoint.appendChild(popupHolder);
n@38 135 insertPoint.appendChild(blank);
n@38 136 hidePopup();
n@22 137
n@36 138 var preTest = xmlSetup.find('PreTest');
n@36 139 var postTest = xmlSetup.find('PostTest');
n@22 140 preTest = preTest[0];
n@22 141 postTest = postTest[0];
n@38 142
n@38 143 currentState = 'preTest';
n@22 144
n@22 145 // Create Pre-Test Box
n@22 146 if (preTest != undefined && preTest.children.length >= 1)
n@22 147 {
n@38 148 showPopup();
n@39 149 preTestPopupStart(preTest);
n@22 150 }
n@38 151
n@38 152 // Inject into HTML
n@38 153 testContent.appendChild(title); // Insert the title
n@38 154 testContent.appendChild(interfaceButtons);
n@38 155 testContent.appendChild(sliderBox);
n@38 156 testContent.appendChild(feedbackHolder);
n@38 157 insertPoint.appendChild(testContent);
n@22 158
n@36 159 // Load the full interface
n@39 160
nicholas@2 161 }
nicholas@5 162
n@39 163 function loadTest(id)
n@34 164 {
n@34 165 // Used to load a specific test page
n@39 166 var textXML = testXMLSetups[id];
n@34 167
n@34 168 var feedbackHolder = document.getElementById('feedbackHolder');
n@34 169 var canvas = document.getElementById('slider');
n@34 170 feedbackHolder.innerHTML = null;
n@34 171 canvas.innerHTML = null;
n@34 172
n@34 173 // Extract the hostURL attribute. If not set, create an empty string.
n@34 174 var hostURL = textXML.attributes['hostURL'];
n@34 175 if (hostURL == undefined) {
n@34 176 hostURL = "";
n@34 177 } else {
n@34 178 hostURL = hostURL.value;
n@34 179 }
n@34 180 // Extract the sampleRate. If set, convert the string to a Number.
n@34 181 var hostFs = textXML.attributes['sampleRate'];
n@34 182 if (hostFs != undefined) {
n@34 183 hostFs = Number(hostFs.value);
n@34 184 }
n@34 185
n@34 186 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@34 187 if (hostFs != undefined) {
n@34 188 if (Number(hostFs) != audioContext.sampleRate) {
n@34 189 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@34 190 alert(errStr);
n@34 191 return;
n@34 192 }
n@34 193 }
n@34 194 // Find all the audioElements from the audioHolder
n@34 195 var audioElements = $(textXML).find('audioElements');
n@34 196 audioElements.each(function(index,element){
n@34 197 // Find URL of track
n@34 198 // In this jQuery loop, variable 'this' holds the current audioElement.
n@34 199
n@34 200 // Now load each audio sample. First create the new track by passing the full URL
n@34 201 var trackURL = hostURL + this.attributes['url'].value;
n@34 202 audioEngineContext.newTrack(trackURL);
n@34 203 // Create document objects to hold the comment boxes
n@34 204 var trackComment = document.createElement('div');
n@37 205 trackComment.className = 'comment-div';
n@34 206 // Create a string next to each comment asking for a comment
n@34 207 var trackString = document.createElement('span');
n@34 208 trackString.innerHTML = 'Comment on track '+index;
n@34 209 // Create the HTML5 comment box 'textarea'
n@34 210 var trackCommentBox = document.createElement('textarea');
n@34 211 trackCommentBox.rows = '4';
n@34 212 trackCommentBox.cols = '100';
n@34 213 trackCommentBox.name = 'trackComment'+index;
n@34 214 trackCommentBox.className = 'trackComment';
n@34 215 var br = document.createElement('br');
n@34 216 // Add to the holder.
n@34 217 trackComment.appendChild(trackString);
n@34 218 trackComment.appendChild(br);
n@34 219 trackComment.appendChild(trackCommentBox);
n@34 220 feedbackHolder.appendChild(trackComment);
n@34 221
n@34 222 // Create a slider per track
n@34 223
n@34 224 var trackSliderObj = document.createElement('div');
n@34 225 trackSliderObj.className = 'track-slider';
n@34 226 trackSliderObj.id = 'track-slider-'+index;
n@34 227 // Distribute it randomnly
n@34 228 var w = window.innerWidth - 100;
n@34 229 w = Math.random()*w;
n@34 230 trackSliderObj.style.left = Math.floor(w)+50+'px';
n@34 231 trackSliderObj.innerHTML = '<span>'+index+'</span>';
n@34 232 trackSliderObj.draggable = true;
n@34 233 trackSliderObj.ondragend = dragEnd;
n@34 234
n@34 235 // Onclick, switch playback to that track
n@34 236 trackSliderObj.onclick = function() {
n@34 237 // Get the track ID from the object ID
n@34 238 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@34 239 audioEngineContext.selectedTrack(id);
n@34 240 };
n@34 241
n@34 242 canvas.appendChild(trackSliderObj);
n@34 243 });
n@39 244
n@39 245 // Now process any pre-test commands
n@39 246
n@39 247 var preTest = testXMLSetups.find('PreTest')[0];
n@39 248 if (preTest.children.length > 0)
n@39 249 {
n@39 250 currentState = 'testRunPre-'+id;
n@39 251 preTestPopupStart(preTest);
n@39 252 showPopup();
n@39 253 } else {
n@39 254 currentState = 'testRun-'+id;
n@39 255 }
n@39 256 }
n@39 257
n@39 258 function preTestPopupStart(preTest)
n@39 259 {
n@39 260 var popupHolder = document.getElementById('popupHolder');
n@39 261 popupHolder.innerHTML = null;
n@39 262 // Parse the first box
n@39 263 var preTestOption = document.createElement('div');
n@39 264 preTestOption.id = 'preTest';
n@39 265 preTestOption.style.marginTop = '25px';
n@39 266 preTestOption.align = "center";
n@39 267 var child = preTest.children[0];
n@39 268 if (child.nodeName == 'statement')
n@39 269 {
n@39 270 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@39 271 } else if (child.nodeName == 'question')
n@39 272 {
n@39 273 var questionId = child.attributes['id'].value;
n@39 274 var textHold = document.createElement('span');
n@39 275 textHold.innerHTML = child.innerHTML;
n@39 276 textHold.id = questionId + 'response';
n@39 277 var textEnter = document.createElement('textarea');
n@39 278 preTestOption.appendChild(textHold);
n@39 279 preTestOption.appendChild(textEnter);
n@39 280 }
n@39 281 var nextButton = document.createElement('button');
n@39 282 nextButton.className = 'popupButton';
n@39 283 nextButton.value = '0';
n@39 284 nextButton.innerHTML = 'Next';
n@39 285 nextButton.onclick = popupButtonClick;
n@39 286
n@39 287 popupHolder.appendChild(preTestOption);
n@39 288 popupHolder.appendChild(nextButton);
n@34 289 }
n@34 290
n@38 291 function popupButtonClick()
n@38 292 {
n@38 293 // Global call from the 'Next' button click
n@38 294 if (currentState == 'preTest')
n@38 295 {
n@38 296 // At the start of the preTest routine!
n@39 297 var xmlTree = projectXML.find('setup');
n@39 298 var preTest = xmlTree.find('PreTest')[0];
n@39 299 this.value = preTestButtonClick(preTest,this.value);
n@39 300 } else if (currentState.substr(0,10) == 'testRunPre')
n@39 301 {
n@39 302 //Specific test pre-test
n@39 303 var testId = currentState.substr(11,currentState.length-10);
n@39 304 var preTest = testXMLSetups.find('PreTest')[testId];
n@39 305 this.value = preTestButtonClick(preTest,this.value);
n@38 306 }
n@38 307 }
n@38 308
n@39 309 function preTestButtonClick(preTest,index)
n@34 310 {
n@34 311 // Called on click of pre-test button
n@36 312 // Need to find and parse preTest again!
n@36 313 var preTestOption = document.getElementById('preTest');
n@36 314 // Check if current state is a question!
n@38 315 if (preTest.children[index].nodeName == 'question') {
n@38 316 var questionId = preTest.children[index].attributes['id'].value;
n@36 317 var questionHold = document.createElement('comment');
n@36 318 var questionResponse = document.getElementById(questionId + 'response');
n@36 319 questionHold.id = questionId;
n@36 320 questionHold.innerHTML = questionResponse.value;
n@36 321 preTestQuestions.appendChild(questionHold);
n@36 322 }
n@38 323 index++;
n@38 324 if (index < preTest.children.length)
n@36 325 {
n@36 326 // More to process
n@38 327 var child = preTest.children[index];
n@36 328 if (child.nodeName == 'statement')
n@36 329 {
n@36 330 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@36 331 } else if (child.nodeName == 'question')
n@36 332 {
n@36 333 var textHold = document.createElement('span');
n@36 334 textHold.innerHTML = child.innerHTML;
n@36 335 var textEnter = document.createElement('textarea');
n@36 336 textEnter.id = child.attributes['id'].value + 'response';
n@36 337 var br = document.createElement('br');
n@36 338 preTestOption.innerHTML = null;
n@36 339 preTestOption.appendChild(textHold);
n@36 340 preTestOption.appendChild(br);
n@36 341 preTestOption.appendChild(textEnter);
n@36 342 }
n@36 343 } else {
n@36 344 // Time to clear
n@37 345 preTestOption.innerHTML = null;
n@37 346 hidePopup();
n@38 347 // Progress the state!
n@39 348 advanceState();
n@36 349 }
n@38 350 return index;
n@36 351 }
n@36 352
n@36 353 function showPopup()
n@36 354 {
n@37 355 var popupHolder = document.getElementById('popupHolder');
n@38 356 popupHolder.style.zIndex = 3;
n@37 357 popupHolder.style.visibility = 'visible';
n@37 358 var blank = document.getElementsByClassName('testHalt')[0];
n@37 359 blank.style.zIndex = 2;
n@37 360 blank.style.visibility = 'visible';
n@36 361 }
n@36 362
n@36 363 function hidePopup()
n@36 364 {
n@37 365 var popupHolder = document.getElementById('popupHolder');
n@37 366 popupHolder.style.zIndex = -1;
n@37 367 popupHolder.style.visibility = 'hidden';
n@37 368 var blank = document.getElementsByClassName('testHalt')[0];
n@37 369 blank.style.zIndex = -2;
n@37 370 blank.style.visibility = 'hidden';
n@34 371 }
n@34 372
nicholas@5 373 function dragEnd(ev) {
nicholas@5 374 // Function call when a div has been dropped
n@33 375 var slider = document.getElementById('slider');
nicholas@5 376 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
nicholas@5 377 this.style.left = (ev.x)+'px';
nicholas@5 378 } else {
nicholas@5 379 if (ev.x<50) {
nicholas@5 380 this.style.left = '50px';
nicholas@5 381 } else {
nicholas@5 382 this.style.left = window.innerWidth-50 + 'px';
nicholas@5 383 }
nicholas@5 384 }
nicholas@5 385 }
nicholas@7 386
n@39 387 function advanceState()
n@39 388 {
n@39 389 console.log(currentState);
n@39 390 if (currentState == 'preTest')
n@39 391 {
n@39 392 // End of pre-test, begin the test
n@39 393 loadTest(0);
n@39 394 } else if (currentState.substr(0,10) == 'testRunPre')
n@39 395 {
n@39 396 // Start the test
n@39 397 var testId = currentState.substr(11,currentState.length-10);
n@39 398 currentState = 'testRun-'+testId;
n@40 399 } else if (currentState.substr(0,7) == 'testRun')
n@40 400 {
n@40 401 var testId = currentState.substr(8,currentState.length-7);
n@40 402 // Check if we have any post tests to perform
n@40 403 var postXML = testXMLSetups.find('PostTest')[testId];
n@40 404 if (postXML.children.length > 0)
n@40 405 {
n@40 406 console.log('POST TEST')
n@40 407 }
n@40 408 // else
n@40 409
n@40 410
n@40 411 // No post tests, check if we have another test to perform instead
n@40 412 if (testXMLSetups.length-1 > testId)
n@40 413 {
n@40 414 // Yes we have another test to perform
n@40 415 currentState = 'testRun-'+Number(testId)+1;
n@40 416 loadTest(testId+1);
n@40 417 } else {
n@40 418 console.log('Testing Completed!');
n@40 419 // Check for any post tests
n@40 420 var xmlSetup = projectXML.find('setup');
n@40 421 var postTest = xmlSetup.find('PostTest');
n@40 422 console.log(postTest);
n@40 423 }
n@39 424 }
n@39 425 console.log(currentState);
n@39 426 }
n@39 427
n@40 428 function buttonSubmitClick()
n@40 429 {
n@40 430 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
n@40 431 if (currentState.substr(0,7) == 'testRun')
n@40 432 {
n@40 433 advanceState();
n@40 434 }
n@40 435 }
n@40 436
nicholas@7 437 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 438 function interfaceXMLSave(){
nicholas@7 439 // Create the XML string to be exported with results
nicholas@7 440 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@7 441 var trackSliderObjects = document.getElementsByClassName('track-slider');
nicholas@7 442 var commentObjects = document.getElementsByClassName('trackComment');
nicholas@7 443 var rateMin = 50;
nicholas@7 444 var rateMax = window.innerWidth-50;
nicholas@7 445 for (var i=0; i<trackSliderObjects.length; i++)
nicholas@7 446 {
n@24 447 var trackObj = document.createElement("audioElement");
nicholas@7 448 trackObj.id = i;
n@24 449 trackObj.url = audioEngineContext.audioObjects[i].url;
nicholas@7 450 var slider = document.createElement("Rating");
nicholas@7 451 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
nicholas@7 452 rate = (rate-rateMin)/rateMax;
n@25 453 slider.innerHTML = Math.floor(rate*100);
nicholas@7 454 var comment = document.createElement("Comment");
n@25 455 comment.innerHTML = commentObjects[i].value;
nicholas@7 456 trackObj.appendChild(slider);
nicholas@7 457 trackObj.appendChild(comment);
nicholas@7 458 xmlDoc.appendChild(trackObj);
nicholas@7 459 }
nicholas@7 460
n@23 461 // Append Pre/Post Questions
n@23 462 xmlDoc.appendChild(preTestQuestions);
n@23 463 xmlDoc.appendChild(postTestQuestions);
n@23 464
nicholas@7 465 return xmlDoc;
nicholas@7 466 }
nicholas@7 467