annotate ape.js @ 294:df7263170715 Dev_main

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