annotate ape.js @ 44:c57aba852764 Dev_main

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