annotate ape.js @ 115:8a87f83c7e65

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