annotate interfaces/AB.js @ 550:aa55372146a0 Dev_main

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