annotate ape.js @ 1000:0dbb90fa6e90

Bug #1257: Made initialiseTest more robust
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 29 May 2015 12:38:26 +0100
parents ffeef0ac7a5f
children 20823d1467b1
rev   line source
nicholas@2 1 /**
nicholas@2 2 * ape.js
nicholas@2 3 * Create the APE interface
nicholas@2 4 */
nicholas@2 5
n@662 6 // preTest - In preTest state
n@662 7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
n@662 8 // testRunPost-ID - Post test of test ID
n@662 9 // testRunPre-ID - Pre-test of test ID
n@662 10 // postTest - End of test, final submission!
n@662 11
n@656 12
nicholas@2 13 // Once this is loaded and parsed, begin execution
nicholas@2 14 loadInterface(projectXML);
nicholas@2 15
nicholas@2 16 function loadInterface(xmlDoc) {
nicholas@2 17
n@701 18 // Get the dimensions of the screen available to the page
nicholas@2 19 var width = window.innerWidth;
nicholas@2 20 var height = window.innerHeight;
nicholas@2 21
nicholas@2 22 // The injection point into the HTML page
nicholas@2 23 var insertPoint = document.getElementById("topLevelBody");
n@706 24 var testContent = document.createElement('div');
nicholas@934 25
n@706 26 testContent.id = 'testContent';
nicholas@2 27
nicholas@7 28
nicholas@2 29 // Decode parts of the xmlDoc that are needed
nicholas@2 30 // xmlDoc MUST already be parsed by jQuery!
nicholas@2 31 var xmlSetup = xmlDoc.find('setup');
nicholas@2 32 // Should put in an error function here incase of malprocessed or malformed XML
nicholas@2 33
nicholas@964 34 // Create pre and post test questions
nicholas@964 35
nicholas@964 36 var preTest = xmlSetup.find('PreTest');
nicholas@964 37 var postTest = xmlSetup.find('PostTest');
nicholas@964 38 preTest = preTest[0];
nicholas@964 39 postTest = postTest[0];
nicholas@964 40
nicholas@964 41 if (preTest == undefined) {preTest = document.createElement("preTest");}
nicholas@964 42 if (postTest == undefined){postTest= document.createElement("postTest");}
nicholas@964 43
nicholas@964 44 testState.stateMap.push(preTest);
nicholas@964 45
n@658 46 // Extract the different test XML DOM trees
n@674 47 var audioHolders = xmlDoc.find('audioHolder');
nicholas@964 48 var testXMLSetups = [];
n@674 49 audioHolders.each(function(index,element) {
n@674 50 var repeatN = element.attributes['repeatCount'].value;
n@674 51 for (var r=0; r<=repeatN; r++) {
nicholas@964 52 testXMLSetups.push(element);
n@674 53 }
n@674 54 });
n@668 55
n@674 56 // New check if we need to randomise the test order
n@674 57 var randomise = xmlSetup[0].attributes['randomiseOrder'];
n@674 58 if (randomise != undefined) {
nicholas@946 59 if (randomise.value === 'true'){
nicholas@946 60 randomise = true;
nicholas@946 61 } else {
nicholas@946 62 randomise = false;
nicholas@946 63 }
n@674 64 } else {
n@674 65 randomise = false;
n@674 66 }
nicholas@946 67
n@674 68 if (randomise)
n@674 69 {
n@678 70 testXMLSetups = randomiseOrder(testXMLSetups);
n@674 71 }
nicholas@964 72
nicholas@964 73 $(testXMLSetups).each(function(index,elem){
nicholas@964 74 testState.stateMap.push(elem);
nicholas@964 75 })
nicholas@964 76
nicholas@964 77 testState.stateMap.push(postTest);
n@668 78
n@674 79 // Obtain the metrics enabled
n@674 80 var metricNode = xmlSetup.find('Metric');
n@674 81 var metricNode = metricNode.find('metricEnable');
n@674 82 metricNode.each(function(index,node){
n@674 83 var enabled = node.textContent;
n@674 84 switch(enabled)
n@674 85 {
n@674 86 case 'testTimer':
n@674 87 sessionMetrics.prototype.enableTestTimer = true;
n@674 88 break;
n@674 89 case 'elementTimer':
n@674 90 sessionMetrics.prototype.enableElementTimer = true;
n@674 91 break;
n@674 92 case 'elementTracker':
n@674 93 sessionMetrics.prototype.enableElementTracker = true;
n@674 94 break;
n@674 95 case 'elementInitalPosition':
n@674 96 sessionMetrics.prototype.enableElementInitialPosition = true;
n@674 97 break;
n@674 98 case 'elementFlagListenedTo':
n@674 99 sessionMetrics.prototype.enableFlagListenedTo = true;
n@674 100 break;
n@674 101 case 'elementFlagMoved':
n@674 102 sessionMetrics.prototype.enableFlagMoved = true;
n@674 103 break;
n@674 104 case 'elementFlagComments':
n@674 105 sessionMetrics.prototype.enableFlagComments = true;
n@674 106 break;
n@674 107 }
n@674 108 });
n@658 109
n@676 110 // Create APE specific metric functions
n@676 111 audioEngineContext.metric.initialiseTest = function()
n@676 112 {
n@676 113 var sliders = document.getElementsByClassName('track-slider');
n@676 114 for (var i=0; i<sliders.length; i++)
n@676 115 {
n@676 116 audioEngineContext.audioObjects[i].metric.initialised(convSliderPosToRate(i));
n@676 117 }
n@676 118 };
n@676 119
n@676 120 audioEngineContext.metric.sliderMoveStart = function(id)
n@676 121 {
n@676 122 if (this.data == -1)
n@676 123 {
n@676 124 this.data = id;
n@676 125 } else {
n@676 126 console.log('ERROR: Metric tracker detecting two moves!');
n@676 127 this.data = -1;
n@676 128 }
n@676 129 };
n@676 130 audioEngineContext.metric.sliderMoved = function()
n@676 131 {
n@676 132 var time = audioEngineContext.timer.getTestTime();
n@676 133 var id = this.data;
n@676 134 this.data = -1;
n@676 135 var position = convSliderPosToRate(id);
BrechtDeMan@936 136 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
n@676 137 if (audioEngineContext.timer.testStarted)
n@676 138 {
n@676 139 audioEngineContext.audioObjects[id].metric.moved(time,position);
n@676 140 }
n@676 141 };
n@676 142
n@676 143 audioEngineContext.metric.sliderPlayed = function(id)
n@676 144 {
n@676 145 var time = audioEngineContext.timer.getTestTime();
n@676 146 if (audioEngineContext.timer.testStarted)
n@676 147 {
n@676 148 if (this.lastClicked >= 0)
n@676 149 {
n@676 150 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
n@676 151 }
n@676 152 this.lastClicked = id;
n@676 153 audioEngineContext.audioObjects[id].metric.listening(time);
n@676 154 }
BrechtDeMan@936 155 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@676 156 };
n@676 157
nicholas@2 158 // Create the top div for the Title element
nicholas@2 159 var titleAttr = xmlSetup[0].attributes['title'];
nicholas@2 160 var title = document.createElement('div');
nicholas@2 161 title.className = "title";
nicholas@2 162 title.align = "center";
nicholas@2 163 var titleSpan = document.createElement('span');
nicholas@2 164
nicholas@2 165 // Set title to that defined in XML, else set to default
nicholas@2 166 if (titleAttr != undefined) {
n@709 167 titleSpan.innerHTML = titleAttr.value;
nicholas@2 168 } else {
BrechtDeMan@975 169 titleSpan.innerHTML = 'Listening test';
nicholas@2 170 }
nicholas@2 171 // Insert the titleSpan element into the title div element.
nicholas@2 172 title.appendChild(titleSpan);
nicholas@2 173
n@671 174 var pagetitle = document.createElement('div');
n@671 175 pagetitle.className = "pageTitle";
n@671 176 pagetitle.align = "center";
n@671 177 var titleSpan = document.createElement('span');
n@671 178 titleSpan.id = "pageTitle";
n@671 179 pagetitle.appendChild(titleSpan);
n@671 180
nicholas@7 181 // Store the return URL path in global projectReturn
nicholas@7 182 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
nicholas@7 183
nicholas@7 184 // Create Interface buttons!
nicholas@7 185 var interfaceButtons = document.createElement('div');
nicholas@7 186 interfaceButtons.id = 'interface-buttons';
nicholas@7 187
nicholas@7 188 // MANUAL DOWNLOAD POINT
nicholas@7 189 // If project return is null, this MUST be specified as the location to create the download link
nicholas@7 190 var downloadPoint = document.createElement('div');
nicholas@7 191 downloadPoint.id = 'download-point';
nicholas@7 192
nicholas@7 193 // Create playback start/stop points
nicholas@7 194 var playback = document.createElement("button");
BrechtDeMan@939 195 playback.innerHTML = 'Stop';
n@673 196 playback.id = 'playback-button';
n@701 197 // onclick function. Check if it is playing or not, call the correct function in the
n@701 198 // audioEngine, change the button text to reflect the next state.
nicholas@7 199 playback.onclick = function() {
BrechtDeMan@939 200 if (audioEngineContext.status == 1) {
BrechtDeMan@939 201 audioEngineContext.stop();
n@709 202 this.innerHTML = 'Stop';
BrechtDeMan@936 203 var time = audioEngineContext.timer.getTestTime();
BrechtDeMan@936 204 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@7 205 }
n@701 206 };
nicholas@7 207 // Create Submit (save) button
nicholas@7 208 var submit = document.createElement("button");
n@709 209 submit.innerHTML = 'Submit';
n@667 210 submit.onclick = buttonSubmitClick;
n@673 211 submit.id = 'submit-button';
n@701 212 // Append the interface buttons into the interfaceButtons object.
nicholas@7 213 interfaceButtons.appendChild(playback);
nicholas@7 214 interfaceButtons.appendChild(submit);
nicholas@7 215 interfaceButtons.appendChild(downloadPoint);
nicholas@7 216
nicholas@2 217 // Now create the slider and HTML5 canvas boxes
nicholas@2 218
n@701 219 // Create the div box to center align
nicholas@2 220 var sliderBox = document.createElement('div');
nicholas@2 221 sliderBox.className = 'sliderCanvasDiv';
n@701 222 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 223
n@701 224 // Create the slider box to hold the slider elements
nicholas@5 225 var canvas = document.createElement('div');
nicholas@2 226 canvas.id = 'slider';
n@963 227 canvas.align = "left";
n@963 228 canvas.addEventListener('dragover',function(event){
n@963 229 event.preventDefault();
n@963 230 return false;
n@963 231 },false);
n@963 232 var sliderMargin = document.createAttribute('marginsize');
n@963 233 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
n@701 234 // Must have a known EXACT width, as this is used later to determine the ratings
n@963 235 var w = (Number(sliderMargin.nodeValue)+8)*2;
n@963 236 canvas.style.width = width - w +"px";
n@963 237 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
n@963 238 canvas.setAttributeNode(sliderMargin);
nicholas@2 239 sliderBox.appendChild(canvas);
n@701 240
n@671 241 // Create the div to hold any scale objects
n@671 242 var scale = document.createElement('div');
n@671 243 scale.className = 'sliderScale';
n@671 244 scale.id = 'sliderScaleHolder';
n@671 245 scale.align = 'left';
n@671 246 sliderBox.appendChild(scale);
n@671 247
n@701 248 // Global parent for the comment boxes on the page
nicholas@3 249 var feedbackHolder = document.createElement('div');
n@658 250 feedbackHolder.id = 'feedbackHolder';
n@656 251
n@662 252 testContent.style.zIndex = 1;
n@662 253 insertPoint.innerHTML = null; // Clear the current schema
n@662 254
n@662 255 currentState = 'preTest';
n@656 256
n@662 257 // Inject into HTML
n@662 258 testContent.appendChild(title); // Insert the title
n@671 259 testContent.appendChild(pagetitle);
n@662 260 testContent.appendChild(interfaceButtons);
n@662 261 testContent.appendChild(sliderBox);
n@662 262 testContent.appendChild(feedbackHolder);
n@662 263 insertPoint.appendChild(testContent);
n@656 264
n@660 265 // Load the full interface
nicholas@964 266 testState.initialise();
nicholas@964 267 testState.advanceState();
n@663 268
nicholas@934 269 testWaitIndicator();
n@656 270 }
n@656 271
nicholas@964 272 function loadTest(textXML)
n@658 273 {
nicholas@947 274
nicholas@947 275 // Reset audioEngineContext.Metric globals for new test
n@950 276 audioEngineContext.newTestPage();
nicholas@947 277
nicholas@964 278 var id = textXML.id;
n@658 279
n@658 280 var feedbackHolder = document.getElementById('feedbackHolder');
n@658 281 var canvas = document.getElementById('slider');
n@658 282 feedbackHolder.innerHTML = null;
n@658 283 canvas.innerHTML = null;
n@671 284
n@671 285 // Setup question title
n@671 286 var interfaceObj = $(textXML).find('interface');
n@671 287 var titleNode = interfaceObj.find('title');
n@671 288 if (titleNode[0] != undefined)
n@671 289 {
n@671 290 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
n@671 291 }
n@671 292 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
n@963 293 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
n@671 294 var scale = document.getElementById('sliderScaleHolder');
n@671 295 scale.innerHTML = null;
n@671 296 interfaceObj.find('scale').each(function(index,scaleObj){
n@963 297 var value = document.createAttribute('value');
n@671 298 var position = Number(scaleObj.attributes['position'].value)*0.01;
n@963 299 value.nodeValue = position;
n@671 300 var pixelPosition = (position*positionScale)+offset;
n@671 301 var scaleDOM = document.createElement('span');
n@671 302 scaleDOM.textContent = scaleObj.textContent;
n@671 303 scale.appendChild(scaleDOM);
n@671 304 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
n@963 305 scaleDOM.setAttributeNode(value);
n@671 306 });
n@658 307
n@701 308 // Extract the hostURL attribute. If not set, create an empty string.
n@658 309 var hostURL = textXML.attributes['hostURL'];
nicholas@7 310 if (hostURL == undefined) {
nicholas@7 311 hostURL = "";
nicholas@7 312 } else {
nicholas@7 313 hostURL = hostURL.value;
nicholas@7 314 }
n@701 315 // Extract the sampleRate. If set, convert the string to a Number.
n@658 316 var hostFs = textXML.attributes['sampleRate'];
n@700 317 if (hostFs != undefined) {
n@700 318 hostFs = Number(hostFs.value);
nicholas@7 319 }
nicholas@7 320
nicholas@7 321 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@700 322 if (hostFs != undefined) {
nicholas@7 323 if (Number(hostFs) != audioContext.sampleRate) {
nicholas@7 324 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nicholas@7 325 alert(errStr);
nicholas@7 326 return;
nicholas@7 327 }
nicholas@7 328 }
n@669 329
nicholas@689 330 var commentShow = textXML.attributes['elementComments'];
nicholas@689 331 if (commentShow != undefined) {
nicholas@689 332 if (commentShow.value == 'false') {commentShow = false;}
nicholas@689 333 else {commentShow = true;}
nicholas@689 334 } else {commentShow = true;}
nicholas@689 335
n@681 336 var loopPlayback = textXML.attributes['loop'];
n@681 337 if (loopPlayback != undefined)
n@681 338 {
n@681 339 loopPlayback = loopPlayback.value;
n@681 340 if (loopPlayback == 'true') {
n@681 341 loopPlayback = true;
n@681 342 } else {
n@681 343 loopPlayback = false;
n@681 344 }
n@681 345 } else {
n@681 346 loopPlayback = false;
n@681 347 }
n@681 348 audioEngineContext.loopPlayback = loopPlayback;
n@681 349 // Create AudioEngine bindings for playback
n@681 350 if (loopPlayback) {
n@681 351 audioEngineContext.selectedTrack = function(id) {
n@681 352 for (var i=0; i<this.audioObjects.length; i++)
n@681 353 {
n@681 354 if (id == i) {
nicholas@967 355 this.audioObjects[i].loopStart();
n@681 356 } else {
nicholas@967 357 this.audioObjects[i].loopStop();
n@681 358 }
n@681 359 }
n@681 360 };
n@681 361 } else {
n@681 362 audioEngineContext.selectedTrack = function(id) {
n@681 363 for (var i=0; i<this.audioObjects.length; i++)
n@681 364 {
n@996 365 this.audioObjects[i].outputGain.gain.value = 0.0;
n@996 366 this.audioObjects[i].stop();
n@681 367 }
n@998 368 if (this.status == 1) {
n@998 369 this.audioObjects[id].outputGain.gain.value = 1.0;
n@998 370 this.audioObjects[id].play(audioContext.currentTime+0.01);
BrechtDeMan@969 371 this.audioObjects[id].flagAsPlayed();
n@998 372 }
n@681 373 };
n@681 374 }
n@681 375
n@670 376 currentTestHolder = document.createElement('audioHolder');
n@670 377 currentTestHolder.id = textXML.id;
n@670 378 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
n@670 379
n@669 380 var randomise = textXML.attributes['randomiseOrder'];
n@669 381 if (randomise != undefined) {randomise = randomise.value;}
n@669 382 else {randomise = false;}
n@669 383
n@658 384 var audioElements = $(textXML).find('audioElements');
nicholas@682 385 currentTrackOrder = [];
n@658 386 audioElements.each(function(index,element){
n@669 387 // Find any blind-repeats
BrechtDeMan@938 388 // Not implemented yet, but just in case
n@669 389 currentTrackOrder[index] = element;
n@669 390 });
n@669 391 if (randomise) {
n@678 392 currentTrackOrder = randomiseOrder(currentTrackOrder);
n@669 393 }
n@669 394
nicholas@682 395 // Delete any previous audioObjects associated with the audioEngine
nicholas@682 396 audioEngineContext.audioObjects = [];
nicholas@682 397
n@701 398 // Find all the audioElements from the audioHolder
n@669 399 $(currentTrackOrder).each(function(index,element){
nicholas@7 400 // Find URL of track
n@701 401 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 402
n@701 403 // Now load each audio sample. First create the new track by passing the full URL
nicholas@7 404 var trackURL = hostURL + this.attributes['url'].value;
nicholas@7 405 audioEngineContext.newTrack(trackURL);
nicholas@689 406
nicholas@689 407 if (commentShow) {
nicholas@689 408 // Create document objects to hold the comment boxes
nicholas@689 409 var trackComment = document.createElement('div');
nicholas@689 410 trackComment.className = 'comment-div';
nicholas@689 411 // Create a string next to each comment asking for a comment
nicholas@689 412 var trackString = document.createElement('span');
nicholas@689 413 trackString.innerHTML = 'Comment on track '+index;
nicholas@689 414 // Create the HTML5 comment box 'textarea'
nicholas@689 415 var trackCommentBox = document.createElement('textarea');
nicholas@689 416 trackCommentBox.rows = '4';
nicholas@689 417 trackCommentBox.cols = '100';
nicholas@689 418 trackCommentBox.name = 'trackComment'+index;
nicholas@689 419 trackCommentBox.className = 'trackComment';
nicholas@689 420 var br = document.createElement('br');
nicholas@689 421 // Add to the holder.
nicholas@689 422 trackComment.appendChild(trackString);
nicholas@689 423 trackComment.appendChild(br);
nicholas@689 424 trackComment.appendChild(trackCommentBox);
nicholas@689 425 feedbackHolder.appendChild(trackComment);
nicholas@689 426 }
n@701 427
nicholas@5 428 // Create a slider per track
nicholas@5 429
nicholas@5 430 var trackSliderObj = document.createElement('div');
nicholas@5 431 trackSliderObj.className = 'track-slider';
nicholas@5 432 trackSliderObj.id = 'track-slider-'+index;
nicholas@5 433 // Distribute it randomnly
n@1000 434 var w = window.innerWidth - (offset+8)*2;
nicholas@5 435 w = Math.random()*w;
n@1000 436 w = Math.floor(w+(offset+8));
n@1000 437 trackSliderObj.style.left = w+'px';
nicholas@5 438 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nicholas@5 439 trackSliderObj.draggable = true;
nicholas@5 440 trackSliderObj.ondragend = dragEnd;
n@673 441 trackSliderObj.ondragstart = function()
n@673 442 {
n@673 443 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 444 audioEngineContext.metric.sliderMoveStart(id);
n@673 445 };
nicholas@8 446
nicholas@8 447 // Onclick, switch playback to that track
nicholas@8 448 trackSliderObj.onclick = function() {
nicholas@945 449 // Start the test on first click, that way timings are more accurate.
nicholas@945 450 audioEngineContext.play();
nicholas@934 451 if (audioEngineContext.audioObjectsReady) {
nicholas@934 452 // Cannot continue to issue play command until audioObjects reported as ready!
nicholas@934 453 // Get the track ID from the object ID
nicholas@934 454 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
nicholas@934 455 //audioEngineContext.metric.sliderPlayed(id);
nicholas@934 456 audioEngineContext.selectedTrack(id);
nicholas@934 457 // Currently playing track red, rest green
nicholas@934 458 document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nicholas@934 459 for (var i = 0; i<$(currentTrackOrder).length; i++)
nicholas@934 460 {
nicholas@934 461 if (i!=index) // Make all other sliders green
nicholas@934 462 {
nicholas@934 463 document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)";
nicholas@934 464 }
nicholas@934 465
nicholas@934 466 }
nicholas@934 467 }
n@700 468 };
nicholas@8 469
nicholas@5 470 canvas.appendChild(trackSliderObj);
n@1000 471 audioEngineContext.audioObjects[index].metric.initialised(convSliderPosToRate(index));
BrechtDeMan@940 472
n@700 473 });
nicholas@3 474
nicholas@688 475 // Append any commentQuestion boxes
nicholas@688 476 var commentQuestions = $(textXML).find('CommentQuestion');
nicholas@688 477 $(commentQuestions).each(function(index,element) {
nicholas@688 478 // Create document objects to hold the comment boxes
nicholas@688 479 var trackComment = document.createElement('div');
nicholas@688 480 trackComment.className = 'comment-div commentQuestion';
nicholas@688 481 trackComment.id = element.attributes['id'].value;
nicholas@688 482 // Create a string next to each comment asking for a comment
nicholas@688 483 var trackString = document.createElement('span');
nicholas@688 484 trackString.innerHTML = element.textContent;
nicholas@688 485 // Create the HTML5 comment box 'textarea'
nicholas@688 486 var trackCommentBox = document.createElement('textarea');
nicholas@688 487 trackCommentBox.rows = '4';
nicholas@688 488 trackCommentBox.cols = '100';
nicholas@688 489 trackCommentBox.name = 'commentQuestion'+index;
nicholas@688 490 trackCommentBox.className = 'trackComment';
nicholas@688 491 var br = document.createElement('br');
nicholas@688 492 // Add to the holder.
nicholas@688 493 trackComment.appendChild(trackString);
nicholas@688 494 trackComment.appendChild(br);
nicholas@688 495 trackComment.appendChild(trackCommentBox);
nicholas@688 496 feedbackHolder.appendChild(trackComment);
nicholas@688 497 });
n@663 498 }
n@663 499
nicholas@5 500
nicholas@5 501 function dragEnd(ev) {
nicholas@5 502 // Function call when a div has been dropped
n@657 503 var slider = document.getElementById('slider');
n@963 504 var marginSize = Number(slider.attributes['marginsize'].value);
n@675 505 var w = slider.style.width;
n@675 506 w = Number(w.substr(0,w.length-2));
n@675 507 var x = ev.x;
n@963 508 if (x >= marginSize && x < w+marginSize) {
n@675 509 this.style.left = (x)+'px';
nicholas@5 510 } else {
n@963 511 if (x<marginSize) {
n@963 512 this.style.left = marginSize+'px';
nicholas@5 513 } else {
n@963 514 this.style.left = (w+marginSize) + 'px';
nicholas@5 515 }
nicholas@5 516 }
n@673 517 audioEngineContext.metric.sliderMoved();
n@656 518 }
n@656 519
BrechtDeMan@939 520 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@663 521 {
nicholas@944 522 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@944 523 if (hasBeenPlayed.length == 0) {
nicholas@944 524 if (audioEngineContext.status == 1) {
nicholas@944 525 var playback = document.getElementById('playback-button');
nicholas@944 526 playback.click();
nicholas@944 527 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nicholas@944 528 } else
nicholas@944 529 {
nicholas@944 530 if (audioEngineContext.timer.testStarted == false)
nicholas@944 531 {
nicholas@944 532 alert('You have not started the test! Please press start to begin the test!');
nicholas@944 533 return;
nicholas@944 534 }
nicholas@944 535 }
nicholas@964 536 testState.advanceState();
BrechtDeMan@940 537 } else // if a fragment has not been played yet
BrechtDeMan@940 538 {
nicholas@944 539 str = "";
nicholas@944 540 if (hasBeenPlayed.length > 1) {
nicholas@944 541 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@944 542 str = str + hasBeenPlayed[i];
nicholas@944 543 if (i < hasBeenPlayed.length-2){
nicholas@944 544 str += ", ";
nicholas@944 545 } else if (i == hasBeenPlayed.length-2) {
nicholas@944 546 str += " or ";
nicholas@944 547 }
nicholas@944 548 }
nicholas@944 549 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@944 550 } else {
nicholas@944 551 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@944 552 }
BrechtDeMan@940 553 return;
BrechtDeMan@940 554 }
n@664 555 }
n@664 556
n@675 557 function convSliderPosToRate(id)
n@675 558 {
n@675 559 var w = document.getElementById('slider').style.width;
n@963 560 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@675 561 var maxPix = w.substr(0,w.length-2);
n@675 562 var slider = document.getElementsByClassName('track-slider')[id];
n@675 563 var pix = slider.style.left;
n@675 564 pix = pix.substr(0,pix.length-2);
n@963 565 var rate = (pix-marginsize)/maxPix;
n@675 566 return rate;
n@675 567 }
n@675 568
n@963 569 function resizeWindow(event){
n@963 570 // Function called when the window has been resized.
n@963 571 // MANDATORY FUNCTION
n@963 572
n@963 573 // Store the slider marker values
n@963 574 var holdValues = [];
n@963 575 $(".track-slider").each(function(index,sliderObj){
n@963 576 holdValues.push(convSliderPosToRate(index));
n@963 577 });
n@963 578
n@963 579 var width = event.target.innerWidth;
n@963 580 var canvas = document.getElementById('sliderCanvasHolder');
n@963 581 var sliderDiv = canvas.children[0];
n@963 582 var sliderScaleDiv = canvas.children[1];
n@963 583 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@963 584 var w = (marginsize+8)*2;
n@963 585 sliderDiv.style.width = width - w + 'px';
n@963 586 var width = width - w;
n@963 587 // Move sliders into new position
n@963 588 $(".track-slider").each(function(index,sliderObj){
n@963 589 var pos = holdValues[index];
n@963 590 var pix = pos * width;
n@963 591 sliderObj.style.left = pix+marginsize+'px';
n@963 592 });
n@963 593
n@963 594 // Move scale labels
n@963 595 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@963 596 var position = Number(scaleObj.attributes['value'].value);
n@963 597 var pixelPosition = (position*width)+marginsize;
n@963 598 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@963 599 });
n@963 600 }
n@963 601
nicholas@964 602 function pageXMLSave(store, testXML, testId)
n@668 603 {
n@668 604 // Saves a specific test page
nicholas@964 605 var xmlDoc = store;
n@674 606 // Check if any session wide metrics are enabled
nicholas@690 607
nicholas@964 608 var commentShow = testXML.attributes['elementComments'];
nicholas@690 609 if (commentShow != undefined) {
nicholas@690 610 if (commentShow.value == 'false') {commentShow = false;}
nicholas@690 611 else {commentShow = true;}
nicholas@690 612 } else {commentShow = true;}
nicholas@690 613
n@674 614 var metric = document.createElement('metric');
n@674 615 if (audioEngineContext.metric.enableTestTimer)
n@674 616 {
n@674 617 var testTime = document.createElement('metricResult');
n@674 618 testTime.id = 'testTime';
n@674 619 testTime.textContent = audioEngineContext.timer.testDuration;
n@674 620 metric.appendChild(testTime);
n@674 621 }
n@674 622 xmlDoc.appendChild(metric);
n@669 623 var trackSliderObjects = document.getElementsByClassName('track-slider');
n@669 624 var commentObjects = document.getElementsByClassName('comment-div');
n@669 625 for (var i=0; i<trackSliderObjects.length; i++)
n@669 626 {
n@669 627 var audioElement = document.createElement('audioElement');
n@669 628 audioElement.id = currentTrackOrder[i].attributes['id'].value;
n@669 629 audioElement.url = currentTrackOrder[i].attributes['url'].value;
n@675 630 var value = document.createElement('value');
n@675 631 value.innerHTML = convSliderPosToRate(i);
nicholas@690 632 if (commentShow) {
nicholas@690 633 var comment = document.createElement("comment");
nicholas@690 634 var question = document.createElement("question");
nicholas@690 635 var response = document.createElement("response");
nicholas@690 636 question.textContent = commentObjects[i].children[0].textContent;
nicholas@690 637 response.textContent = commentObjects[i].children[2].value;
BrechtDeMan@936 638 console.log('Comment ' + i + ': ' + commentObjects[i].children[2].value); // DEBUG/SAFETY
nicholas@690 639 comment.appendChild(question);
nicholas@690 640 comment.appendChild(response);
nicholas@690 641 audioElement.appendChild(comment);
nicholas@690 642 }
n@669 643 audioElement.appendChild(value);
n@674 644 // Check for any per element metrics
n@674 645 var metric = document.createElement('metric');
n@674 646 var elementMetric = audioEngineContext.audioObjects[i].metric;
n@674 647 if (audioEngineContext.metric.enableElementTimer) {
n@674 648 var elementTimer = document.createElement('metricResult');
n@674 649 elementTimer.id = 'elementTimer';
n@674 650 elementTimer.textContent = elementMetric.listenedTimer;
n@674 651 metric.appendChild(elementTimer);
n@674 652 }
n@674 653 if (audioEngineContext.metric.enableElementTracker) {
n@674 654 var elementTrackerFull = document.createElement('metricResult');
n@674 655 elementTrackerFull.id = 'elementTrackerFull';
n@674 656 var data = elementMetric.movementTracker;
n@674 657 for (var k=0; k<data.length; k++)
n@674 658 {
n@674 659 var timePos = document.createElement('timePos');
n@674 660 timePos.id = k;
n@674 661 var time = document.createElement('time');
n@674 662 time.textContent = data[k][0];
n@674 663 var position = document.createElement('position');
n@674 664 position.textContent = data[k][1];
n@674 665 timePos.appendChild(time);
n@674 666 timePos.appendChild(position);
n@674 667 elementTrackerFull.appendChild(timePos);
n@674 668 }
n@674 669 metric.appendChild(elementTrackerFull);
n@674 670 }
n@674 671 if (audioEngineContext.metric.enableElementInitialPosition) {
n@674 672 var elementInitial = document.createElement('metricResult');
n@674 673 elementInitial.id = 'elementInitialPosition';
n@674 674 elementInitial.textContent = elementMetric.initialPosition;
n@674 675 metric.appendChild(elementInitial);
n@674 676 }
n@674 677 if (audioEngineContext.metric.enableFlagListenedTo) {
n@674 678 var flagListenedTo = document.createElement('metricResult');
n@674 679 flagListenedTo.id = 'elementFlagListenedTo';
n@674 680 flagListenedTo.textContent = elementMetric.wasListenedTo;
n@674 681 metric.appendChild(flagListenedTo);
n@674 682 }
n@674 683 if (audioEngineContext.metric.enableFlagMoved) {
n@674 684 var flagMoved = document.createElement('metricResult');
n@674 685 flagMoved.id = 'elementFlagMoved';
n@674 686 flagMoved.textContent = elementMetric.wasMoved;
n@674 687 metric.appendChild(flagMoved);
n@674 688 }
n@674 689 if (audioEngineContext.metric.enableFlagComments) {
n@674 690 var flagComments = document.createElement('metricResult');
n@674 691 flagComments.id = 'elementFlagComments';
n@674 692 if (response.textContent.length == 0) {flag.textContent = 'false';}
n@674 693 else {flag.textContet = 'true';}
n@674 694 metric.appendChild(flagComments);
n@674 695 }
n@674 696 audioElement.appendChild(metric);
n@669 697 xmlDoc.appendChild(audioElement);
n@669 698 }
nicholas@688 699 var commentQuestion = document.getElementsByClassName('commentQuestion');
nicholas@688 700 for (var i=0; i<commentQuestion.length; i++)
nicholas@688 701 {
nicholas@688 702 var cqHolder = document.createElement('CommentQuestion');
nicholas@688 703 var comment = document.createElement('comment');
nicholas@688 704 var question = document.createElement('question');
nicholas@688 705 cqHolder.id = commentQuestion[i].id;
nicholas@688 706 comment.textContent = commentQuestion[i].children[2].value;
nicholas@688 707 question.textContent = commentQuestion[i].children[0].textContent;
n@960 708 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
nicholas@688 709 cqHolder.appendChild(question);
nicholas@688 710 cqHolder.appendChild(comment);
nicholas@688 711 xmlDoc.appendChild(cqHolder);
nicholas@688 712 }
nicholas@964 713 store = xmlDoc;
nicholas@690 714 }