annotate interfaces/AB.js @ 1112:28aa066720ed

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