annotate interfaces/AB.js @ 1176:12ef00ae9dd5

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