annotate ape.js @ 127:4ce9f5887eee

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