annotate interfaces/AB.js @ 2177:43e1cb74cafb

Fixed Feature #1270. All interfaces (except ABX) support outside-reference.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 22 Mar 2016 12:09:08 +0000
parents 686f1fb84d7c
children 487f3767aca6
rev   line source
nickjillings@1341 1 // Once this is loaded and parsed, begin execution
nickjillings@1341 2 loadInterface();
nickjillings@1341 3
nickjillings@1341 4 function loadInterface() {
nickjillings@1341 5 // Get the dimensions of the screen available to the page
nickjillings@1341 6 var width = window.innerWidth;
nickjillings@1341 7 var height = window.innerHeight;
nickjillings@1341 8 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
nickjillings@1341 9
nickjillings@2110 10 // Custom comparator Object
nickjillings@2110 11 Interface.prototype.comparator = null;
nickjillings@1341 12
nickjillings@1341 13 // The injection point into the HTML page
nickjillings@1341 14 interfaceContext.insertPoint = document.getElementById("topLevelBody");
nickjillings@1341 15 var testContent = document.createElement('div');
nickjillings@1341 16 testContent.id = 'testContent';
nickjillings@1341 17
nickjillings@1341 18 // Create the top div for the Title element
nickjillings@1341 19 var titleAttr = specification.title;
nickjillings@1341 20 var title = document.createElement('div');
nickjillings@1341 21 title.className = "title";
nickjillings@1341 22 title.align = "center";
nickjillings@1341 23 var titleSpan = document.createElement('span');
nickjillings@1341 24
nickjillings@1341 25 // Set title to that defined in XML, else set to default
nickjillings@1341 26 if (titleAttr != undefined) {
nickjillings@1341 27 titleSpan.textContent = titleAttr;
nickjillings@1341 28 } else {
nickjillings@1341 29 titleSpan.textContent = 'Listening test';
nickjillings@1341 30 }
nickjillings@1341 31 // Insert the titleSpan element into the title div element.
nickjillings@1341 32 title.appendChild(titleSpan);
nickjillings@1341 33
nickjillings@1341 34 var pagetitle = document.createElement('div');
nickjillings@1341 35 pagetitle.className = "pageTitle";
nickjillings@1341 36 pagetitle.align = "center";
nickjillings@1341 37 var titleSpan = document.createElement('span');
nickjillings@1341 38 titleSpan.id = "pageTitle";
nickjillings@1341 39 pagetitle.appendChild(titleSpan);
nickjillings@1341 40
nickjillings@1341 41 // Create Interface buttons!
nickjillings@1341 42 var interfaceButtons = document.createElement('div');
nickjillings@1341 43 interfaceButtons.id = 'interface-buttons';
nickjillings@1341 44 interfaceButtons.style.height = '25px';
nickjillings@1341 45
nickjillings@1341 46 // Create playback start/stop points
nickjillings@1341 47 var playback = document.createElement("button");
nickjillings@1341 48 playback.innerHTML = 'Stop';
nickjillings@1341 49 playback.id = 'playback-button';
nickjillings@1341 50 playback.style.float = 'left';
nickjillings@1341 51 // onclick function. Check if it is playing or not, call the correct function in the
nickjillings@1341 52 // audioEngine, change the button text to reflect the next state.
nickjillings@1341 53 playback.onclick = function() {
nickjillings@1341 54 if (audioEngineContext.status == 1) {
nickjillings@1341 55 audioEngineContext.stop();
nickjillings@1341 56 this.innerHTML = 'Stop';
nickjillings@1341 57 var time = audioEngineContext.timer.getTestTime();
nickjillings@1341 58 console.log('Stopped at ' + time); // DEBUG/SAFETY
nickjillings@1341 59 }
nickjillings@1341 60 };
nickjillings@1341 61 // Append the interface buttons into the interfaceButtons object.
nickjillings@1341 62 interfaceButtons.appendChild(playback);
nickjillings@1341 63
nickjillings@1341 64 // Global parent for the comment boxes on the page
nickjillings@1341 65 var feedbackHolder = document.createElement('div');
nickjillings@1341 66 feedbackHolder.id = 'feedbackHolder';
nickjillings@1341 67
nickjillings@1341 68 // Construct the AB Boxes
nickjillings@1341 69 var boxes = document.createElement('div');
nickjillings@1341 70 boxes.align = "center";
nickjillings@1341 71 boxes.id = "box-holders";
nickjillings@1341 72 boxes.style.float = "left";
nickjillings@1341 73
nickjillings@1341 74 var submit = document.createElement('button');
nickjillings@1341 75 submit.id = "submit";
nickjillings@1341 76 submit.onclick = buttonSubmitClick;
nickjillings@1341 77 submit.className = "big-button";
nickjillings@1341 78 submit.textContent = "submit";
nickjillings@1341 79 submit.style.position = "relative";
nickjillings@1341 80 submit.style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@1341 81
nickjillings@1341 82 feedbackHolder.appendChild(boxes);
nickjillings@1341 83
nickjillings@1341 84 // Inject into HTML
nickjillings@1341 85 testContent.appendChild(title); // Insert the title
nickjillings@1341 86 testContent.appendChild(pagetitle);
nickjillings@1341 87 testContent.appendChild(interfaceButtons);
nickjillings@1341 88 testContent.appendChild(feedbackHolder);
nickjillings@1341 89 testContent.appendChild(submit);
nickjillings@1341 90 interfaceContext.insertPoint.appendChild(testContent);
nickjillings@1341 91
nickjillings@1341 92 // Load the full interface
nickjillings@1341 93 testState.initialise();
nickjillings@1341 94 testState.advanceState();
nickjillings@1341 95 }
nickjillings@1341 96
nickjillings@1341 97 function loadTest(audioHolderObject)
nickjillings@1341 98 {
nickjillings@1341 99 var feedbackHolder = document.getElementById('feedbackHolder');
nickjillings@1341 100 var interfaceObj = audioHolderObject.interfaces;
nickjillings@1341 101 if (interfaceObj.length > 1)
nickjillings@1341 102 {
nickjillings@1341 103 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node");
nickjillings@1341 104 }
nickjillings@1341 105 interfaceObj = interfaceObj[0];
nickjillings@1341 106
nickjillings@1341 107 if(interfaceObj.title != null)
nickjillings@1341 108 {
nickjillings@1341 109 document.getElementById("pageTitle").textContent = interfaceObj.title;
nickjillings@1341 110 }
nickjillings@1356 111
nickjillings@1356 112 var interfaceOptions = specification.interfaces.options.concat(interfaceObj.options);
nickjillings@1356 113 for (var option of interfaceOptions)
nickjillings@1356 114 {
nickjillings@1356 115 if (option.type == "show")
nickjillings@1356 116 {
nickjillings@1356 117 switch(option.name) {
nickjillings@1356 118 case "playhead":
nickjillings@1356 119 var playbackHolder = document.getElementById('playback-holder');
nickjillings@1356 120 if (playbackHolder == null)
nickjillings@1356 121 {
nickjillings@1356 122 playbackHolder = document.createElement('div');
nickjillings@1356 123 playbackHolder.style.width = "100%";
nickjillings@1356 124 playbackHolder.style.float = "left";
nickjillings@1356 125 playbackHolder.align = 'center';
nickjillings@1356 126 playbackHolder.appendChild(interfaceContext.playhead.object);
nickjillings@1356 127 feedbackHolder.appendChild(playbackHolder);
nickjillings@1356 128 }
nickjillings@1356 129 break;
nickjillings@1356 130 case "page-count":
nickjillings@1356 131 var pagecountHolder = document.getElementById('page-count');
nickjillings@1356 132 if (pagecountHolder == null)
nickjillings@1356 133 {
nickjillings@1356 134 pagecountHolder = document.createElement('div');
nickjillings@1356 135 pagecountHolder.id = 'page-count';
nickjillings@1356 136 }
nickjillings@2125 137 pagecountHolder.innerHTML = '<span>Page '+(testState.stateIndex+1)+' of '+testState.stateMap.length+'</span>';
nickjillings@1356 138 var inject = document.getElementById('interface-buttons');
nickjillings@1356 139 inject.appendChild(pagecountHolder);
nickjillings@1356 140 break;
nickjillings@1356 141 case "volume":
nickjillings@1356 142 if (document.getElementById('master-volume-holder') == null)
nickjillings@1356 143 {
nickjillings@1356 144 feedbackHolder.appendChild(interfaceContext.volume.object);
nickjillings@1356 145 }
nickjillings@1356 146 break;
nickjillings@1356 147 }
nickjillings@1356 148 }
nickjillings@1356 149 }
nickjillings@1341 150
nickjillings@2110 151 // Populate the comparator object
nickjillings@2110 152 interfaceContext.comparator = new comparator(audioHolderObject);
nickjillings@1316 153 if (audioHolderObject.showElementComments)
nickjillings@1316 154 {
nickjillings@1316 155 var commentHolder = document.createElement('div');
nickjillings@1316 156 commentHolder.id = 'commentHolder';
nickjillings@1316 157 document.getElementById('testContent').appendChild(commentHolder);
nickjillings@1316 158 // Generate one comment box per presented page
nickjillings@1316 159 for (var element of audioEngineContext.audioObjects)
nickjillings@1316 160 {
nickjillings@2117 161 interfaceContext.commentBoxes.createCommentBox(element);
nickjillings@1316 162 }
nickjillings@2117 163 interfaceContext.commentBoxes.showCommentBoxes(commentHolder,true);
nickjillings@1316 164 }
nickjillings@1341 165 resizeWindow(null);
nickjillings@1341 166 }
nickjillings@1341 167
nickjillings@2110 168 function comparator(audioHolderObject)
nickjillings@1341 169 {
nickjillings@2110 170 this.comparatorBox = function(audioElement,id,text)
nickjillings@1341 171 {
nickjillings@1341 172 this.parent = audioElement;
nickjillings@1341 173 this.id = id;
nickjillings@1341 174 this.value = 0;
nickjillings@1341 175 this.disabled = true;
nickjillings@1341 176 this.box = document.createElement('div');
nickjillings@2110 177 this.box.className = 'comparator-holder';
nickjillings@1341 178 this.box.setAttribute('track-id',audioElement.id);
nickjillings@2110 179 this.box.id = 'comparator-'+text;
nickjillings@1341 180 this.selector = document.createElement('div');
nickjillings@2110 181 this.selector.className = 'comparator-selector disabled';
nickjillings@1341 182 var selectorText = document.createElement('span');
nickjillings@1341 183 selectorText.textContent = text;
nickjillings@1341 184 this.selector.appendChild(selectorText);
nickjillings@1341 185 this.playback = document.createElement('button');
nickjillings@2110 186 this.playback.className = 'comparator-button';
nickjillings@1341 187 this.playback.disabled = true;
nickjillings@1341 188 this.playback.textContent = "Listen";
nickjillings@1341 189 this.box.appendChild(this.selector);
nickjillings@1341 190 this.box.appendChild(this.playback);
nickjillings@1349 191 this.selector.onclick = function(event)
nickjillings@1341 192 {
nickjillings@1341 193 var time = audioEngineContext.timer.getTestTime();
nickjillings@1341 194 if ($(event.currentTarget).hasClass('disabled'))
nickjillings@1341 195 {
nickjillings@1341 196 console.log("Please wait until sample has loaded");
nickjillings@1341 197 return;
nickjillings@1341 198 }
nickjillings@1341 199 if (audioEngineContext.status == 0)
nickjillings@1341 200 {
nickjillings@1341 201 alert("Please listen to the samples before making a selection");
nickjillings@1341 202 console.log("Please listen to the samples before making a selection");
nickjillings@1341 203 return;
nickjillings@2112 204 }
nickjillings@1341 205 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@2110 206 interfaceContext.comparator.selected = id;
nickjillings@2112 207 if ($(event.currentTarget).hasClass("selected")) {
nickjillings@2112 208 $(".comparator-selector").removeClass('selected');
nickjillings@2112 209 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
nickjillings@2112 210 {
nickjillings@2112 211 var obj = interfaceContext.comparator.comparators[i];
nickjillings@2112 212 obj.parent.metric.moved(time,0);
nickjillings@2112 213 }
nickjillings@2112 214 } else {
nickjillings@2112 215 $(".comparator-selector").removeClass('selected');
nickjillings@2112 216 $(event.currentTarget).addClass('selected');
nickjillings@2112 217 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
nickjillings@2112 218 {
nickjillings@2112 219 var obj = interfaceContext.comparator.comparators[i];
nickjillings@2112 220 if (i == id) {
nickjillings@2112 221 obj.value = 1;
nickjillings@2112 222 } else {
nickjillings@2112 223 obj.value = 0;
nickjillings@2112 224 }
nickjillings@2112 225 obj.parent.metric.moved(time,obj.value);
nickjillings@2112 226 }
nickjillings@2112 227 console.log("Selected "+id+' ('+time+')');
nickjillings@2112 228 }
nickjillings@1341 229 };
nickjillings@1376 230 this.playback.setAttribute("playstate","ready");
nickjillings@1349 231 this.playback.onclick = function(event)
nickjillings@1341 232 {
nickjillings@1341 233 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1376 234 if (event.currentTarget.getAttribute("playstate") == "ready")
nickjillings@1376 235 {
nickjillings@1376 236 audioEngineContext.play(id);
nickjillings@1376 237 } else if (event.currentTarget.getAttribute("playstate") == "playing") {
nickjillings@1376 238 audioEngineContext.stop();
nickjillings@1376 239 }
nickjillings@1376 240
nickjillings@1341 241 };
nickjillings@1341 242
nickjillings@1341 243 this.enable = function()
nickjillings@1341 244 {
nickjillings@1341 245 if (this.parent.state == 1)
nickjillings@1341 246 {
nickjillings@1341 247 $(this.selector).removeClass('disabled');
nickjillings@1341 248 this.playback.disabled = false;
nickjillings@1341 249 }
nickjillings@1341 250 };
nickjillings@1341 251 this.updateLoading = function(progress)
nickjillings@1341 252 {
nickjillings@1341 253 if (progress != 100)
nickjillings@1341 254 {
nickjillings@1341 255 progress = String(progress);
nickjillings@1341 256 progress = progress.split('.')[0];
nickjillings@1341 257 this.playback.textContent = progress+'%';
nickjillings@1341 258 } else {
nickjillings@1376 259 this.playback.textContent = "Play";
nickjillings@1341 260 }
nickjillings@1341 261 };
nickjillings@2113 262 this.error = function() {
nickjillings@2113 263 // audioObject has an error!!
nickjillings@2113 264 this.playback.textContent = "Error";
nickjillings@2113 265 $(this.playback).addClass("error-colour");
nickjillings@2113 266 }
nickjillings@1360 267 this.startPlayback = function()
nickjillings@1360 268 {
nickjillings@2110 269 $('.comparator-button').text('Listen');
nickjillings@1376 270 $(this.playback).text('Stop');
nickjillings@1376 271 this.playback.setAttribute("playstate","playing");
nickjillings@1360 272 };
nickjillings@1360 273 this.stopPlayback = function()
nickjillings@1360 274 {
nickjillings@1360 275 $(this.playback).text('Listen');
nickjillings@1376 276 this.playback.setAttribute("playstate","ready");
nickjillings@1360 277 };
nickjillings@1341 278 this.exportXMLDOM = function(audioObject)
nickjillings@1341 279 {
nickjillings@1341 280 var node = storage.document.createElement('value');
nickjillings@1341 281 node.textContent = this.value;
nickjillings@1341 282 return node;
nickjillings@1341 283 };
nickjillings@1341 284 this.getValue = function() {
nickjillings@1341 285 return this.value;
nickjillings@1341 286 };
nickjillings@1341 287 this.getPresentedId = function()
nickjillings@1341 288 {
nickjillings@1341 289 return this.selector.children[0].textContent;
nickjillings@1341 290 };
nickjillings@1341 291 this.canMove = function()
nickjillings@1341 292 {
nickjillings@1341 293 return false;
nickjillings@1341 294 };
nickjillings@1341 295 };
nickjillings@1341 296
nickjillings@1341 297 this.boxHolders = document.getElementById('box-holders');
nickjillings@1341 298 this.boxHolders.innerHTML = null;
nickjillings@2110 299 this.comparators = [];
nickjillings@1341 300 this.selected = null;
nickjillings@1341 301
nickjillings@1341 302 // First generate the Audio Objects for the Audio Engine
nickjillings@1341 303 for (var index=0; index<audioHolderObject.audioElements.length; index++)
nickjillings@1341 304 {
nickjillings@1341 305 var element = audioHolderObject.audioElements[index];
nickjillings@2177 306 var audioObject = audioEngineContext.newTrack(element);
nickjillings@1341 307 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
nickjillings@1341 308 {
nickjillings@2177 309 var orNode = new interfaceContext.outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons'));
nickjillings@2177 310 audioObject.bindInterface(orNode);
nickjillings@2177 311 } else {
nickjillings@2177 312 var label;
nickjillings@2177 313 switch(audioObject.specification.parent.label) {
nickjillings@2177 314 case "none":
nickjillings@2177 315 label = "";
nickjillings@2177 316 break;
nickjillings@2177 317 case "number":
nickjillings@2177 318 label = ""+index;
nickjillings@2177 319 break;
nickjillings@2177 320 case "letter":
nickjillings@2177 321 label = String.fromCharCode(97 + index);
nickjillings@2177 322 break;
nickjillings@2177 323 default:
nickjillings@2177 324 label = String.fromCharCode(65 + index);
nickjillings@2177 325 break;
nickjillings@2177 326 }
nickjillings@2177 327 var node = new this.comparatorBox(audioObject,index,label);
nickjillings@2177 328 audioObject.bindInterface(node);
nickjillings@2177 329 this.comparators.push(node);
nickjillings@2177 330 this.boxHolders.appendChild(node.box);
nickjillings@1295 331 }
nickjillings@1341 332 }
nickjillings@1341 333 return this;
nickjillings@1341 334 }
nickjillings@1341 335
nickjillings@1341 336 function resizeWindow(event)
nickjillings@1341 337 {
nickjillings@1341 338 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@2110 339 var numObj = interfaceContext.comparator.comparators.length;
nickjillings@1341 340 var boxW = numObj*312;
nickjillings@1341 341 var diff = window.innerWidth - boxW;
nickjillings@1341 342 while (diff < 0)
nickjillings@1341 343 {
nickjillings@1341 344 numObj = Math.ceil(numObj/2);
nickjillings@1341 345 boxW = numObj*312;
nickjillings@1341 346 diff = window.innerWidth - boxW;
nickjillings@1341 347 }
nickjillings@1341 348 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
nickjillings@1341 349 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
nickjillings@1341 350 document.getElementById('box-holders').style.width = boxW + 'px';
nickjillings@2177 351
nickjillings@2177 352 var outsideRef = document.getElementById('outside-reference');
nickjillings@2177 353 if(outsideRef != null)
nickjillings@2177 354 {
nickjillings@2177 355 outsideRef.style.left = (window.innerWidth-120)/2 + 'px';
nickjillings@2177 356 }
nickjillings@1341 357 }
nickjillings@1341 358
nickjillings@1341 359 function buttonSubmitClick()
nickjillings@1341 360 {
nickjillings@1341 361 var checks = [];
nickjillings@1341 362 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
nickjillings@1341 363 checks = checks.concat(specification.interfaces.options);
nickjillings@1341 364 var canContinue = true;
nickjillings@1341 365
nickjillings@1341 366 for (var i=0; i<checks.length; i++) {
nickjillings@1341 367 if (checks[i].type == 'check')
nickjillings@1341 368 {
nickjillings@1341 369 switch(checks[i].name) {
nickjillings@1341 370 case 'fragmentPlayed':
nickjillings@1341 371 // Check if all fragments have been played
nickjillings@1341 372 var checkState = interfaceContext.checkAllPlayed();
nickjillings@1341 373 if (checkState == false) {canContinue = false;}
nickjillings@1341 374 break;
nickjillings@1341 375 case 'fragmentFullPlayback':
nickjillings@1341 376 // Check all fragments have been played to their full length
nickjillings@1341 377 var checkState = interfaceContext.checkFragmentsFullyPlayed();
nickjillings@1341 378 if (checkState == false) {canContinue = false;}
nickjillings@1341 379 break;
nickjillings@1341 380 case 'fragmentMoved':
nickjillings@1341 381 // Check all fragment sliders have been moved.
nickjillings@1341 382 var checkState = interfaceContext.checkAllMoved();
nickjillings@1341 383 if (checkState == false) {canContinue = false;}
nickjillings@1341 384 break;
nickjillings@1341 385 case 'fragmentComments':
nickjillings@1341 386 // Check all fragment sliders have been moved.
nickjillings@1341 387 var checkState = interfaceContext.checkAllCommented();
nickjillings@1341 388 if (checkState == false) {canContinue = false;}
nickjillings@1341 389 break;
nickjillings@1341 390 default:
nickjillings@1341 391 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
nickjillings@1341 392 break;
nickjillings@1341 393 }
nickjillings@1341 394
nickjillings@1341 395 }
nickjillings@1341 396 if (!canContinue) {break;}
nickjillings@1341 397 }
nickjillings@1341 398 if (canContinue)
nickjillings@1341 399 {
nickjillings@1341 400 if (audioEngineContext.status == 1) {
nickjillings@1341 401 var playback = document.getElementById('playback-button');
nickjillings@1341 402 playback.click();
nickjillings@1341 403 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nickjillings@1341 404 } else
nickjillings@1341 405 {
nickjillings@1341 406 if (audioEngineContext.timer.testStarted == false)
nickjillings@1341 407 {
nickjillings@1341 408 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1341 409 return;
nickjillings@1341 410 }
nickjillings@1341 411 }
nickjillings@1341 412 testState.advanceState();
nickjillings@1341 413 }
nickjillings@1341 414 }
nickjillings@1341 415
nickjillings@1341 416 function pageXMLSave(store, pageSpecification)
nickjillings@1341 417 {
nickjillings@1341 418 // MANDATORY
nickjillings@1341 419 // Saves a specific test page
nickjillings@1341 420 // You can use this space to add any extra nodes to your XML <audioHolder> saves
nickjillings@1341 421 // Get the current <page> information in store (remember to appendChild your data to it)
nickjillings@1341 422 // pageSpecification is the current page node configuration
nickjillings@1341 423 // To create new XML nodes, use storage.document.createElement();
nickjillings@1341 424 }