annotate interfaces/discrete.js @ 1154:3ba07a441364

Adding volume control interface object. Minor discrete layout format change.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 22 Jan 2016 11:10:33 +0000
parents c87a02efdc37
children 92f26057b934
rev   line source
n@1145 1 // Once this is loaded and parsed, begin execution
n@1145 2 loadInterface();
n@1145 3
n@1145 4 function loadInterface() {
n@1145 5 // Use this to do any one-time page / element construction. For instance, placing any stationary text objects,
n@1145 6 // holding div's, or setting up any nodes which are present for the entire test sequence
n@1145 7
n@1145 8 // The injection point into the HTML page
n@1145 9 interfaceContext.insertPoint = document.getElementById("topLevelBody");
n@1145 10 var testContent = document.createElement('div');
n@1145 11 testContent.id = 'testContent';
n@1145 12
n@1145 13 // Create the top div for the Title element
n@1145 14 var titleAttr = specification.title;
n@1145 15 var title = document.createElement('div');
n@1145 16 title.className = "title";
n@1145 17 title.align = "center";
n@1145 18 var titleSpan = document.createElement('span');
n@1145 19
n@1145 20 // Set title to that defined in XML, else set to default
n@1145 21 if (titleAttr != undefined) {
n@1145 22 titleSpan.textContent = titleAttr;
n@1145 23 } else {
n@1145 24 titleSpan.textContent = 'Listening test';
n@1145 25 }
n@1145 26 // Insert the titleSpan element into the title div element.
n@1145 27 title.appendChild(titleSpan);
n@1145 28
n@1145 29 var pagetitle = document.createElement('div');
n@1145 30 pagetitle.className = "pageTitle";
n@1145 31 pagetitle.align = "center";
n@1145 32 var titleSpan = document.createElement('span');
n@1145 33 titleSpan.id = "pageTitle";
n@1145 34 pagetitle.appendChild(titleSpan);
n@1145 35
n@1145 36 // Create Interface buttons!
n@1145 37 var interfaceButtons = document.createElement('div');
n@1145 38 interfaceButtons.id = 'interface-buttons';
n@1145 39 interfaceButtons.style.height = '25px';
n@1145 40
n@1145 41 // Create playback start/stop points
n@1145 42 var playback = document.createElement("button");
n@1145 43 playback.innerHTML = 'Stop';
n@1145 44 playback.id = 'playback-button';
n@1145 45 playback.style.float = 'left';
n@1145 46 // onclick function. Check if it is playing or not, call the correct function in the
n@1145 47 // audioEngine, change the button text to reflect the next state.
n@1145 48 playback.onclick = function() {
n@1145 49 if (audioEngineContext.status == 1) {
n@1145 50 audioEngineContext.stop();
n@1145 51 this.innerHTML = 'Stop';
n@1145 52 var time = audioEngineContext.timer.getTestTime();
n@1145 53 console.log('Stopped at ' + time); // DEBUG/SAFETY
n@1145 54 }
n@1145 55 };
n@1145 56 // Create Submit (save) button
n@1145 57 var submit = document.createElement("button");
n@1145 58 submit.innerHTML = 'Submit';
n@1145 59 submit.onclick = buttonSubmitClick;
n@1145 60 submit.id = 'submit-button';
n@1145 61 submit.style.float = 'left';
n@1145 62 // Append the interface buttons into the interfaceButtons object.
n@1145 63 interfaceButtons.appendChild(playback);
n@1145 64 interfaceButtons.appendChild(submit);
n@1145 65
n@1145 66 // Create a slider box
n@1145 67 var sliderBox = document.createElement('div');
n@1145 68 sliderBox.style.width = "100%";
n@1145 69 sliderBox.style.height = window.innerHeight - 200+12 + 'px';
n@1145 70 sliderBox.style.marginBottom = '10px';
n@1145 71 sliderBox.id = 'slider';
n@1145 72 var scaleHolder = document.createElement('div');
n@1145 73 scaleHolder.id = "scale-holder";
n@1145 74 scaleHolder.style.marginLeft = "107px";
n@1145 75 sliderBox.appendChild(scaleHolder);
n@1145 76 var scaleText = document.createElement('div');
n@1145 77 scaleText.id = "scale-text-holder";
n@1145 78 scaleText.style.height = "25px";
n@1145 79 scaleText.style.width = "100%";
n@1145 80 scaleHolder.appendChild(scaleText);
n@1145 81 var scaleCanvas = document.createElement('canvas');
n@1145 82 scaleCanvas.id = "scale-canvas";
n@1146 83 scaleCanvas.style.marginLeft = "150px";
n@1145 84 scaleHolder.appendChild(scaleCanvas);
n@1145 85 var sliderObjectHolder = document.createElement('div');
n@1145 86 sliderObjectHolder.id = 'slider-holder';
n@1145 87 sliderObjectHolder.align = "center";
n@1145 88 sliderBox.appendChild(sliderObjectHolder);
n@1145 89
n@1145 90 // Global parent for the comment boxes on the page
n@1145 91 var feedbackHolder = document.createElement('div');
n@1145 92 feedbackHolder.id = 'feedbackHolder';
n@1145 93
n@1145 94 testContent.style.zIndex = 1;
n@1145 95 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@1145 96
n@1145 97 // Inject into HTML
n@1145 98 testContent.appendChild(title); // Insert the title
n@1145 99 testContent.appendChild(pagetitle);
n@1145 100 testContent.appendChild(interfaceButtons);
n@1145 101 testContent.appendChild(sliderBox);
n@1145 102 testContent.appendChild(feedbackHolder);
n@1145 103 interfaceContext.insertPoint.appendChild(testContent);
n@1145 104
n@1145 105 // Load the full interface
n@1145 106 testState.initialise();
n@1145 107 testState.advanceState();
n@1145 108 };
n@1145 109
n@1145 110 function loadTest(page)
n@1145 111 {
n@1145 112 // Called each time a new test page is to be build. The page specification node is the only item passed in
n@1145 113 var id = page.id;
n@1145 114
n@1145 115 var feedbackHolder = document.getElementById('feedbackHolder');
n@1145 116 var interfaceObj = page.interfaces;
n@1145 117 if (interfaceObj.length > 1)
n@1145 118 {
n@1145 119 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node");
n@1145 120 }
n@1145 121 interfaceObj = interfaceObj[0];
n@1145 122 if(interfaceObj.title != null)
n@1145 123 {
n@1145 124 document.getElementById("pageTitle").textContent = interfaceObj.title;
n@1145 125 }
n@1145 126
n@1145 127 // Delete outside reference
n@1145 128 var outsideReferenceHolder = document.getElementById('outside-reference');
n@1145 129 if (outsideReferenceHolder != null) {
n@1145 130 document.getElementById('interface-buttons').removeChild(outsideReferenceHolder);
n@1145 131 }
n@1145 132
n@1145 133 var sliderBox = document.getElementById('slider-holder');
n@1145 134 feedbackHolder.innerHTML = null;
n@1145 135 sliderBox.innerHTML = null;
n@1145 136
n@1145 137 var commentBoxPrefix = "Comment on track";
n@1145 138 if (interfaceObj.commentBoxPrefix != undefined) {
n@1145 139 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@1145 140 }
n@1145 141 var loopPlayback = page.loop;
n@1145 142
n@1145 143 $(page.commentQuestions).each(function(index,element) {
n@1145 144 var node = interfaceContext.createCommentQuestion(element);
n@1145 145 feedbackHolder.appendChild(node.holder);
n@1145 146 });
n@1145 147
n@1145 148 // Find all the audioElements from the audioHolder
n@1145 149 var label = 0;
n@1146 150 var interfaceScales = testState.currentStateMap.interfaces[0].scales;
n@1145 151 $(page.audioElements).each(function(index,element){
n@1145 152 // Find URL of track
n@1145 153 // In this jQuery loop, variable 'this' holds the current audioElement.
n@1145 154
n@1145 155 var audioObject = audioEngineContext.newTrack(element);
n@1145 156 if (element.type == 'outside-reference')
n@1145 157 {
n@1145 158 // Construct outside reference;
n@1145 159 var orNode = new outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons'));
n@1145 160 audioObject.bindInterface(orNode);
n@1145 161 } else {
n@1145 162 var node = interfaceContext.createCommentBox(audioObject);
n@1145 163
n@1145 164 // Create a slider per track
n@1146 165 var sliderObj = new discreteObject(audioObject,label,interfaceScales);
n@1145 166 sliderBox.appendChild(sliderObj.holder);
n@1145 167 audioObject.bindInterface(sliderObj);
n@1145 168 label += 1;
n@1145 169 }
n@1145 170
n@1145 171 });
n@1145 172
n@1145 173 // Auto-align
n@1145 174 resizeWindow(null);
n@1145 175 }
n@1145 176
n@1146 177 function discreteObject(audioObject,label,interfaceScales)
n@1145 178 {
n@1145 179 // An example node, you can make this however you want for each audioElement.
n@1145 180 // However, every audioObject (audioEngineContext.audioObject) MUST have an interface object with the following
n@1145 181 // You attach them by calling audioObject.bindInterface( )
n@1146 182 if (interfaceScales == null || interfaceScales.length == 0)
n@1145 183 {
n@1145 184 console.log("WARNING: The discrete radio's are built depending on the number of scale points specified! Ensure you have some specified. Defaulting to 5 for now!");
n@1145 185 numOptions = 5;
n@1145 186 }
n@1145 187 this.parent = audioObject;
n@1145 188
n@1145 189 this.holder = document.createElement('div');
n@1145 190 this.title = document.createElement('div');
n@1145 191 this.discreteHolder = document.createElement('div');
n@1145 192 this.discretes = [];
n@1145 193 this.play = document.createElement('button');
n@1145 194
n@1145 195 this.holder.className = 'track-slider';
n@1145 196 this.holder.style.width = window.innerWidth-200 + 'px';
n@1145 197 this.holder.appendChild(this.title);
n@1145 198 this.holder.appendChild(this.discreteHolder);
n@1145 199 this.holder.appendChild(this.play);
n@1145 200 this.holder.setAttribute('trackIndex',audioObject.id);
n@1145 201 this.title.textContent = label;
n@1145 202 this.title.className = 'track-slider-title';
n@1145 203
n@1145 204 this.discreteHolder.className = "track-slider-range";
n@1146 205 this.discreteHolder.style.width = window.innerWidth-500 + 'px';
n@1146 206 for (var i=0; i<interfaceScales.length; i++)
n@1145 207 {
n@1145 208 var node = document.createElement('input');
n@1145 209 node.setAttribute('type','radio');
n@1146 210 node.className = 'track-radio';
n@1146 211 node.setAttribute('position',interfaceScales[i].position);
n@1145 212 node.setAttribute('name',audioObject.specification.id);
n@1145 213 node.setAttribute('id',audioObject.specification.id+'-'+String(i));
n@1145 214 this.discretes.push(node);
n@1145 215 this.discreteHolder.appendChild(node);
n@1146 216 node.onclick = function(event)
n@1145 217 {
n@1146 218 if (audioEngineContext.status == 0)
n@1146 219 {
n@1146 220 event.currentTarget.checked = false;
n@1146 221 return;
n@1146 222 }
n@1145 223 var time = audioEngineContext.timer.getTestTime();
n@1145 224 var id = Number(event.currentTarget.parentNode.parentNode.getAttribute('trackIndex'));
n@1146 225 var value = event.currentTarget.getAttribute('position') / 100.0;
n@1146 226 audioEngineContext.audioObjects[id].metric.moved(time,value);
n@1146 227 console.log('slider '+id+' moved to '+value+' ('+time+')');
n@1145 228 };
n@1145 229 }
n@1145 230
n@1145 231 this.play.className = 'track-slider-button';
n@1145 232 this.play.textContent = "Loading...";
n@1145 233 this.play.value = audioObject.id;
n@1145 234 this.play.disabled = true;
n@1145 235 this.play.onclick = function(event)
n@1145 236 {
n@1145 237 var id = Number(event.currentTarget.value);
n@1145 238 //audioEngineContext.metric.sliderPlayed(id);
n@1145 239 audioEngineContext.play(id);
n@1145 240 $(".track-slider").removeClass('track-slider-playing');
n@1145 241 $(event.currentTarget.parentElement).addClass('track-slider-playing');
n@1145 242 var outsideReference = document.getElementById('outside-reference');
n@1145 243 if (outsideReference != null) {
n@1145 244 $(outsideReference).removeClass('track-slider-playing');
n@1145 245 }
n@1145 246 };
n@1145 247 this.resize = function(event)
n@1145 248 {
n@1145 249 this.holder.style.width = window.innerWidth-200 + 'px';
n@1146 250 this.discreteHolder.style.width = window.innerWidth-500 + 'px';
n@1146 251 //text.style.left = (posPix+150-($(text).width()/2)) +'px';
n@1146 252 for (var i=0; i<this.discretes.length; i++)
n@1146 253 {
n@1146 254 var width = $(this.discreteHolder).width() - 20;
n@1146 255 var node = this.discretes[i];
n@1146 256 var nodeW = $(node).width();
n@1146 257 var position = node.getAttribute('position');
n@1146 258 var posPix = Math.round(width * (position / 100.0));
n@1146 259 node.style.left = (posPix+10 - (nodeW/2)) + 'px';
n@1146 260 }
n@1145 261 };
n@1145 262 this.enable = function()
n@1145 263 {
n@1145 264 // This is used to tell the interface object that playback of this node is ready
n@1145 265 this.play.disabled = false;
n@1145 266 this.play.textContent = "Play";
n@1145 267 $(this.slider).removeClass('track-slider-disabled');
n@1145 268 };
n@1145 269 this.updateLoading = function(progress)
n@1145 270 {
n@1145 271 // progress is a value from 0 to 100 indicating the current download state of media files
n@1145 272 };
n@1145 273 this.getValue = function()
n@1145 274 {
n@1146 275 // Return the current value of the object. If there is no value, return -1
n@1146 276 var value = -1;
n@1146 277 for (var i=0; i<this.discretes.length; i++)
n@1146 278 {
n@1146 279 if (this.discretes[i].checked == true)
n@1146 280 {
n@1146 281 value = this.discretes[i].getAttribute('position') / 100.0;
n@1146 282 break;
n@1146 283 }
n@1146 284 }
n@1146 285 return value;
n@1145 286 };
n@1145 287 this.getPresentedId = function()
n@1145 288 {
n@1145 289 // Return the presented ID of the object. For instance, the APE has sliders starting from 0. Whilst AB has alphabetical scale
n@1145 290 return this.title.textContent;
n@1145 291 };
n@1145 292 this.canMove = function()
n@1145 293 {
n@1145 294 // Return either true or false if the interface object can be moved. AB / Reference cannot, whilst sliders can and therefore have a continuous scale.
n@1145 295 // These are checked primarily if the interface check option 'fragmentMoved' is enabled.
n@1145 296 return true;
n@1145 297 };
n@1145 298 this.exportXMLDOM = function(audioObject) {
n@1145 299 // Called by the audioObject holding this element to export the interface <value> node.
n@1145 300 // If there is no value node (such as outside reference), return null
n@1145 301 // If there are multiple value nodes (such as multiple scale / 2D scales), return an array of nodes with each value node having an 'interfaceName' attribute
n@1145 302 // Use storage.document.createElement('value'); to generate the XML node.
n@1146 303 var node = storage.document.createElement('value');
n@1146 304 node.textContent = this.getValue();
n@1146 305 return node;
n@1145 306 };
n@1145 307 };
n@1145 308
n@1145 309 function resizeWindow(event)
n@1145 310 {
n@1145 311 // Called on every window resize event, use this to scale your page properly
n@1145 312 var numObj = document.getElementsByClassName('track-slider').length;
n@1145 313 var totalHeight = (numObj * 66)-30;
n@1145 314 document.getElementById('scale-holder').style.width = window.innerWidth-220 + 'px';
n@1145 315 var canvas = document.getElementById('scale-canvas');
n@1146 316 canvas.width = window.innerWidth-520;
n@1145 317 canvas.height = totalHeight;
n@1145 318 for (var i in audioEngineContext.audioObjects)
n@1145 319 {
n@1145 320 if (audioEngineContext.audioObjects[i].specification.type != 'outside-reference'){
n@1145 321 audioEngineContext.audioObjects[i].interfaceDOM.resize(event);
n@1145 322 }
n@1145 323 }
n@1154 324 document.getElementById('slider-holder').style.height = totalHeight + 'px';
n@1154 325 document.getElementById('slider').style.height = totalHeight + 70 + 'px';
n@1145 326 drawScale();
n@1145 327 }
n@1145 328
n@1145 329 function drawScale()
n@1145 330 {
n@1145 331 var interfaceObj = testState.currentStateMap.interfaces[0];
n@1145 332 var scales = testState.currentStateMap.interfaces[0].scales;
n@1145 333 scales = scales.sort(function(a,b) {
n@1145 334 return a.position - b.position;
n@1145 335 });
n@1145 336 var canvas = document.getElementById('scale-canvas');
n@1145 337 var ctx = canvas.getContext("2d");
n@1145 338 var height = canvas.height;
n@1145 339 var width = canvas.width;
n@1145 340 var textHolder = document.getElementById('scale-text-holder');
n@1145 341 textHolder.innerHTML = null;
n@1145 342 ctx.fillStyle = "#000000";
n@1145 343 ctx.setLineDash([1,4]);
n@1145 344 for (var scale of scales)
n@1145 345 {
n@1145 346 var posPercent = scale.position / 100.0;
n@1145 347 var posPix = Math.round(width * posPercent);
n@1145 348 if(posPix<=0){posPix=1;}
n@1145 349 if(posPix>=width){posPix=width-1;}
n@1145 350 ctx.moveTo(posPix,0);
n@1145 351 ctx.lineTo(posPix,height);
n@1145 352 ctx.stroke();
n@1145 353
n@1145 354 var text = document.createElement('div');
n@1145 355 text.align = "center";
n@1145 356 var textC = document.createElement('span');
n@1145 357 textC.textContent = scale.text;
n@1145 358 text.appendChild(textC);
n@1145 359 text.className = "scale-text";
n@1145 360 textHolder.appendChild(text);
n@1146 361 text.style.width = $(text.children[0]).width()+'px';
n@1146 362 text.style.left = (posPix+150-($(text).width()/2)) +'px';
n@1145 363 }
n@1145 364 }
n@1145 365
n@1145 366 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@1145 367 {
n@1145 368 var checks = [];
n@1145 369 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
n@1145 370 checks = checks.concat(specification.interfaces.options);
n@1145 371 var canContinue = true;
n@1145 372
n@1145 373 // Check that the anchor and reference objects are correctly placed
n@1145 374 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@1145 375 if (interfaceContext.checkHiddenReference() == false) {return;}
n@1145 376
n@1145 377 for (var i=0; i<checks.length; i++) {
n@1145 378 if (checks[i].type == 'check')
n@1145 379 {
n@1145 380 switch(checks[i].name) {
n@1145 381 case 'fragmentPlayed':
n@1145 382 // Check if all fragments have been played
n@1145 383 var checkState = interfaceContext.checkAllPlayed();
n@1145 384 if (checkState == false) {canContinue = false;}
n@1145 385 break;
n@1145 386 case 'fragmentFullPlayback':
n@1145 387 // Check all fragments have been played to their full length
n@1145 388 var checkState = interfaceContext.checkAllPlayed();
n@1145 389 if (checkState == false) {canContinue = false;}
n@1145 390 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@1145 391 break;
n@1145 392 case 'fragmentMoved':
n@1145 393 // Check all fragment sliders have been moved.
n@1145 394 var checkState = interfaceContext.checkAllMoved();
n@1145 395 if (checkState == false) {canContinue = false;}
n@1145 396 break;
n@1145 397 case 'fragmentComments':
n@1145 398 // Check all fragment sliders have been moved.
n@1145 399 var checkState = interfaceContext.checkAllCommented();
n@1145 400 if (checkState == false) {canContinue = false;}
n@1145 401 break;
n@1145 402 //case 'scalerange':
n@1145 403 // Check the scale is used to its full width outlined by the node
n@1145 404 //var checkState = interfaceContext.checkScaleRange();
n@1145 405 //if (checkState == false) {canContinue = false;}
n@1145 406 // break;
n@1145 407 default:
n@1145 408 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@1145 409 break;
n@1145 410 }
n@1145 411
n@1145 412 }
n@1145 413 if (!canContinue) {break;}
n@1145 414 }
n@1145 415
n@1145 416 if (canContinue) {
n@1145 417 if (audioEngineContext.status == 1) {
n@1145 418 var playback = document.getElementById('playback-button');
n@1145 419 playback.click();
n@1145 420 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
n@1145 421 } else
n@1145 422 {
n@1145 423 if (audioEngineContext.timer.testStarted == false)
n@1145 424 {
n@1145 425 alert('You have not started the test! Please press start to begin the test!');
n@1145 426 return;
n@1145 427 }
n@1145 428 }
n@1145 429 testState.advanceState();
n@1145 430 }
n@1145 431 }
n@1145 432
n@1145 433 function pageXMLSave(store, pageSpecification)
n@1145 434 {
n@1145 435 // MANDATORY
n@1145 436 // Saves a specific test page
n@1145 437 // You can use this space to add any extra nodes to your XML <audioHolder> saves
n@1145 438 // Get the current <page> information in store (remember to appendChild your data to it)
n@1145 439 // pageSpecification is the current page node configuration
n@1145 440 // To create new XML nodes, use storage.document.createElement();
n@1145 441 }