annotate ape.js @ 371:ad267c5e32ae

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