annotate interfaces/AB.js @ 1209:943b0ba7ec83

Bug #1624 fix: New comment box management node in InterfaceContext.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Tue, 23 Feb 2016 10:49:48 +0000
parents 0e49d874ba97
children d5898c3ae2ba
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@1202 10 // Custom comparator Object
n@1202 11 Interface.prototype.comparator = 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@1202 151 // Populate the comparator object
n@1202 152 interfaceContext.comparator = new comparator(audioHolderObject);
n@1116 153 if (audioHolderObject.showElementComments)
n@1116 154 {
n@1116 155 var commentHolder = document.createElement('div');
n@1116 156 commentHolder.id = 'commentHolder';
n@1116 157 document.getElementById('testContent').appendChild(commentHolder);
n@1116 158 // Generate one comment box per presented page
n@1116 159 for (var element of audioEngineContext.audioObjects)
n@1116 160 {
n@1209 161 interfaceContext.commentBoxes.createCommentBox(element);
n@1116 162 }
n@1209 163 interfaceContext.commentBoxes.showCommentBoxes(commentHolder,true);
n@1116 164 }
n@1141 165 resizeWindow(null);
n@1141 166 }
n@1141 167
n@1202 168 function comparator(audioHolderObject)
n@1141 169 {
n@1202 170 this.comparatorBox = function(audioElement,id,text)
n@1141 171 {
n@1141 172 this.parent = audioElement;
n@1141 173 this.id = id;
n@1141 174 this.value = 0;
n@1141 175 this.disabled = true;
n@1141 176 this.box = document.createElement('div');
n@1202 177 this.box.className = 'comparator-holder';
n@1141 178 this.box.setAttribute('track-id',audioElement.id);
n@1202 179 this.box.id = 'comparator-'+text;
n@1141 180 this.selector = document.createElement('div');
n@1202 181 this.selector.className = 'comparator-selector disabled';
n@1141 182 var selectorText = document.createElement('span');
n@1141 183 selectorText.textContent = text;
n@1141 184 this.selector.appendChild(selectorText);
n@1141 185 this.playback = document.createElement('button');
n@1202 186 this.playback.className = 'comparator-button';
n@1141 187 this.playback.disabled = true;
n@1141 188 this.playback.textContent = "Listen";
n@1141 189 this.box.appendChild(this.selector);
n@1141 190 this.box.appendChild(this.playback);
n@1149 191 this.selector.onclick = function(event)
n@1141 192 {
n@1141 193 var time = audioEngineContext.timer.getTestTime();
n@1141 194 if ($(event.currentTarget).hasClass('disabled'))
n@1141 195 {
n@1141 196 console.log("Please wait until sample has loaded");
n@1141 197 return;
n@1141 198 }
n@1141 199 if (audioEngineContext.status == 0)
n@1141 200 {
n@1141 201 alert("Please listen to the samples before making a selection");
n@1141 202 console.log("Please listen to the samples before making a selection");
n@1141 203 return;
n@1204 204 }
n@1141 205 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1202 206 interfaceContext.comparator.selected = id;
n@1204 207 if ($(event.currentTarget).hasClass("selected")) {
n@1204 208 $(".comparator-selector").removeClass('selected');
n@1204 209 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
n@1204 210 {
n@1204 211 var obj = interfaceContext.comparator.comparators[i];
n@1204 212 obj.parent.metric.moved(time,0);
n@1204 213 }
n@1204 214 } else {
n@1204 215 $(".comparator-selector").removeClass('selected');
n@1204 216 $(event.currentTarget).addClass('selected');
n@1204 217 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
n@1204 218 {
n@1204 219 var obj = interfaceContext.comparator.comparators[i];
n@1204 220 if (i == id) {
n@1204 221 obj.value = 1;
n@1204 222 } else {
n@1204 223 obj.value = 0;
n@1204 224 }
n@1204 225 obj.parent.metric.moved(time,obj.value);
n@1204 226 }
n@1204 227 console.log("Selected "+id+' ('+time+')');
n@1204 228 }
n@1141 229 };
n@1176 230 this.playback.setAttribute("playstate","ready");
n@1149 231 this.playback.onclick = function(event)
n@1141 232 {
n@1141 233 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1176 234 if (event.currentTarget.getAttribute("playstate") == "ready")
n@1176 235 {
n@1176 236 audioEngineContext.play(id);
n@1176 237 } else if (event.currentTarget.getAttribute("playstate") == "playing") {
n@1176 238 audioEngineContext.stop();
n@1176 239 }
n@1176 240
n@1141 241 };
n@1141 242
n@1141 243 this.enable = function()
n@1141 244 {
n@1141 245 if (this.parent.state == 1)
n@1141 246 {
n@1141 247 $(this.selector).removeClass('disabled');
n@1141 248 this.playback.disabled = false;
n@1141 249 }
n@1141 250 };
n@1141 251 this.updateLoading = function(progress)
n@1141 252 {
n@1141 253 if (progress != 100)
n@1141 254 {
n@1141 255 progress = String(progress);
n@1141 256 progress = progress.split('.')[0];
n@1141 257 this.playback.textContent = progress+'%';
n@1141 258 } else {
n@1176 259 this.playback.textContent = "Play";
n@1141 260 }
n@1141 261 };
n@1205 262 this.error = function() {
n@1205 263 // audioObject has an error!!
n@1205 264 this.playback.textContent = "Error";
n@1205 265 $(this.playback).addClass("error-colour");
n@1205 266 }
n@1160 267 this.startPlayback = function()
n@1160 268 {
n@1202 269 $('.comparator-button').text('Listen');
n@1176 270 $(this.playback).text('Stop');
n@1176 271 this.playback.setAttribute("playstate","playing");
n@1160 272 };
n@1160 273 this.stopPlayback = function()
n@1160 274 {
n@1160 275 $(this.playback).text('Listen');
n@1176 276 this.playback.setAttribute("playstate","ready");
n@1160 277 };
n@1141 278 this.exportXMLDOM = function(audioObject)
n@1141 279 {
n@1141 280 var node = storage.document.createElement('value');
n@1141 281 node.textContent = this.value;
n@1141 282 return node;
n@1141 283 };
n@1141 284 this.getValue = function() {
n@1141 285 return this.value;
n@1141 286 };
n@1141 287 this.getPresentedId = function()
n@1141 288 {
n@1141 289 return this.selector.children[0].textContent;
n@1141 290 };
n@1141 291 this.canMove = function()
n@1141 292 {
n@1141 293 return false;
n@1141 294 };
n@1141 295 };
n@1141 296
n@1141 297 this.boxHolders = document.getElementById('box-holders');
n@1141 298 this.boxHolders.innerHTML = null;
n@1202 299 this.comparators = [];
n@1141 300 this.selected = null;
n@1141 301
n@1141 302 // First generate the Audio Objects for the Audio Engine
n@1141 303 for (var index=0; index<audioHolderObject.audioElements.length; index++)
n@1141 304 {
n@1141 305 var element = audioHolderObject.audioElements[index];
n@1141 306 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
n@1141 307 {
n@1141 308 console.log("WARNING - AB cannot have fixed reference");
n@1141 309 }
n@1141 310 var audioObject = audioEngineContext.newTrack(element);
n@1202 311 var node = new this.comparatorBox(audioObject,index,String.fromCharCode(65 + index));
n@1141 312 audioObject.bindInterface(node);
n@1202 313 this.comparators.push(node);
n@1141 314 this.boxHolders.appendChild(node.box);
n@1141 315 }
n@1141 316 return this;
n@1141 317 }
n@1141 318
n@1141 319 function resizeWindow(event)
n@1141 320 {
n@1141 321 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
n@1202 322 var numObj = interfaceContext.comparator.comparators.length;
n@1141 323 var boxW = numObj*312;
n@1141 324 var diff = window.innerWidth - boxW;
n@1141 325 while (diff < 0)
n@1141 326 {
n@1141 327 numObj = Math.ceil(numObj/2);
n@1141 328 boxW = numObj*312;
n@1141 329 diff = window.innerWidth - boxW;
n@1141 330 }
n@1141 331 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
n@1141 332 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
n@1141 333 document.getElementById('box-holders').style.width = boxW + 'px';
n@1141 334 }
n@1141 335
n@1141 336 function buttonSubmitClick()
n@1141 337 {
n@1141 338 var checks = [];
n@1141 339 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
n@1141 340 checks = checks.concat(specification.interfaces.options);
n@1141 341 var canContinue = true;
n@1141 342
n@1141 343 for (var i=0; i<checks.length; i++) {
n@1141 344 if (checks[i].type == 'check')
n@1141 345 {
n@1141 346 switch(checks[i].name) {
n@1141 347 case 'fragmentPlayed':
n@1141 348 // Check if all fragments have been played
n@1141 349 var checkState = interfaceContext.checkAllPlayed();
n@1141 350 if (checkState == false) {canContinue = false;}
n@1141 351 break;
n@1141 352 case 'fragmentFullPlayback':
n@1141 353 // Check all fragments have been played to their full length
n@1141 354 var checkState = interfaceContext.checkFragmentsFullyPlayed();
n@1141 355 if (checkState == false) {canContinue = false;}
n@1141 356 break;
n@1141 357 case 'fragmentMoved':
n@1141 358 // Check all fragment sliders have been moved.
n@1141 359 var checkState = interfaceContext.checkAllMoved();
n@1141 360 if (checkState == false) {canContinue = false;}
n@1141 361 break;
n@1141 362 case 'fragmentComments':
n@1141 363 // Check all fragment sliders have been moved.
n@1141 364 var checkState = interfaceContext.checkAllCommented();
n@1141 365 if (checkState == false) {canContinue = false;}
n@1141 366 break;
n@1141 367 default:
n@1141 368 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@1141 369 break;
n@1141 370 }
n@1141 371
n@1141 372 }
n@1141 373 if (!canContinue) {break;}
n@1141 374 }
n@1141 375 if (canContinue)
n@1141 376 {
n@1141 377 if (audioEngineContext.status == 1) {
n@1141 378 var playback = document.getElementById('playback-button');
n@1141 379 playback.click();
n@1141 380 // 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 381 } else
n@1141 382 {
n@1141 383 if (audioEngineContext.timer.testStarted == false)
n@1141 384 {
n@1141 385 alert('You have not started the test! Please press start to begin the test!');
n@1141 386 return;
n@1141 387 }
n@1141 388 }
n@1141 389 testState.advanceState();
n@1141 390 }
n@1141 391 }
n@1141 392
n@1141 393 function pageXMLSave(store, pageSpecification)
n@1141 394 {
n@1141 395 // MANDATORY
n@1141 396 // Saves a specific test page
n@1141 397 // You can use this space to add any extra nodes to your XML <audioHolder> saves
n@1141 398 // Get the current <page> information in store (remember to appendChild your data to it)
n@1141 399 // pageSpecification is the current page node configuration
n@1141 400 // To create new XML nodes, use storage.document.createElement();
n@1141 401 }