annotate interfaces/AB.js @ 1376:7af61d6fd41b

Bug #1567: AB.js interfaces, the playback toggle now controls both playback start and playback stop commands. Button text updated to reflect.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Thu, 11 Feb 2016 14:16:09 +0000
parents c277da1e89ca
children a4ad9e55b5b8
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@1341 10 // Custom Comparitor Object
nickjillings@1341 11 Interface.prototype.comparitor = 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@1356 137 pagecountHolder.innerHTML = '<span>Page '+(audioHolderObject.presentedId+1)+' of '+specification.pages.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@1341 151 // Populate the comparitor object
nickjillings@1341 152 interfaceContext.comparitor = new Comparitor(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@1316 161 interfaceContext.createCommentBox(element);
nickjillings@1316 162 }
nickjillings@1316 163 interfaceContext.showCommentBoxes(commentHolder,true);
nickjillings@1316 164 }
nickjillings@1341 165 resizeWindow(null);
nickjillings@1341 166 }
nickjillings@1341 167
nickjillings@1341 168 function Comparitor(audioHolderObject)
nickjillings@1341 169 {
nickjillings@1341 170 this.comparitorBox = 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@1341 177 this.box.className = 'comparitor-holder';
nickjillings@1341 178 this.box.setAttribute('track-id',audioElement.id);
nickjillings@1341 179 this.box.id = 'comparitor-'+text;
nickjillings@1341 180 this.selector = document.createElement('div');
nickjillings@1341 181 this.selector.className = 'comparitor-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@1341 186 this.playback.className = 'comparitor-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@1341 204 }
nickjillings@1341 205 $(".comparitor-selector").removeClass('selected');
nickjillings@1341 206 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1341 207 interfaceContext.comparitor.selected = id;
nickjillings@1341 208 $(event.currentTarget).addClass('selected');
nickjillings@1341 209 for (var i=0; i<interfaceContext.comparitor.comparitors.length; i++)
nickjillings@1341 210 {
nickjillings@1341 211 var obj = interfaceContext.comparitor.comparitors[i];
nickjillings@1341 212 if (i == id) {
nickjillings@1341 213 obj.value = 1;
nickjillings@1341 214 } else {
nickjillings@1341 215 obj.value = 0;
nickjillings@1341 216 }
nickjillings@1341 217 obj.parent.metric.moved(time,obj.value);
nickjillings@1341 218 }
nickjillings@1341 219 console.log("Selected "+id+' ('+time+')');
nickjillings@1341 220 };
nickjillings@1376 221 this.playback.setAttribute("playstate","ready");
nickjillings@1349 222 this.playback.onclick = function(event)
nickjillings@1341 223 {
nickjillings@1341 224 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1376 225 if (event.currentTarget.getAttribute("playstate") == "ready")
nickjillings@1376 226 {
nickjillings@1376 227 audioEngineContext.play(id);
nickjillings@1376 228 } else if (event.currentTarget.getAttribute("playstate") == "playing") {
nickjillings@1376 229 audioEngineContext.stop();
nickjillings@1376 230 }
nickjillings@1376 231
nickjillings@1341 232 };
nickjillings@1341 233
nickjillings@1341 234 this.enable = function()
nickjillings@1341 235 {
nickjillings@1341 236 if (this.parent.state == 1)
nickjillings@1341 237 {
nickjillings@1341 238 $(this.selector).removeClass('disabled');
nickjillings@1341 239 this.playback.disabled = false;
nickjillings@1341 240 }
nickjillings@1341 241 };
nickjillings@1341 242 this.updateLoading = function(progress)
nickjillings@1341 243 {
nickjillings@1341 244 if (progress != 100)
nickjillings@1341 245 {
nickjillings@1341 246 progress = String(progress);
nickjillings@1341 247 progress = progress.split('.')[0];
nickjillings@1341 248 this.playback.textContent = progress+'%';
nickjillings@1341 249 } else {
nickjillings@1376 250 this.playback.textContent = "Play";
nickjillings@1341 251 }
nickjillings@1341 252 };
nickjillings@1360 253 this.startPlayback = function()
nickjillings@1360 254 {
nickjillings@1360 255 $('.comparitor-button').text('Listen');
nickjillings@1376 256 $(this.playback).text('Stop');
nickjillings@1376 257 this.playback.setAttribute("playstate","playing");
nickjillings@1360 258 };
nickjillings@1360 259 this.stopPlayback = function()
nickjillings@1360 260 {
nickjillings@1360 261 $(this.playback).text('Listen');
nickjillings@1376 262 this.playback.setAttribute("playstate","ready");
nickjillings@1360 263 };
nickjillings@1341 264 this.exportXMLDOM = function(audioObject)
nickjillings@1341 265 {
nickjillings@1341 266 var node = storage.document.createElement('value');
nickjillings@1341 267 node.textContent = this.value;
nickjillings@1341 268 return node;
nickjillings@1341 269 };
nickjillings@1341 270 this.getValue = function() {
nickjillings@1341 271 return this.value;
nickjillings@1341 272 };
nickjillings@1341 273 this.getPresentedId = function()
nickjillings@1341 274 {
nickjillings@1341 275 return this.selector.children[0].textContent;
nickjillings@1341 276 };
nickjillings@1341 277 this.canMove = function()
nickjillings@1341 278 {
nickjillings@1341 279 return false;
nickjillings@1341 280 };
nickjillings@1341 281 };
nickjillings@1341 282
nickjillings@1341 283 this.boxHolders = document.getElementById('box-holders');
nickjillings@1341 284 this.boxHolders.innerHTML = null;
nickjillings@1341 285 this.comparitors = [];
nickjillings@1341 286 this.selected = null;
nickjillings@1341 287
nickjillings@1341 288 // First generate the Audio Objects for the Audio Engine
nickjillings@1341 289 for (var index=0; index<audioHolderObject.audioElements.length; index++)
nickjillings@1341 290 {
nickjillings@1341 291 var element = audioHolderObject.audioElements[index];
nickjillings@1341 292 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
nickjillings@1341 293 {
nickjillings@1341 294 console.log("WARNING - AB cannot have fixed reference");
nickjillings@1341 295 }
nickjillings@1341 296 var audioObject = audioEngineContext.newTrack(element);
nickjillings@1341 297 var node = new this.comparitorBox(audioObject,index,String.fromCharCode(65 + index));
nickjillings@1341 298 audioObject.bindInterface(node);
nickjillings@1341 299 this.comparitors.push(node);
nickjillings@1341 300 this.boxHolders.appendChild(node.box);
nickjillings@1341 301 }
nickjillings@1341 302 return this;
nickjillings@1341 303 }
nickjillings@1341 304
nickjillings@1341 305 function resizeWindow(event)
nickjillings@1341 306 {
nickjillings@1341 307 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@1341 308 var numObj = interfaceContext.comparitor.comparitors.length;
nickjillings@1341 309 var boxW = numObj*312;
nickjillings@1341 310 var diff = window.innerWidth - boxW;
nickjillings@1341 311 while (diff < 0)
nickjillings@1341 312 {
nickjillings@1341 313 numObj = Math.ceil(numObj/2);
nickjillings@1341 314 boxW = numObj*312;
nickjillings@1341 315 diff = window.innerWidth - boxW;
nickjillings@1341 316 }
nickjillings@1341 317 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
nickjillings@1341 318 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
nickjillings@1341 319 document.getElementById('box-holders').style.width = boxW + 'px';
nickjillings@1341 320 }
nickjillings@1341 321
nickjillings@1341 322 function buttonSubmitClick()
nickjillings@1341 323 {
nickjillings@1341 324 var checks = [];
nickjillings@1341 325 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
nickjillings@1341 326 checks = checks.concat(specification.interfaces.options);
nickjillings@1341 327 var canContinue = true;
nickjillings@1341 328
nickjillings@1341 329 for (var i=0; i<checks.length; i++) {
nickjillings@1341 330 if (checks[i].type == 'check')
nickjillings@1341 331 {
nickjillings@1341 332 switch(checks[i].name) {
nickjillings@1341 333 case 'fragmentPlayed':
nickjillings@1341 334 // Check if all fragments have been played
nickjillings@1341 335 var checkState = interfaceContext.checkAllPlayed();
nickjillings@1341 336 if (checkState == false) {canContinue = false;}
nickjillings@1341 337 break;
nickjillings@1341 338 case 'fragmentFullPlayback':
nickjillings@1341 339 // Check all fragments have been played to their full length
nickjillings@1341 340 var checkState = interfaceContext.checkFragmentsFullyPlayed();
nickjillings@1341 341 if (checkState == false) {canContinue = false;}
nickjillings@1341 342 break;
nickjillings@1341 343 case 'fragmentMoved':
nickjillings@1341 344 // Check all fragment sliders have been moved.
nickjillings@1341 345 var checkState = interfaceContext.checkAllMoved();
nickjillings@1341 346 if (checkState == false) {canContinue = false;}
nickjillings@1341 347 break;
nickjillings@1341 348 case 'fragmentComments':
nickjillings@1341 349 // Check all fragment sliders have been moved.
nickjillings@1341 350 var checkState = interfaceContext.checkAllCommented();
nickjillings@1341 351 if (checkState == false) {canContinue = false;}
nickjillings@1341 352 break;
nickjillings@1341 353 default:
nickjillings@1341 354 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
nickjillings@1341 355 break;
nickjillings@1341 356 }
nickjillings@1341 357
nickjillings@1341 358 }
nickjillings@1341 359 if (!canContinue) {break;}
nickjillings@1341 360 }
nickjillings@1341 361 if (canContinue)
nickjillings@1341 362 {
nickjillings@1341 363 if (audioEngineContext.status == 1) {
nickjillings@1341 364 var playback = document.getElementById('playback-button');
nickjillings@1341 365 playback.click();
nickjillings@1341 366 // 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 367 } else
nickjillings@1341 368 {
nickjillings@1341 369 if (audioEngineContext.timer.testStarted == false)
nickjillings@1341 370 {
nickjillings@1341 371 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1341 372 return;
nickjillings@1341 373 }
nickjillings@1341 374 }
nickjillings@1341 375 testState.advanceState();
nickjillings@1341 376 }
nickjillings@1341 377 }
nickjillings@1341 378
nickjillings@1341 379 function pageXMLSave(store, pageSpecification)
nickjillings@1341 380 {
nickjillings@1341 381 // MANDATORY
nickjillings@1341 382 // Saves a specific test page
nickjillings@1341 383 // You can use this space to add any extra nodes to your XML <audioHolder> saves
nickjillings@1341 384 // Get the current <page> information in store (remember to appendChild your data to it)
nickjillings@1341 385 // pageSpecification is the current page node configuration
nickjillings@1341 386 // To create new XML nodes, use storage.document.createElement();
nickjillings@1341 387 }