annotate interfaces/horizontal-sliders.js @ 476:9ae9d1fb80bf Dev_main

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