annotate ape.js @ 996:902f22e182f6

Fixed Bug 1204: Multiple clicks creates multiple instances.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 01 May 2015 16:07:15 +0100
parents 62b638aa1849
children 355631b936d3
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@996 404 this.audioObjects[i].outputGain.gain.value = 0.0;
n@996 405 this.audioObjects[i].stop();
n@681 406 }
n@996 407 this.audioObjects[id].outputGain.gain.value = 1.0;
n@996 408 this.audioObjects[id].play(audioContext.currentTime+0.01);
n@681 409 };
n@681 410 }
n@681 411
n@670 412 currentTestHolder = document.createElement('audioHolder');
n@670 413 currentTestHolder.id = textXML.id;
n@670 414 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
n@670 415 var currentPreTestHolder = document.createElement('preTest');
n@670 416 var currentPostTestHolder = document.createElement('postTest');
n@670 417 currentTestHolder.appendChild(currentPreTestHolder);
n@670 418 currentTestHolder.appendChild(currentPostTestHolder);
n@670 419
n@669 420 var randomise = textXML.attributes['randomiseOrder'];
n@669 421 if (randomise != undefined) {randomise = randomise.value;}
n@669 422 else {randomise = false;}
n@669 423
n@658 424 var audioElements = $(textXML).find('audioElements');
nicholas@682 425 currentTrackOrder = [];
n@658 426 audioElements.each(function(index,element){
n@669 427 // Find any blind-repeats
n@669 428 // Not implemented yet, but just incase
n@669 429 currentTrackOrder[index] = element;
n@669 430 });
n@669 431 if (randomise) {
n@678 432 currentTrackOrder = randomiseOrder(currentTrackOrder);
n@669 433 }
n@669 434
nicholas@682 435 // Delete any previous audioObjects associated with the audioEngine
nicholas@682 436 audioEngineContext.audioObjects = [];
nicholas@682 437
n@701 438 // Find all the audioElements from the audioHolder
n@669 439 $(currentTrackOrder).each(function(index,element){
nicholas@7 440 // Find URL of track
n@701 441 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 442
n@701 443 // Now load each audio sample. First create the new track by passing the full URL
nicholas@7 444 var trackURL = hostURL + this.attributes['url'].value;
nicholas@7 445 audioEngineContext.newTrack(trackURL);
nicholas@689 446
nicholas@689 447 if (commentShow) {
nicholas@689 448 // Create document objects to hold the comment boxes
nicholas@689 449 var trackComment = document.createElement('div');
nicholas@689 450 trackComment.className = 'comment-div';
nicholas@689 451 // Create a string next to each comment asking for a comment
nicholas@689 452 var trackString = document.createElement('span');
nicholas@689 453 trackString.innerHTML = 'Comment on track '+index;
nicholas@689 454 // Create the HTML5 comment box 'textarea'
nicholas@689 455 var trackCommentBox = document.createElement('textarea');
nicholas@689 456 trackCommentBox.rows = '4';
nicholas@689 457 trackCommentBox.cols = '100';
nicholas@689 458 trackCommentBox.name = 'trackComment'+index;
nicholas@689 459 trackCommentBox.className = 'trackComment';
nicholas@689 460 var br = document.createElement('br');
nicholas@689 461 // Add to the holder.
nicholas@689 462 trackComment.appendChild(trackString);
nicholas@689 463 trackComment.appendChild(br);
nicholas@689 464 trackComment.appendChild(trackCommentBox);
nicholas@689 465 feedbackHolder.appendChild(trackComment);
nicholas@689 466 }
n@701 467
nicholas@5 468 // Create a slider per track
nicholas@5 469
nicholas@5 470 var trackSliderObj = document.createElement('div');
nicholas@5 471 trackSliderObj.className = 'track-slider';
nicholas@5 472 trackSliderObj.id = 'track-slider-'+index;
nicholas@5 473 // Distribute it randomnly
nicholas@5 474 var w = window.innerWidth - 100;
nicholas@5 475 w = Math.random()*w;
nicholas@5 476 trackSliderObj.style.left = Math.floor(w)+50+'px';
nicholas@5 477 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nicholas@5 478 trackSliderObj.draggable = true;
nicholas@5 479 trackSliderObj.ondragend = dragEnd;
n@673 480 trackSliderObj.ondragstart = function()
n@673 481 {
n@673 482 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 483 audioEngineContext.metric.sliderMoveStart(id);
n@673 484 };
nicholas@8 485
nicholas@8 486 // Onclick, switch playback to that track
nicholas@8 487 trackSliderObj.onclick = function() {
nicholas@8 488 // Get the track ID from the object ID
nicholas@8 489 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 490 audioEngineContext.metric.sliderPlayed(id);
nicholas@8 491 audioEngineContext.selectedTrack(id);
n@700 492 };
nicholas@8 493
nicholas@5 494 canvas.appendChild(trackSliderObj);
n@700 495 });
nicholas@3 496
nicholas@688 497 // Append any commentQuestion boxes
nicholas@688 498 var commentQuestions = $(textXML).find('CommentQuestion');
nicholas@688 499 $(commentQuestions).each(function(index,element) {
nicholas@688 500 // Create document objects to hold the comment boxes
nicholas@688 501 var trackComment = document.createElement('div');
nicholas@688 502 trackComment.className = 'comment-div commentQuestion';
nicholas@688 503 trackComment.id = element.attributes['id'].value;
nicholas@688 504 // Create a string next to each comment asking for a comment
nicholas@688 505 var trackString = document.createElement('span');
nicholas@688 506 trackString.innerHTML = element.textContent;
nicholas@688 507 // Create the HTML5 comment box 'textarea'
nicholas@688 508 var trackCommentBox = document.createElement('textarea');
nicholas@688 509 trackCommentBox.rows = '4';
nicholas@688 510 trackCommentBox.cols = '100';
nicholas@688 511 trackCommentBox.name = 'commentQuestion'+index;
nicholas@688 512 trackCommentBox.className = 'trackComment';
nicholas@688 513 var br = document.createElement('br');
nicholas@688 514 // Add to the holder.
nicholas@688 515 trackComment.appendChild(trackString);
nicholas@688 516 trackComment.appendChild(br);
nicholas@688 517 trackComment.appendChild(trackCommentBox);
nicholas@688 518 feedbackHolder.appendChild(trackComment);
nicholas@688 519 });
nicholas@2 520
n@663 521 // Now process any pre-test commands
n@706 522
n@668 523 var preTest = $(testXMLSetups[id]).find('PreTest')[0];
n@663 524 if (preTest.children.length > 0)
n@663 525 {
n@663 526 currentState = 'testRunPre-'+id;
n@663 527 preTestPopupStart(preTest);
n@663 528 showPopup();
n@663 529 } else {
n@663 530 currentState = 'testRun-'+id;
n@663 531 }
n@663 532 }
n@663 533
n@663 534 function preTestPopupStart(preTest)
n@663 535 {
n@663 536 var popupHolder = document.getElementById('popupHolder');
n@663 537 popupHolder.innerHTML = null;
n@663 538 // Parse the first box
n@663 539 var preTestOption = document.createElement('div');
n@663 540 preTestOption.id = 'preTest';
n@663 541 preTestOption.style.marginTop = '25px';
n@663 542 preTestOption.align = "center";
n@663 543 var child = preTest.children[0];
n@663 544 if (child.nodeName == 'statement')
n@663 545 {
n@663 546 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@663 547 } else if (child.nodeName == 'question')
n@663 548 {
n@663 549 var questionId = child.attributes['id'].value;
n@663 550 var textHold = document.createElement('span');
n@663 551 textHold.innerHTML = child.innerHTML;
n@663 552 textHold.id = questionId + 'response';
n@663 553 var textEnter = document.createElement('textarea');
n@663 554 preTestOption.appendChild(textHold);
n@663 555 preTestOption.appendChild(textEnter);
n@663 556 }
n@663 557 var nextButton = document.createElement('button');
n@663 558 nextButton.className = 'popupButton';
n@663 559 nextButton.value = '0';
n@663 560 nextButton.innerHTML = 'Next';
n@663 561 nextButton.onclick = popupButtonClick;
n@706 562
n@663 563 popupHolder.appendChild(preTestOption);
n@663 564 popupHolder.appendChild(nextButton);
n@658 565 }
n@658 566
n@662 567 function popupButtonClick()
n@662 568 {
n@662 569 // Global call from the 'Next' button click
n@662 570 if (currentState == 'preTest')
n@706 571 {
n@662 572 // At the start of the preTest routine!
n@663 573 var xmlTree = projectXML.find('setup');
n@663 574 var preTest = xmlTree.find('PreTest')[0];
n@663 575 this.value = preTestButtonClick(preTest,this.value);
n@663 576 } else if (currentState.substr(0,10) == 'testRunPre')
n@663 577 {
n@663 578 //Specific test pre-test
n@663 579 var testId = currentState.substr(11,currentState.length-10);
n@668 580 var preTest = $(testXMLSetups[testId]).find('PreTest')[0];
n@663 581 this.value = preTestButtonClick(preTest,this.value);
n@666 582 } else if (currentState.substr(0,11) == 'testRunPost')
n@666 583 {
n@666 584 // Specific test post-test
n@666 585 var testId = currentState.substr(12,currentState.length-11);
n@668 586 var preTest = $(testXMLSetups[testId]).find('PostTest')[0];
n@666 587 this.value = preTestButtonClick(preTest,this.value);
n@665 588 } else if (currentState == 'postTest')
n@665 589 {
n@665 590 // At the end of the test, running global post test
n@665 591 var xmlTree = projectXML.find('setup');
n@665 592 var PostTest = xmlTree.find('PostTest')[0];
n@665 593 this.value = preTestButtonClick(PostTest,this.value);
n@706 594 }
n@662 595 }
n@662 596
n@663 597 function preTestButtonClick(preTest,index)
n@658 598 {
n@658 599 // Called on click of pre-test button
n@660 600 // Need to find and parse preTest again!
n@660 601 var preTestOption = document.getElementById('preTest');
n@660 602 // Check if current state is a question!
n@662 603 if (preTest.children[index].nodeName == 'question') {
n@662 604 var questionId = preTest.children[index].attributes['id'].value;
n@660 605 var questionHold = document.createElement('comment');
n@660 606 var questionResponse = document.getElementById(questionId + 'response');
nicholas@687 607 var mandatory = preTest.children[index].attributes['mandatory'];
nicholas@687 608 if (mandatory != undefined){
nicholas@687 609 if (mandatory.value == 'true') {mandatory = true;}
nicholas@687 610 else {mandatory = false;}
nicholas@687 611 } else {mandatory = false;}
nicholas@687 612 if (mandatory == true && questionResponse.value.length == 0) {
nicholas@687 613 return index;
nicholas@687 614 }
n@660 615 questionHold.id = questionId;
n@660 616 questionHold.innerHTML = questionResponse.value;
n@670 617 postPopupResponse(questionHold);
n@660 618 }
n@662 619 index++;
n@662 620 if (index < preTest.children.length)
n@706 621 {
n@660 622 // More to process
n@662 623 var child = preTest.children[index];
n@706 624 if (child.nodeName == 'statement')
n@706 625 {
n@706 626 preTestOption.innerHTML = '<span>'+child.innerHTML+'</span>';
n@706 627 } else if (child.nodeName == 'question')
n@706 628 {
n@706 629 var textHold = document.createElement('span');
n@706 630 textHold.innerHTML = child.innerHTML;
n@706 631 var textEnter = document.createElement('textarea');
n@660 632 textEnter.id = child.attributes['id'].value + 'response';
n@660 633 var br = document.createElement('br');
n@660 634 preTestOption.innerHTML = null;
n@706 635 preTestOption.appendChild(textHold);
n@660 636 preTestOption.appendChild(br);
n@706 637 preTestOption.appendChild(textEnter);
n@706 638 }
n@660 639 } else {
n@660 640 // Time to clear
n@661 641 preTestOption.innerHTML = null;
n@667 642 if (currentState != 'postTest') {
n@667 643 hidePopup();
n@667 644 // Progress the state!
n@667 645 advanceState();
n@667 646 } else {
n@667 647 a = createProjectSave(projectReturn);
n@667 648 preTestOption.appendChild(a);
n@667 649 }
n@706 650 }
n@662 651 return index;
n@660 652 }
n@706 653
n@670 654 function postPopupResponse(response)
n@670 655 {
n@670 656 if (currentState == 'preTest') {
n@670 657 preTestQuestions.appendChild(response);
n@670 658 } else if (currentState == 'postTest') {
n@670 659 postTestQuestions.appendChild(response);
n@670 660 } else {
n@670 661 // Inside a specific test
n@670 662 if (currentState.substr(0,10) == 'testRunPre') {
n@670 663 // Pre Test
n@670 664 var store = $(currentTestHolder).find('preTest');
n@670 665 } else {
n@670 666 // Post Test
n@670 667 var store = $(currentTestHolder).find('postTest');
n@670 668 }
n@670 669 store[0].appendChild(response);
n@670 670 }
n@670 671 }
n@670 672
n@660 673 function showPopup()
n@660 674 {
n@661 675 var popupHolder = document.getElementById('popupHolder');
n@662 676 popupHolder.style.zIndex = 3;
n@661 677 popupHolder.style.visibility = 'visible';
n@661 678 var blank = document.getElementsByClassName('testHalt')[0];
n@661 679 blank.style.zIndex = 2;
n@661 680 blank.style.visibility = 'visible';
n@660 681 }
n@660 682
n@660 683 function hidePopup()
n@660 684 {
n@661 685 var popupHolder = document.getElementById('popupHolder');
n@661 686 popupHolder.style.zIndex = -1;
n@661 687 popupHolder.style.visibility = 'hidden';
n@661 688 var blank = document.getElementsByClassName('testHalt')[0];
n@661 689 blank.style.zIndex = -2;
n@661 690 blank.style.visibility = 'hidden';
nicholas@2 691 }
nicholas@5 692
nicholas@5 693 function dragEnd(ev) {
nicholas@5 694 // Function call when a div has been dropped
n@657 695 var slider = document.getElementById('slider');
n@675 696 var w = slider.style.width;
n@675 697 w = Number(w.substr(0,w.length-2));
n@675 698 var x = ev.x;
n@675 699 if (x >= 42 && x < w+42) {
n@675 700 this.style.left = (x)+'px';
nicholas@5 701 } else {
n@675 702 if (x<42) {
n@675 703 this.style.left = '42px';
nicholas@5 704 } else {
n@675 705 this.style.left = (w+42) + 'px';
nicholas@5 706 }
nicholas@5 707 }
n@673 708 audioEngineContext.metric.sliderMoved();
n@656 709 }
n@656 710
n@663 711 function advanceState()
n@663 712 {
n@663 713 console.log(currentState);
n@663 714 if (currentState == 'preTest')
n@663 715 {
n@663 716 // End of pre-test, begin the test
n@663 717 loadTest(0);
n@663 718 } else if (currentState.substr(0,10) == 'testRunPre')
n@663 719 {
n@663 720 // Start the test
n@663 721 var testId = currentState.substr(11,currentState.length-10);
n@663 722 currentState = 'testRun-'+testId;
n@666 723 } else if (currentState.substr(0,11) == 'testRunPost')
n@666 724 {
n@668 725 var testId = currentState.substr(12,currentState.length-11);
n@666 726 testEnded(testId);
n@664 727 } else if (currentState.substr(0,7) == 'testRun')
n@664 728 {
n@664 729 var testId = currentState.substr(8,currentState.length-7);
n@664 730 // Check if we have any post tests to perform
n@668 731 var postXML = $(testXMLSetups[testId]).find('PostTest')[0];
nicholas@692 732 if (postXML == undefined) {
nicholas@692 733 testEnded(testId);
nicholas@692 734 }
nicholas@692 735 else if (postXML.children.length > 0)
n@664 736 {
n@666 737 currentState = 'testRunPost-'+testId;
n@666 738 showPopup();
n@666 739 preTestPopupStart(postXML);
n@664 740 }
n@666 741 else {
n@664 742
n@664 743
n@666 744 // No post tests, check if we have another test to perform instead
nicholas@692 745
n@664 746 }
n@663 747 }
n@663 748 console.log(currentState);
n@663 749 }
n@663 750
n@666 751 function testEnded(testId)
n@666 752 {
n@669 753 pageXMLSave(testId);
n@666 754 if (testXMLSetups.length-1 > testId)
n@666 755 {
n@666 756 // Yes we have another test to perform
n@668 757 testId = (Number(testId)+1);
n@668 758 currentState = 'testRun-'+testId;
n@668 759 loadTest(testId);
n@666 760 } else {
n@666 761 console.log('Testing Completed!');
n@666 762 currentState = 'postTest';
n@666 763 // Check for any post tests
n@666 764 var xmlSetup = projectXML.find('setup');
n@666 765 var postTest = xmlSetup.find('PostTest')[0];
n@666 766 showPopup();
n@666 767 preTestPopupStart(postTest);
n@666 768 }
n@666 769 }
n@666 770
n@664 771 function buttonSubmitClick()
n@664 772 {
n@673 773 if (audioEngineContext.status == 1) {
n@673 774 var playback = document.getElementById('playback-button');
n@673 775 playback.click();
n@664 776 // 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 777 } else
n@679 778 {
n@679 779 if (audioEngineContext.timer.testStarted == false)
n@679 780 {
n@679 781 alert('You have not started the test! Please press start to begin the test!');
n@679 782 return;
n@679 783 }
n@673 784 }
n@664 785 if (currentState.substr(0,7) == 'testRun')
n@664 786 {
n@673 787 audioEngineContext.timer.stopTest();
n@664 788 advanceState();
n@664 789 }
n@664 790 }
n@664 791
n@675 792 function convSliderPosToRate(id)
n@675 793 {
n@675 794 var w = document.getElementById('slider').style.width;
n@675 795 var maxPix = w.substr(0,w.length-2);
n@675 796 var slider = document.getElementsByClassName('track-slider')[id];
n@675 797 var pix = slider.style.left;
n@675 798 pix = pix.substr(0,pix.length-2);
n@675 799 var rate = (pix-42)/maxPix;
n@675 800 return rate;
n@675 801 }
n@675 802
n@668 803 function pageXMLSave(testId)
n@668 804 {
n@668 805 // Saves a specific test page
n@670 806 var xmlDoc = currentTestHolder;
n@674 807 // Check if any session wide metrics are enabled
nicholas@690 808
nicholas@690 809 var commentShow = testXMLSetups[testId].attributes['elementComments'];
nicholas@690 810 if (commentShow != undefined) {
nicholas@690 811 if (commentShow.value == 'false') {commentShow = false;}
nicholas@690 812 else {commentShow = true;}
nicholas@690 813 } else {commentShow = true;}
nicholas@690 814
n@674 815 var metric = document.createElement('metric');
n@674 816 if (audioEngineContext.metric.enableTestTimer)
n@674 817 {
n@674 818 var testTime = document.createElement('metricResult');
n@674 819 testTime.id = 'testTime';
n@674 820 testTime.textContent = audioEngineContext.timer.testDuration;
n@674 821 metric.appendChild(testTime);
n@674 822 }
n@674 823 xmlDoc.appendChild(metric);
n@669 824 var trackSliderObjects = document.getElementsByClassName('track-slider');
n@669 825 var commentObjects = document.getElementsByClassName('comment-div');
n@669 826 for (var i=0; i<trackSliderObjects.length; i++)
n@669 827 {
n@669 828 var audioElement = document.createElement('audioElement');
n@669 829 audioElement.id = currentTrackOrder[i].attributes['id'].value;
n@669 830 audioElement.url = currentTrackOrder[i].attributes['url'].value;
n@675 831 var value = document.createElement('value');
n@675 832 value.innerHTML = convSliderPosToRate(i);
nicholas@690 833 if (commentShow) {
nicholas@690 834 var comment = document.createElement("comment");
nicholas@690 835 var question = document.createElement("question");
nicholas@690 836 var response = document.createElement("response");
nicholas@690 837 question.textContent = commentObjects[i].children[0].textContent;
nicholas@690 838 response.textContent = commentObjects[i].children[2].value;
nicholas@690 839 comment.appendChild(question);
nicholas@690 840 comment.appendChild(response);
nicholas@690 841 audioElement.appendChild(comment);
nicholas@690 842 }
n@669 843 audioElement.appendChild(value);
n@674 844 // Check for any per element metrics
n@674 845 var metric = document.createElement('metric');
n@674 846 var elementMetric = audioEngineContext.audioObjects[i].metric;
n@674 847 if (audioEngineContext.metric.enableElementTimer) {
n@674 848 var elementTimer = document.createElement('metricResult');
n@674 849 elementTimer.id = 'elementTimer';
n@674 850 elementTimer.textContent = elementMetric.listenedTimer;
n@674 851 metric.appendChild(elementTimer);
n@674 852 }
n@674 853 if (audioEngineContext.metric.enableElementTracker) {
n@674 854 var elementTrackerFull = document.createElement('metricResult');
n@674 855 elementTrackerFull.id = 'elementTrackerFull';
n@674 856 var data = elementMetric.movementTracker;
n@674 857 for (var k=0; k<data.length; k++)
n@674 858 {
n@674 859 var timePos = document.createElement('timePos');
n@674 860 timePos.id = k;
n@674 861 var time = document.createElement('time');
n@674 862 time.textContent = data[k][0];
n@674 863 var position = document.createElement('position');
n@674 864 position.textContent = data[k][1];
n@674 865 timePos.appendChild(time);
n@674 866 timePos.appendChild(position);
n@674 867 elementTrackerFull.appendChild(timePos);
n@674 868 }
n@674 869 metric.appendChild(elementTrackerFull);
n@674 870 }
n@674 871 if (audioEngineContext.metric.enableElementInitialPosition) {
n@674 872 var elementInitial = document.createElement('metricResult');
n@674 873 elementInitial.id = 'elementInitialPosition';
n@674 874 elementInitial.textContent = elementMetric.initialPosition;
n@674 875 metric.appendChild(elementInitial);
n@674 876 }
n@674 877 if (audioEngineContext.metric.enableFlagListenedTo) {
n@674 878 var flagListenedTo = document.createElement('metricResult');
n@674 879 flagListenedTo.id = 'elementFlagListenedTo';
n@674 880 flagListenedTo.textContent = elementMetric.wasListenedTo;
n@674 881 metric.appendChild(flagListenedTo);
n@674 882 }
n@674 883 if (audioEngineContext.metric.enableFlagMoved) {
n@674 884 var flagMoved = document.createElement('metricResult');
n@674 885 flagMoved.id = 'elementFlagMoved';
n@674 886 flagMoved.textContent = elementMetric.wasMoved;
n@674 887 metric.appendChild(flagMoved);
n@674 888 }
n@674 889 if (audioEngineContext.metric.enableFlagComments) {
n@674 890 var flagComments = document.createElement('metricResult');
n@674 891 flagComments.id = 'elementFlagComments';
n@674 892 if (response.textContent.length == 0) {flag.textContent = 'false';}
n@674 893 else {flag.textContet = 'true';}
n@674 894 metric.appendChild(flagComments);
n@674 895 }
n@674 896 audioElement.appendChild(metric);
n@669 897 xmlDoc.appendChild(audioElement);
n@669 898 }
nicholas@688 899 var commentQuestion = document.getElementsByClassName('commentQuestion');
nicholas@688 900 for (var i=0; i<commentQuestion.length; i++)
nicholas@688 901 {
nicholas@688 902 var cqHolder = document.createElement('CommentQuestion');
nicholas@688 903 var comment = document.createElement('comment');
nicholas@688 904 var question = document.createElement('question');
nicholas@688 905 cqHolder.id = commentQuestion[i].id;
nicholas@688 906 comment.textContent = commentQuestion[i].children[2].value;
nicholas@688 907 question.textContent = commentQuestion[i].children[0].textContent;
nicholas@688 908 cqHolder.appendChild(question);
nicholas@688 909 cqHolder.appendChild(comment);
nicholas@688 910 xmlDoc.appendChild(cqHolder);
nicholas@688 911 }
n@669 912 testResultsHolders[testId] = xmlDoc;
nicholas@5 913 }
nicholas@7 914
nicholas@7 915 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@7 916 function interfaceXMLSave(){
nicholas@7 917 // Create the XML string to be exported with results
nicholas@7 918 var xmlDoc = document.createElement("BrowserEvaluationResult");
n@669 919 for (var i=0; i<testResultsHolders.length; i++)
nicholas@7 920 {
n@669 921 xmlDoc.appendChild(testResultsHolders[i]);
nicholas@7 922 }
n@707 923 // Append Pre/Post Questions
n@707 924 xmlDoc.appendChild(preTestQuestions);
n@707 925 xmlDoc.appendChild(postTestQuestions);
n@707 926
nicholas@7 927 return xmlDoc;
nicholas@690 928 }