annotate interfaces/AB.js @ 1096:9820063ea96a

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