annotate interfaces/ape.js @ 2796:35037fb7a843

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