annotate ape.js @ 1322:ed4bee105e84

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