annotate ape.js @ 66:66475d1022ca Dev_main

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