annotate ape.js @ 1186:fd5390509d52

Added tag 1.1.0 for changeset 63c4163fc705
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 23 Dec 2015 14:48:48 +0000
parents f9c9a40f33bd
children
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@656 6
nicholas@2 7 // Once this is loaded and parsed, begin execution
n@911 8 loadInterface();
nicholas@2 9
n@911 10 function loadInterface() {
nicholas@2 11
n@701 12 // Get the dimensions of the screen available to the page
nicholas@2 13 var width = window.innerWidth;
nicholas@2 14 var height = window.innerHeight;
nicholas@2 15
nicholas@2 16 // The injection point into the HTML page
n@912 17 interfaceContext.insertPoint = document.getElementById("topLevelBody");
n@706 18 var testContent = document.createElement('div');
nicholas@934 19
n@706 20 testContent.id = 'testContent';
n@676 21
nicholas@887 22 // Bindings for interfaceContext
n@805 23 interfaceContext.checkAllPlayed = function()
nicholas@887 24 {
nicholas@887 25 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@887 26 if (hasBeenPlayed.length > 0) // if a fragment has not been played yet
nicholas@887 27 {
nicholas@887 28 str = "";
nicholas@887 29 if (hasBeenPlayed.length > 1) {
nicholas@887 30 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@887 31 str = str + hasBeenPlayed[i];
nicholas@887 32 if (i < hasBeenPlayed.length-2){
nicholas@887 33 str += ", ";
nicholas@887 34 } else if (i == hasBeenPlayed.length-2) {
nicholas@887 35 str += " or ";
nicholas@887 36 }
nicholas@887 37 }
nicholas@887 38 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 39 } else {
nicholas@887 40 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 41 }
nicholas@887 42 return false;
nicholas@887 43 }
nicholas@887 44 return true;
nicholas@891 45 };
nicholas@887 46
n@805 47 interfaceContext.checkAllMoved = function() {
nicholas@887 48 var state = true;
n@787 49 var str = 'You have not moved the following sliders. ';
n@787 50 for (var i=0; i<this.interfaceSliders.length; i++)
nicholas@887 51 {
n@787 52 var interfaceTID = [];
n@787 53 for (var j=0; j<this.interfaceSliders[i].metrics.length; j++)
n@787 54 {
n@787 55 if (this.interfaceSliders[i].metrics[j].wasMoved == false)
n@787 56 {
n@787 57 state = false;
n@787 58 interfaceTID.push(j);
n@787 59 }
n@787 60 }
n@787 61 if (interfaceTID.length != 0)
n@787 62 {
nicholas@755 63 var interfaceName = this.interfaceSliders[i].interfaceObject.title;
nicholas@755 64 if (interfaceName == undefined) {
nicholas@755 65 str += 'On axis '+String(i+1)+' you must move ';
nicholas@755 66 } else {
nicholas@755 67 str += 'On axis "'+interfaceName+'" you must move ';
nicholas@755 68 }
n@787 69 if (interfaceTID.length == 1)
n@787 70 {
nicholas@755 71 str += 'slider '+interfaceTID[0]+'. ';
n@787 72 }
n@787 73 else {
n@787 74 str += 'sliders ';
n@787 75 for (var k=0; k<interfaceTID.length-1; k++)
n@787 76 {
n@787 77 str += interfaceTID[k]+', ';
n@787 78 }
n@787 79 str += interfaceTID[interfaceTID.length-1] +'. ';
n@787 80 }
nicholas@887 81 }
nicholas@887 82 }
n@787 83 if (state != true)
n@787 84 {
n@787 85 alert(str);
n@787 86 console.log(str);
nicholas@887 87 }
nicholas@887 88 return state;
nicholas@891 89 };
nicholas@887 90
nicholas@887 91 Interface.prototype.checkAllCommented = function() {
nicholas@887 92 var audioObjs = audioEngineContext.audioObjects;
nicholas@887 93 var audioHolder = testState.stateMap[testState.stateIndex];
nicholas@887 94 var state = true;
nicholas@887 95 if (audioHolder.elementComments) {
nicholas@887 96 var strNums = [];
nicholas@887 97 for (var i=0; i<audioObjs.length; i++)
nicholas@887 98 {
nicholas@887 99 if (audioObjs[i].commentDOM.trackCommentBox.value.length == 0) {
nicholas@887 100 state = false;
nicholas@887 101 strNums.push(i);
nicholas@887 102 }
nicholas@887 103 }
nicholas@887 104 if (state == false) {
nicholas@887 105 if (strNums.length > 1) {
nicholas@887 106 var str = "";
nicholas@887 107 for (var i=0; i<strNums.length; i++) {
nicholas@887 108 str = str + strNums[i];
nicholas@887 109 if (i < strNums.length-2){
nicholas@887 110 str += ", ";
nicholas@887 111 } else if (i == strNums.length-2) {
nicholas@887 112 str += " or ";
nicholas@887 113 }
nicholas@887 114 }
nicholas@887 115 alert('You have not commented on fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 116 } else {
nicholas@887 117 alert('You have not commented on fragment ' + strNums[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@887 118 }
nicholas@887 119 }
nicholas@887 120 }
nicholas@887 121 return state;
nicholas@891 122 };
nicholas@887 123
nicholas@1039 124 Interface.prototype.checkScaleRange = function()
nicholas@1039 125 {
nicholas@1039 126 var audioObjs = audioEngineContext.audioObjects;
nicholas@1039 127 var audioHolder = testState.stateMap[testState.stateIndex];
n@788 128 var state = true;
n@788 129 var str = '';
n@788 130 for (var i=0; i<this.interfaceSliders.length; i++)
nicholas@1039 131 {
n@776 132 var minScale;
n@776 133 var maxScale;
n@788 134 var interfaceObject = interfaceContext.interfaceSliders[0].interfaceObject;
n@788 135 for (var j=0; j<interfaceObject.options.length; j++)
n@776 136 {
n@788 137 if (interfaceObject.options[j].check == "scalerange") {
n@788 138 minScale = interfaceObject.options[j].min;
n@788 139 maxScale = interfaceObject.options[j].max;
n@776 140 break;
n@776 141 }
n@776 142 }
n@788 143 var minRanking = convSliderPosToRate(this.interfaceSliders[i].sliders[0]);
n@788 144 var maxRanking = minRanking;
n@788 145 for (var j=1; j<this.interfaceSliders[i].sliders.length; j++)
n@788 146 {
n@788 147 var ranking = convSliderPosToRate(this.interfaceSliders[i].sliders[j]);
n@788 148 if (ranking < minRanking)
n@788 149 {
n@788 150 minRanking = ranking;
n@788 151 } else if (ranking > maxRanking)
n@788 152 {
n@788 153 maxRanking = ranking;
n@776 154 }
n@776 155 }
n@788 156 if (minRanking > minScale || maxRanking < maxScale)
n@788 157 {
n@788 158 state = false;
n@788 159 str += 'On axis "'+this.interfaceSliders[i].interfaceObject.title+'" you have not used the full width of the scale. ';
nicholas@1039 160 }
nicholas@1039 161 }
n@788 162 if (state != true)
n@788 163 {
n@788 164 alert(str);
n@788 165 console.log(str);
nicholas@1039 166 }
n@788 167 return state;
nicholas@1039 168 };
nicholas@1039 169
n@832 170 Interface.prototype.objectSelected = null;
n@832 171 Interface.prototype.objectMoved = false;
n@832 172 Interface.prototype.selectObject = function(object)
n@832 173 {
n@832 174 if (this.objectSelected == null)
n@832 175 {
n@832 176 this.objectSelected = object;
n@832 177 this.objectMoved = false;
n@832 178 }
n@832 179 };
n@832 180 Interface.prototype.moveObject = function()
n@832 181 {
n@832 182 if (this.objectMoved == false)
n@832 183 {
n@832 184 this.objectMoved = true;
n@832 185 }
n@832 186 };
n@832 187 Interface.prototype.releaseObject = function()
n@832 188 {
n@832 189 this.objectSelected = null;
n@832 190 this.objectMoved = false;
n@832 191 };
n@832 192 Interface.prototype.getSelectedObject = function()
n@832 193 {
n@832 194 return this.objectSelected;
n@832 195 };
n@832 196 Interface.prototype.hasSelectedObjectMoved = function()
n@832 197 {
n@832 198 return this.objectMoved;
n@832 199 };
n@832 200
nicholas@763 201 // Bindings for slider interfaces
nicholas@763 202 Interface.prototype.interfaceSliders = [];
nicholas@763 203
n@907 204 // Bindings for audioObjects
n@907 205
nicholas@2 206 // Create the top div for the Title element
n@911 207 var titleAttr = specification.title;
nicholas@2 208 var title = document.createElement('div');
nicholas@2 209 title.className = "title";
nicholas@2 210 title.align = "center";
nicholas@2 211 var titleSpan = document.createElement('span');
nicholas@2 212
nicholas@2 213 // Set title to that defined in XML, else set to default
nicholas@2 214 if (titleAttr != undefined) {
n@911 215 titleSpan.textContent = titleAttr;
nicholas@2 216 } else {
n@911 217 titleSpan.textContent = 'Listening test';
nicholas@2 218 }
nicholas@2 219 // Insert the titleSpan element into the title div element.
nicholas@2 220 title.appendChild(titleSpan);
nicholas@2 221
nicholas@7 222 // Create Interface buttons!
nicholas@7 223 var interfaceButtons = document.createElement('div');
nicholas@7 224 interfaceButtons.id = 'interface-buttons';
nicholas@7 225
nicholas@7 226 // Create playback start/stop points
nicholas@7 227 var playback = document.createElement("button");
BrechtDeMan@939 228 playback.innerHTML = 'Stop';
n@673 229 playback.id = 'playback-button';
n@701 230 // onclick function. Check if it is playing or not, call the correct function in the
n@701 231 // audioEngine, change the button text to reflect the next state.
nicholas@7 232 playback.onclick = function() {
BrechtDeMan@939 233 if (audioEngineContext.status == 1) {
BrechtDeMan@939 234 audioEngineContext.stop();
n@709 235 this.innerHTML = 'Stop';
BrechtDeMan@936 236 var time = audioEngineContext.timer.getTestTime();
BrechtDeMan@936 237 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@7 238 }
n@701 239 };
nicholas@7 240 // Create Submit (save) button
nicholas@7 241 var submit = document.createElement("button");
n@709 242 submit.innerHTML = 'Submit';
n@667 243 submit.onclick = buttonSubmitClick;
n@673 244 submit.id = 'submit-button';
n@701 245 // Append the interface buttons into the interfaceButtons object.
nicholas@7 246 interfaceButtons.appendChild(playback);
nicholas@7 247 interfaceButtons.appendChild(submit);
nicholas@7 248
n@776 249 var sliderHolder = document.createElement("div");
n@776 250 sliderHolder.id = "slider-holder";
nicholas@2 251
n@671 252
n@701 253 // Global parent for the comment boxes on the page
nicholas@3 254 var feedbackHolder = document.createElement('div');
n@658 255 feedbackHolder.id = 'feedbackHolder';
n@656 256
n@662 257 testContent.style.zIndex = 1;
n@912 258 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@656 259
n@662 260 // Inject into HTML
n@662 261 testContent.appendChild(title); // Insert the title
n@662 262 testContent.appendChild(interfaceButtons);
n@776 263 testContent.appendChild(sliderHolder);
n@662 264 testContent.appendChild(feedbackHolder);
n@912 265 interfaceContext.insertPoint.appendChild(testContent);
n@656 266
n@660 267 // Load the full interface
nicholas@964 268 testState.initialise();
nicholas@964 269 testState.advanceState();
n@663 270
n@656 271 }
n@656 272
n@911 273 function loadTest(audioHolderObject)
n@658 274 {
n@776 275 var width = window.innerWidth;
n@776 276 var height = window.innerHeight;
n@911 277 var id = audioHolderObject.id;
n@658 278
nicholas@763 279 interfaceContext.interfaceSliders = [];
nicholas@763 280
n@658 281 var feedbackHolder = document.getElementById('feedbackHolder');
n@776 282 var sliderHolder = document.getElementById('slider-holder');
n@658 283 feedbackHolder.innerHTML = null;
n@776 284 sliderHolder.innerHTML = null;
n@767 285
n@749 286 // Delete outside reference
n@749 287 var outsideReferenceHolder = document.getElementById('outside-reference');
n@749 288 if (outsideReferenceHolder != null) {
n@749 289 document.getElementById('interface-buttons').removeChild(outsideReferenceHolder);
n@749 290 }
n@671 291
nicholas@1055 292 var interfaceObj = audioHolderObject.interfaces;
nicholas@1055 293 for (var k=0; k<interfaceObj.length; k++) {
n@776 294 // Create the div box to center align
nicholas@763 295 interfaceContext.interfaceSliders.push(new interfaceSliderHolder(interfaceObj[k]));
nicholas@1055 296 for (var i=0; i<interfaceObj[k].options.length; i++)
nicholas@1055 297 {
nicholas@1055 298 if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'playhead')
nicholas@1055 299 {
nicholas@1055 300 var playbackHolder = document.getElementById('playback-holder');
nicholas@1055 301 if (playbackHolder == null)
nicholas@1055 302 {
nicholas@1055 303 playbackHolder = document.createElement('div');
nicholas@1055 304 playbackHolder.style.width = "100%";
nicholas@1055 305 playbackHolder.align = 'center';
nicholas@1055 306 playbackHolder.appendChild(interfaceContext.playhead.object);
nicholas@1055 307 feedbackHolder.appendChild(playbackHolder);
nicholas@1055 308 }
nicholas@867 309 } else if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'page-count')
nicholas@867 310 {
nicholas@867 311 var pagecountHolder = document.getElementById('page-count');
nicholas@867 312 if (pagecountHolder == null)
nicholas@867 313 {
nicholas@867 314 pagecountHolder = document.createElement('div');
nicholas@867 315 pagecountHolder.id = 'page-count';
nicholas@867 316 }
BrechtDeMan@753 317 pagecountHolder.innerHTML = '<span>Page '+(audioHolderObject.presentedId+1)+' of '+specification.audioHolders.length+'</span>';
nicholas@867 318 var inject = document.getElementById('interface-buttons');
nicholas@867 319 inject.appendChild(pagecountHolder);
nicholas@1055 320 }
nicholas@1055 321 }
nicholas@1055 322 }
nicholas@1055 323
BrechtDeMan@810 324 var commentBoxPrefix = "Comment on fragment";
n@669 325
n@911 326 var commentShow = audioHolderObject.elementComments;
nicholas@689 327
n@911 328 var loopPlayback = audioHolderObject.loop;
n@911 329
n@670 330 currentTestHolder = document.createElement('audioHolder');
n@911 331 currentTestHolder.id = audioHolderObject.id;
n@911 332 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@670 333
n@701 334 // Find all the audioElements from the audioHolder
n@911 335 $(audioHolderObject.audioElements).each(function(index,element){
nicholas@7 336 // Find URL of track
n@701 337 // In this jQuery loop, variable 'this' holds the current audioElement.
n@701 338
n@804 339 // Check if an outside reference
n@804 340 if (index == audioHolderObject.outsideReference)
n@804 341 {
n@804 342 return;
n@804 343 }
n@804 344
n@701 345 // Now load each audio sample. First create the new track by passing the full URL
n@911 346 var trackURL = audioHolderObject.hostURL + element.url;
n@912 347 var audioObject = audioEngineContext.newTrack(element);
nicholas@689 348
n@1029 349 var node = interfaceContext.createCommentBox(audioObject);
n@701 350
nicholas@5 351 // Create a slider per track
n@776 352 audioObject.interfaceDOM = new sliderObject(audioObject,interfaceObj);
n@785 353 audioObject.metric.initialPosition = convSliderPosToRate(audioObject.interfaceDOM.trackSliderObjects[0]);
n@773 354 if (audioObject.state == 1)
n@773 355 {
n@773 356 audioObject.interfaceDOM.enable();
n@773 357 }
BrechtDeMan@940 358
n@700 359 });
n@831 360 $('.track-slider').mousedown(function(event) {
n@832 361 interfaceContext.selectObject($(this)[0]);
n@830 362 });
n@812 363 $('.track-slider').on('touchstart',null,function(event) {
n@812 364 interfaceContext.selectObject($(this)[0]);
n@812 365 });
n@830 366
n@831 367 $('.track-slider').mousemove(function(event) {
n@831 368 event.preventDefault();
n@831 369 });
n@831 370
n@776 371 $('.slider').mousemove(function(event) {
n@830 372 event.preventDefault();
n@832 373 var obj = interfaceContext.getSelectedObject();
n@832 374 if (obj == null) {return;}
n@832 375 $(obj).css("left",event.clientX + "px");
n@832 376 interfaceContext.moveObject();
n@832 377 });
n@812 378
n@812 379 $('.slider').on('touchmove',null,function(event) {
n@812 380 event.preventDefault();
n@812 381 var obj = interfaceContext.getSelectedObject();
n@812 382 if (obj == null) {return;}
n@812 383 var move = event.originalEvent.targetTouches[0].clientX - 6;
n@812 384 $(obj).css("left",move + "px");
n@812 385 interfaceContext.moveObject();
n@812 386 });
n@832 387
n@832 388 $(document).mouseup(function(event){
n@832 389 event.preventDefault();
n@832 390 var obj = interfaceContext.getSelectedObject();
n@832 391 if (obj == null) {return;}
nicholas@764 392 var interfaceID = obj.parentElement.getAttribute("interfaceid");
nicholas@764 393 var trackID = obj.getAttribute("trackindex");
n@832 394 if (interfaceContext.hasSelectedObjectMoved() == true)
n@830 395 {
n@832 396 var l = $(obj).css("left");
n@832 397 var id = obj.getAttribute('trackIndex');
n@832 398 var time = audioEngineContext.timer.getTestTime();
n@832 399 var rate = convSliderPosToRate(obj);
n@832 400 audioEngineContext.audioObjects[id].metric.moved(time,rate);
nicholas@764 401 interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate);
n@832 402 console.log("slider "+id+" moved to "+rate+' ('+time+')');
n@832 403 } else {
n@832 404 var id = Number(obj.attributes['trackIndex'].value);
n@832 405 //audioEngineContext.metric.sliderPlayed(id);
n@832 406 audioEngineContext.play(id);
n@832 407 // Currently playing track red, rest green
n@832 408
n@832 409 $('.track-slider').removeClass('track-slider-playing');
n@776 410 var name = ".track-slider-"+obj.getAttribute("trackindex");
n@776 411 $(name).addClass('track-slider-playing');
n@832 412 $('.comment-div').removeClass('comment-box-playing');
n@832 413 $('#comment-div-'+id).addClass('comment-box-playing');
n@832 414 var outsideReference = document.getElementById('outside-reference');
n@832 415 if (outsideReference != undefined)
n@832 416 $(outsideReference).removeClass('track-slider-playing');
n@830 417 }
n@832 418 interfaceContext.releaseObject();
n@830 419 });
n@830 420
n@812 421 $('.slider').on('touchend',null,function(event){
n@812 422 var obj = interfaceContext.getSelectedObject();
n@812 423 if (obj == null) {return;}
n@812 424 var interfaceID = obj.parentElement.getAttribute("interfaceid");
n@812 425 var trackID = obj.getAttribute("trackindex");
n@812 426 if (interfaceContext.hasSelectedObjectMoved() == true)
n@812 427 {
n@812 428 var l = $(obj).css("left");
n@812 429 var id = obj.getAttribute('trackIndex');
n@812 430 var time = audioEngineContext.timer.getTestTime();
n@812 431 var rate = convSliderPosToRate(obj);
n@812 432 audioEngineContext.audioObjects[id].metric.moved(time,rate);
n@812 433 interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate);
n@812 434 console.log("slider "+id+" moved to "+rate+' ('+time+')');
n@812 435 }
n@812 436 interfaceContext.releaseObject();
n@812 437 });
n@812 438
n@830 439
n@912 440 if (commentShow) {
n@912 441 interfaceContext.showCommentBoxes(feedbackHolder,true);
n@912 442 }
nicholas@3 443
n@911 444 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@1026 445 var node = interfaceContext.createCommentQuestion(element);
n@1026 446 feedbackHolder.appendChild(node.holder);
nicholas@688 447 });
n@1007 448
nicholas@1044 449 // Construct outside reference;
nicholas@1044 450 if (audioHolderObject.outsideReference != null) {
nicholas@1044 451 var outsideReferenceHolder = document.createElement('div');
nicholas@1044 452 outsideReferenceHolder.id = 'outside-reference';
n@783 453 outsideReferenceHolder.className = 'outside-reference';
nicholas@1044 454 outsideReferenceHolderspan = document.createElement('span');
nicholas@1044 455 outsideReferenceHolderspan.textContent = 'Reference';
nicholas@1044 456 outsideReferenceHolder.appendChild(outsideReferenceHolderspan);
nicholas@1044 457
n@804 458 var audioObject = audioEngineContext.newTrack(audioHolderObject.audioElements[audioHolderObject.outsideReference]);
nicholas@1044 459
n@852 460 outsideReferenceHolder.onclick = function(event)
nicholas@1044 461 {
nicholas@1044 462 audioEngineContext.play(audioEngineContext.audioObjects.length-1);
nicholas@1044 463 $('.track-slider').removeClass('track-slider-playing');
nicholas@1044 464 $('.comment-div').removeClass('comment-box-playing');
n@852 465 if (event.currentTarget.nodeName == 'DIV') {
n@852 466 $(event.currentTarget).addClass('track-slider-playing');
nicholas@1044 467 } else {
n@852 468 $(event.currentTarget.parentElement).addClass('track-slider-playing');
nicholas@1044 469 }
nicholas@1044 470 };
nicholas@1044 471
nicholas@1044 472 document.getElementById('interface-buttons').appendChild(outsideReferenceHolder);
nicholas@1044 473 }
nicholas@1044 474
n@1007 475
nicholas@864 476 //testWaitIndicator();
n@663 477 }
n@663 478
nicholas@763 479 function interfaceSliderHolder(interfaceObject)
nicholas@763 480 {
nicholas@763 481 this.sliders = [];
nicholas@764 482 this.metrics = [];
nicholas@763 483 this.id = document.getElementsByClassName("sliderCanvasDiv").length;
nicholas@763 484 this.name = interfaceObject.name;
nicholas@763 485 this.interfaceObject = interfaceObject;
nicholas@763 486 this.sliderDOM = document.createElement('div');
nicholas@763 487 this.sliderDOM.className = 'sliderCanvasDiv';
nicholas@763 488 this.sliderDOM.id = 'sliderCanvasHolder-'+this.id;
nicholas@763 489
nicholas@763 490 var pagetitle = document.createElement('div');
nicholas@763 491 pagetitle.className = "pageTitle";
nicholas@763 492 pagetitle.align = "center";
nicholas@763 493 var titleSpan = document.createElement('span');
nicholas@763 494 titleSpan.id = "pageTitle-"+this.id;
nicholas@763 495 if (interfaceObject.title != undefined && typeof interfaceObject.title == "string")
nicholas@763 496 {
nicholas@763 497 titleSpan.textContent = interfaceObject.title;
nicholas@755 498 } else {
nicholas@755 499 titleSpan.textContent = "Axis "+String(this.id+1);
nicholas@763 500 }
nicholas@763 501 pagetitle.appendChild(titleSpan);
nicholas@763 502 this.sliderDOM.appendChild(pagetitle);
nicholas@763 503
nicholas@763 504 // Create the slider box to hold the slider elements
nicholas@763 505 this.canvas = document.createElement('div');
nicholas@763 506 if (this.name != undefined)
nicholas@763 507 this.canvas.id = 'slider-'+this.name;
nicholas@763 508 else
nicholas@763 509 this.canvas.id = 'slider-'+this.id;
nicholas@764 510 this.canvas.setAttribute("interfaceid",this.id);
nicholas@763 511 this.canvas.className = 'slider';
nicholas@763 512 this.canvas.align = "left";
nicholas@763 513 this.canvas.addEventListener('dragover',function(event){
nicholas@763 514 event.preventDefault();
nicholas@763 515 event.dataTransfer.effectAllowed = 'none';
nicholas@763 516 event.dataTransfer.dropEffect = 'copy';
nicholas@763 517 return false;
nicholas@763 518 },false);
nicholas@763 519 var sliderMargin = document.createAttribute('marginsize');
nicholas@763 520 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
nicholas@763 521 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@763 522 var w = (Number(sliderMargin.nodeValue)+8)*2;
nicholas@763 523 this.canvas.style.width = window.innerWidth - w +"px";
nicholas@763 524 this.canvas.style.marginLeft = sliderMargin.nodeValue +'px';
nicholas@763 525 this.canvas.setAttributeNode(sliderMargin);
nicholas@763 526 this.sliderDOM.appendChild(this.canvas);
nicholas@763 527
nicholas@763 528 // Create the div to hold any scale objects
nicholas@763 529 this.scale = document.createElement('div');
nicholas@763 530 this.scale.className = 'sliderScale';
nicholas@763 531 this.scale.id = 'sliderScaleHolder-'+this.id;
nicholas@763 532 this.scale.align = 'left';
nicholas@763 533 this.sliderDOM.appendChild(this.scale);
nicholas@763 534 var positionScale = this.canvas.style.width.substr(0,this.canvas.style.width.length-2);
nicholas@763 535 var offset = Number(this.canvas.attributes['marginsize'].value);
nicholas@763 536 for (var index=0; index<interfaceObject.scale.length; index++)
nicholas@763 537 {
nicholas@763 538 var scaleObj = interfaceObject.scale[index];
nicholas@763 539 var value = document.createAttribute('value');
nicholas@763 540 var position = Number(scaleObj[0])*0.01;
nicholas@763 541 value.nodeValue = position;
nicholas@763 542 var pixelPosition = (position*positionScale)+offset;
nicholas@763 543 var scaleDOM = document.createElement('span');
nicholas@763 544 scaleDOM.textContent = scaleObj[1];
nicholas@763 545 this.scale.appendChild(scaleDOM);
nicholas@763 546 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
nicholas@763 547 scaleDOM.setAttributeNode(value);
nicholas@763 548 }
nicholas@763 549
nicholas@763 550 var dest = document.getElementById("slider-holder");
nicholas@763 551 dest.appendChild(this.sliderDOM);
nicholas@763 552
nicholas@763 553 this.createSliderObject = function(audioObject)
n@776 554 {
n@776 555 var trackObj = document.createElement('div');
n@776 556 trackObj.className = 'track-slider track-slider-disabled track-slider-'+audioObject.id;
nicholas@763 557 trackObj.id = 'track-slider-'+this.id+'-'+audioObject.id;
n@776 558 trackObj.setAttribute('trackIndex',audioObject.id);
n@776 559 trackObj.innerHTML = '<span>'+audioObject.id+'</span>';
nicholas@763 560 if (this.name != undefined) {
nicholas@763 561 trackObj.setAttribute('interface-name',this.name);
n@776 562 } else {
nicholas@763 563 trackObj.setAttribute('interface-name',this.id);
n@776 564 }
nicholas@763 565 var offset = Number(this.canvas.attributes['marginsize'].value);
n@776 566 // Distribute it randomnly
n@776 567 var w = window.innerWidth - (offset+8)*2;
n@776 568 w = Math.random()*w;
n@776 569 w = Math.floor(w+(offset+8));
n@776 570 trackObj.style.left = w+'px';
nicholas@763 571 this.canvas.appendChild(trackObj);
nicholas@763 572 this.sliders.push(trackObj);
nicholas@764 573 this.metrics.push(new metricTracker(this));
nicholas@764 574 this.metrics[this.metrics.length-1].initialPosition = convSliderPosToRate(trackObj);
nicholas@763 575 return trackObj;
nicholas@763 576 };
nicholas@763 577
nicholas@763 578 this.resize = function(event)
nicholas@763 579 {
nicholas@763 580 var holdValues = [];
nicholas@763 581 for (var index = 0; index < this.sliders.length; index++)
nicholas@763 582 {
nicholas@763 583 holdValues.push(convSliderPosToRate(this.sliders[index]));
nicholas@763 584 }
nicholas@763 585 var width = event.target.innerWidth;
nicholas@763 586 var sliderDiv = this.canvas;
nicholas@763 587 var sliderScaleDiv = this.scale;
nicholas@763 588 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
nicholas@763 589 var w = (marginsize+8)*2;
nicholas@763 590 sliderDiv.style.width = width - w + 'px';
nicholas@763 591 var width = width - w;
nicholas@763 592 // Move sliders into new position
nicholas@763 593 for (var index = 0; index < this.sliders.length; index++)
nicholas@763 594 {
nicholas@763 595 var pos = holdValues[index];
nicholas@763 596 var pix = pos * width;
nicholas@763 597 this.sliders[index].style.left = pix+marginsize+'px';
nicholas@763 598 }
nicholas@763 599
nicholas@763 600 // Move scale labels
nicholas@763 601 for (var index = 0; index < this.scale.children.length; index++)
nicholas@763 602 {
nicholas@763 603 var scaleObj = this.scale.children[index];
nicholas@763 604 var position = Number(scaleObj.attributes['value'].value);
nicholas@763 605 var pixelPosition = (position*width)+marginsize;
nicholas@763 606 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
nicholas@763 607 }
n@783 608 };
nicholas@763 609 }
nicholas@763 610
nicholas@763 611 function sliderObject(audioObject,interfaceObjects) {
n@913 612 // Create a new slider object;
n@913 613 this.parent = audioObject;
nicholas@763 614 this.trackSliderObjects = [];
nicholas@763 615 for (var i=0; i<interfaceContext.interfaceSliders.length; i++)
nicholas@763 616 {
nicholas@763 617 var trackObj = interfaceContext.interfaceSliders[i].createSliderObject(audioObject);
nicholas@763 618 this.trackSliderObjects.push(trackObj);
n@776 619 }
n@913 620
n@913 621 // Onclick, switch playback to that track
n@913 622
nicholas@864 623 this.enable = function() {
nicholas@864 624 if (this.parent.state == 1)
nicholas@864 625 {
n@776 626 $(this.trackSliderObjects).each(function(i,trackObj){
n@776 627 $(trackObj).removeClass('track-slider-disabled');
n@776 628 });
nicholas@864 629 }
nicholas@864 630 };
nicholas@760 631 this.updateLoading = function(progress)
nicholas@760 632 {
nicholas@760 633 if (progress != 100)
nicholas@760 634 {
nicholas@760 635 progress = String(progress);
nicholas@760 636 progress = progress.split('.')[0];
nicholas@760 637 this.trackSliderObjects[0].children[0].textContent = progress+'%';
nicholas@760 638 } else {
nicholas@760 639 this.trackSliderObjects[0].children[0].textContent = this.parent.id;
nicholas@760 640 }
nicholas@760 641 };
nicholas@891 642 this.exportXMLDOM = function(audioObject) {
n@913 643 // Called by the audioObject holding this element. Must be present
n@776 644 var obj = [];
n@776 645 $(this.trackSliderObjects).each(function(i,trackObj){
n@776 646 var node = document.createElement('value');
BrechtDeMan@810 647 node.setAttribute("interface-name",trackObj.getAttribute("interface-name"));
n@776 648 node.textContent = convSliderPosToRate(trackObj);
n@776 649 obj.push(node);
n@776 650 });
n@776 651
n@776 652 return obj;
n@913 653 };
nicholas@891 654 this.getValue = function() {
nicholas@891 655 return convSliderPosToRate(this.trackSliderObj);
nicholas@892 656 };
n@913 657 }
nicholas@5 658
n@849 659 function buttonSubmitClick()
n@663 660 {
nicholas@1040 661 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
nicholas@887 662 var canContinue = true;
nicholas@891 663
nicholas@891 664 // Check that the anchor and reference objects are correctly placed
nicholas@1043 665 if (interfaceContext.checkHiddenAnchor() == false) {return;}
nicholas@1043 666 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@891 667
nicholas@887 668 for (var i=0; i<checks.length; i++) {
nicholas@887 669 if (checks[i].type == 'check')
nicholas@887 670 {
nicholas@887 671 switch(checks[i].check) {
nicholas@887 672 case 'fragmentPlayed':
nicholas@887 673 // Check if all fragments have been played
nicholas@887 674 var checkState = interfaceContext.checkAllPlayed();
nicholas@887 675 if (checkState == false) {canContinue = false;}
nicholas@887 676 break;
nicholas@887 677 case 'fragmentFullPlayback':
nicholas@887 678 // Check all fragments have been played to their full length
n@837 679 var checkState = interfaceContext.checkFragmentsFullyPlayed();
nicholas@887 680 if (checkState == false) {canContinue = false;}
nicholas@887 681 break;
nicholas@887 682 case 'fragmentMoved':
nicholas@887 683 // Check all fragment sliders have been moved.
nicholas@887 684 var checkState = interfaceContext.checkAllMoved();
nicholas@887 685 if (checkState == false) {canContinue = false;}
nicholas@887 686 break;
nicholas@887 687 case 'fragmentComments':
nicholas@887 688 // Check all fragment sliders have been moved.
nicholas@887 689 var checkState = interfaceContext.checkAllCommented();
nicholas@887 690 if (checkState == false) {canContinue = false;}
nicholas@887 691 break;
nicholas@1039 692 case 'scalerange':
nicholas@1039 693 // Check the scale is used to its full width outlined by the node
nicholas@1039 694 var checkState = interfaceContext.checkScaleRange();
nicholas@1039 695 if (checkState == false) {canContinue = false;}
nicholas@1039 696 break;
nicholas@762 697 default:
nicholas@762 698 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
nicholas@762 699 break;
nicholas@887 700 }
nicholas@887 701
nicholas@887 702 }
nicholas@1039 703 if (!canContinue) {break;}
nicholas@887 704 }
nicholas@887 705
nicholas@887 706 if (canContinue) {
nicholas@944 707 if (audioEngineContext.status == 1) {
nicholas@944 708 var playback = document.getElementById('playback-button');
nicholas@944 709 playback.click();
nicholas@944 710 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nicholas@944 711 } else
nicholas@944 712 {
nicholas@944 713 if (audioEngineContext.timer.testStarted == false)
nicholas@944 714 {
BrechtDeMan@747 715 alert('You have not started the test! Please click a fragment to begin the test!');
nicholas@944 716 return;
nicholas@944 717 }
nicholas@944 718 }
nicholas@964 719 testState.advanceState();
nicholas@887 720 }
n@664 721 }
n@664 722
n@776 723 function convSliderPosToRate(trackSlider)
n@675 724 {
n@776 725 var slider = trackSlider.parentElement;
n@776 726 var w = slider.style.width;
n@776 727 var marginsize = Number(slider.attributes['marginsize'].value);
n@675 728 var maxPix = w.substr(0,w.length-2);
n@776 729 var pix = trackSlider.style.left;
n@675 730 pix = pix.substr(0,pix.length-2);
n@963 731 var rate = (pix-marginsize)/maxPix;
n@675 732 return rate;
n@675 733 }
n@675 734
n@963 735 function resizeWindow(event){
n@963 736 // Function called when the window has been resized.
n@963 737 // MANDATORY FUNCTION
n@963 738
nicholas@764 739 // Resize the slider objects
nicholas@763 740 for (var i=0; i<interfaceContext.interfaceSliders.length; i++)
nicholas@763 741 {
nicholas@763 742 interfaceContext.interfaceSliders[i].resize(event);
nicholas@763 743 }
n@963 744 }
n@963 745
n@911 746 function pageXMLSave(store, testXML)
n@668 747 {
n@775 748 // MANDATORY
n@668 749 // Saves a specific test page
nicholas@764 750 // You can use this space to add any extra nodes to your XML <audioHolder> saves
nicholas@764 751 // Get the current <audioHolder> information in store (remember to appendChild your data to it)
n@785 752 if (interfaceContext.interfaceSliders.length == 1)
n@674 753 {
n@785 754 // If there is only one axis, there only needs to be one metric return
n@785 755 return;
n@674 756 }
nicholas@764 757 var audioelements = store.getElementsByTagName("audioelement");
nicholas@764 758 for (var i=0; i<audioelements.length; i++)
n@669 759 {
nicholas@764 760 // Have to append the metric specific nodes
n@785 761 if (testXML.outsideReference == null || testXML.outsideReference.id != audioelements[i].id)
nicholas@764 762 {
n@786 763 var inject = audioelements[i].getElementsByTagName("metric");
n@786 764 if (inject.length == 0)
n@786 765 {
n@786 766 inject = document.createElement("metric");
n@786 767 } else {
n@786 768 inject = inject[0];
n@786 769 }
n@785 770 for (var k=0; k<interfaceContext.interfaceSliders.length; k++)
n@785 771 {
n@785 772 var node = interfaceContext.interfaceSliders[k].metrics[i].exportXMLDOM();
n@786 773 var mrnodes = node.getElementsByTagName("metricresult");
n@786 774 for (var j=0; j<mrnodes.length; j++)
n@786 775 {
n@786 776 var name = mrnodes[j].getAttribute("name");
n@786 777 if (name == "elementTracker" || name == "elementTrackerFull" || name == "elementInitialPosition" || name == "elementFlagMoved")
n@786 778 {
n@786 779 mrnodes[j].setAttribute("interface-name",interfaceContext.interfaceSliders[k].name);
n@786 780 mrnodes[j].setAttribute("interface-id",k);
n@786 781 inject.appendChild(mrnodes[j]);
n@786 782 }
n@786 783 }
n@785 784 }
nicholas@764 785 }
n@669 786 }
nicholas@690 787 }