annotate ape.js @ 1521:dfbe08826f64

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