annotate interfaces/AB.js @ 536:efac13499354 Dev_main

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