annotate interfaces/AB.js @ 2314:cbc26d0f104a

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Fri, 29 Apr 2016 15:21:00 +0100
parents 426995e02e79
children dee115f3efee
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 {
nicholas@2307 211 var obj = interfaceContext.comparator.comparators[i];
nickjillings@2112 212 obj.parent.metric.moved(time,0);
nicholas@2307 213 obj.value = 0;
nickjillings@2112 214 }
nickjillings@2112 215 } else {
nickjillings@2112 216 $(".comparator-selector").removeClass('selected');
nickjillings@2112 217 $(event.currentTarget).addClass('selected');
nickjillings@2112 218 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
nickjillings@2112 219 {
nickjillings@2112 220 var obj = interfaceContext.comparator.comparators[i];
nickjillings@2112 221 if (i == id) {
nickjillings@2112 222 obj.value = 1;
nickjillings@2112 223 } else {
nickjillings@2112 224 obj.value = 0;
nickjillings@2112 225 }
nickjillings@2112 226 obj.parent.metric.moved(time,obj.value);
nickjillings@2112 227 }
nickjillings@2112 228 console.log("Selected "+id+' ('+time+')');
nickjillings@2112 229 }
nickjillings@1341 230 };
nickjillings@1376 231 this.playback.setAttribute("playstate","ready");
nickjillings@1349 232 this.playback.onclick = function(event)
nickjillings@1341 233 {
nickjillings@1341 234 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1376 235 if (event.currentTarget.getAttribute("playstate") == "ready")
nickjillings@1376 236 {
nickjillings@1376 237 audioEngineContext.play(id);
nickjillings@1376 238 } else if (event.currentTarget.getAttribute("playstate") == "playing") {
nickjillings@1376 239 audioEngineContext.stop();
nickjillings@1376 240 }
nickjillings@1376 241
nickjillings@1341 242 };
nickjillings@1341 243
nickjillings@1341 244 this.enable = function()
nickjillings@1341 245 {
nickjillings@1341 246 if (this.parent.state == 1)
nickjillings@1341 247 {
nickjillings@1341 248 $(this.selector).removeClass('disabled');
nickjillings@1341 249 this.playback.disabled = false;
nickjillings@1341 250 }
nickjillings@1341 251 };
nickjillings@1341 252 this.updateLoading = function(progress)
nickjillings@1341 253 {
nickjillings@1341 254 if (progress != 100)
nickjillings@1341 255 {
nickjillings@1341 256 progress = String(progress);
nickjillings@1341 257 progress = progress.split('.')[0];
nickjillings@1341 258 this.playback.textContent = progress+'%';
nickjillings@1341 259 } else {
nickjillings@1376 260 this.playback.textContent = "Play";
nickjillings@1341 261 }
nickjillings@1341 262 };
nickjillings@2113 263 this.error = function() {
nickjillings@2113 264 // audioObject has an error!!
nickjillings@2113 265 this.playback.textContent = "Error";
nickjillings@2113 266 $(this.playback).addClass("error-colour");
nickjillings@2113 267 }
nickjillings@1360 268 this.startPlayback = function()
nickjillings@1360 269 {
nickjillings@2110 270 $('.comparator-button').text('Listen');
nickjillings@1376 271 $(this.playback).text('Stop');
nickjillings@1376 272 this.playback.setAttribute("playstate","playing");
nickjillings@1360 273 };
nickjillings@1360 274 this.stopPlayback = function()
nickjillings@1360 275 {
nickjillings@1360 276 $(this.playback).text('Listen');
nickjillings@1376 277 this.playback.setAttribute("playstate","ready");
nickjillings@1360 278 };
nickjillings@1341 279 this.exportXMLDOM = function(audioObject)
nickjillings@1341 280 {
nickjillings@1341 281 var node = storage.document.createElement('value');
nickjillings@1341 282 node.textContent = this.value;
nickjillings@1341 283 return node;
nickjillings@1341 284 };
nickjillings@1341 285 this.getValue = function() {
nickjillings@1341 286 return this.value;
nickjillings@1341 287 };
nickjillings@1341 288 this.getPresentedId = function()
nickjillings@1341 289 {
nickjillings@1341 290 return this.selector.children[0].textContent;
nickjillings@1341 291 };
nickjillings@1341 292 this.canMove = function()
nickjillings@1341 293 {
nickjillings@1341 294 return false;
nickjillings@1341 295 };
nickjillings@1341 296 };
nickjillings@1341 297
nickjillings@1341 298 this.boxHolders = document.getElementById('box-holders');
nickjillings@1341 299 this.boxHolders.innerHTML = null;
nickjillings@2110 300 this.comparators = [];
nickjillings@1341 301 this.selected = null;
nickjillings@1341 302
nickjillings@1341 303 // First generate the Audio Objects for the Audio Engine
nickjillings@1341 304 for (var index=0; index<audioHolderObject.audioElements.length; index++)
nickjillings@1341 305 {
nickjillings@1341 306 var element = audioHolderObject.audioElements[index];
nickjillings@2177 307 var audioObject = audioEngineContext.newTrack(element);
nickjillings@1341 308 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
nickjillings@1341 309 {
nickjillings@2177 310 var orNode = new interfaceContext.outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons'));
nickjillings@2177 311 audioObject.bindInterface(orNode);
nickjillings@2177 312 } else {
nickjillings@2177 313 var label;
nickjillings@2177 314 switch(audioObject.specification.parent.label) {
nickjillings@2177 315 case "none":
nickjillings@2177 316 label = "";
nickjillings@2177 317 break;
nickjillings@2177 318 case "number":
nickjillings@2177 319 label = ""+index;
nickjillings@2177 320 break;
nickjillings@2177 321 case "letter":
nickjillings@2177 322 label = String.fromCharCode(97 + index);
nickjillings@2177 323 break;
nickjillings@2177 324 default:
nickjillings@2177 325 label = String.fromCharCode(65 + index);
nickjillings@2177 326 break;
nickjillings@2177 327 }
nickjillings@2177 328 var node = new this.comparatorBox(audioObject,index,label);
nickjillings@2177 329 audioObject.bindInterface(node);
nickjillings@2177 330 this.comparators.push(node);
nickjillings@2177 331 this.boxHolders.appendChild(node.box);
nickjillings@1295 332 }
nickjillings@1341 333 }
nickjillings@1341 334 return this;
nickjillings@1341 335 }
nickjillings@1341 336
nickjillings@1341 337 function resizeWindow(event)
nickjillings@1341 338 {
nickjillings@1341 339 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@2110 340 var numObj = interfaceContext.comparator.comparators.length;
nickjillings@1341 341 var boxW = numObj*312;
nickjillings@1341 342 var diff = window.innerWidth - boxW;
nickjillings@1341 343 while (diff < 0)
nickjillings@1341 344 {
nickjillings@1341 345 numObj = Math.ceil(numObj/2);
nickjillings@1341 346 boxW = numObj*312;
nickjillings@1341 347 diff = window.innerWidth - boxW;
nickjillings@1341 348 }
nickjillings@1341 349 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
nickjillings@1341 350 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
nickjillings@1341 351 document.getElementById('box-holders').style.width = boxW + 'px';
nickjillings@2177 352
nickjillings@2177 353 var outsideRef = document.getElementById('outside-reference');
nickjillings@2177 354 if(outsideRef != null)
nickjillings@2177 355 {
nickjillings@2177 356 outsideRef.style.left = (window.innerWidth-120)/2 + 'px';
nickjillings@2177 357 }
nickjillings@1341 358 }
nickjillings@1341 359
nickjillings@1341 360 function buttonSubmitClick()
nickjillings@1341 361 {
nickjillings@1341 362 var checks = [];
nickjillings@1341 363 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
nickjillings@1341 364 checks = checks.concat(specification.interfaces.options);
nickjillings@1341 365 var canContinue = true;
nickjillings@1341 366
nickjillings@1341 367 for (var i=0; i<checks.length; i++) {
nickjillings@1341 368 if (checks[i].type == 'check')
nickjillings@1341 369 {
nickjillings@1341 370 switch(checks[i].name) {
nickjillings@1341 371 case 'fragmentPlayed':
nickjillings@1341 372 // Check if all fragments have been played
nickjillings@1341 373 var checkState = interfaceContext.checkAllPlayed();
nickjillings@1341 374 if (checkState == false) {canContinue = false;}
nickjillings@1341 375 break;
nickjillings@1341 376 case 'fragmentFullPlayback':
nickjillings@1341 377 // Check all fragments have been played to their full length
nickjillings@1341 378 var checkState = interfaceContext.checkFragmentsFullyPlayed();
nickjillings@1341 379 if (checkState == false) {canContinue = false;}
nickjillings@1341 380 break;
nickjillings@1341 381 case 'fragmentMoved':
nickjillings@1341 382 // Check all fragment sliders have been moved.
nickjillings@1341 383 var checkState = interfaceContext.checkAllMoved();
nickjillings@1341 384 if (checkState == false) {canContinue = false;}
nickjillings@1341 385 break;
nickjillings@1341 386 case 'fragmentComments':
nickjillings@1341 387 // Check all fragment sliders have been moved.
nickjillings@1341 388 var checkState = interfaceContext.checkAllCommented();
nickjillings@1341 389 if (checkState == false) {canContinue = false;}
nickjillings@1341 390 break;
nicholas@2310 391 case 'scalerange':
nicholas@2310 392 // Check the scale has been used effectively
nicholas@2310 393 var checkState = interfaceContext.checkScaleRange(checks[i].min,checks[i].max);
nicholas@2310 394 if (checkState == false) {canContinue = false;}
nicholas@2310 395 break;
nickjillings@1341 396 default:
nickjillings@1341 397 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
nickjillings@1341 398 break;
nickjillings@1341 399 }
nickjillings@1341 400
nickjillings@1341 401 }
nickjillings@1341 402 if (!canContinue) {break;}
nickjillings@1341 403 }
nickjillings@1341 404 if (canContinue)
nickjillings@1341 405 {
nickjillings@1341 406 if (audioEngineContext.status == 1) {
nickjillings@1341 407 var playback = document.getElementById('playback-button');
nickjillings@1341 408 playback.click();
nickjillings@1341 409 // 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 410 } else
nickjillings@1341 411 {
nickjillings@1341 412 if (audioEngineContext.timer.testStarted == false)
nickjillings@1341 413 {
nickjillings@1341 414 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1341 415 return;
nickjillings@1341 416 }
nickjillings@1341 417 }
nickjillings@1341 418 testState.advanceState();
nickjillings@1341 419 }
nickjillings@1341 420 }
nickjillings@1341 421
nickjillings@1341 422 function pageXMLSave(store, pageSpecification)
nickjillings@1341 423 {
nickjillings@1341 424 // MANDATORY
nickjillings@1341 425 // Saves a specific test page
nickjillings@1341 426 // You can use this space to add any extra nodes to your XML <audioHolder> saves
nickjillings@1341 427 // Get the current <page> information in store (remember to appendChild your data to it)
nickjillings@1341 428 // pageSpecification is the current page node configuration
nickjillings@1341 429 // To create new XML nodes, use storage.document.createElement();
nickjillings@1341 430 }