annotate ape.js @ 667:2cc25941c433

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