annotate ape.js @ 1007:e5d57ea0e3c9

Feature #1252: Test Wait Indicator now fully implemented with auto clearing on test ready.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 01 Jun 2015 09:46:51 +0100
parents d141519a99f2
children eefdac420017
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 };
n@676 114
n@676 115 audioEngineContext.metric.sliderMoveStart = function(id)
n@676 116 {
n@676 117 if (this.data == -1)
n@676 118 {
n@676 119 this.data = id;
n@676 120 } else {
n@676 121 console.log('ERROR: Metric tracker detecting two moves!');
n@676 122 this.data = -1;
n@676 123 }
n@676 124 };
n@676 125 audioEngineContext.metric.sliderMoved = function()
n@676 126 {
n@676 127 var time = audioEngineContext.timer.getTestTime();
n@676 128 var id = this.data;
n@676 129 this.data = -1;
n@676 130 var position = convSliderPosToRate(id);
BrechtDeMan@936 131 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
n@676 132 if (audioEngineContext.timer.testStarted)
n@676 133 {
n@676 134 audioEngineContext.audioObjects[id].metric.moved(time,position);
n@676 135 }
n@676 136 };
n@676 137
n@676 138 audioEngineContext.metric.sliderPlayed = function(id)
n@676 139 {
n@676 140 var time = audioEngineContext.timer.getTestTime();
n@676 141 if (audioEngineContext.timer.testStarted)
n@676 142 {
n@676 143 if (this.lastClicked >= 0)
n@676 144 {
n@676 145 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
n@676 146 }
n@676 147 this.lastClicked = id;
n@676 148 audioEngineContext.audioObjects[id].metric.listening(time);
n@676 149 }
BrechtDeMan@936 150 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@676 151 };
n@676 152
nicholas@2 153 // Create the top div for the Title element
nicholas@2 154 var titleAttr = xmlSetup[0].attributes['title'];
nicholas@2 155 var title = document.createElement('div');
nicholas@2 156 title.className = "title";
nicholas@2 157 title.align = "center";
nicholas@2 158 var titleSpan = document.createElement('span');
nicholas@2 159
nicholas@2 160 // Set title to that defined in XML, else set to default
nicholas@2 161 if (titleAttr != undefined) {
n@709 162 titleSpan.innerHTML = titleAttr.value;
nicholas@2 163 } else {
BrechtDeMan@975 164 titleSpan.innerHTML = 'Listening test';
nicholas@2 165 }
nicholas@2 166 // Insert the titleSpan element into the title div element.
nicholas@2 167 title.appendChild(titleSpan);
nicholas@2 168
n@671 169 var pagetitle = document.createElement('div');
n@671 170 pagetitle.className = "pageTitle";
n@671 171 pagetitle.align = "center";
n@671 172 var titleSpan = document.createElement('span');
n@671 173 titleSpan.id = "pageTitle";
n@671 174 pagetitle.appendChild(titleSpan);
n@671 175
nicholas@7 176 // Store the return URL path in global projectReturn
n@933 177 projectReturn = xmlSetup[0].attributes['projectReturn'];
n@933 178 if (projectReturn == undefined) {
n@933 179 console.log("WARNING - projectReturn not specified! Will assume null.");
n@933 180 projectReturn = "null";
n@933 181 } else {
n@933 182 projectReturn = projectReturn.value;
n@933 183 }
nicholas@7 184
nicholas@7 185 // Create Interface buttons!
nicholas@7 186 var interfaceButtons = document.createElement('div');
nicholas@7 187 interfaceButtons.id = 'interface-buttons';
nicholas@7 188
nicholas@7 189 // MANUAL DOWNLOAD POINT
nicholas@7 190 // If project return is null, this MUST be specified as the location to create the download link
nicholas@7 191 var downloadPoint = document.createElement('div');
nicholas@7 192 downloadPoint.id = 'download-point';
nicholas@7 193
nicholas@7 194 // Create playback start/stop points
nicholas@7 195 var playback = document.createElement("button");
BrechtDeMan@939 196 playback.innerHTML = 'Stop';
n@673 197 playback.id = 'playback-button';
n@701 198 // onclick function. Check if it is playing or not, call the correct function in the
n@701 199 // audioEngine, change the button text to reflect the next state.
nicholas@7 200 playback.onclick = function() {
BrechtDeMan@939 201 if (audioEngineContext.status == 1) {
BrechtDeMan@939 202 audioEngineContext.stop();
n@709 203 this.innerHTML = 'Stop';
BrechtDeMan@936 204 var time = audioEngineContext.timer.getTestTime();
BrechtDeMan@936 205 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@7 206 }
n@701 207 };
nicholas@7 208 // Create Submit (save) button
nicholas@7 209 var submit = document.createElement("button");
n@709 210 submit.innerHTML = 'Submit';
n@667 211 submit.onclick = buttonSubmitClick;
n@673 212 submit.id = 'submit-button';
n@701 213 // Append the interface buttons into the interfaceButtons object.
nicholas@7 214 interfaceButtons.appendChild(playback);
nicholas@7 215 interfaceButtons.appendChild(submit);
nicholas@7 216 interfaceButtons.appendChild(downloadPoint);
nicholas@7 217
nicholas@2 218 // Now create the slider and HTML5 canvas boxes
nicholas@2 219
n@701 220 // Create the div box to center align
nicholas@2 221 var sliderBox = document.createElement('div');
nicholas@2 222 sliderBox.className = 'sliderCanvasDiv';
n@701 223 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 224
n@701 225 // Create the slider box to hold the slider elements
nicholas@5 226 var canvas = document.createElement('div');
nicholas@2 227 canvas.id = 'slider';
n@963 228 canvas.align = "left";
n@963 229 canvas.addEventListener('dragover',function(event){
n@963 230 event.preventDefault();
n@963 231 return false;
n@963 232 },false);
n@963 233 var sliderMargin = document.createAttribute('marginsize');
n@963 234 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
n@701 235 // Must have a known EXACT width, as this is used later to determine the ratings
n@963 236 var w = (Number(sliderMargin.nodeValue)+8)*2;
n@963 237 canvas.style.width = width - w +"px";
n@963 238 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
n@963 239 canvas.setAttributeNode(sliderMargin);
nicholas@2 240 sliderBox.appendChild(canvas);
n@701 241
n@671 242 // Create the div to hold any scale objects
n@671 243 var scale = document.createElement('div');
n@671 244 scale.className = 'sliderScale';
n@671 245 scale.id = 'sliderScaleHolder';
n@671 246 scale.align = 'left';
n@671 247 sliderBox.appendChild(scale);
n@671 248
n@701 249 // Global parent for the comment boxes on the page
nicholas@3 250 var feedbackHolder = document.createElement('div');
n@658 251 feedbackHolder.id = 'feedbackHolder';
n@656 252
n@662 253 testContent.style.zIndex = 1;
n@662 254 insertPoint.innerHTML = null; // Clear the current schema
n@662 255
n@662 256 currentState = 'preTest';
n@656 257
n@662 258 // Inject into HTML
n@662 259 testContent.appendChild(title); // Insert the title
n@671 260 testContent.appendChild(pagetitle);
n@662 261 testContent.appendChild(interfaceButtons);
n@662 262 testContent.appendChild(sliderBox);
n@662 263 testContent.appendChild(feedbackHolder);
n@662 264 insertPoint.appendChild(testContent);
n@656 265
n@660 266 // Load the full interface
nicholas@964 267 testState.initialise();
nicholas@964 268 testState.advanceState();
n@663 269
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);
n@998 371 }
n@681 372 };
n@681 373 }
n@681 374
n@670 375 currentTestHolder = document.createElement('audioHolder');
n@670 376 currentTestHolder.id = textXML.id;
n@670 377 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
n@670 378
n@669 379 var randomise = textXML.attributes['randomiseOrder'];
n@669 380 if (randomise != undefined) {randomise = randomise.value;}
n@669 381 else {randomise = false;}
n@669 382
n@658 383 var audioElements = $(textXML).find('audioElements');
nicholas@682 384 currentTrackOrder = [];
n@658 385 audioElements.each(function(index,element){
n@669 386 // Find any blind-repeats
BrechtDeMan@938 387 // Not implemented yet, but just in case
n@669 388 currentTrackOrder[index] = element;
n@669 389 });
n@669 390 if (randomise) {
n@678 391 currentTrackOrder = randomiseOrder(currentTrackOrder);
n@669 392 }
n@669 393
nicholas@682 394 // Delete any previous audioObjects associated with the audioEngine
nicholas@682 395 audioEngineContext.audioObjects = [];
nicholas@682 396
n@701 397 // Find all the audioElements from the audioHolder
n@669 398 $(currentTrackOrder).each(function(index,element){
nicholas@7 399 // Find URL of track
n@701 400 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 401
n@701 402 // Now load each audio sample. First create the new track by passing the full URL
nicholas@7 403 var trackURL = hostURL + this.attributes['url'].value;
nicholas@7 404 audioEngineContext.newTrack(trackURL);
nicholas@689 405
nicholas@689 406 if (commentShow) {
nicholas@689 407 // Create document objects to hold the comment boxes
nicholas@689 408 var trackComment = document.createElement('div');
nicholas@689 409 trackComment.className = 'comment-div';
nicholas@689 410 // Create a string next to each comment asking for a comment
nicholas@689 411 var trackString = document.createElement('span');
nicholas@689 412 trackString.innerHTML = 'Comment on track '+index;
nicholas@689 413 // Create the HTML5 comment box 'textarea'
nicholas@689 414 var trackCommentBox = document.createElement('textarea');
nicholas@689 415 trackCommentBox.rows = '4';
nicholas@689 416 trackCommentBox.cols = '100';
nicholas@689 417 trackCommentBox.name = 'trackComment'+index;
nicholas@689 418 trackCommentBox.className = 'trackComment';
nicholas@689 419 var br = document.createElement('br');
nicholas@689 420 // Add to the holder.
nicholas@689 421 trackComment.appendChild(trackString);
nicholas@689 422 trackComment.appendChild(br);
nicholas@689 423 trackComment.appendChild(trackCommentBox);
nicholas@689 424 feedbackHolder.appendChild(trackComment);
nicholas@689 425 }
n@701 426
nicholas@5 427 // Create a slider per track
nicholas@5 428
nicholas@5 429 var trackSliderObj = document.createElement('div');
nicholas@5 430 trackSliderObj.className = 'track-slider';
nicholas@5 431 trackSliderObj.id = 'track-slider-'+index;
nicholas@5 432 // Distribute it randomnly
n@1000 433 var w = window.innerWidth - (offset+8)*2;
nicholas@5 434 w = Math.random()*w;
n@1000 435 w = Math.floor(w+(offset+8));
n@1000 436 trackSliderObj.style.left = w+'px';
nicholas@5 437 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nicholas@5 438 trackSliderObj.draggable = true;
nicholas@5 439 trackSliderObj.ondragend = dragEnd;
n@673 440 trackSliderObj.ondragstart = function()
n@673 441 {
n@673 442 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
n@673 443 audioEngineContext.metric.sliderMoveStart(id);
n@673 444 };
nicholas@8 445
nicholas@8 446 // Onclick, switch playback to that track
nicholas@8 447 trackSliderObj.onclick = function() {
nicholas@945 448 // Start the test on first click, that way timings are more accurate.
nicholas@945 449 audioEngineContext.play();
nicholas@934 450 if (audioEngineContext.audioObjectsReady) {
nicholas@934 451 // Cannot continue to issue play command until audioObjects reported as ready!
nicholas@934 452 // Get the track ID from the object ID
nicholas@934 453 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
nicholas@934 454 //audioEngineContext.metric.sliderPlayed(id);
nicholas@934 455 audioEngineContext.selectedTrack(id);
nicholas@934 456 // Currently playing track red, rest green
nicholas@934 457 document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nicholas@934 458 for (var i = 0; i<$(currentTrackOrder).length; i++)
nicholas@934 459 {
nicholas@934 460 if (i!=index) // Make all other sliders green
nicholas@934 461 {
nicholas@934 462 document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)";
nicholas@934 463 }
nicholas@934 464
nicholas@934 465 }
nicholas@934 466 }
n@700 467 };
nicholas@8 468
nicholas@5 469 canvas.appendChild(trackSliderObj);
n@1000 470 audioEngineContext.audioObjects[index].metric.initialised(convSliderPosToRate(index));
BrechtDeMan@940 471
n@700 472 });
nicholas@3 473
nicholas@688 474 // Append any commentQuestion boxes
nicholas@688 475 var commentQuestions = $(textXML).find('CommentQuestion');
nicholas@688 476 $(commentQuestions).each(function(index,element) {
nicholas@688 477 // Create document objects to hold the comment boxes
nicholas@688 478 var trackComment = document.createElement('div');
nicholas@688 479 trackComment.className = 'comment-div commentQuestion';
nicholas@688 480 trackComment.id = element.attributes['id'].value;
nicholas@688 481 // Create a string next to each comment asking for a comment
nicholas@688 482 var trackString = document.createElement('span');
nicholas@688 483 trackString.innerHTML = element.textContent;
nicholas@688 484 // Create the HTML5 comment box 'textarea'
nicholas@688 485 var trackCommentBox = document.createElement('textarea');
nicholas@688 486 trackCommentBox.rows = '4';
nicholas@688 487 trackCommentBox.cols = '100';
nicholas@688 488 trackCommentBox.name = 'commentQuestion'+index;
nicholas@688 489 trackCommentBox.className = 'trackComment';
nicholas@688 490 var br = document.createElement('br');
nicholas@688 491 // Add to the holder.
nicholas@688 492 trackComment.appendChild(trackString);
nicholas@688 493 trackComment.appendChild(br);
nicholas@688 494 trackComment.appendChild(trackCommentBox);
nicholas@688 495 feedbackHolder.appendChild(trackComment);
nicholas@688 496 });
n@1007 497
n@1007 498
n@1007 499 testWaitIndicator();
n@663 500 }
n@663 501
nicholas@5 502
nicholas@5 503 function dragEnd(ev) {
nicholas@5 504 // Function call when a div has been dropped
n@657 505 var slider = document.getElementById('slider');
n@963 506 var marginSize = Number(slider.attributes['marginsize'].value);
n@675 507 var w = slider.style.width;
n@675 508 w = Number(w.substr(0,w.length-2));
n@675 509 var x = ev.x;
n@963 510 if (x >= marginSize && x < w+marginSize) {
n@675 511 this.style.left = (x)+'px';
nicholas@5 512 } else {
n@963 513 if (x<marginSize) {
n@963 514 this.style.left = marginSize+'px';
nicholas@5 515 } else {
n@963 516 this.style.left = (w+marginSize) + 'px';
nicholas@5 517 }
nicholas@5 518 }
n@673 519 audioEngineContext.metric.sliderMoved();
n@656 520 }
n@656 521
BrechtDeMan@939 522 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@663 523 {
nicholas@944 524 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@944 525 if (hasBeenPlayed.length == 0) {
nicholas@944 526 if (audioEngineContext.status == 1) {
nicholas@944 527 var playback = document.getElementById('playback-button');
nicholas@944 528 playback.click();
nicholas@944 529 // 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 530 } else
nicholas@944 531 {
nicholas@944 532 if (audioEngineContext.timer.testStarted == false)
nicholas@944 533 {
nicholas@944 534 alert('You have not started the test! Please press start to begin the test!');
nicholas@944 535 return;
nicholas@944 536 }
nicholas@944 537 }
nicholas@964 538 testState.advanceState();
BrechtDeMan@940 539 } else // if a fragment has not been played yet
BrechtDeMan@940 540 {
nicholas@944 541 str = "";
nicholas@944 542 if (hasBeenPlayed.length > 1) {
nicholas@944 543 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@944 544 str = str + hasBeenPlayed[i];
nicholas@944 545 if (i < hasBeenPlayed.length-2){
nicholas@944 546 str += ", ";
nicholas@944 547 } else if (i == hasBeenPlayed.length-2) {
nicholas@944 548 str += " or ";
nicholas@944 549 }
nicholas@944 550 }
nicholas@944 551 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@944 552 } else {
nicholas@944 553 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@944 554 }
BrechtDeMan@940 555 return;
BrechtDeMan@940 556 }
n@664 557 }
n@664 558
n@675 559 function convSliderPosToRate(id)
n@675 560 {
n@675 561 var w = document.getElementById('slider').style.width;
n@963 562 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@675 563 var maxPix = w.substr(0,w.length-2);
n@675 564 var slider = document.getElementsByClassName('track-slider')[id];
n@675 565 var pix = slider.style.left;
n@675 566 pix = pix.substr(0,pix.length-2);
n@963 567 var rate = (pix-marginsize)/maxPix;
n@675 568 return rate;
n@675 569 }
n@675 570
n@963 571 function resizeWindow(event){
n@963 572 // Function called when the window has been resized.
n@963 573 // MANDATORY FUNCTION
n@963 574
n@963 575 // Store the slider marker values
n@963 576 var holdValues = [];
n@963 577 $(".track-slider").each(function(index,sliderObj){
n@963 578 holdValues.push(convSliderPosToRate(index));
n@963 579 });
n@963 580
n@963 581 var width = event.target.innerWidth;
n@963 582 var canvas = document.getElementById('sliderCanvasHolder');
n@963 583 var sliderDiv = canvas.children[0];
n@963 584 var sliderScaleDiv = canvas.children[1];
n@963 585 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@963 586 var w = (marginsize+8)*2;
n@963 587 sliderDiv.style.width = width - w + 'px';
n@963 588 var width = width - w;
n@963 589 // Move sliders into new position
n@963 590 $(".track-slider").each(function(index,sliderObj){
n@963 591 var pos = holdValues[index];
n@963 592 var pix = pos * width;
n@963 593 sliderObj.style.left = pix+marginsize+'px';
n@963 594 });
n@963 595
n@963 596 // Move scale labels
n@963 597 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@963 598 var position = Number(scaleObj.attributes['value'].value);
n@963 599 var pixelPosition = (position*width)+marginsize;
n@963 600 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@963 601 });
n@963 602 }
n@963 603
nicholas@964 604 function pageXMLSave(store, testXML, testId)
n@668 605 {
n@668 606 // Saves a specific test page
nicholas@964 607 var xmlDoc = store;
n@674 608 // Check if any session wide metrics are enabled
nicholas@690 609
nicholas@964 610 var commentShow = testXML.attributes['elementComments'];
nicholas@690 611 if (commentShow != undefined) {
nicholas@690 612 if (commentShow.value == 'false') {commentShow = false;}
nicholas@690 613 else {commentShow = true;}
nicholas@690 614 } else {commentShow = true;}
nicholas@690 615
n@674 616 var metric = document.createElement('metric');
n@674 617 if (audioEngineContext.metric.enableTestTimer)
n@674 618 {
n@674 619 var testTime = document.createElement('metricResult');
n@674 620 testTime.id = 'testTime';
n@674 621 testTime.textContent = audioEngineContext.timer.testDuration;
n@674 622 metric.appendChild(testTime);
n@674 623 }
n@674 624 xmlDoc.appendChild(metric);
n@669 625 var trackSliderObjects = document.getElementsByClassName('track-slider');
n@669 626 var commentObjects = document.getElementsByClassName('comment-div');
n@669 627 for (var i=0; i<trackSliderObjects.length; i++)
n@669 628 {
n@669 629 var audioElement = document.createElement('audioElement');
n@669 630 audioElement.id = currentTrackOrder[i].attributes['id'].value;
n@669 631 audioElement.url = currentTrackOrder[i].attributes['url'].value;
n@675 632 var value = document.createElement('value');
n@675 633 value.innerHTML = convSliderPosToRate(i);
nicholas@690 634 if (commentShow) {
nicholas@690 635 var comment = document.createElement("comment");
nicholas@690 636 var question = document.createElement("question");
nicholas@690 637 var response = document.createElement("response");
nicholas@690 638 question.textContent = commentObjects[i].children[0].textContent;
nicholas@690 639 response.textContent = commentObjects[i].children[2].value;
BrechtDeMan@936 640 console.log('Comment ' + i + ': ' + commentObjects[i].children[2].value); // DEBUG/SAFETY
nicholas@690 641 comment.appendChild(question);
nicholas@690 642 comment.appendChild(response);
nicholas@690 643 audioElement.appendChild(comment);
nicholas@690 644 }
n@669 645 audioElement.appendChild(value);
n@674 646 // Check for any per element metrics
n@674 647 var metric = document.createElement('metric');
n@674 648 var elementMetric = audioEngineContext.audioObjects[i].metric;
n@674 649 if (audioEngineContext.metric.enableElementTimer) {
n@674 650 var elementTimer = document.createElement('metricResult');
n@674 651 elementTimer.id = 'elementTimer';
n@674 652 elementTimer.textContent = elementMetric.listenedTimer;
n@674 653 metric.appendChild(elementTimer);
n@674 654 }
n@674 655 if (audioEngineContext.metric.enableElementTracker) {
n@674 656 var elementTrackerFull = document.createElement('metricResult');
n@674 657 elementTrackerFull.id = 'elementTrackerFull';
n@674 658 var data = elementMetric.movementTracker;
n@674 659 for (var k=0; k<data.length; k++)
n@674 660 {
n@674 661 var timePos = document.createElement('timePos');
n@674 662 timePos.id = k;
n@674 663 var time = document.createElement('time');
n@674 664 time.textContent = data[k][0];
n@674 665 var position = document.createElement('position');
n@674 666 position.textContent = data[k][1];
n@674 667 timePos.appendChild(time);
n@674 668 timePos.appendChild(position);
n@674 669 elementTrackerFull.appendChild(timePos);
n@674 670 }
n@674 671 metric.appendChild(elementTrackerFull);
n@674 672 }
n@674 673 if (audioEngineContext.metric.enableElementInitialPosition) {
n@674 674 var elementInitial = document.createElement('metricResult');
n@674 675 elementInitial.id = 'elementInitialPosition';
n@674 676 elementInitial.textContent = elementMetric.initialPosition;
n@674 677 metric.appendChild(elementInitial);
n@674 678 }
n@674 679 if (audioEngineContext.metric.enableFlagListenedTo) {
n@674 680 var flagListenedTo = document.createElement('metricResult');
n@674 681 flagListenedTo.id = 'elementFlagListenedTo';
n@674 682 flagListenedTo.textContent = elementMetric.wasListenedTo;
n@674 683 metric.appendChild(flagListenedTo);
n@674 684 }
n@674 685 if (audioEngineContext.metric.enableFlagMoved) {
n@674 686 var flagMoved = document.createElement('metricResult');
n@674 687 flagMoved.id = 'elementFlagMoved';
n@674 688 flagMoved.textContent = elementMetric.wasMoved;
n@674 689 metric.appendChild(flagMoved);
n@674 690 }
n@674 691 if (audioEngineContext.metric.enableFlagComments) {
n@674 692 var flagComments = document.createElement('metricResult');
n@674 693 flagComments.id = 'elementFlagComments';
n@674 694 if (response.textContent.length == 0) {flag.textContent = 'false';}
n@674 695 else {flag.textContet = 'true';}
n@674 696 metric.appendChild(flagComments);
n@674 697 }
n@674 698 audioElement.appendChild(metric);
n@669 699 xmlDoc.appendChild(audioElement);
n@669 700 }
nicholas@688 701 var commentQuestion = document.getElementsByClassName('commentQuestion');
nicholas@688 702 for (var i=0; i<commentQuestion.length; i++)
nicholas@688 703 {
nicholas@688 704 var cqHolder = document.createElement('CommentQuestion');
nicholas@688 705 var comment = document.createElement('comment');
nicholas@688 706 var question = document.createElement('question');
nicholas@688 707 cqHolder.id = commentQuestion[i].id;
nicholas@688 708 comment.textContent = commentQuestion[i].children[2].value;
nicholas@688 709 question.textContent = commentQuestion[i].children[0].textContent;
n@960 710 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
nicholas@688 711 cqHolder.appendChild(question);
nicholas@688 712 cqHolder.appendChild(comment);
nicholas@688 713 xmlDoc.appendChild(cqHolder);
nicholas@688 714 }
nicholas@964 715 store = xmlDoc;
nicholas@690 716 }