annotate interfaces/horizontal-sliders.js @ 2202:61c8e13f1e2e

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