annotate interfaces/AB.js @ 1350:b6389ceaeaa5

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