annotate ape.js @ 668:6455bee85090

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