annotate ape.js @ 1533:317faa29ab11

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