annotate ape.js @ 1022:0fdca5989285

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