annotate interfaces/AB.js @ 1156:79291bafbf5e

Added master volume. Ensured playhead, page-count and volume <interfaceoption> show options are enabled on all test pages.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 25 Jan 2016 17:29:34 +0000
parents 250b64aea676
children 159b87e5de67
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@1141 153 resizeWindow(null);
n@1141 154 }
n@1141 155
n@1141 156 function Comparitor(audioHolderObject)
n@1141 157 {
n@1141 158 this.comparitorBox = function(audioElement,id,text)
n@1141 159 {
n@1141 160 this.parent = audioElement;
n@1141 161 this.id = id;
n@1141 162 this.value = 0;
n@1141 163 this.disabled = true;
n@1141 164 this.box = document.createElement('div');
n@1141 165 this.box.className = 'comparitor-holder';
n@1141 166 this.box.setAttribute('track-id',audioElement.id);
n@1141 167 this.box.id = 'comparitor-'+text;
n@1141 168 this.selector = document.createElement('div');
n@1141 169 this.selector.className = 'comparitor-selector disabled';
n@1141 170 var selectorText = document.createElement('span');
n@1141 171 selectorText.textContent = text;
n@1141 172 this.selector.appendChild(selectorText);
n@1141 173 this.playback = document.createElement('button');
n@1141 174 this.playback.className = 'comparitor-button';
n@1141 175 this.playback.disabled = true;
n@1141 176 this.playback.textContent = "Listen";
n@1141 177 this.box.appendChild(this.selector);
n@1141 178 this.box.appendChild(this.playback);
n@1149 179 this.selector.onclick = function(event)
n@1141 180 {
n@1141 181 var time = audioEngineContext.timer.getTestTime();
n@1141 182 if ($(event.currentTarget).hasClass('disabled'))
n@1141 183 {
n@1141 184 console.log("Please wait until sample has loaded");
n@1141 185 return;
n@1141 186 }
n@1141 187 if (audioEngineContext.status == 0)
n@1141 188 {
n@1141 189 alert("Please listen to the samples before making a selection");
n@1141 190 console.log("Please listen to the samples before making a selection");
n@1141 191 return;
n@1141 192 }
n@1141 193 $(".comparitor-selector").removeClass('selected');
n@1141 194 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1141 195 interfaceContext.comparitor.selected = id;
n@1141 196 $(event.currentTarget).addClass('selected');
n@1141 197 for (var i=0; i<interfaceContext.comparitor.comparitors.length; i++)
n@1141 198 {
n@1141 199 var obj = interfaceContext.comparitor.comparitors[i];
n@1141 200 if (i == id) {
n@1141 201 obj.value = 1;
n@1141 202 } else {
n@1141 203 obj.value = 0;
n@1141 204 }
n@1141 205 obj.parent.metric.moved(time,obj.value);
n@1141 206 }
n@1141 207 console.log("Selected "+id+' ('+time+')');
n@1141 208 };
n@1149 209 this.playback.onclick = function(event)
n@1141 210 {
n@1141 211 $('.comparitor-button').text('Listen');
n@1141 212 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1141 213 audioEngineContext.play(id);
n@1141 214 $(event.currentTarget).text('Playing');
n@1141 215 };
n@1141 216
n@1141 217 this.enable = function()
n@1141 218 {
n@1141 219 if (this.parent.state == 1)
n@1141 220 {
n@1141 221 $(this.selector).removeClass('disabled');
n@1141 222 this.playback.disabled = false;
n@1141 223 }
n@1141 224 };
n@1141 225 this.updateLoading = function(progress)
n@1141 226 {
n@1141 227 if (progress != 100)
n@1141 228 {
n@1141 229 progress = String(progress);
n@1141 230 progress = progress.split('.')[0];
n@1141 231 this.playback.textContent = progress+'%';
n@1141 232 } else {
n@1141 233 this.playback.textContent = "Listen";
n@1141 234 }
n@1141 235 };
n@1141 236 this.exportXMLDOM = function(audioObject)
n@1141 237 {
n@1141 238 var node = storage.document.createElement('value');
n@1141 239 node.textContent = this.value;
n@1141 240 return node;
n@1141 241 };
n@1141 242 this.getValue = function() {
n@1141 243 return this.value;
n@1141 244 };
n@1141 245 this.getPresentedId = function()
n@1141 246 {
n@1141 247 return this.selector.children[0].textContent;
n@1141 248 };
n@1141 249 this.canMove = function()
n@1141 250 {
n@1141 251 return false;
n@1141 252 };
n@1141 253 };
n@1141 254
n@1141 255 this.boxHolders = document.getElementById('box-holders');
n@1141 256 this.boxHolders.innerHTML = null;
n@1141 257 this.comparitors = [];
n@1141 258 this.selected = null;
n@1141 259
n@1141 260 // First generate the Audio Objects for the Audio Engine
n@1141 261 for (var index=0; index<audioHolderObject.audioElements.length; index++)
n@1141 262 {
n@1141 263 var element = audioHolderObject.audioElements[index];
n@1141 264 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
n@1141 265 {
n@1141 266 console.log("WARNING - AB cannot have fixed reference");
n@1141 267 }
n@1141 268 var audioObject = audioEngineContext.newTrack(element);
n@1141 269 var node = new this.comparitorBox(audioObject,index,String.fromCharCode(65 + index));
n@1141 270 audioObject.bindInterface(node);
n@1141 271 this.comparitors.push(node);
n@1141 272 this.boxHolders.appendChild(node.box);
n@1141 273 }
n@1141 274 return this;
n@1141 275 }
n@1141 276
n@1141 277 function resizeWindow(event)
n@1141 278 {
n@1141 279 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
n@1141 280 var numObj = interfaceContext.comparitor.comparitors.length;
n@1141 281 var boxW = numObj*312;
n@1141 282 var diff = window.innerWidth - boxW;
n@1141 283 while (diff < 0)
n@1141 284 {
n@1141 285 numObj = Math.ceil(numObj/2);
n@1141 286 boxW = numObj*312;
n@1141 287 diff = window.innerWidth - boxW;
n@1141 288 }
n@1141 289 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
n@1141 290 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
n@1141 291 document.getElementById('box-holders').style.width = boxW + 'px';
n@1141 292 }
n@1141 293
n@1141 294 function buttonSubmitClick()
n@1141 295 {
n@1141 296 var checks = [];
n@1141 297 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
n@1141 298 checks = checks.concat(specification.interfaces.options);
n@1141 299 var canContinue = true;
n@1141 300
n@1141 301 for (var i=0; i<checks.length; i++) {
n@1141 302 if (checks[i].type == 'check')
n@1141 303 {
n@1141 304 switch(checks[i].name) {
n@1141 305 case 'fragmentPlayed':
n@1141 306 // Check if all fragments have been played
n@1141 307 var checkState = interfaceContext.checkAllPlayed();
n@1141 308 if (checkState == false) {canContinue = false;}
n@1141 309 break;
n@1141 310 case 'fragmentFullPlayback':
n@1141 311 // Check all fragments have been played to their full length
n@1141 312 var checkState = interfaceContext.checkFragmentsFullyPlayed();
n@1141 313 if (checkState == false) {canContinue = false;}
n@1141 314 break;
n@1141 315 case 'fragmentMoved':
n@1141 316 // Check all fragment sliders have been moved.
n@1141 317 var checkState = interfaceContext.checkAllMoved();
n@1141 318 if (checkState == false) {canContinue = false;}
n@1141 319 break;
n@1141 320 case 'fragmentComments':
n@1141 321 // Check all fragment sliders have been moved.
n@1141 322 var checkState = interfaceContext.checkAllCommented();
n@1141 323 if (checkState == false) {canContinue = false;}
n@1141 324 break;
n@1141 325 default:
n@1141 326 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@1141 327 break;
n@1141 328 }
n@1141 329
n@1141 330 }
n@1141 331 if (!canContinue) {break;}
n@1141 332 }
n@1141 333 if (canContinue)
n@1141 334 {
n@1141 335 if (audioEngineContext.status == 1) {
n@1141 336 var playback = document.getElementById('playback-button');
n@1141 337 playback.click();
n@1141 338 // 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 339 } else
n@1141 340 {
n@1141 341 if (audioEngineContext.timer.testStarted == false)
n@1141 342 {
n@1141 343 alert('You have not started the test! Please press start to begin the test!');
n@1141 344 return;
n@1141 345 }
n@1141 346 }
n@1141 347 testState.advanceState();
n@1141 348 }
n@1141 349 }
n@1141 350
n@1141 351 function pageXMLSave(store, pageSpecification)
n@1141 352 {
n@1141 353 // MANDATORY
n@1141 354 // Saves a specific test page
n@1141 355 // You can use this space to add any extra nodes to your XML <audioHolder> saves
n@1141 356 // Get the current <page> information in store (remember to appendChild your data to it)
n@1141 357 // pageSpecification is the current page node configuration
n@1141 358 // To create new XML nodes, use storage.document.createElement();
n@1141 359 }