annotate ape.js @ 43:2fd137d891a7 Dev_main

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