annotate interfaces/AB.js @ 1149:250b64aea676

Minor fixes for OSX Firefox support (Firefox 43)
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 14 Jan 2016 15:37:38 +0000
parents df4284a31f69
children 92f26057b934
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@1141 111
n@1141 112 // Populate the comparitor object
n@1141 113 interfaceContext.comparitor = new Comparitor(audioHolderObject);
n@1141 114 resizeWindow(null);
n@1141 115 }
n@1141 116
n@1141 117 function Comparitor(audioHolderObject)
n@1141 118 {
n@1141 119 this.comparitorBox = function(audioElement,id,text)
n@1141 120 {
n@1141 121 this.parent = audioElement;
n@1141 122 this.id = id;
n@1141 123 this.value = 0;
n@1141 124 this.disabled = true;
n@1141 125 this.box = document.createElement('div');
n@1141 126 this.box.className = 'comparitor-holder';
n@1141 127 this.box.setAttribute('track-id',audioElement.id);
n@1141 128 this.box.id = 'comparitor-'+text;
n@1141 129 this.selector = document.createElement('div');
n@1141 130 this.selector.className = 'comparitor-selector disabled';
n@1141 131 var selectorText = document.createElement('span');
n@1141 132 selectorText.textContent = text;
n@1141 133 this.selector.appendChild(selectorText);
n@1141 134 this.playback = document.createElement('button');
n@1141 135 this.playback.className = 'comparitor-button';
n@1141 136 this.playback.disabled = true;
n@1141 137 this.playback.textContent = "Listen";
n@1141 138 this.box.appendChild(this.selector);
n@1141 139 this.box.appendChild(this.playback);
n@1149 140 this.selector.onclick = function(event)
n@1141 141 {
n@1141 142 var time = audioEngineContext.timer.getTestTime();
n@1141 143 if ($(event.currentTarget).hasClass('disabled'))
n@1141 144 {
n@1141 145 console.log("Please wait until sample has loaded");
n@1141 146 return;
n@1141 147 }
n@1141 148 if (audioEngineContext.status == 0)
n@1141 149 {
n@1141 150 alert("Please listen to the samples before making a selection");
n@1141 151 console.log("Please listen to the samples before making a selection");
n@1141 152 return;
n@1141 153 }
n@1141 154 $(".comparitor-selector").removeClass('selected');
n@1141 155 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1141 156 interfaceContext.comparitor.selected = id;
n@1141 157 $(event.currentTarget).addClass('selected');
n@1141 158 for (var i=0; i<interfaceContext.comparitor.comparitors.length; i++)
n@1141 159 {
n@1141 160 var obj = interfaceContext.comparitor.comparitors[i];
n@1141 161 if (i == id) {
n@1141 162 obj.value = 1;
n@1141 163 } else {
n@1141 164 obj.value = 0;
n@1141 165 }
n@1141 166 obj.parent.metric.moved(time,obj.value);
n@1141 167 }
n@1141 168 console.log("Selected "+id+' ('+time+')');
n@1141 169 };
n@1149 170 this.playback.onclick = function(event)
n@1141 171 {
n@1141 172 $('.comparitor-button').text('Listen');
n@1141 173 var id = event.currentTarget.parentElement.getAttribute('track-id');
n@1141 174 audioEngineContext.play(id);
n@1141 175 $(event.currentTarget).text('Playing');
n@1141 176 };
n@1141 177
n@1141 178 this.enable = function()
n@1141 179 {
n@1141 180 if (this.parent.state == 1)
n@1141 181 {
n@1141 182 $(this.selector).removeClass('disabled');
n@1141 183 this.playback.disabled = false;
n@1141 184 }
n@1141 185 };
n@1141 186 this.updateLoading = function(progress)
n@1141 187 {
n@1141 188 if (progress != 100)
n@1141 189 {
n@1141 190 progress = String(progress);
n@1141 191 progress = progress.split('.')[0];
n@1141 192 this.playback.textContent = progress+'%';
n@1141 193 } else {
n@1141 194 this.playback.textContent = "Listen";
n@1141 195 }
n@1141 196 };
n@1141 197 this.exportXMLDOM = function(audioObject)
n@1141 198 {
n@1141 199 var node = storage.document.createElement('value');
n@1141 200 node.textContent = this.value;
n@1141 201 return node;
n@1141 202 };
n@1141 203 this.getValue = function() {
n@1141 204 return this.value;
n@1141 205 };
n@1141 206 this.getPresentedId = function()
n@1141 207 {
n@1141 208 return this.selector.children[0].textContent;
n@1141 209 };
n@1141 210 this.canMove = function()
n@1141 211 {
n@1141 212 return false;
n@1141 213 };
n@1141 214 };
n@1141 215
n@1141 216 this.boxHolders = document.getElementById('box-holders');
n@1141 217 this.boxHolders.innerHTML = null;
n@1141 218 this.comparitors = [];
n@1141 219 this.selected = null;
n@1141 220
n@1141 221 // First generate the Audio Objects for the Audio Engine
n@1141 222 for (var index=0; index<audioHolderObject.audioElements.length; index++)
n@1141 223 {
n@1141 224 var element = audioHolderObject.audioElements[index];
n@1141 225 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
n@1141 226 {
n@1141 227 console.log("WARNING - AB cannot have fixed reference");
n@1141 228 }
n@1141 229 var audioObject = audioEngineContext.newTrack(element);
n@1141 230 var node = new this.comparitorBox(audioObject,index,String.fromCharCode(65 + index));
n@1141 231 audioObject.bindInterface(node);
n@1141 232 this.comparitors.push(node);
n@1141 233 this.boxHolders.appendChild(node.box);
n@1141 234 }
n@1141 235 return this;
n@1141 236 }
n@1141 237
n@1141 238 function resizeWindow(event)
n@1141 239 {
n@1141 240 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
n@1141 241 var numObj = interfaceContext.comparitor.comparitors.length;
n@1141 242 var boxW = numObj*312;
n@1141 243 var diff = window.innerWidth - boxW;
n@1141 244 while (diff < 0)
n@1141 245 {
n@1141 246 numObj = Math.ceil(numObj/2);
n@1141 247 boxW = numObj*312;
n@1141 248 diff = window.innerWidth - boxW;
n@1141 249 }
n@1141 250 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
n@1141 251 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
n@1141 252 document.getElementById('box-holders').style.width = boxW + 'px';
n@1141 253 }
n@1141 254
n@1141 255 function buttonSubmitClick()
n@1141 256 {
n@1141 257 var checks = [];
n@1141 258 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
n@1141 259 checks = checks.concat(specification.interfaces.options);
n@1141 260 var canContinue = true;
n@1141 261
n@1141 262 for (var i=0; i<checks.length; i++) {
n@1141 263 if (checks[i].type == 'check')
n@1141 264 {
n@1141 265 switch(checks[i].name) {
n@1141 266 case 'fragmentPlayed':
n@1141 267 // Check if all fragments have been played
n@1141 268 var checkState = interfaceContext.checkAllPlayed();
n@1141 269 if (checkState == false) {canContinue = false;}
n@1141 270 break;
n@1141 271 case 'fragmentFullPlayback':
n@1141 272 // Check all fragments have been played to their full length
n@1141 273 var checkState = interfaceContext.checkFragmentsFullyPlayed();
n@1141 274 if (checkState == false) {canContinue = false;}
n@1141 275 break;
n@1141 276 case 'fragmentMoved':
n@1141 277 // Check all fragment sliders have been moved.
n@1141 278 var checkState = interfaceContext.checkAllMoved();
n@1141 279 if (checkState == false) {canContinue = false;}
n@1141 280 break;
n@1141 281 case 'fragmentComments':
n@1141 282 // Check all fragment sliders have been moved.
n@1141 283 var checkState = interfaceContext.checkAllCommented();
n@1141 284 if (checkState == false) {canContinue = false;}
n@1141 285 break;
n@1141 286 default:
n@1141 287 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@1141 288 break;
n@1141 289 }
n@1141 290
n@1141 291 }
n@1141 292 if (!canContinue) {break;}
n@1141 293 }
n@1141 294 if (canContinue)
n@1141 295 {
n@1141 296 if (audioEngineContext.status == 1) {
n@1141 297 var playback = document.getElementById('playback-button');
n@1141 298 playback.click();
n@1141 299 // 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 300 } else
n@1141 301 {
n@1141 302 if (audioEngineContext.timer.testStarted == false)
n@1141 303 {
n@1141 304 alert('You have not started the test! Please press start to begin the test!');
n@1141 305 return;
n@1141 306 }
n@1141 307 }
n@1141 308 testState.advanceState();
n@1141 309 }
n@1141 310 }
n@1141 311
n@1141 312 function pageXMLSave(store, pageSpecification)
n@1141 313 {
n@1141 314 // MANDATORY
n@1141 315 // Saves a specific test page
n@1141 316 // You can use this space to add any extra nodes to your XML <audioHolder> saves
n@1141 317 // Get the current <page> information in store (remember to appendChild your data to it)
n@1141 318 // pageSpecification is the current page node configuration
n@1141 319 // To create new XML nodes, use storage.document.createElement();
n@1141 320 }