annotate ape.js @ 852:36a0054a0ee0

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