annotate ape.js @ 138:29e89da26051 Dev_main

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