annotate ape.js @ 849:8d191cf6c50b

APE: Multiple Screen proof
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 10 Sep 2015 12:53:55 +0100
parents bfbd2c6ab8fb
children b797a8b347d0
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();
nicholas@842 242 return false;
nicholas@842 243 },false);
nicholas@842 244 var sliderMargin = document.createAttribute('marginsize');
nicholas@842 245 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
nicholas@842 246 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@842 247 var w = (Number(sliderMargin.nodeValue)+8)*2;
nicholas@842 248 canvas.style.width = width - w +"px";
nicholas@842 249 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
nicholas@842 250 canvas.setAttributeNode(sliderMargin);
nicholas@842 251 sliderBox.appendChild(canvas);
nicholas@842 252
nicholas@842 253 // Create the div to hold any scale objects
nicholas@842 254 var scale = document.createElement('div');
nicholas@842 255 scale.className = 'sliderScale';
nicholas@842 256 scale.id = 'sliderScaleHolder';
nicholas@842 257 scale.align = 'left';
nicholas@842 258 sliderBox.appendChild(scale);
nicholas@842 259
nicholas@842 260 // Global parent for the comment boxes on the page
nicholas@842 261 var feedbackHolder = document.createElement('div');
nicholas@842 262 feedbackHolder.id = 'feedbackHolder';
nicholas@842 263
nicholas@842 264 testContent.style.zIndex = 1;
nicholas@842 265 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
nicholas@842 266
nicholas@842 267 // Inject into HTML
nicholas@842 268 testContent.appendChild(title); // Insert the title
nicholas@842 269 testContent.appendChild(pagetitle);
nicholas@842 270 testContent.appendChild(interfaceButtons);
nicholas@842 271 testContent.appendChild(sliderBox);
nicholas@842 272 testContent.appendChild(feedbackHolder);
nicholas@842 273 interfaceContext.insertPoint.appendChild(testContent);
nicholas@842 274
nicholas@842 275 // Load the full interface
nicholas@842 276 testState.initialise();
nicholas@842 277 testState.advanceState();
nicholas@842 278
nicholas@842 279 }
nicholas@842 280
nicholas@842 281 function loadTest(audioHolderObject)
nicholas@842 282 {
nicholas@842 283
nicholas@842 284 // Reset audioEngineContext.Metric globals for new test
nicholas@842 285 audioEngineContext.newTestPage();
nicholas@842 286
nicholas@842 287 var id = audioHolderObject.id;
nicholas@842 288
nicholas@842 289 var feedbackHolder = document.getElementById('feedbackHolder');
nicholas@842 290 var canvas = document.getElementById('slider');
nicholas@842 291 feedbackHolder.innerHTML = null;
nicholas@842 292 canvas.innerHTML = null;
nicholas@842 293
nicholas@842 294 var interfaceObj = audioHolderObject.interfaces;
nicholas@842 295 for (var k=0; k<interfaceObj.length; k++) {
nicholas@842 296 for (var i=0; i<interfaceObj[k].options.length; i++)
nicholas@842 297 {
nicholas@842 298 if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'playhead')
nicholas@842 299 {
nicholas@842 300 var playbackHolder = document.getElementById('playback-holder');
nicholas@842 301 if (playbackHolder == null)
nicholas@842 302 {
nicholas@842 303 playbackHolder = document.createElement('div');
nicholas@842 304 playbackHolder.style.width = "100%";
nicholas@842 305 playbackHolder.align = 'center';
nicholas@842 306 playbackHolder.appendChild(interfaceContext.playhead.object);
nicholas@842 307 feedbackHolder.appendChild(playbackHolder);
nicholas@842 308 }
nicholas@842 309 } else if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'page-count')
nicholas@842 310 {
nicholas@842 311 var pagecountHolder = document.getElementById('page-count');
nicholas@842 312 if (pagecountHolder == null)
nicholas@842 313 {
nicholas@842 314 pagecountHolder = document.createElement('div');
nicholas@842 315 pagecountHolder.id = 'page-count';
nicholas@842 316 }
nicholas@842 317 pagecountHolder.innerHTML = '<span>Test '+(audioHolderObject.presentedId+1)+' of '+specification.audioHolders.length+'</span>';
nicholas@842 318 var inject = document.getElementById('interface-buttons');
nicholas@842 319 inject.appendChild(pagecountHolder);
nicholas@842 320 }
nicholas@842 321 }
nicholas@842 322 }
nicholas@842 323 // Setup question title
nicholas@842 324
nicholas@842 325 var commentBoxPrefix = "Comment on track";
nicholas@842 326 if (interfaceObj.length != 0) {
nicholas@842 327 interfaceObj = interfaceObj[0];
nicholas@842 328 var titleNode = interfaceObj.title;
nicholas@842 329 if (titleNode != undefined)
nicholas@842 330 {
nicholas@842 331 document.getElementById('pageTitle').textContent = titleNode;
nicholas@842 332 }
nicholas@842 333 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
nicholas@842 334 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
nicholas@842 335 var scale = document.getElementById('sliderScaleHolder');
nicholas@842 336 scale.innerHTML = null;
nicholas@842 337 $(interfaceObj.scale).each(function(index,scaleObj){
nicholas@842 338 var value = document.createAttribute('value');
nicholas@842 339 var position = Number(scaleObj[0])*0.01;
nicholas@842 340 value.nodeValue = position;
nicholas@842 341 var pixelPosition = (position*positionScale)+offset;
nicholas@842 342 var scaleDOM = document.createElement('span');
nicholas@842 343 scaleDOM.textContent = scaleObj[1];
nicholas@842 344 scale.appendChild(scaleDOM);
nicholas@842 345 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
nicholas@842 346 scaleDOM.setAttributeNode(value);
nicholas@842 347 });
nicholas@842 348
nicholas@842 349 if (interfaceObj.commentBoxPrefix != undefined) {
nicholas@842 350 commentBoxPrefix = interfaceObj.commentBoxPrefix;
nicholas@842 351 }
nicholas@842 352 }
nicholas@842 353
nicholas@842 354 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nicholas@842 355 if (audioHolderObject.sampleRate != undefined) {
nicholas@842 356 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nicholas@842 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.';
nicholas@842 358 alert(errStr);
nicholas@842 359 return;
nicholas@842 360 }
nicholas@842 361 }
nicholas@842 362
nicholas@842 363 var commentShow = audioHolderObject.elementComments;
nicholas@842 364
nicholas@842 365 var loopPlayback = audioHolderObject.loop;
nicholas@842 366
nicholas@842 367 audioEngineContext.loopPlayback = loopPlayback;
nicholas@842 368 // Create AudioEngine bindings for playback
nicholas@842 369 audioEngineContext.selectedTrack = function(id) {
nicholas@842 370 console.log('Deprecated');
nicholas@842 371 };
nicholas@842 372
nicholas@842 373 currentTestHolder = document.createElement('audioHolder');
nicholas@842 374 currentTestHolder.id = audioHolderObject.id;
nicholas@842 375 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
nicholas@842 376
nicholas@842 377 // Delete any previous audioObjects associated with the audioEngine
nicholas@842 378 audioEngineContext.audioObjects = [];
nicholas@842 379 interfaceContext.deleteCommentBoxes();
nicholas@842 380 interfaceContext.deleteCommentQuestions();
nicholas@842 381
nicholas@842 382 // Find all the audioElements from the audioHolder
nicholas@842 383 $(audioHolderObject.audioElements).each(function(index,element){
nicholas@842 384 // Find URL of track
nicholas@842 385 // In this jQuery loop, variable 'this' holds the current audioElement.
nicholas@842 386
nicholas@842 387 // Now load each audio sample. First create the new track by passing the full URL
nicholas@842 388 var trackURL = audioHolderObject.hostURL + element.url;
nicholas@842 389 var audioObject = audioEngineContext.newTrack(element);
nicholas@842 390
nicholas@842 391 var node = interfaceContext.createCommentBox(audioObject);
nicholas@842 392
nicholas@842 393 // Create a slider per track
nicholas@842 394 audioObject.interfaceDOM = new sliderObject(audioObject);
nicholas@842 395
nicholas@842 396 // Distribute it randomnly
nicholas@842 397 var w = window.innerWidth - (offset+8)*2;
nicholas@842 398 w = Math.random()*w;
nicholas@842 399 w = Math.floor(w+(offset+8));
nicholas@842 400 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
nicholas@842 401
nicholas@842 402 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
nicholas@842 403 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
nicholas@842 404
nicholas@842 405 });
nicholas@842 406 if (commentShow) {
nicholas@842 407 interfaceContext.showCommentBoxes(feedbackHolder,true);
nicholas@842 408 }
nicholas@842 409
nicholas@842 410 $(audioHolderObject.commentQuestions).each(function(index,element) {
nicholas@842 411 var node = interfaceContext.createCommentQuestion(element);
nicholas@842 412 feedbackHolder.appendChild(node.holder);
nicholas@842 413 });
nicholas@842 414
nicholas@842 415 // Construct outside reference;
nicholas@842 416 if (audioHolderObject.outsideReference != null) {
nicholas@842 417 var outsideReferenceHolder = document.createElement('div');
nicholas@842 418 outsideReferenceHolder.id = 'outside-reference';
nicholas@842 419 outsideReferenceHolderspan = document.createElement('span');
nicholas@842 420 outsideReferenceHolderspan.textContent = 'Reference';
nicholas@842 421 outsideReferenceHolder.appendChild(outsideReferenceHolderspan);
nicholas@842 422
nicholas@842 423 var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference);
nicholas@842 424
nicholas@842 425 outsideReferenceHolder.onclick = function()
nicholas@842 426 {
nicholas@842 427 audioEngineContext.play(audioEngineContext.audioObjects.length-1);
nicholas@842 428 $('.track-slider').removeClass('track-slider-playing');
nicholas@842 429 $('.comment-div').removeClass('comment-box-playing');
nicholas@842 430 if (event.srcElement.nodeName == 'DIV') {
nicholas@842 431 $(event.srcElement).addClass('track-slider-playing');
nicholas@842 432 } else {
nicholas@842 433 $(event.srcElement.parentElement).addClass('track-slider-playing');
nicholas@842 434 }
nicholas@842 435 };
nicholas@842 436
nicholas@842 437 document.getElementById('interface-buttons').appendChild(outsideReferenceHolder);
nicholas@842 438 }
nicholas@842 439
nicholas@842 440
nicholas@842 441 //testWaitIndicator();
nicholas@842 442 }
nicholas@842 443
nicholas@842 444 function sliderObject(audioObject) {
nicholas@842 445 // Create a new slider object;
nicholas@842 446 this.parent = audioObject;
nicholas@842 447 this.trackSliderObj = document.createElement('div');
nicholas@842 448 this.trackSliderObj.className = 'track-slider track-slider-disabled';
nicholas@842 449 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
nicholas@842 450
nicholas@842 451 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
nicholas@842 452 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
nicholas@842 453 this.trackSliderObj.draggable = true;
nicholas@842 454 this.trackSliderObj.ondragend = dragEnd;
nicholas@842 455
nicholas@842 456 // Onclick, switch playback to that track
nicholas@842 457 this.trackSliderObj.onclick = function() {
nicholas@842 458 // Start the test on first click, that way timings are more accurate.
nicholas@842 459 audioEngineContext.play();
nicholas@842 460 if (audioEngineContext.audioObjectsReady) {
nicholas@842 461 // Cannot continue to issue play command until audioObjects reported as ready!
nicholas@842 462 // Get the track ID from the object ID
nicholas@842 463 var element;
nicholas@842 464 if (event.srcElement.nodeName == "SPAN") {
nicholas@842 465 element = event.srcElement.parentNode;
nicholas@842 466 } else {
nicholas@842 467 element = event.srcElement;
nicholas@842 468 }
nicholas@842 469 var id = Number(element.attributes['trackIndex'].value);
nicholas@842 470 //audioEngineContext.metric.sliderPlayed(id);
nicholas@842 471 audioEngineContext.play(id);
nicholas@842 472 // Currently playing track red, rest green
nicholas@842 473
nicholas@842 474 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nicholas@842 475 $('.track-slider').removeClass('track-slider-playing');
nicholas@842 476 $(element).addClass('track-slider-playing');
nicholas@842 477 $('.comment-div').removeClass('comment-box-playing');
nicholas@842 478 $('#comment-div-'+id).addClass('comment-box-playing');
nicholas@842 479 var outsideReference = document.getElementById('outside-reference');
nicholas@842 480 if (outsideReference != undefined)
nicholas@842 481 $(outsideReference).removeClass('track-slider-playing');
nicholas@842 482 }
nicholas@842 483 };
nicholas@842 484
nicholas@842 485 this.enable = function() {
nicholas@842 486 if (this.parent.state == 1)
nicholas@842 487 {
nicholas@842 488 $(this.trackSliderObj).removeClass('track-slider-disabled');
nicholas@842 489 }
nicholas@842 490 };
nicholas@842 491
nicholas@842 492 this.exportXMLDOM = function(audioObject) {
nicholas@842 493 // Called by the audioObject holding this element. Must be present
nicholas@842 494 var node = document.createElement('value');
nicholas@842 495 node.textContent = convSliderPosToRate(this.trackSliderObj);
nicholas@842 496 return node;
nicholas@842 497 };
nicholas@842 498 this.getValue = function() {
nicholas@842 499 return convSliderPosToRate(this.trackSliderObj);
nicholas@842 500 };
nicholas@842 501 }
nicholas@842 502
nicholas@842 503 function dragEnd(ev) {
nicholas@842 504 // Function call when a div has been dropped
nicholas@842 505 var slider = document.getElementById('slider');
nicholas@842 506 var marginSize = Number(slider.attributes['marginsize'].value);
nicholas@842 507 var w = slider.style.width;
nicholas@842 508 w = Number(w.substr(0,w.length-2));
nicholas@842 509 var x = ev.x;
n@849 510 x += Math.abs(ev.view.screenLeft);
n@849 511 x = x % ev.view.outerWidth;
nicholas@842 512 if (x >= marginSize && x < w+marginSize) {
nicholas@842 513 this.style.left = (x)+'px';
nicholas@842 514 } else {
nicholas@842 515 if (x<marginSize) {
nicholas@842 516 this.style.left = marginSize+'px';
nicholas@842 517 } else {
nicholas@842 518 this.style.left = (w+marginSize) + 'px';
nicholas@842 519 }
nicholas@842 520 }
nicholas@842 521 var time = audioEngineContext.timer.getTestTime();
nicholas@842 522 var id = Number(ev.srcElement.getAttribute('trackindex'));
nicholas@842 523 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
nicholas@842 524 console.log('slider '+id+' moved to '+convSliderPosToRate(ev.srcElement)+' ('+time+')');
nicholas@842 525 }
nicholas@842 526
n@849 527 function buttonSubmitClick()
nicholas@842 528 {
nicholas@842 529 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
nicholas@842 530 var canContinue = true;
nicholas@842 531
nicholas@842 532 // Check that the anchor and reference objects are correctly placed
nicholas@842 533 if (interfaceContext.checkHiddenAnchor() == false) {return;}
nicholas@842 534 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@842 535
nicholas@842 536 for (var i=0; i<checks.length; i++) {
nicholas@842 537 if (checks[i].type == 'check')
nicholas@842 538 {
nicholas@842 539 switch(checks[i].check) {
nicholas@842 540 case 'fragmentPlayed':
nicholas@842 541 // Check if all fragments have been played
nicholas@842 542 var checkState = interfaceContext.checkAllPlayed();
nicholas@842 543 if (checkState == false) {canContinue = false;}
nicholas@842 544 break;
nicholas@842 545 case 'fragmentFullPlayback':
nicholas@842 546 // Check all fragments have been played to their full length
nicholas@842 547 var checkState = interfaceContext.checkAllPlayed();
nicholas@842 548 if (checkState == false) {canContinue = false;}
nicholas@842 549 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
nicholas@842 550 break;
nicholas@842 551 case 'fragmentMoved':
nicholas@842 552 // Check all fragment sliders have been moved.
nicholas@842 553 var checkState = interfaceContext.checkAllMoved();
nicholas@842 554 if (checkState == false) {canContinue = false;}
nicholas@842 555 break;
nicholas@842 556 case 'fragmentComments':
nicholas@842 557 // Check all fragment sliders have been moved.
nicholas@842 558 var checkState = interfaceContext.checkAllCommented();
nicholas@842 559 if (checkState == false) {canContinue = false;}
nicholas@842 560 break;
nicholas@842 561 case 'scalerange':
nicholas@842 562 // Check the scale is used to its full width outlined by the node
nicholas@842 563 var checkState = interfaceContext.checkScaleRange();
nicholas@842 564 if (checkState == false) {canContinue = false;}
nicholas@842 565 break;
nicholas@842 566 }
nicholas@842 567
nicholas@842 568 }
nicholas@842 569 if (!canContinue) {break;}
nicholas@842 570 }
nicholas@842 571
nicholas@842 572 if (canContinue) {
nicholas@842 573 if (audioEngineContext.status == 1) {
nicholas@842 574 var playback = document.getElementById('playback-button');
nicholas@842 575 playback.click();
nicholas@842 576 // 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 577 } else
nicholas@842 578 {
nicholas@842 579 if (audioEngineContext.timer.testStarted == false)
nicholas@842 580 {
nicholas@842 581 alert('You have not started the test! Please press start to begin the test!');
nicholas@842 582 return;
nicholas@842 583 }
nicholas@842 584 }
nicholas@842 585 testState.advanceState();
nicholas@842 586 }
nicholas@842 587 }
nicholas@842 588
nicholas@842 589 function convSliderPosToRate(slider)
nicholas@842 590 {
nicholas@842 591 var w = document.getElementById('slider').style.width;
nicholas@842 592 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
nicholas@842 593 var maxPix = w.substr(0,w.length-2);
nicholas@842 594 var pix = slider.style.left;
nicholas@842 595 pix = pix.substr(0,pix.length-2);
nicholas@842 596 var rate = (pix-marginsize)/maxPix;
nicholas@842 597 return rate;
nicholas@842 598 }
nicholas@842 599
nicholas@842 600 function resizeWindow(event){
nicholas@842 601 // Function called when the window has been resized.
nicholas@842 602 // MANDATORY FUNCTION
nicholas@842 603
nicholas@842 604 // Store the slider marker values
nicholas@842 605 var holdValues = [];
nicholas@842 606 $(".track-slider").each(function(index,sliderObj){
nicholas@842 607 holdValues.push(convSliderPosToRate(sliderObj));
nicholas@842 608 });
nicholas@842 609
nicholas@842 610 var width = event.target.innerWidth;
nicholas@842 611 var canvas = document.getElementById('sliderCanvasHolder');
nicholas@842 612 var sliderDiv = canvas.children[0];
nicholas@842 613 var sliderScaleDiv = canvas.children[1];
nicholas@842 614 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
nicholas@842 615 var w = (marginsize+8)*2;
nicholas@842 616 sliderDiv.style.width = width - w + 'px';
nicholas@842 617 var width = width - w;
nicholas@842 618 // Move sliders into new position
nicholas@842 619 $(".track-slider").each(function(index,sliderObj){
nicholas@842 620 var pos = holdValues[index];
nicholas@842 621 var pix = pos * width;
nicholas@842 622 sliderObj.style.left = pix+marginsize+'px';
nicholas@842 623 });
nicholas@842 624
nicholas@842 625 // Move scale labels
nicholas@842 626 $(sliderScaleDiv.children).each(function(index,scaleObj){
nicholas@842 627 var position = Number(scaleObj.attributes['value'].value);
nicholas@842 628 var pixelPosition = (position*width)+marginsize;
nicholas@842 629 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
nicholas@842 630 });
nicholas@842 631 }
nicholas@842 632
nicholas@842 633 function pageXMLSave(store, testXML)
nicholas@842 634 {
nicholas@842 635 // Saves a specific test page
nicholas@842 636 var xmlDoc = store;
nicholas@842 637 // Check if any session wide metrics are enabled
nicholas@842 638
nicholas@842 639 var commentShow = testXML.elementComments;
nicholas@842 640
nicholas@842 641 var metric = document.createElement('metric');
nicholas@842 642 if (audioEngineContext.metric.enableTestTimer)
nicholas@842 643 {
nicholas@842 644 var testTime = document.createElement('metricResult');
nicholas@842 645 testTime.id = 'testTime';
nicholas@842 646 testTime.textContent = audioEngineContext.timer.testDuration;
nicholas@842 647 metric.appendChild(testTime);
nicholas@842 648 }
nicholas@842 649 xmlDoc.appendChild(metric);
nicholas@842 650 var audioObjects = audioEngineContext.audioObjects;
nicholas@842 651 for (var i=0; i<audioObjects.length; i++)
nicholas@842 652 {
nicholas@842 653 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
nicholas@842 654 audioElement.setAttribute('presentedId',i);
nicholas@842 655 xmlDoc.appendChild(audioElement);
nicholas@842 656 }
nicholas@842 657
nicholas@842 658 $(interfaceContext.commentQuestions).each(function(index,element){
nicholas@842 659 var node = element.exportXMLDOM();
nicholas@842 660 xmlDoc.appendChild(node);
nicholas@842 661 });
nicholas@842 662 store = xmlDoc;
nicholas@842 663 }