annotate ape.js @ 110:94b91d63e286

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