annotate ape.js @ 42:b163842c6002 Dev_main

Really complete state machine with post test commands
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 10 Apr 2015 15:21:49 +0100
parents 86d332f77174
children 2fd137d891a7
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@42 306 } else if (currentState.substr(0,11) == 'testRunPost')
n@42 307 {
n@42 308 // Specific test post-test
n@42 309 var testId = currentState.substr(12,currentState.length-11);
n@42 310 var preTest = testXMLSetups.find('PostTest')[testId];
n@42 311 this.value = preTestButtonClick(preTest,this.value);
n@41 312 } else if (currentState == 'postTest')
n@41 313 {
n@41 314 // At the end of the test, running global post test
n@41 315 var xmlTree = projectXML.find('setup');
n@41 316 var PostTest = xmlTree.find('PostTest')[0];
n@41 317 this.value = preTestButtonClick(PostTest,this.value);
n@38 318 }
n@38 319 }
n@38 320
n@39 321 function preTestButtonClick(preTest,index)
n@34 322 {
n@34 323 // Called on click of pre-test button
n@36 324 // Need to find and parse preTest again!
n@36 325 var preTestOption = document.getElementById('preTest');
n@36 326 // Check if current state is a question!
n@38 327 if (preTest.children[index].nodeName == 'question') {
n@38 328 var questionId = preTest.children[index].attributes['id'].value;
n@36 329 var questionHold = document.createElement('comment');
n@36 330 var questionResponse = document.getElementById(questionId + 'response');
n@36 331 questionHold.id = questionId;
n@36 332 questionHold.innerHTML = questionResponse.value;
n@36 333 preTestQuestions.appendChild(questionHold);
n@36 334 }
n@38 335 index++;
n@38 336 if (index < preTest.children.length)
n@36 337 {
n@36 338 // More to process
n@38 339 var child = preTest.children[index];
n@36 340 if (child.nodeName == 'statement')
n@36 341 {
n@36 342 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@36 343 } else if (child.nodeName == 'question')
n@36 344 {
n@36 345 var textHold = document.createElement('span');
n@36 346 textHold.innerHTML = child.innerHTML;
n@36 347 var textEnter = document.createElement('textarea');
n@36 348 textEnter.id = child.attributes['id'].value + 'response';
n@36 349 var br = document.createElement('br');
n@36 350 preTestOption.innerHTML = null;
n@36 351 preTestOption.appendChild(textHold);
n@36 352 preTestOption.appendChild(br);
n@36 353 preTestOption.appendChild(textEnter);
n@36 354 }
n@36 355 } else {
n@36 356 // Time to clear
n@37 357 preTestOption.innerHTML = null;
n@37 358 hidePopup();
n@38 359 // Progress the state!
n@39 360 advanceState();
n@36 361 }
n@38 362 return index;
n@36 363 }
n@36 364
n@36 365 function showPopup()
n@36 366 {
n@37 367 var popupHolder = document.getElementById('popupHolder');
n@38 368 popupHolder.style.zIndex = 3;
n@37 369 popupHolder.style.visibility = 'visible';
n@37 370 var blank = document.getElementsByClassName('testHalt')[0];
n@37 371 blank.style.zIndex = 2;
n@37 372 blank.style.visibility = 'visible';
n@36 373 }
n@36 374
n@36 375 function hidePopup()
n@36 376 {
n@37 377 var popupHolder = document.getElementById('popupHolder');
n@37 378 popupHolder.style.zIndex = -1;
n@37 379 popupHolder.style.visibility = 'hidden';
n@37 380 var blank = document.getElementsByClassName('testHalt')[0];
n@37 381 blank.style.zIndex = -2;
n@37 382 blank.style.visibility = 'hidden';
n@34 383 }
n@34 384
nicholas@5 385 function dragEnd(ev) {
nicholas@5 386 // Function call when a div has been dropped
n@33 387 var slider = document.getElementById('slider');
nicholas@5 388 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
nicholas@5 389 this.style.left = (ev.x)+'px';
nicholas@5 390 } else {
nicholas@5 391 if (ev.x<50) {
nicholas@5 392 this.style.left = '50px';
nicholas@5 393 } else {
nicholas@5 394 this.style.left = window.innerWidth-50 + 'px';
nicholas@5 395 }
nicholas@5 396 }
nicholas@5 397 }
nicholas@7 398
n@39 399 function advanceState()
n@39 400 {
n@39 401 console.log(currentState);
n@39 402 if (currentState == 'preTest')
n@39 403 {
n@39 404 // End of pre-test, begin the test
n@39 405 loadTest(0);
n@39 406 } else if (currentState.substr(0,10) == 'testRunPre')
n@39 407 {
n@39 408 // Start the test
n@39 409 var testId = currentState.substr(11,currentState.length-10);
n@39 410 currentState = 'testRun-'+testId;
n@42 411 } else if (currentState.substr(0,11) == 'testRunPost')
n@42 412 {
n@42 413 testEnded(testId);
n@40 414 } else if (currentState.substr(0,7) == 'testRun')
n@40 415 {
n@40 416 var testId = currentState.substr(8,currentState.length-7);
n@40 417 // Check if we have any post tests to perform
n@40 418 var postXML = testXMLSetups.find('PostTest')[testId];
n@40 419 if (postXML.children.length > 0)
n@40 420 {
n@42 421 currentState = 'testRunPost-'+testId;
n@42 422 showPopup();
n@42 423 preTestPopupStart(postXML);
n@40 424 }
n@42 425 else {
n@40 426
n@40 427
n@42 428 // No post tests, check if we have another test to perform instead
n@42 429 testEnded(testId);
n@40 430 }
n@39 431 }
n@39 432 console.log(currentState);
n@39 433 }
n@39 434
n@42 435 function testEnded(testId)
n@42 436 {
n@42 437 if (testXMLSetups.length-1 > testId)
n@42 438 {
n@42 439 // Yes we have another test to perform
n@42 440 currentState = 'testRun-'+Number(testId)+1;
n@42 441 loadTest(testId+1);
n@42 442 } else {
n@42 443 console.log('Testing Completed!');
n@42 444 currentState = 'postTest';
n@42 445 // Check for any post tests
n@42 446 var xmlSetup = projectXML.find('setup');
n@42 447 var postTest = xmlSetup.find('PostTest')[0];
n@42 448 showPopup();
n@42 449 preTestPopupStart(postTest);
n@42 450 }
n@42 451 }
n@42 452
n@40 453 function buttonSubmitClick()
n@40 454 {
n@40 455 // 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 456 if (currentState.substr(0,7) == 'testRun')
n@40 457 {
n@40 458 advanceState();
n@40 459 }
n@40 460 }
n@40 461
nicholas@7 462 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 463 function interfaceXMLSave(){
nicholas@7 464 // Create the XML string to be exported with results
nicholas@7 465 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@7 466 var trackSliderObjects = document.getElementsByClassName('track-slider');
nicholas@7 467 var commentObjects = document.getElementsByClassName('trackComment');
nicholas@7 468 var rateMin = 50;
nicholas@7 469 var rateMax = window.innerWidth-50;
nicholas@7 470 for (var i=0; i<trackSliderObjects.length; i++)
nicholas@7 471 {
n@24 472 var trackObj = document.createElement("audioElement");
nicholas@7 473 trackObj.id = i;
n@24 474 trackObj.url = audioEngineContext.audioObjects[i].url;
nicholas@7 475 var slider = document.createElement("Rating");
nicholas@7 476 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
nicholas@7 477 rate = (rate-rateMin)/rateMax;
n@25 478 slider.innerHTML = Math.floor(rate*100);
nicholas@7 479 var comment = document.createElement("Comment");
n@25 480 comment.innerHTML = commentObjects[i].value;
nicholas@7 481 trackObj.appendChild(slider);
nicholas@7 482 trackObj.appendChild(comment);
nicholas@7 483 xmlDoc.appendChild(trackObj);
nicholas@7 484 }
nicholas@7 485
n@23 486 // Append Pre/Post Questions
n@23 487 xmlDoc.appendChild(preTestQuestions);
n@23 488 xmlDoc.appendChild(postTestQuestions);
n@23 489
nicholas@7 490 return xmlDoc;
nicholas@7 491 }
nicholas@7 492