annotate ape.js @ 1055:6bd41ef8b1e0

Slider now enabled optionally based on tag <option name='playhead'/> in interfaces
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Wed, 01 Jul 2015 13:49:01 +0100
parents 329dfa208333
children 99cb3436759e
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
n@911 14 loadInterface();
nicholas@2 15
n@911 16 function loadInterface() {
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
n@912 23 interfaceContext.insertPoint = document.getElementById("topLevelBody");
n@706 24 var testContent = document.createElement('div');
nicholas@934 25
n@706 26 testContent.id = 'testContent';
n@906 27
n@658 28
n@676 29 // Create APE specific metric functions
n@676 30 audioEngineContext.metric.initialiseTest = function()
n@676 31 {
n@676 32 };
n@676 33
n@676 34 audioEngineContext.metric.sliderMoved = function()
n@676 35 {
n@676 36 var id = this.data;
n@676 37 this.data = -1;
n@676 38 var position = convSliderPosToRate(id);
BrechtDeMan@936 39 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
n@676 40 if (audioEngineContext.timer.testStarted)
n@676 41 {
n@676 42 audioEngineContext.audioObjects[id].metric.moved(time,position);
n@676 43 }
n@676 44 };
n@676 45
n@676 46 audioEngineContext.metric.sliderPlayed = function(id)
n@676 47 {
n@676 48 var time = audioEngineContext.timer.getTestTime();
n@676 49 if (audioEngineContext.timer.testStarted)
n@676 50 {
n@676 51 if (this.lastClicked >= 0)
n@676 52 {
n@676 53 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
n@676 54 }
n@676 55 this.lastClicked = id;
n@676 56 audioEngineContext.audioObjects[id].metric.listening(time);
n@676 57 }
BrechtDeMan@936 58 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@676 59 };
n@676 60
nicholas@887 61 // Bindings for interfaceContext
nicholas@887 62 Interface.prototype.checkAllPlayed = function()
nicholas@887 63 {
nicholas@887 64 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@887 65 if (hasBeenPlayed.length > 0) // if a fragment has not been played yet
nicholas@887 66 {
nicholas@887 67 str = "";
nicholas@887 68 if (hasBeenPlayed.length > 1) {
nicholas@887 69 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@887 70 str = str + hasBeenPlayed[i];
nicholas@887 71 if (i < hasBeenPlayed.length-2){
nicholas@887 72 str += ", ";
nicholas@887 73 } else if (i == hasBeenPlayed.length-2) {
nicholas@887 74 str += " or ";
nicholas@887 75 }
nicholas@887 76 }
nicholas@887 77 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 78 } else {
nicholas@887 79 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 80 }
nicholas@887 81 return false;
nicholas@887 82 }
nicholas@887 83 return true;
nicholas@891 84 };
nicholas@887 85
nicholas@887 86 Interface.prototype.checkAllMoved = function() {
nicholas@887 87 var audioObjs = audioEngineContext.audioObjects;
nicholas@887 88 var state = true;
nicholas@887 89 var strNums = [];
nicholas@887 90 for (var i=0; i<audioObjs.length; i++)
nicholas@887 91 {
nicholas@887 92 if (audioObjs[i].metric.wasMoved == false) {
nicholas@887 93 state = false;
nicholas@887 94 strNums.push(i);
nicholas@887 95 }
nicholas@887 96 }
nicholas@887 97 if (state == false) {
nicholas@887 98 if (strNums.length > 1) {
nicholas@887 99 var str = "";
nicholas@887 100 for (var i=0; i<strNums.length; i++) {
nicholas@887 101 str = str + strNums[i];
nicholas@887 102 if (i < strNums.length-2){
nicholas@887 103 str += ", ";
nicholas@887 104 } else if (i == strNums.length-2) {
nicholas@887 105 str += " or ";
nicholas@887 106 }
nicholas@887 107 }
nicholas@887 108 alert('You have not moved fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 109 } else {
nicholas@887 110 alert('You have not moved fragment ' + strNums[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 111 }
nicholas@887 112 }
nicholas@887 113 return state;
nicholas@891 114 };
nicholas@887 115
nicholas@887 116 Interface.prototype.checkAllCommented = function() {
nicholas@887 117 var audioObjs = audioEngineContext.audioObjects;
nicholas@887 118 var audioHolder = testState.stateMap[testState.stateIndex];
nicholas@887 119 var state = true;
nicholas@887 120 if (audioHolder.elementComments) {
nicholas@887 121 var strNums = [];
nicholas@887 122 for (var i=0; i<audioObjs.length; i++)
nicholas@887 123 {
nicholas@887 124 if (audioObjs[i].commentDOM.trackCommentBox.value.length == 0) {
nicholas@887 125 state = false;
nicholas@887 126 strNums.push(i);
nicholas@887 127 }
nicholas@887 128 }
nicholas@887 129 if (state == false) {
nicholas@887 130 if (strNums.length > 1) {
nicholas@887 131 var str = "";
nicholas@887 132 for (var i=0; i<strNums.length; i++) {
nicholas@887 133 str = str + strNums[i];
nicholas@887 134 if (i < strNums.length-2){
nicholas@887 135 str += ", ";
nicholas@887 136 } else if (i == strNums.length-2) {
nicholas@887 137 str += " or ";
nicholas@887 138 }
nicholas@887 139 }
nicholas@887 140 alert('You have not commented on fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 141 } else {
nicholas@887 142 alert('You have not commented on fragment ' + strNums[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 143 }
nicholas@887 144 }
nicholas@887 145 }
nicholas@887 146 return state;
nicholas@891 147 };
nicholas@887 148
nicholas@1039 149 Interface.prototype.checkScaleRange = function()
nicholas@1039 150 {
nicholas@1039 151 var audioObjs = audioEngineContext.audioObjects;
nicholas@1039 152 var audioHolder = testState.stateMap[testState.stateIndex];
nicholas@1039 153 var interfaces = audioHolder.interfaces;
nicholas@1039 154
nicholas@1039 155 var minRanking = audioObjs[0].interfaceDOM.getValue();
nicholas@1039 156 var maxRanking = minRanking;
nicholas@1039 157
nicholas@1039 158 var minScale;
nicholas@1039 159 var maxScale;
nicholas@1039 160 for (var i=0; i<interfaces[0].options.length; i++)
nicholas@1039 161 {
nicholas@1039 162 if (interfaces[0].options[i].check == "scalerange") {
nicholas@1039 163 minScale = interfaces[0].options[i].min;
nicholas@1039 164 maxScale = interfaces[0].options[i].max;
nicholas@1039 165 }
nicholas@1039 166 }
nicholas@1039 167
nicholas@1039 168 for (var i=1; i<audioObjs.length; i++){
nicholas@1044 169 if (audioObjs[i].specification.type != 'outsidereference') {
nicholas@1044 170 var ranking = audioObjs[i].interfaceDOM.getValue();
nicholas@1044 171 if (ranking < minRanking) { minRanking = ranking;}
nicholas@1044 172 if (ranking > maxRanking) { maxRanking = ranking;}
nicholas@1044 173 }
nicholas@1039 174 }
nicholas@1039 175 if (minRanking > minScale || maxRanking < maxScale) {
nicholas@1039 176 alert('Please use the full width of the scale');
nicholas@1039 177 return false;
nicholas@1039 178 } else {
nicholas@1039 179 return true;
nicholas@1039 180 }
nicholas@1039 181 };
nicholas@1039 182
n@907 183 // Bindings for audioObjects
n@907 184
nicholas@2 185 // Create the top div for the Title element
n@911 186 var titleAttr = specification.title;
nicholas@2 187 var title = document.createElement('div');
nicholas@2 188 title.className = "title";
nicholas@2 189 title.align = "center";
nicholas@2 190 var titleSpan = document.createElement('span');
nicholas@2 191
nicholas@2 192 // Set title to that defined in XML, else set to default
nicholas@2 193 if (titleAttr != undefined) {
n@911 194 titleSpan.textContent = titleAttr;
nicholas@2 195 } else {
n@911 196 titleSpan.textContent = 'Listening test';
nicholas@2 197 }
nicholas@2 198 // Insert the titleSpan element into the title div element.
nicholas@2 199 title.appendChild(titleSpan);
nicholas@2 200
n@671 201 var pagetitle = document.createElement('div');
n@671 202 pagetitle.className = "pageTitle";
n@671 203 pagetitle.align = "center";
n@671 204 var titleSpan = document.createElement('span');
n@671 205 titleSpan.id = "pageTitle";
n@671 206 pagetitle.appendChild(titleSpan);
n@671 207
nicholas@7 208 // Create Interface buttons!
nicholas@7 209 var interfaceButtons = document.createElement('div');
nicholas@7 210 interfaceButtons.id = 'interface-buttons';
nicholas@7 211
nicholas@7 212 // Create playback start/stop points
nicholas@7 213 var playback = document.createElement("button");
BrechtDeMan@939 214 playback.innerHTML = 'Stop';
n@673 215 playback.id = 'playback-button';
n@701 216 // onclick function. Check if it is playing or not, call the correct function in the
n@701 217 // audioEngine, change the button text to reflect the next state.
nicholas@7 218 playback.onclick = function() {
BrechtDeMan@939 219 if (audioEngineContext.status == 1) {
BrechtDeMan@939 220 audioEngineContext.stop();
n@709 221 this.innerHTML = 'Stop';
BrechtDeMan@936 222 var time = audioEngineContext.timer.getTestTime();
BrechtDeMan@936 223 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@7 224 }
n@701 225 };
nicholas@7 226 // Create Submit (save) button
nicholas@7 227 var submit = document.createElement("button");
n@709 228 submit.innerHTML = 'Submit';
n@667 229 submit.onclick = buttonSubmitClick;
n@673 230 submit.id = 'submit-button';
n@701 231 // Append the interface buttons into the interfaceButtons object.
nicholas@7 232 interfaceButtons.appendChild(playback);
nicholas@7 233 interfaceButtons.appendChild(submit);
nicholas@7 234
nicholas@2 235 // Now create the slider and HTML5 canvas boxes
nicholas@2 236
n@701 237 // Create the div box to center align
nicholas@2 238 var sliderBox = document.createElement('div');
nicholas@2 239 sliderBox.className = 'sliderCanvasDiv';
n@701 240 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 241
n@701 242 // Create the slider box to hold the slider elements
nicholas@5 243 var canvas = document.createElement('div');
nicholas@2 244 canvas.id = 'slider';
n@963 245 canvas.align = "left";
n@963 246 canvas.addEventListener('dragover',function(event){
n@963 247 event.preventDefault();
n@963 248 return false;
n@963 249 },false);
n@963 250 var sliderMargin = document.createAttribute('marginsize');
n@963 251 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
n@701 252 // Must have a known EXACT width, as this is used later to determine the ratings
n@963 253 var w = (Number(sliderMargin.nodeValue)+8)*2;
n@963 254 canvas.style.width = width - w +"px";
n@963 255 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
n@963 256 canvas.setAttributeNode(sliderMargin);
nicholas@2 257 sliderBox.appendChild(canvas);
n@701 258
n@671 259 // Create the div to hold any scale objects
n@671 260 var scale = document.createElement('div');
n@671 261 scale.className = 'sliderScale';
n@671 262 scale.id = 'sliderScaleHolder';
n@671 263 scale.align = 'left';
n@671 264 sliderBox.appendChild(scale);
n@671 265
n@701 266 // Global parent for the comment boxes on the page
nicholas@3 267 var feedbackHolder = document.createElement('div');
n@658 268 feedbackHolder.id = 'feedbackHolder';
n@656 269
n@662 270 testContent.style.zIndex = 1;
n@912 271 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@656 272
n@662 273 // Inject into HTML
n@662 274 testContent.appendChild(title); // Insert the title
n@671 275 testContent.appendChild(pagetitle);
n@662 276 testContent.appendChild(interfaceButtons);
n@662 277 testContent.appendChild(sliderBox);
n@662 278 testContent.appendChild(feedbackHolder);
n@912 279 interfaceContext.insertPoint.appendChild(testContent);
n@656 280
n@660 281 // Load the full interface
nicholas@964 282 testState.initialise();
nicholas@964 283 testState.advanceState();
n@663 284
n@656 285 }
n@656 286
n@911 287 function loadTest(audioHolderObject)
n@658 288 {
nicholas@947 289
nicholas@947 290 // Reset audioEngineContext.Metric globals for new test
n@950 291 audioEngineContext.newTestPage();
nicholas@947 292
n@911 293 var id = audioHolderObject.id;
n@658 294
n@658 295 var feedbackHolder = document.getElementById('feedbackHolder');
n@658 296 var canvas = document.getElementById('slider');
n@658 297 feedbackHolder.innerHTML = null;
n@658 298 canvas.innerHTML = null;
n@671 299
nicholas@1055 300 var interfaceObj = audioHolderObject.interfaces;
nicholas@1055 301 for (var k=0; k<interfaceObj.length; k++) {
nicholas@1055 302 for (var i=0; i<interfaceObj[k].options.length; i++)
nicholas@1055 303 {
nicholas@1055 304 if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'playhead')
nicholas@1055 305 {
nicholas@1055 306 var playbackHolder = document.getElementById('playback-holder');
nicholas@1055 307 if (playbackHolder == null)
nicholas@1055 308 {
nicholas@1055 309 playbackHolder = document.createElement('div');
nicholas@1055 310 playbackHolder.style.width = "100%";
nicholas@1055 311 playbackHolder.align = 'center';
nicholas@1055 312 playbackHolder.appendChild(interfaceContext.playhead.object);
nicholas@1055 313 feedbackHolder.appendChild(playbackHolder);
nicholas@1055 314 }
nicholas@1055 315 break;
nicholas@1055 316 }
nicholas@1055 317 }
nicholas@1055 318 }
n@671 319 // Setup question title
nicholas@1055 320
n@914 321 var commentBoxPrefix = "Comment on track";
n@914 322 if (interfaceObj.length != 0) {
n@914 323 interfaceObj = interfaceObj[0];
n@914 324 var titleNode = interfaceObj.title;
n@914 325 if (titleNode != undefined)
n@914 326 {
n@914 327 document.getElementById('pageTitle').textContent = titleNode;
n@914 328 }
n@914 329 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
n@914 330 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
n@914 331 var scale = document.getElementById('sliderScaleHolder');
n@914 332 scale.innerHTML = null;
n@914 333 $(interfaceObj.scale).each(function(index,scaleObj){
n@914 334 var value = document.createAttribute('value');
n@914 335 var position = Number(scaleObj[0])*0.01;
n@914 336 value.nodeValue = position;
n@914 337 var pixelPosition = (position*positionScale)+offset;
n@914 338 var scaleDOM = document.createElement('span');
n@914 339 scaleDOM.textContent = scaleObj[1];
n@914 340 scale.appendChild(scaleDOM);
n@914 341 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
n@914 342 scaleDOM.setAttributeNode(value);
n@914 343 });
n@914 344
n@914 345 if (interfaceObj.commentBoxPrefix != undefined) {
n@914 346 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@914 347 }
n@1022 348 }
n@658 349
nicholas@7 350 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@911 351 if (audioHolderObject.sampleRate != undefined) {
n@911 352 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
n@914 353 var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nicholas@7 354 alert(errStr);
nicholas@7 355 return;
nicholas@7 356 }
nicholas@7 357 }
n@669 358
n@911 359 var commentShow = audioHolderObject.elementComments;
nicholas@689 360
n@911 361 var loopPlayback = audioHolderObject.loop;
n@911 362
n@681 363 audioEngineContext.loopPlayback = loopPlayback;
n@681 364 // Create AudioEngine bindings for playback
n@895 365 audioEngineContext.selectedTrack = function(id) {
n@895 366 console.log('Deprecated');
n@895 367 };
n@681 368
n@670 369 currentTestHolder = document.createElement('audioHolder');
n@911 370 currentTestHolder.id = audioHolderObject.id;
n@911 371 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@670 372
nicholas@682 373 // Delete any previous audioObjects associated with the audioEngine
nicholas@682 374 audioEngineContext.audioObjects = [];
nicholas@900 375 interfaceContext.deleteCommentBoxes();
nicholas@1045 376 interfaceContext.deleteCommentQuestions();
nicholas@682 377
n@701 378 // Find all the audioElements from the audioHolder
n@911 379 $(audioHolderObject.audioElements).each(function(index,element){
nicholas@7 380 // Find URL of track
n@701 381 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 382
n@701 383 // Now load each audio sample. First create the new track by passing the full URL
n@911 384 var trackURL = audioHolderObject.hostURL + element.url;
n@912 385 var audioObject = audioEngineContext.newTrack(element);
nicholas@689 386
n@1029 387 var node = interfaceContext.createCommentBox(audioObject);
n@701 388
nicholas@5 389 // Create a slider per track
n@913 390 audioObject.interfaceDOM = new sliderObject(audioObject);
n@1008 391
nicholas@5 392 // Distribute it randomnly
n@1000 393 var w = window.innerWidth - (offset+8)*2;
nicholas@5 394 w = Math.random()*w;
n@1000 395 w = Math.floor(w+(offset+8));
n@913 396 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
nicholas@8 397
n@913 398 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
n@913 399 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
BrechtDeMan@940 400
n@700 401 });
n@912 402 if (commentShow) {
n@912 403 interfaceContext.showCommentBoxes(feedbackHolder,true);
n@912 404 }
nicholas@3 405
n@911 406 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@1026 407 var node = interfaceContext.createCommentQuestion(element);
n@1026 408 feedbackHolder.appendChild(node.holder);
nicholas@688 409 });
n@1007 410
nicholas@1044 411 // Construct outside reference;
nicholas@1044 412 if (audioHolderObject.outsideReference != null) {
nicholas@1044 413 var outsideReferenceHolder = document.createElement('div');
nicholas@1044 414 outsideReferenceHolder.id = 'outside-reference';
nicholas@1044 415 outsideReferenceHolderspan = document.createElement('span');
nicholas@1044 416 outsideReferenceHolderspan.textContent = 'Reference';
nicholas@1044 417 outsideReferenceHolder.appendChild(outsideReferenceHolderspan);
nicholas@1044 418
nicholas@1044 419 var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference);
nicholas@1044 420
nicholas@1044 421 outsideReferenceHolder.onclick = function()
nicholas@1044 422 {
nicholas@1044 423 audioEngineContext.play(audioEngineContext.audioObjects.length-1);
nicholas@1044 424 $('.track-slider').removeClass('track-slider-playing');
nicholas@1044 425 $('.comment-div').removeClass('comment-box-playing');
nicholas@1044 426 if (event.srcElement.nodeName == 'DIV') {
nicholas@1044 427 $(event.srcElement).addClass('track-slider-playing');
nicholas@1044 428 } else {
nicholas@1044 429 $(event.srcElement.parentElement).addClass('track-slider-playing');
nicholas@1044 430 }
nicholas@1044 431 };
nicholas@1044 432
nicholas@1044 433 document.getElementById('interface-buttons').appendChild(outsideReferenceHolder);
nicholas@1044 434 }
nicholas@1044 435
n@1007 436
n@1007 437 testWaitIndicator();
n@663 438 }
n@663 439
n@913 440 function sliderObject(audioObject) {
n@913 441 // Create a new slider object;
n@913 442 this.parent = audioObject;
n@913 443 this.trackSliderObj = document.createElement('div');
n@913 444 this.trackSliderObj.className = 'track-slider';
n@913 445 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
n@913 446
n@913 447 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
n@913 448 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
n@913 449 this.trackSliderObj.draggable = true;
n@913 450 this.trackSliderObj.ondragend = dragEnd;
n@913 451
n@913 452 // Onclick, switch playback to that track
n@913 453 this.trackSliderObj.onclick = function() {
n@913 454 // Start the test on first click, that way timings are more accurate.
n@913 455 audioEngineContext.play();
n@913 456 if (audioEngineContext.audioObjectsReady) {
n@913 457 // Cannot continue to issue play command until audioObjects reported as ready!
n@913 458 // Get the track ID from the object ID
nicholas@899 459 var element;
nicholas@899 460 if (event.srcElement.nodeName == "SPAN") {
nicholas@899 461 element = event.srcElement.parentNode;
nicholas@899 462 } else {
nicholas@899 463 element = event.srcElement;
nicholas@899 464 }
nicholas@899 465 var id = Number(element.attributes['trackIndex'].value);
n@913 466 //audioEngineContext.metric.sliderPlayed(id);
n@895 467 audioEngineContext.play(id);
n@913 468 // Currently playing track red, rest green
n@913 469
n@913 470 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
n@913 471 $('.track-slider').removeClass('track-slider-playing');
nicholas@899 472 $(element).addClass('track-slider-playing');
n@913 473 $('.comment-div').removeClass('comment-box-playing');
n@913 474 $('#comment-div-'+id).addClass('comment-box-playing');
nicholas@1044 475 var outsideReference = document.getElementById('outside-reference');
nicholas@1044 476 if (outsideReference != undefined)
nicholas@1044 477 $(outsideReference).removeClass('track-slider-playing');
n@913 478 }
n@913 479 };
n@913 480
nicholas@891 481 this.exportXMLDOM = function(audioObject) {
n@913 482 // Called by the audioObject holding this element. Must be present
n@913 483 var node = document.createElement('value');
n@913 484 node.textContent = convSliderPosToRate(this.trackSliderObj);
n@913 485 return node;
n@913 486 };
nicholas@891 487 this.getValue = function() {
nicholas@891 488 return convSliderPosToRate(this.trackSliderObj);
nicholas@892 489 };
n@913 490 }
nicholas@5 491
nicholas@5 492 function dragEnd(ev) {
nicholas@5 493 // Function call when a div has been dropped
n@657 494 var slider = document.getElementById('slider');
n@963 495 var marginSize = Number(slider.attributes['marginsize'].value);
n@675 496 var w = slider.style.width;
n@675 497 w = Number(w.substr(0,w.length-2));
n@675 498 var x = ev.x;
n@963 499 if (x >= marginSize && x < w+marginSize) {
n@675 500 this.style.left = (x)+'px';
nicholas@5 501 } else {
n@963 502 if (x<marginSize) {
n@963 503 this.style.left = marginSize+'px';
nicholas@5 504 } else {
n@963 505 this.style.left = (w+marginSize) + 'px';
nicholas@5 506 }
nicholas@5 507 }
n@913 508 var time = audioEngineContext.timer.getTestTime();
n@913 509 var id = Number(ev.srcElement.getAttribute('trackindex'));
n@913 510 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
BrechtDeMan@1053 511 console.log('slider '+id+' moved to '+convSliderPosToRate(ev.srcElement)+' ('+time+')');
n@656 512 }
n@656 513
BrechtDeMan@939 514 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@663 515 {
nicholas@1040 516 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
nicholas@887 517 var canContinue = true;
nicholas@891 518
nicholas@891 519 // Check that the anchor and reference objects are correctly placed
nicholas@1043 520 if (interfaceContext.checkHiddenAnchor() == false) {return;}
nicholas@1043 521 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@891 522
nicholas@887 523 for (var i=0; i<checks.length; i++) {
nicholas@887 524 if (checks[i].type == 'check')
nicholas@887 525 {
nicholas@887 526 switch(checks[i].check) {
nicholas@887 527 case 'fragmentPlayed':
nicholas@887 528 // Check if all fragments have been played
nicholas@887 529 var checkState = interfaceContext.checkAllPlayed();
nicholas@887 530 if (checkState == false) {canContinue = false;}
nicholas@887 531 break;
nicholas@887 532 case 'fragmentFullPlayback':
nicholas@887 533 // Check all fragments have been played to their full length
nicholas@887 534 var checkState = interfaceContext.checkAllPlayed();
nicholas@887 535 if (checkState == false) {canContinue = false;}
nicholas@887 536 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
nicholas@887 537 break;
nicholas@887 538 case 'fragmentMoved':
nicholas@887 539 // Check all fragment sliders have been moved.
nicholas@887 540 var checkState = interfaceContext.checkAllMoved();
nicholas@887 541 if (checkState == false) {canContinue = false;}
nicholas@887 542 break;
nicholas@887 543 case 'fragmentComments':
nicholas@887 544 // Check all fragment sliders have been moved.
nicholas@887 545 var checkState = interfaceContext.checkAllCommented();
nicholas@887 546 if (checkState == false) {canContinue = false;}
nicholas@887 547 break;
nicholas@1039 548 case 'scalerange':
nicholas@1039 549 // Check the scale is used to its full width outlined by the node
nicholas@1039 550 var checkState = interfaceContext.checkScaleRange();
nicholas@1039 551 if (checkState == false) {canContinue = false;}
nicholas@1039 552 break;
nicholas@887 553 }
nicholas@887 554
nicholas@887 555 }
nicholas@1039 556 if (!canContinue) {break;}
nicholas@887 557 }
nicholas@887 558
nicholas@887 559 if (canContinue) {
nicholas@944 560 if (audioEngineContext.status == 1) {
nicholas@944 561 var playback = document.getElementById('playback-button');
nicholas@944 562 playback.click();
nicholas@944 563 // 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 564 } else
nicholas@944 565 {
nicholas@944 566 if (audioEngineContext.timer.testStarted == false)
nicholas@944 567 {
nicholas@944 568 alert('You have not started the test! Please press start to begin the test!');
nicholas@944 569 return;
nicholas@944 570 }
nicholas@944 571 }
nicholas@964 572 testState.advanceState();
nicholas@887 573 }
n@664 574 }
n@664 575
n@913 576 function convSliderPosToRate(slider)
n@675 577 {
n@675 578 var w = document.getElementById('slider').style.width;
n@963 579 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@675 580 var maxPix = w.substr(0,w.length-2);
n@675 581 var pix = slider.style.left;
n@675 582 pix = pix.substr(0,pix.length-2);
n@963 583 var rate = (pix-marginsize)/maxPix;
n@675 584 return rate;
n@675 585 }
n@675 586
n@963 587 function resizeWindow(event){
n@963 588 // Function called when the window has been resized.
n@963 589 // MANDATORY FUNCTION
n@963 590
n@963 591 // Store the slider marker values
n@963 592 var holdValues = [];
n@963 593 $(".track-slider").each(function(index,sliderObj){
n@963 594 holdValues.push(convSliderPosToRate(index));
n@963 595 });
n@963 596
n@963 597 var width = event.target.innerWidth;
n@963 598 var canvas = document.getElementById('sliderCanvasHolder');
n@963 599 var sliderDiv = canvas.children[0];
n@963 600 var sliderScaleDiv = canvas.children[1];
n@963 601 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@963 602 var w = (marginsize+8)*2;
n@963 603 sliderDiv.style.width = width - w + 'px';
n@963 604 var width = width - w;
n@963 605 // Move sliders into new position
n@963 606 $(".track-slider").each(function(index,sliderObj){
n@963 607 var pos = holdValues[index];
n@963 608 var pix = pos * width;
n@963 609 sliderObj.style.left = pix+marginsize+'px';
n@963 610 });
n@963 611
n@963 612 // Move scale labels
n@963 613 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@963 614 var position = Number(scaleObj.attributes['value'].value);
n@963 615 var pixelPosition = (position*width)+marginsize;
n@963 616 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@963 617 });
n@963 618 }
n@963 619
n@911 620 function pageXMLSave(store, testXML)
n@668 621 {
n@668 622 // Saves a specific test page
nicholas@964 623 var xmlDoc = store;
n@674 624 // Check if any session wide metrics are enabled
nicholas@690 625
n@911 626 var commentShow = testXML.elementComments;
nicholas@690 627
n@674 628 var metric = document.createElement('metric');
n@674 629 if (audioEngineContext.metric.enableTestTimer)
n@674 630 {
n@674 631 var testTime = document.createElement('metricResult');
n@674 632 testTime.id = 'testTime';
n@674 633 testTime.textContent = audioEngineContext.timer.testDuration;
n@674 634 metric.appendChild(testTime);
n@674 635 }
n@674 636 xmlDoc.appendChild(metric);
n@908 637 var audioObjects = audioEngineContext.audioObjects;
n@908 638 for (var i=0; i<audioObjects.length; i++)
n@669 639 {
n@913 640 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
nicholas@1041 641 audioElement.setAttribute('presentedId',i);
n@669 642 xmlDoc.appendChild(audioElement);
n@669 643 }
n@1026 644
n@1026 645 $(interfaceContext.commentQuestions).each(function(index,element){
n@1026 646 var node = element.exportXMLDOM();
n@1026 647 xmlDoc.appendChild(node);
n@1026 648 });
nicholas@964 649 store = xmlDoc;
nicholas@690 650 }