annotate ape.js @ 989:33f2782341fb

Added interface2.png
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 27 Apr 2015 16:49:32 +0100
parents 62b638aa1849
children 902f22e182f6
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@662 6 var currentState; // Keep track of the current state (pre/post test, which test, final test? first test?)
n@662 7 // preTest - In preTest state
n@662 8 // testRun-ID - In test running, test Id number at the end 'testRun-2'
n@662 9 // testRunPost-ID - Post test of test ID
n@662 10 // testRunPre-ID - Pre-test of test ID
n@662 11 // postTest - End of test, final submission!
n@662 12
n@656 13
nicholas@2 14 // Once this is loaded and parsed, begin execution
nicholas@2 15 loadInterface(projectXML);
nicholas@2 16
nicholas@2 17 function loadInterface(xmlDoc) {
nicholas@2 18
n@701 19 // Get the dimensions of the screen available to the page
nicholas@2 20 var width = window.innerWidth;
nicholas@2 21 var height = window.innerHeight;
nicholas@2 22
nicholas@2 23 // The injection point into the HTML page
nicholas@2 24 var insertPoint = document.getElementById("topLevelBody");
n@706 25 var testContent = document.createElement('div');
n@706 26 testContent.id = 'testContent';
nicholas@2 27
nicholas@7 28
nicholas@2 29 // Decode parts of the xmlDoc that are needed
nicholas@2 30 // xmlDoc MUST already be parsed by jQuery!
nicholas@2 31 var xmlSetup = xmlDoc.find('setup');
nicholas@2 32 // Should put in an error function here incase of malprocessed or malformed XML
nicholas@2 33
n@658 34 // Extract the different test XML DOM trees
n@674 35 var audioHolders = xmlDoc.find('audioHolder');
n@674 36 audioHolders.each(function(index,element) {
n@674 37 var repeatN = element.attributes['repeatCount'].value;
n@674 38 for (var r=0; r<=repeatN; r++) {
n@674 39 testXMLSetups[testXMLSetups.length] = element;
n@674 40 }
n@674 41 });
n@668 42
n@674 43 // New check if we need to randomise the test order
n@674 44 var randomise = xmlSetup[0].attributes['randomiseOrder'];
n@674 45 if (randomise != undefined) {
n@674 46 randomise = Boolean(randomise.value);
n@674 47 } else {
n@674 48 randomise = false;
n@674 49 }
n@674 50 if (randomise)
n@674 51 {
n@678 52 testXMLSetups = randomiseOrder(testXMLSetups);
n@674 53 }
n@668 54
n@674 55 // Obtain the metrics enabled
n@674 56 var metricNode = xmlSetup.find('Metric');
n@674 57 var metricNode = metricNode.find('metricEnable');
n@674 58 metricNode.each(function(index,node){
n@674 59 var enabled = node.textContent;
n@674 60 switch(enabled)
n@674 61 {
n@674 62 case 'testTimer':
n@674 63 sessionMetrics.prototype.enableTestTimer = true;
n@674 64 break;
n@674 65 case 'elementTimer':
n@674 66 sessionMetrics.prototype.enableElementTimer = true;
n@674 67 break;
n@674 68 case 'elementTracker':
n@674 69 sessionMetrics.prototype.enableElementTracker = true;
n@674 70 break;
n@674 71 case 'elementInitalPosition':
n@674 72 sessionMetrics.prototype.enableElementInitialPosition = true;
n@674 73 break;
n@674 74 case 'elementFlagListenedTo':
n@674 75 sessionMetrics.prototype.enableFlagListenedTo = true;
n@674 76 break;
n@674 77 case 'elementFlagMoved':
n@674 78 sessionMetrics.prototype.enableFlagMoved = true;
n@674 79 break;
n@674 80 case 'elementFlagComments':
n@674 81 sessionMetrics.prototype.enableFlagComments = true;
n@674 82 break;
n@674 83 }
n@674 84 });
n@658 85
n@676 86 // Create APE specific metric functions
n@676 87 audioEngineContext.metric.initialiseTest = function()
n@676 88 {
n@676 89 var sliders = document.getElementsByClassName('track-slider');
n@676 90 for (var i=0; i<sliders.length; i++)
n@676 91 {
n@676 92 audioEngineContext.audioObjects[i].metric.initialised(convSliderPosToRate(i));
n@676 93 }
n@676 94 };
n@676 95
n@676 96 audioEngineContext.metric.sliderMoveStart = function(id)
n@676 97 {
n@676 98 if (this.data == -1)
n@676 99 {
n@676 100 this.data = id;
n@676 101 } else {
n@676 102 console.log('ERROR: Metric tracker detecting two moves!');
n@676 103 this.data = -1;
n@676 104 }
n@676 105 };
n@676 106 audioEngineContext.metric.sliderMoved = function()
n@676 107 {
n@676 108 var time = audioEngineContext.timer.getTestTime();
n@676 109 var id = this.data;
n@676 110 this.data = -1;
n@676 111 var position = convSliderPosToRate(id);
n@676 112 if (audioEngineContext.timer.testStarted)
n@676 113 {
n@676 114 audioEngineContext.audioObjects[id].metric.moved(time,position);
n@676 115 }
n@676 116 };
n@676 117
n@676 118 audioEngineContext.metric.sliderPlayed = function(id)
n@676 119 {
n@676 120 var time = audioEngineContext.timer.getTestTime();
n@676 121 if (audioEngineContext.timer.testStarted)
n@676 122 {
n@676 123 if (this.lastClicked >= 0)
n@676 124 {
n@676 125 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
n@676 126 }
n@676 127 this.lastClicked = id;
n@676 128 audioEngineContext.audioObjects[id].metric.listening(time);
n@676 129 }
n@676 130 };
n@676 131
nicholas@2 132 // Create the top div for the Title element
nicholas@2 133 var titleAttr = xmlSetup[0].attributes['title'];
nicholas@2 134 var title = document.createElement('div');
nicholas@2 135 title.className = "title";
nicholas@2 136 title.align = "center";
nicholas@2 137 var titleSpan = document.createElement('span');
nicholas@2 138
nicholas@2 139 // Set title to that defined in XML, else set to default
nicholas@2 140 if (titleAttr != undefined) {
n@709 141 titleSpan.innerHTML = titleAttr.value;
nicholas@2 142 } else {
BrechtDeMan@975 143 titleSpan.innerHTML = 'Listening test';
nicholas@2 144 }
nicholas@2 145 // Insert the titleSpan element into the title div element.
nicholas@2 146 title.appendChild(titleSpan);
nicholas@2 147
n@671 148 var pagetitle = document.createElement('div');
n@671 149 pagetitle.className = "pageTitle";
n@671 150 pagetitle.align = "center";
n@671 151 var titleSpan = document.createElement('span');
n@671 152 titleSpan.id = "pageTitle";
n@671 153 pagetitle.appendChild(titleSpan);
n@671 154
nicholas@7 155 // Store the return URL path in global projectReturn
nicholas@7 156 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
nicholas@7 157
nicholas@7 158 // Create Interface buttons!
nicholas@7 159 var interfaceButtons = document.createElement('div');
nicholas@7 160 interfaceButtons.id = 'interface-buttons';
nicholas@7 161
nicholas@7 162 // MANUAL DOWNLOAD POINT
nicholas@7 163 // If project return is null, this MUST be specified as the location to create the download link
nicholas@7 164 var downloadPoint = document.createElement('div');
nicholas@7 165 downloadPoint.id = 'download-point';
nicholas@7 166
nicholas@7 167 // Create playback start/stop points
nicholas@7 168 var playback = document.createElement("button");
n@709 169 playback.innerHTML = 'Start';
n@673 170 playback.id = 'playback-button';
n@701 171 // onclick function. Check if it is playing or not, call the correct function in the
n@701 172 // audioEngine, change the button text to reflect the next state.
nicholas@7 173 playback.onclick = function() {
nicholas@7 174 if (audioEngineContext.status == 0) {
nicholas@7 175 audioEngineContext.play();
n@709 176 this.innerHTML = 'Stop';
nicholas@7 177 } else {
nicholas@7 178 audioEngineContext.stop();
n@709 179 this.innerHTML = 'Start';
nicholas@7 180 }
n@701 181 };
nicholas@7 182 // Create Submit (save) button
nicholas@7 183 var submit = document.createElement("button");
n@709 184 submit.innerHTML = 'Submit';
n@667 185 submit.onclick = buttonSubmitClick;
n@673 186 submit.id = 'submit-button';
n@701 187 // Append the interface buttons into the interfaceButtons object.
nicholas@7 188 interfaceButtons.appendChild(playback);
nicholas@7 189 interfaceButtons.appendChild(submit);
nicholas@7 190 interfaceButtons.appendChild(downloadPoint);
nicholas@7 191
nicholas@2 192 // Now create the slider and HTML5 canvas boxes
nicholas@2 193
n@701 194 // Create the div box to center align
nicholas@2 195 var sliderBox = document.createElement('div');
nicholas@2 196 sliderBox.className = 'sliderCanvasDiv';
n@701 197 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 198 sliderBox.align = 'center';
nicholas@2 199
n@701 200 // Create the slider box to hold the slider elements
nicholas@5 201 var canvas = document.createElement('div');
nicholas@2 202 canvas.id = 'slider';
n@701 203 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@5 204 canvas.style.width = width - 100 +"px";
nicholas@5 205 canvas.align = "left";
nicholas@2 206 sliderBox.appendChild(canvas);
n@701 207
n@671 208 // Create the div to hold any scale objects
n@671 209 var scale = document.createElement('div');
n@671 210 scale.className = 'sliderScale';
n@671 211 scale.id = 'sliderScaleHolder';
n@671 212 scale.align = 'left';
n@671 213 sliderBox.appendChild(scale);
n@671 214
n@701 215 // Global parent for the comment boxes on the page
nicholas@3 216 var feedbackHolder = document.createElement('div');
n@658 217 feedbackHolder.id = 'feedbackHolder';
n@656 218
n@662 219 testContent.style.zIndex = 1;
n@662 220 insertPoint.innerHTML = null; // Clear the current schema
n@662 221
n@656 222 // Create pre and post test questions
n@662 223 var blank = document.createElement('div');
n@662 224 blank.className = 'testHalt';
n@656 225
n@662 226 var popupHolder = document.createElement('div');
n@662 227 popupHolder.id = 'popupHolder';
n@662 228 popupHolder.className = 'popupHolder';
n@662 229 popupHolder.style.position = 'absolute';
n@662 230 popupHolder.style.left = (window.innerWidth/2)-250 + 'px';
n@662 231 popupHolder.style.top = (window.innerHeight/2)-125 + 'px';
n@662 232 insertPoint.appendChild(popupHolder);
n@662 233 insertPoint.appendChild(blank);
n@662 234 hidePopup();
n@656 235
n@660 236 var preTest = xmlSetup.find('PreTest');
n@660 237 var postTest = xmlSetup.find('PostTest');
n@656 238 preTest = preTest[0];
n@656 239 postTest = postTest[0];
n@662 240
n@662 241 currentState = 'preTest';
n@656 242
n@656 243 // Create Pre-Test Box
n@656 244 if (preTest != undefined && preTest.children.length >= 1)
n@656 245 {
n@662 246 showPopup();
n@663 247 preTestPopupStart(preTest);
n@656 248 }
n@662 249
n@662 250 // Inject into HTML
n@662 251 testContent.appendChild(title); // Insert the title
n@671 252 testContent.appendChild(pagetitle);
n@662 253 testContent.appendChild(interfaceButtons);
n@662 254 testContent.appendChild(sliderBox);
n@662 255 testContent.appendChild(feedbackHolder);
n@662 256 insertPoint.appendChild(testContent);
n@656 257
n@660 258 // Load the full interface
n@663 259
n@656 260 }
n@656 261
n@663 262 function loadTest(id)
n@658 263 {
n@658 264 // Used to load a specific test page
n@663 265 var textXML = testXMLSetups[id];
n@658 266
n@658 267 var feedbackHolder = document.getElementById('feedbackHolder');
n@658 268 var canvas = document.getElementById('slider');
n@658 269 feedbackHolder.innerHTML = null;
n@658 270 canvas.innerHTML = null;
n@671 271
n@671 272 // Setup question title
n@671 273 var interfaceObj = $(textXML).find('interface');
n@671 274 var titleNode = interfaceObj.find('title');
n@671 275 if (titleNode[0] != undefined)
n@671 276 {
n@671 277 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
n@671 278 }
n@671 279 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
n@671 280 var offset = 50-8; // Half the offset of the slider (window width -100) minus the body padding of 8
n@671 281 // TODO: AUTOMATE ABOVE!!
n@671 282 var scale = document.getElementById('sliderScaleHolder');
n@671 283 scale.innerHTML = null;
n@671 284 interfaceObj.find('scale').each(function(index,scaleObj){
n@671 285 var position = Number(scaleObj.attributes['position'].value)*0.01;
n@671 286 var pixelPosition = (position*positionScale)+offset;
n@671 287 var scaleDOM = document.createElement('span');
n@671 288 scaleDOM.textContent = scaleObj.textContent;
n@671 289 scale.appendChild(scaleDOM);
n@671 290 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
n@671 291 });
n@658 292
n@701 293 // Extract the hostURL attribute. If not set, create an empty string.
n@658 294 var hostURL = textXML.attributes['hostURL'];
nicholas@7 295 if (hostURL == undefined) {
nicholas@7 296 hostURL = "";
nicholas@7 297 } else {
nicholas@7 298 hostURL = hostURL.value;
nicholas@7 299 }
n@701 300 // Extract the sampleRate. If set, convert the string to a Number.
n@658 301 var hostFs = textXML.attributes['sampleRate'];
n@700 302 if (hostFs != undefined) {
n@700 303 hostFs = Number(hostFs.value);
nicholas@7 304 }
nicholas@7 305
nicholas@7 306 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@700 307 if (hostFs != undefined) {
nicholas@7 308 if (Number(hostFs) != audioContext.sampleRate) {
nicholas@7 309 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nicholas@7 310 alert(errStr);
nicholas@7 311 return;
nicholas@7 312 }
nicholas@7 313 }
n@669 314
nicholas@689 315 var commentShow = textXML.attributes['elementComments'];
nicholas@689 316 if (commentShow != undefined) {
nicholas@689 317 if (commentShow.value == 'false') {commentShow = false;}
nicholas@689 318 else {commentShow = true;}
nicholas@689 319 } else {commentShow = true;}
nicholas@689 320
n@681 321 var loopPlayback = textXML.attributes['loop'];
n@681 322 if (loopPlayback != undefined)
n@681 323 {
n@681 324 loopPlayback = loopPlayback.value;
n@681 325 if (loopPlayback == 'true') {
n@681 326 loopPlayback = true;
n@681 327 } else {
n@681 328 loopPlayback = false;
n@681 329 }
n@681 330 } else {
n@681 331 loopPlayback = false;
n@681 332 }
n@681 333 audioEngineContext.loopPlayback = loopPlayback;
n@681 334
n@681 335 // Create AudioEngine bindings for playback
n@681 336 if (loopPlayback) {
n@681 337 audioEngineContext.play = function() {
n@681 338 // Send play command to all playback buffers for synchronised start
n@681 339 // Also start timer callbacks to detect if playback has finished
n@681 340 if (this.status == 0) {
n@681 341 this.timer.startTest();
n@681 342 // First get current clock
n@681 343 var timer = audioContext.currentTime;
n@681 344 // Add 3 seconds
n@681 345 timer += 3.0;
n@681 346 // Send play to all tracks
n@681 347 for (var i=0; i<this.audioObjects.length; i++)
n@681 348 {
n@681 349 this.audioObjects[i].play(timer);
n@681 350 }
n@681 351 this.status = 1;
n@681 352 }
n@681 353 };
n@681 354
n@681 355 audioEngineContext.stop = function() {
n@681 356 // Send stop and reset command to all playback buffers
n@681 357 if (this.status == 1) {
n@681 358 if (this.loopPlayback) {
n@681 359 for (var i=0; i<this.audioObjects.length; i++)
n@681 360 {
n@681 361 this.audioObjects[i].stop();
n@681 362 }
n@681 363 }
n@681 364 this.status = 0;
n@681 365 }
n@681 366 };
n@681 367
n@681 368 audioEngineContext.selectedTrack = function(id) {
n@681 369 for (var i=0; i<this.audioObjects.length; i++)
n@681 370 {
n@681 371 if (id == i) {
n@681 372 this.audioObjects[i].outputGain.gain.value = 1.0;
n@681 373 } else {
n@681 374 this.audioObjects[i].outputGain.gain.value = 0.0;
n@681 375 }
n@681 376 }
n@681 377 };
n@681 378 } else {
n@681 379 audioEngineContext.play = function() {
n@681 380 // Send play command to all playback buffers for synchronised start
n@681 381 // Also start timer callbacks to detect if playback has finished
n@681 382 if (this.status == 0) {
n@681 383 this.timer.startTest();
n@681 384 this.status = 1;
n@681 385 }
n@681 386 };
n@681 387
n@681 388 audioEngineContext.stop = function() {
n@681 389 // Send stop and reset command to all playback buffers
n@681 390 if (this.status == 1) {
n@681 391 if (this.loopPlayback) {
n@681 392 for (var i=0; i<this.audioObjects.length; i++)
n@681 393 {
n@681 394 this.audioObjects[i].stop();
n@681 395 }
n@681 396 }
n@681 397 this.status = 0;
n@681 398 }
n@681 399 };
n@681 400
n@681 401 audioEngineContext.selectedTrack = function(id) {
n@681 402 for (var i=0; i<this.audioObjects.length; i++)
n@681 403 {
n@681 404 if (id == i) {
n@681 405 this.audioObjects[i].outputGain.gain.value = 1.0;
n@681 406 this.audioObjects[i].play(audioContext.currentTime+0.01);
n@681 407 } else {
n@681 408 this.audioObjects[i].outputGain.gain.value = 0.0;
djmoffat@979 409 this.audioObjects[i].stop();
n@681 410 }
n@681 411 }
n@681 412 };
n@681 413 }
n@681 414
n@670 415 currentTestHolder = document.createElement('audioHolder');
n@670 416 currentTestHolder.id = textXML.id;
n@670 417 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
n@670 418 var currentPreTestHolder = document.createElement('preTest');
n@670 419 var currentPostTestHolder = document.createElement('postTest');
n@670 420 currentTestHolder.appendChild(currentPreTestHolder);
n@670 421 currentTestHolder.appendChild(currentPostTestHolder);
n@670 422
n@669 423 var randomise = textXML.attributes['randomiseOrder'];
n@669 424 if (randomise != undefined) {randomise = randomise.value;}
n@669 425 else {randomise = false;}
n@669 426
n@658 427 var audioElements = $(textXML).find('audioElements');
nicholas@682 428 currentTrackOrder = [];
n@658 429 audioElements.each(function(index,element){
n@669 430 // Find any blind-repeats
n@669 431 // Not implemented yet, but just incase
n@669 432 currentTrackOrder[index] = element;
n@669 433 });
n@669 434 if (randomise) {
n@678 435 currentTrackOrder = randomiseOrder(currentTrackOrder);
n@669 436 }
n@669 437
nicholas@682 438 // Delete any previous audioObjects associated with the audioEngine
nicholas@682 439 audioEngineContext.audioObjects = [];
nicholas@682 440
n@701 441 // Find all the audioElements from the audioHolder
n@669 442 $(currentTrackOrder).each(function(index,element){
nicholas@7 443 // Find URL of track
n@701 444 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 445
n@701 446 // Now load each audio sample. First create the new track by passing the full URL
nicholas@7 447 var trackURL = hostURL + this.attributes['url'].value;
nicholas@7 448 audioEngineContext.newTrack(trackURL);
nicholas@689 449
nicholas@689 450 if (commentShow) {
nicholas@689 451 // Create document objects to hold the comment boxes
nicholas@689 452 var trackComment = document.createElement('div');
nicholas@689 453 trackComment.className = 'comment-div';
nicholas@689 454 // Create a string next to each comment asking for a comment
nicholas@689 455 var trackString = document.createElement('span');
nicholas@689 456 trackString.innerHTML = 'Comment on track '+index;
nicholas@689 457 // Create the HTML5 comment box 'textarea'
nicholas@689 458 var trackCommentBox = document.createElement('textarea');
nicholas@689 459 trackCommentBox.rows = '4';
nicholas@689 460 trackCommentBox.cols = '100';
nicholas@689 461 trackCommentBox.name = 'trackComment'+index;
nicholas@689 462 trackCommentBox.className = 'trackComment';
nicholas@689 463 var br = document.createElement('br');
nicholas@689 464 // Add to the holder.
nicholas@689 465 trackComment.appendChild(trackString);
nicholas@689 466 trackComment.appendChild(br);
nicholas@689 467 trackComment.appendChild(trackCommentBox);
nicholas@689 468 feedbackHolder.appendChild(trackComment);
nicholas@689 469 }
n@701 470
nicholas@5 471 // Create a slider per track
nicholas@5 472
nicholas@5 473 var trackSliderObj = document.createElement('div');
nicholas@5 474 trackSliderObj.className = 'track-slider';
nicholas@5 475 trackSliderObj.id = 'track-slider-'+index;
nicholas@5 476 // Distribute it randomnly
nicholas@5 477 var w = window.innerWidth - 100;
nicholas@5 478 w = Math.random()*w;
nicholas@5 479 trackSliderObj.style.left = Math.floor(w)+50+'px';
nicholas@5 480 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nicholas@5 481 trackSliderObj.draggable = true;
nicholas@5 482 trackSliderObj.ondragend = dragEnd;
n@673 483 trackSliderObj.ondragstart = function()
n@673 484 {
n@673 485 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 486 audioEngineContext.metric.sliderMoveStart(id);
n@673 487 };
nicholas@8 488
nicholas@8 489 // Onclick, switch playback to that track
nicholas@8 490 trackSliderObj.onclick = function() {
nicholas@8 491 // Get the track ID from the object ID
nicholas@8 492 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 493 audioEngineContext.metric.sliderPlayed(id);
nicholas@8 494 audioEngineContext.selectedTrack(id);
n@700 495 };
nicholas@8 496
nicholas@5 497 canvas.appendChild(trackSliderObj);
n@700 498 });
nicholas@3 499
nicholas@688 500 // Append any commentQuestion boxes
nicholas@688 501 var commentQuestions = $(textXML).find('CommentQuestion');
nicholas@688 502 $(commentQuestions).each(function(index,element) {
nicholas@688 503 // Create document objects to hold the comment boxes
nicholas@688 504 var trackComment = document.createElement('div');
nicholas@688 505 trackComment.className = 'comment-div commentQuestion';
nicholas@688 506 trackComment.id = element.attributes['id'].value;
nicholas@688 507 // Create a string next to each comment asking for a comment
nicholas@688 508 var trackString = document.createElement('span');
nicholas@688 509 trackString.innerHTML = element.textContent;
nicholas@688 510 // Create the HTML5 comment box 'textarea'
nicholas@688 511 var trackCommentBox = document.createElement('textarea');
nicholas@688 512 trackCommentBox.rows = '4';
nicholas@688 513 trackCommentBox.cols = '100';
nicholas@688 514 trackCommentBox.name = 'commentQuestion'+index;
nicholas@688 515 trackCommentBox.className = 'trackComment';
nicholas@688 516 var br = document.createElement('br');
nicholas@688 517 // Add to the holder.
nicholas@688 518 trackComment.appendChild(trackString);
nicholas@688 519 trackComment.appendChild(br);
nicholas@688 520 trackComment.appendChild(trackCommentBox);
nicholas@688 521 feedbackHolder.appendChild(trackComment);
nicholas@688 522 });
nicholas@2 523
n@663 524 // Now process any pre-test commands
n@706 525
n@668 526 var preTest = $(testXMLSetups[id]).find('PreTest')[0];
n@663 527 if (preTest.children.length > 0)
n@663 528 {
n@663 529 currentState = 'testRunPre-'+id;
n@663 530 preTestPopupStart(preTest);
n@663 531 showPopup();
n@663 532 } else {
n@663 533 currentState = 'testRun-'+id;
n@663 534 }
n@663 535 }
n@663 536
n@663 537 function preTestPopupStart(preTest)
n@663 538 {
n@663 539 var popupHolder = document.getElementById('popupHolder');
n@663 540 popupHolder.innerHTML = null;
n@663 541 // Parse the first box
n@663 542 var preTestOption = document.createElement('div');
n@663 543 preTestOption.id = 'preTest';
n@663 544 preTestOption.style.marginTop = '25px';
n@663 545 preTestOption.align = "center";
n@663 546 var child = preTest.children[0];
n@663 547 if (child.nodeName == 'statement')
n@663 548 {
n@663 549 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@663 550 } else if (child.nodeName == 'question')
n@663 551 {
n@663 552 var questionId = child.attributes['id'].value;
n@663 553 var textHold = document.createElement('span');
n@663 554 textHold.innerHTML = child.innerHTML;
n@663 555 textHold.id = questionId + 'response';
n@663 556 var textEnter = document.createElement('textarea');
n@663 557 preTestOption.appendChild(textHold);
n@663 558 preTestOption.appendChild(textEnter);
n@663 559 }
n@663 560 var nextButton = document.createElement('button');
n@663 561 nextButton.className = 'popupButton';
n@663 562 nextButton.value = '0';
n@663 563 nextButton.innerHTML = 'Next';
n@663 564 nextButton.onclick = popupButtonClick;
n@706 565
n@663 566 popupHolder.appendChild(preTestOption);
n@663 567 popupHolder.appendChild(nextButton);
n@658 568 }
n@658 569
n@662 570 function popupButtonClick()
n@662 571 {
n@662 572 // Global call from the 'Next' button click
n@662 573 if (currentState == 'preTest')
n@706 574 {
n@662 575 // At the start of the preTest routine!
n@663 576 var xmlTree = projectXML.find('setup');
n@663 577 var preTest = xmlTree.find('PreTest')[0];
n@663 578 this.value = preTestButtonClick(preTest,this.value);
n@663 579 } else if (currentState.substr(0,10) == 'testRunPre')
n@663 580 {
n@663 581 //Specific test pre-test
n@663 582 var testId = currentState.substr(11,currentState.length-10);
n@668 583 var preTest = $(testXMLSetups[testId]).find('PreTest')[0];
n@663 584 this.value = preTestButtonClick(preTest,this.value);
n@666 585 } else if (currentState.substr(0,11) == 'testRunPost')
n@666 586 {
n@666 587 // Specific test post-test
n@666 588 var testId = currentState.substr(12,currentState.length-11);
n@668 589 var preTest = $(testXMLSetups[testId]).find('PostTest')[0];
n@666 590 this.value = preTestButtonClick(preTest,this.value);
n@665 591 } else if (currentState == 'postTest')
n@665 592 {
n@665 593 // At the end of the test, running global post test
n@665 594 var xmlTree = projectXML.find('setup');
n@665 595 var PostTest = xmlTree.find('PostTest')[0];
n@665 596 this.value = preTestButtonClick(PostTest,this.value);
n@706 597 }
n@662 598 }
n@662 599
n@663 600 function preTestButtonClick(preTest,index)
n@658 601 {
n@658 602 // Called on click of pre-test button
n@660 603 // Need to find and parse preTest again!
n@660 604 var preTestOption = document.getElementById('preTest');
n@660 605 // Check if current state is a question!
n@662 606 if (preTest.children[index].nodeName == 'question') {
n@662 607 var questionId = preTest.children[index].attributes['id'].value;
n@660 608 var questionHold = document.createElement('comment');
n@660 609 var questionResponse = document.getElementById(questionId + 'response');
nicholas@687 610 var mandatory = preTest.children[index].attributes['mandatory'];
nicholas@687 611 if (mandatory != undefined){
nicholas@687 612 if (mandatory.value == 'true') {mandatory = true;}
nicholas@687 613 else {mandatory = false;}
nicholas@687 614 } else {mandatory = false;}
nicholas@687 615 if (mandatory == true && questionResponse.value.length == 0) {
nicholas@687 616 return index;
nicholas@687 617 }
n@660 618 questionHold.id = questionId;
n@660 619 questionHold.innerHTML = questionResponse.value;
n@670 620 postPopupResponse(questionHold);
n@660 621 }
n@662 622 index++;
n@662 623 if (index < preTest.children.length)
n@706 624 {
n@660 625 // More to process
n@662 626 var child = preTest.children[index];
n@706 627 if (child.nodeName == 'statement')
n@706 628 {
n@706 629 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@706 630 } else if (child.nodeName == 'question')
n@706 631 {
n@706 632 var textHold = document.createElement('span');
n@706 633 textHold.innerHTML = child.innerHTML;
n@706 634 var textEnter = document.createElement('textarea');
n@660 635 textEnter.id = child.attributes['id'].value + 'response';
n@660 636 var br = document.createElement('br');
n@660 637 preTestOption.innerHTML = null;
n@706 638 preTestOption.appendChild(textHold);
n@660 639 preTestOption.appendChild(br);
n@706 640 preTestOption.appendChild(textEnter);
n@706 641 }
n@660 642 } else {
n@660 643 // Time to clear
n@661 644 preTestOption.innerHTML = null;
n@667 645 if (currentState != 'postTest') {
n@667 646 hidePopup();
n@667 647 // Progress the state!
n@667 648 advanceState();
n@667 649 } else {
n@667 650 a = createProjectSave(projectReturn);
n@667 651 preTestOption.appendChild(a);
n@667 652 }
n@706 653 }
n@662 654 return index;
n@660 655 }
n@706 656
n@670 657 function postPopupResponse(response)
n@670 658 {
n@670 659 if (currentState == 'preTest') {
n@670 660 preTestQuestions.appendChild(response);
n@670 661 } else if (currentState == 'postTest') {
n@670 662 postTestQuestions.appendChild(response);
n@670 663 } else {
n@670 664 // Inside a specific test
n@670 665 if (currentState.substr(0,10) == 'testRunPre') {
n@670 666 // Pre Test
n@670 667 var store = $(currentTestHolder).find('preTest');
n@670 668 } else {
n@670 669 // Post Test
n@670 670 var store = $(currentTestHolder).find('postTest');
n@670 671 }
n@670 672 store[0].appendChild(response);
n@670 673 }
n@670 674 }
n@670 675
n@660 676 function showPopup()
n@660 677 {
n@661 678 var popupHolder = document.getElementById('popupHolder');
n@662 679 popupHolder.style.zIndex = 3;
n@661 680 popupHolder.style.visibility = 'visible';
n@661 681 var blank = document.getElementsByClassName('testHalt')[0];
n@661 682 blank.style.zIndex = 2;
n@661 683 blank.style.visibility = 'visible';
n@660 684 }
n@660 685
n@660 686 function hidePopup()
n@660 687 {
n@661 688 var popupHolder = document.getElementById('popupHolder');
n@661 689 popupHolder.style.zIndex = -1;
n@661 690 popupHolder.style.visibility = 'hidden';
n@661 691 var blank = document.getElementsByClassName('testHalt')[0];
n@661 692 blank.style.zIndex = -2;
n@661 693 blank.style.visibility = 'hidden';
nicholas@2 694 }
nicholas@5 695
nicholas@5 696 function dragEnd(ev) {
nicholas@5 697 // Function call when a div has been dropped
n@657 698 var slider = document.getElementById('slider');
n@675 699 var w = slider.style.width;
n@675 700 w = Number(w.substr(0,w.length-2));
n@675 701 var x = ev.x;
n@675 702 if (x >= 42 && x < w+42) {
n@675 703 this.style.left = (x)+'px';
nicholas@5 704 } else {
n@675 705 if (x<42) {
n@675 706 this.style.left = '42px';
nicholas@5 707 } else {
n@675 708 this.style.left = (w+42) + 'px';
nicholas@5 709 }
nicholas@5 710 }
n@673 711 audioEngineContext.metric.sliderMoved();
n@656 712 }
n@656 713
n@663 714 function advanceState()
n@663 715 {
n@663 716 console.log(currentState);
n@663 717 if (currentState == 'preTest')
n@663 718 {
n@663 719 // End of pre-test, begin the test
n@663 720 loadTest(0);
n@663 721 } else if (currentState.substr(0,10) == 'testRunPre')
n@663 722 {
n@663 723 // Start the test
n@663 724 var testId = currentState.substr(11,currentState.length-10);
n@663 725 currentState = 'testRun-'+testId;
n@666 726 } else if (currentState.substr(0,11) == 'testRunPost')
n@666 727 {
n@668 728 var testId = currentState.substr(12,currentState.length-11);
n@666 729 testEnded(testId);
n@664 730 } else if (currentState.substr(0,7) == 'testRun')
n@664 731 {
n@664 732 var testId = currentState.substr(8,currentState.length-7);
n@664 733 // Check if we have any post tests to perform
n@668 734 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
nicholas@692 735 if (postXML == undefined) {
nicholas@692 736 testEnded(testId);
nicholas@692 737 }
nicholas@692 738 else if (postXML.children.length > 0)
n@664 739 {
n@666 740 currentState = 'testRunPost-'+testId;
n@666 741 showPopup();
n@666 742 preTestPopupStart(postXML);
n@664 743 }
n@666 744 else {
n@664 745
n@664 746
n@666 747 // No post tests, check if we have another test to perform instead
nicholas@692 748
n@664 749 }
n@663 750 }
n@663 751 console.log(currentState);
n@663 752 }
n@663 753
n@666 754 function testEnded(testId)
n@666 755 {
n@669 756 pageXMLSave(testId);
n@666 757 if (testXMLSetups.length-1 > testId)
n@666 758 {
n@666 759 // Yes we have another test to perform
n@668 760 testId = (Number(testId)+1);
n@668 761 currentState = 'testRun-'+testId;
n@668 762 loadTest(testId);
n@666 763 } else {
n@666 764 console.log('Testing Completed!');
n@666 765 currentState = 'postTest';
n@666 766 // Check for any post tests
n@666 767 var xmlSetup = projectXML.find('setup');
n@666 768 var postTest = xmlSetup.find('PostTest')[0];
n@666 769 showPopup();
n@666 770 preTestPopupStart(postTest);
n@666 771 }
n@666 772 }
n@666 773
n@664 774 function buttonSubmitClick()
n@664 775 {
n@673 776 if (audioEngineContext.status == 1) {
n@673 777 var playback = document.getElementById('playback-button');
n@673 778 playback.click();
n@664 779 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
n@679 780 } else
n@679 781 {
n@679 782 if (audioEngineContext.timer.testStarted == false)
n@679 783 {
n@679 784 alert('You have not started the test! Please press start to begin the test!');
n@679 785 return;
n@679 786 }
n@673 787 }
n@664 788 if (currentState.substr(0,7) == 'testRun')
n@664 789 {
n@673 790 audioEngineContext.timer.stopTest();
n@664 791 advanceState();
n@664 792 }
n@664 793 }
n@664 794
n@675 795 function convSliderPosToRate(id)
n@675 796 {
n@675 797 var w = document.getElementById('slider').style.width;
n@675 798 var maxPix = w.substr(0,w.length-2);
n@675 799 var slider = document.getElementsByClassName('track-slider')[id];
n@675 800 var pix = slider.style.left;
n@675 801 pix = pix.substr(0,pix.length-2);
n@675 802 var rate = (pix-42)/maxPix;
n@675 803 return rate;
n@675 804 }
n@675 805
n@668 806 function pageXMLSave(testId)
n@668 807 {
n@668 808 // Saves a specific test page
n@670 809 var xmlDoc = currentTestHolder;
n@674 810 // Check if any session wide metrics are enabled
nicholas@690 811
nicholas@690 812 var commentShow = testXMLSetups[testId].attributes['elementComments'];
nicholas@690 813 if (commentShow != undefined) {
nicholas@690 814 if (commentShow.value == 'false') {commentShow = false;}
nicholas@690 815 else {commentShow = true;}
nicholas@690 816 } else {commentShow = true;}
nicholas@690 817
n@674 818 var metric = document.createElement('metric');
n@674 819 if (audioEngineContext.metric.enableTestTimer)
n@674 820 {
n@674 821 var testTime = document.createElement('metricResult');
n@674 822 testTime.id = 'testTime';
n@674 823 testTime.textContent = audioEngineContext.timer.testDuration;
n@674 824 metric.appendChild(testTime);
n@674 825 }
n@674 826 xmlDoc.appendChild(metric);
n@669 827 var trackSliderObjects = document.getElementsByClassName('track-slider');
n@669 828 var commentObjects = document.getElementsByClassName('comment-div');
n@669 829 for (var i=0; i<trackSliderObjects.length; i++)
n@669 830 {
n@669 831 var audioElement = document.createElement('audioElement');
n@669 832 audioElement.id = currentTrackOrder[i].attributes['id'].value;
n@669 833 audioElement.url = currentTrackOrder[i].attributes['url'].value;
n@675 834 var value = document.createElement('value');
n@675 835 value.innerHTML = convSliderPosToRate(i);
nicholas@690 836 if (commentShow) {
nicholas@690 837 var comment = document.createElement("comment");
nicholas@690 838 var question = document.createElement("question");
nicholas@690 839 var response = document.createElement("response");
nicholas@690 840 question.textContent = commentObjects[i].children[0].textContent;
nicholas@690 841 response.textContent = commentObjects[i].children[2].value;
nicholas@690 842 comment.appendChild(question);
nicholas@690 843 comment.appendChild(response);
nicholas@690 844 audioElement.appendChild(comment);
nicholas@690 845 }
n@669 846 audioElement.appendChild(value);
n@674 847 // Check for any per element metrics
n@674 848 var metric = document.createElement('metric');
n@674 849 var elementMetric = audioEngineContext.audioObjects[i].metric;
n@674 850 if (audioEngineContext.metric.enableElementTimer) {
n@674 851 var elementTimer = document.createElement('metricResult');
n@674 852 elementTimer.id = 'elementTimer';
n@674 853 elementTimer.textContent = elementMetric.listenedTimer;
n@674 854 metric.appendChild(elementTimer);
n@674 855 }
n@674 856 if (audioEngineContext.metric.enableElementTracker) {
n@674 857 var elementTrackerFull = document.createElement('metricResult');
n@674 858 elementTrackerFull.id = 'elementTrackerFull';
n@674 859 var data = elementMetric.movementTracker;
n@674 860 for (var k=0; k<data.length; k++)
n@674 861 {
n@674 862 var timePos = document.createElement('timePos');
n@674 863 timePos.id = k;
n@674 864 var time = document.createElement('time');
n@674 865 time.textContent = data[k][0];
n@674 866 var position = document.createElement('position');
n@674 867 position.textContent = data[k][1];
n@674 868 timePos.appendChild(time);
n@674 869 timePos.appendChild(position);
n@674 870 elementTrackerFull.appendChild(timePos);
n@674 871 }
n@674 872 metric.appendChild(elementTrackerFull);
n@674 873 }
n@674 874 if (audioEngineContext.metric.enableElementInitialPosition) {
n@674 875 var elementInitial = document.createElement('metricResult');
n@674 876 elementInitial.id = 'elementInitialPosition';
n@674 877 elementInitial.textContent = elementMetric.initialPosition;
n@674 878 metric.appendChild(elementInitial);
n@674 879 }
n@674 880 if (audioEngineContext.metric.enableFlagListenedTo) {
n@674 881 var flagListenedTo = document.createElement('metricResult');
n@674 882 flagListenedTo.id = 'elementFlagListenedTo';
n@674 883 flagListenedTo.textContent = elementMetric.wasListenedTo;
n@674 884 metric.appendChild(flagListenedTo);
n@674 885 }
n@674 886 if (audioEngineContext.metric.enableFlagMoved) {
n@674 887 var flagMoved = document.createElement('metricResult');
n@674 888 flagMoved.id = 'elementFlagMoved';
n@674 889 flagMoved.textContent = elementMetric.wasMoved;
n@674 890 metric.appendChild(flagMoved);
n@674 891 }
n@674 892 if (audioEngineContext.metric.enableFlagComments) {
n@674 893 var flagComments = document.createElement('metricResult');
n@674 894 flagComments.id = 'elementFlagComments';
n@674 895 if (response.textContent.length == 0) {flag.textContent = 'false';}
n@674 896 else {flag.textContet = 'true';}
n@674 897 metric.appendChild(flagComments);
n@674 898 }
n@674 899 audioElement.appendChild(metric);
n@669 900 xmlDoc.appendChild(audioElement);
n@669 901 }
nicholas@688 902 var commentQuestion = document.getElementsByClassName('commentQuestion');
nicholas@688 903 for (var i=0; i<commentQuestion.length; i++)
nicholas@688 904 {
nicholas@688 905 var cqHolder = document.createElement('CommentQuestion');
nicholas@688 906 var comment = document.createElement('comment');
nicholas@688 907 var question = document.createElement('question');
nicholas@688 908 cqHolder.id = commentQuestion[i].id;
nicholas@688 909 comment.textContent = commentQuestion[i].children[2].value;
nicholas@688 910 question.textContent = commentQuestion[i].children[0].textContent;
nicholas@688 911 cqHolder.appendChild(question);
nicholas@688 912 cqHolder.appendChild(comment);
nicholas@688 913 xmlDoc.appendChild(cqHolder);
nicholas@688 914 }
n@669 915 testResultsHolders[testId] = xmlDoc;
nicholas@5 916 }
nicholas@7 917
nicholas@7 918 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 919 function interfaceXMLSave(){
nicholas@7 920 // Create the XML string to be exported with results
nicholas@7 921 var xmlDoc = document.createElement("BrowserEvaluationResult");
n@669 922 for (var i=0; i<testResultsHolders.length; i++)
nicholas@7 923 {
n@669 924 xmlDoc.appendChild(testResultsHolders[i]);
nicholas@7 925 }
n@707 926 // Append Pre/Post Questions
n@707 927 xmlDoc.appendChild(preTestQuestions);
n@707 928 xmlDoc.appendChild(postTestQuestions);
n@707 929
nicholas@7 930 return xmlDoc;
nicholas@690 931 }