annotate ape.js @ 2061:0fb464ac7313

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