nickjillings@1341: /** nickjillings@1341: * ape.js nickjillings@1341: * Create the APE interface nickjillings@1341: */ nickjillings@1341: nicholas@2696: /*globals window,interfaceContext, document, audioEngineContext, console, $, Interface, testState, storage, specification */ nicholas@2696: /*globals metricTracker */ nickjillings@1341: // Once this is loaded and parsed, begin execution nickjillings@1341: loadInterface(); nickjillings@1341: nickjillings@1341: function loadInterface() { nicholas@2538: nicholas@2538: // Get the dimensions of the screen available to the page nicholas@2538: var width = window.innerWidth; nicholas@2538: var height = window.innerHeight; nicholas@2538: nicholas@2538: // The injection point into the HTML page nicholas@2538: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nicholas@2538: var testContent = document.createElement('div'); nicholas@2538: nicholas@2538: testContent.id = 'testContent'; nicholas@2538: nicholas@2538: // Bindings for interfaceContext nicholas@2538: interfaceContext.checkAllPlayed = function () { nicholas@2696: var hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@2538: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@2538: { nicholas@2538: var str = ""; nicholas@2538: if (hasBeenPlayed.length > 1) { nicholas@2538: for (var i = 0; i < hasBeenPlayed.length; i++) { nicholas@2284: var ao_id = audioEngineContext.audioObjects[hasBeenPlayed[i]].interfaceDOM.getPresentedId(); nicholas@2538: str = str + ao_id; // start from 1 nicholas@2538: if (i < hasBeenPlayed.length - 2) { nicholas@2538: str += ", "; nicholas@2538: } else if (i == hasBeenPlayed.length - 2) { nicholas@2538: str += " or "; nicholas@2538: } nicholas@2538: } nicholas@2313: str = 'You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.'; nicholas@2538: } else { nicholas@2538: str = 'You have not played fragment ' + (audioEngineContext.audioObjects[hasBeenPlayed[0]].interfaceDOM.getPresentedId()) + ' yet. Please listen, rate and comment all samples before submitting.'; nicholas@2538: } nicholas@2313: this.storeErrorNode(str); nicholas@2538: interfaceContext.lightbox.post("Message", str); nicholas@2538: return false; nicholas@2538: } nicholas@2538: return true; nicholas@2538: }; nicholas@2538: nicholas@2538: interfaceContext.checkAllMoved = function () { nicholas@2538: var state = true; nicholas@2538: var str = 'You have not moved the following sliders. '; nicholas@2538: for (var i = 0; i < this.interfaceSliders.length; i++) { nicholas@2538: var interfaceTID = []; nicholas@2538: for (var j = 0; j < this.interfaceSliders[i].metrics.length; j++) { nicholas@2285: var ao_id = this.interfaceSliders[i].sliders[j].getAttribute("trackIndex"); nicholas@2696: if (this.interfaceSliders[i].metrics[j].wasMoved === false && audioEngineContext.audioObjects[ao_id].interfaceDOM.canMove()) { nicholas@2538: state = false; nicholas@2538: interfaceTID.push(j); nicholas@2538: } nicholas@2538: } nicholas@2696: if (interfaceTID.length !== 0) { nicholas@2538: var interfaceName = this.interfaceSliders[i].interfaceObject.title; nicholas@2696: if (interfaceName === undefined) { nicholas@2538: str += 'On axis ' + String(i + 1) + ' you must move '; nicholas@2538: } else { nicholas@2538: str += 'On axis "' + interfaceName + '" you must move '; nicholas@2538: } nicholas@2538: if (interfaceTID.length == 1) { nicholas@2538: str += 'slider ' + (audioEngineContext.audioObjects[interfaceTID[0]].interfaceDOM.getPresentedId()) + '. '; // start from 1 nicholas@2538: } else { nicholas@2538: str += 'sliders '; nicholas@2538: for (var k = 0; k < interfaceTID.length - 1; k++) { nicholas@2538: str += (audioEngineContext.audioObjects[interfaceTID[k]].interfaceDOM.getPresentedId()) + ', '; // start from 1 nicholas@2538: } nicholas@2538: str += (audioEngineContext.audioObjects[interfaceTID[interfaceTID.length - 1]].interfaceDOM.getPresentedId()) + '. '; nicholas@2538: } nicholas@2538: } nicholas@2538: } nicholas@2696: if (state !== true) { nicholas@2313: this.storeErrorNode(str); nicholas@2538: interfaceContext.lightbox.post("Message", str); nicholas@2538: console.log(str); nicholas@2538: } nicholas@2538: return state; nicholas@2538: }; nicholas@2703: nicholas@2698: interfaceContext.checkScaleRange = function () { nicholas@2538: var audioObjs = audioEngineContext.audioObjects; nicholas@2538: var audioHolder = testState.stateMap[testState.stateIndex]; nicholas@2696: var interfaceObject = this.interfaceSliders[0].interfaceObject; nicholas@2538: var state = true; nicholas@2538: var str = ''; nicholas@2696: this.interfaceSliders.forEach(function (sliderHolder, i) { nicholas@2696: var scales = (function () { nicholas@2696: var scaleRange = interfaceObject.options.find(function (a) { nicholas@2696: return a.name == "scalerange"; nicholas@2696: }); nicholas@2696: return { nicholas@2696: min: scaleRange.min, nicholas@2696: max: scaleRange.max nicholas@2696: }; nicholas@2696: })(); nicholas@2704: var range = sliderHolder.sliders.reduce(function (a, b) { nicholas@2704: var v = convSliderPosToRate(b) * 100.0; nicholas@2704: return { nicholas@2704: min: Math.min(a.min, v), nicholas@2704: max: Math.max(a.max, v) n@2795: }; nicholas@2704: }, { nicholas@2704: min: 100, nicholas@2704: max: 0 nicholas@2696: }); nicholas@2704: if (range.min >= scales.min || range.max <= scales.max) { nicholas@2696: state = false; nicholas@2696: str += 'On axis "' + sliderHolder.interfaceObject.title + '" you have not used the full width of the scale. '; nicholas@2538: } nicholas@2696: }); nicholas@2696: if (state !== true) { nicholas@2313: this.storeErrorNode(str); nicholas@2538: interfaceContext.lightbox.post("Message", str); nicholas@2538: console.log(str); nicholas@2538: } nicholas@2538: return state; nicholas@2538: }; nicholas@2538: nicholas@2538: Interface.prototype.objectSelected = null; nicholas@2538: Interface.prototype.objectMoved = false; nicholas@2538: Interface.prototype.selectObject = function (object) { nicholas@2696: if (this.objectSelected === null) { nicholas@2538: this.objectSelected = object; nicholas@2538: this.objectMoved = false; nicholas@2538: } nicholas@2538: }; nicholas@2538: Interface.prototype.moveObject = function () { nicholas@2696: if (this.objectMoved === false) { nicholas@2538: this.objectMoved = true; nicholas@2538: } nicholas@2538: }; nicholas@2538: Interface.prototype.releaseObject = function () { nicholas@2538: this.objectSelected = null; nicholas@2538: this.objectMoved = false; nicholas@2538: }; nicholas@2538: Interface.prototype.getSelectedObject = function () { nicholas@2538: return this.objectSelected; nicholas@2538: }; nicholas@2538: Interface.prototype.hasSelectedObjectMoved = function () { nicholas@2538: return this.objectMoved; nicholas@2538: }; nicholas@2538: nicholas@2538: // Bindings for slider interfaces nicholas@2538: Interface.prototype.interfaceSliders = []; nicholas@2538: nicholas@2538: // Bindings for audioObjects nicholas@2538: nicholas@2538: // Create the top div for the Title element nicholas@2538: var titleAttr = specification.title; nicholas@2538: var title = document.createElement('div'); nicholas@2538: title.className = "title"; nicholas@2538: title.align = "center"; nicholas@2538: var titleSpan = document.createElement('span'); nicholas@2470: titleSpan.id = "test-title"; nicholas@2538: nicholas@2538: // Set title to that defined in XML, else set to default nicholas@2696: if (titleAttr !== undefined) { nicholas@2538: titleSpan.textContent = titleAttr; nicholas@2538: } else { nicholas@2538: titleSpan.textContent = 'Listening test'; nicholas@2538: } nicholas@2538: // Insert the titleSpan element into the title div element. nicholas@2538: title.appendChild(titleSpan); nicholas@2538: nicholas@2538: // Create Interface buttons! nicholas@2538: var interfaceButtons = document.createElement('div'); nicholas@2538: interfaceButtons.id = 'interface-buttons'; nicholas@2538: nicholas@2538: // Create playback start/stop points nicholas@2538: var playback = document.createElement("button"); nicholas@2538: playback.innerHTML = 'Stop'; nicholas@2538: playback.id = 'playback-button'; nicholas@2538: // onclick function. Check if it is playing or not, call the correct function in the nicholas@2538: // audioEngine, change the button text to reflect the next state. nicholas@2538: playback.onclick = function () { nicholas@2538: if (audioEngineContext.status == 1) { nicholas@2538: audioEngineContext.stop(); nicholas@2538: this.innerHTML = 'Stop'; nickjillings@1341: var time = audioEngineContext.timer.getTestTime(); nickjillings@1341: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@2538: } nicholas@2538: }; nicholas@2538: // Create Submit (save) button nicholas@2538: var submit = document.createElement("button"); nicholas@2538: submit.innerHTML = 'Next'; nicholas@2538: submit.onclick = buttonSubmitClick; nicholas@2538: submit.id = 'submit-button'; nicholas@2538: // Append the interface buttons into the interfaceButtons object. nicholas@2538: interfaceButtons.appendChild(playback); nicholas@2538: interfaceButtons.appendChild(submit); nicholas@2538: nicholas@2538: var sliderHolder = document.createElement("div"); nicholas@2538: sliderHolder.id = "slider-holder"; nicholas@2538: nicholas@2396: // Create outside reference holder nicholas@2396: var outsideRef = document.createElement("div"); nicholas@2396: outsideRef.id = "outside-reference-holder"; nicholas@2538: nicholas@2538: // Global parent for the comment boxes on the page nicholas@2538: var feedbackHolder = document.createElement('div'); nicholas@2538: feedbackHolder.id = 'feedbackHolder'; nicholas@2538: nicholas@2538: testContent.style.zIndex = 1; nicholas@2538: interfaceContext.insertPoint.innerHTML = ""; // Clear the current schema nicholas@2538: nicholas@2538: // Inject into HTML nicholas@2538: testContent.appendChild(title); // Insert the title nicholas@2538: testContent.appendChild(interfaceButtons); nicholas@2396: testContent.appendChild(outsideRef); nicholas@2538: testContent.appendChild(sliderHolder); nicholas@2538: testContent.appendChild(feedbackHolder); nicholas@2538: interfaceContext.insertPoint.appendChild(testContent); nickjillings@1341: nicholas@2538: // Load the full interface nicholas@2538: testState.initialise(); nicholas@2538: testState.advanceState(); nicholas@2538: nickjillings@1341: } nickjillings@1341: nicholas@2538: function loadTest(audioHolderObject) { nicholas@2538: var width = window.innerWidth; nicholas@2538: var height = window.innerHeight; nicholas@2538: var id = audioHolderObject.id; nicholas@2538: nicholas@2538: interfaceContext.interfaceSliders = []; nicholas@2538: nicholas@2538: var feedbackHolder = document.getElementById('feedbackHolder'); nicholas@2538: var sliderHolder = document.getElementById('slider-holder'); nicholas@2538: feedbackHolder.innerHTML = ""; nicholas@2538: sliderHolder.innerHTML = ""; nicholas@2538: nicholas@2596: // Set labelType if default to number nicholas@2696: if (audioHolderObject.label === "default" || audioHolderObject.label === "") { nicholas@2596: audioHolderObject.label = "number"; nicholas@2596: } nicholas@2470: // Set the page title nicholas@2470: if (typeof audioHolderObject.title == "string" && audioHolderObject.title.length > 0) { nicholas@2696: document.getElementById("test-title").textContent = audioHolderObject.title; nicholas@2470: } nicholas@2538: nicholas@2538: nicholas@2538: // Delete outside reference nicholas@2538: document.getElementById("outside-reference-holder").innerHTML = ""; nicholas@2538: nicholas@2651: var interfaceObj = interfaceContext.getCombinedInterfaces(audioHolderObject); nicholas@2696: interfaceObj.forEach(function (interfaceObjectInstance) { nicholas@2538: // Create the div box to center align n@2795: interfaceContext.interfaceSliders.push(new interfaceSliderHolder(interfaceObjectInstance, audioHolderObject)); nicholas@2696: }); nicholas@2651: interfaceObj.forEach(function (interface) { nicholas@2696: interface.options.forEach(function (option) { nicholas@2651: if (option.type == "show") { nicholas@2651: switch (option.name) { nicholas@2651: case "playhead": nicholas@2651: var playbackHolder = document.getElementById('playback-holder'); nicholas@2696: if (playbackHolder === null) { nicholas@2651: playbackHolder = document.createElement('div'); nicholas@2651: playbackHolder.style.width = "100%"; nicholas@2651: playbackHolder.align = 'center'; nicholas@2651: playbackHolder.appendChild(interfaceContext.playhead.object); nicholas@2651: feedbackHolder.insertBefore(playbackHolder, feedbackHolder.firstElementChild); nicholas@2651: } nicholas@2651: break; nicholas@2651: case "page-count": nicholas@2651: var pagecountHolder = document.getElementById('page-count'); nicholas@2696: if (pagecountHolder === null) { nicholas@2651: pagecountHolder = document.createElement('div'); nicholas@2651: pagecountHolder.id = 'page-count'; nicholas@2651: } nicholas@2651: pagecountHolder.innerHTML = 'Page ' + (testState.stateIndex + 1) + ' of ' + testState.stateMap.length + ''; nicholas@2651: var inject = document.getElementById('interface-buttons'); nicholas@2651: inject.appendChild(pagecountHolder); nicholas@2651: break; nicholas@2651: case "volume": nicholas@2696: if (document.getElementById('master-volume-holder') === null) { nicholas@2651: feedbackHolder.appendChild(interfaceContext.volume.object); nicholas@2651: } nicholas@2651: break; nicholas@2651: case "comments": nicholas@2651: interfaceContext.commentBoxes.showCommentBoxes(feedbackHolder, true); nicholas@2651: break; nickjillings@1356: } nickjillings@1356: } nicholas@2696: }); nicholas@2651: }); nickjillings@1341: nicholas@2538: var commentBoxPrefix = "Comment on fragment"; nicholas@2538: nicholas@2538: var commentShow = audioHolderObject.elementComments; nicholas@2538: nicholas@2538: var loopPlayback = audioHolderObject.loop; nicholas@2538: nicholas@2696: var currentTestHolder = document.createElement('audioHolder'); nicholas@2538: currentTestHolder.id = audioHolderObject.id; nicholas@2538: currentTestHolder.repeatCount = audioHolderObject.repeatCount; nicholas@2538: nicholas@2538: // Find all the audioElements from the audioHolder nicholas@2538: $(audioHolderObject.audioElements).each(function (index, element) { nicholas@2538: // Find URL of track nicholas@2538: // In this jQuery loop, variable 'this' holds the current audioElement. nicholas@2538: var audioObject = audioEngineContext.newTrack(element); nicholas@2538: // Check if an outside reference nicholas@2538: if (element.type == 'outside-reference') { nicholas@2538: // Construct outside reference; nicholas@2538: var orNode = new outsideReferenceDOM(audioObject, index, document.getElementById("outside-reference-holder")); nicholas@2538: audioObject.bindInterface(orNode); nicholas@2538: } else { nicholas@2538: // Create a slider per track nicholas@2538: var sliderNode = new sliderObject(audioObject, interfaceObj, index); nicholas@2538: audioObject.bindInterface(sliderNode); nickjillings@2117: interfaceContext.commentBoxes.createCommentBox(audioObject); nicholas@2538: } nicholas@2538: }); nicholas@2538: nicholas@2538: // Initialse the interfaceSlider object metrics nicholas@2538: nicholas@2538: $('.track-slider').mousedown(function (event) { nicholas@2538: interfaceContext.selectObject($(this)[0]); nicholas@2538: }); nicholas@2538: $('.track-slider').on('touchstart', null, function (event) { nicholas@2538: interfaceContext.selectObject($(this)[0]); nicholas@2538: }); nicholas@2538: nicholas@2538: $('.track-slider').mousemove(function (event) { nicholas@2538: event.preventDefault(); nicholas@2538: }); nicholas@2538: nicholas@2538: $('.slider').mousemove(function (event) { nicholas@2538: event.preventDefault(); nicholas@2538: var obj = interfaceContext.getSelectedObject(); nicholas@2696: if (obj === null) { nicholas@2538: return; nicholas@2538: } nicholas@2538: var move = event.clientX - 6; nicholas@2459: var w = $(event.currentTarget).width(); nicholas@2538: move = Math.max(50, move); nicholas@2538: move = Math.min(w + 50, move); nicholas@2538: $(obj).css("left", move + "px"); nicholas@2538: interfaceContext.moveObject(); nicholas@2538: }); nicholas@2538: nicholas@2538: $('.slider').on('touchmove', null, function (event) { nicholas@2538: event.preventDefault(); nicholas@2538: var obj = interfaceContext.getSelectedObject(); nicholas@2696: if (obj === null) { nicholas@2538: return; nicholas@2538: } nicholas@2538: var move = event.originalEvent.targetTouches[0].clientX - 6; nicholas@2459: var w = $(event.currentTarget).width(); nicholas@2538: move = Math.max(50, move); nicholas@2538: move = Math.min(w + 50, move); nicholas@2538: $(obj).css("left", move + "px"); nicholas@2538: interfaceContext.moveObject(); nicholas@2538: }); nickjillings@1341: nicholas@2538: $(document).mouseup(function (event) { nicholas@2538: event.preventDefault(); nicholas@2538: var obj = interfaceContext.getSelectedObject(); nicholas@2696: if (obj === null) { nicholas@2538: return; nicholas@2538: } nicholas@2538: var interfaceID = obj.parentElement.getAttribute("interfaceid"); nicholas@2538: var trackID = obj.getAttribute("trackindex"); nicholas@2696: var id; nicholas@2696: if (interfaceContext.hasSelectedObjectMoved() === true) { nicholas@2538: var l = $(obj).css("left"); nicholas@2696: id = obj.getAttribute('trackIndex'); nicholas@2538: var time = audioEngineContext.timer.getTestTime(); nicholas@2538: var rate = convSliderPosToRate(obj); nicholas@2538: audioEngineContext.audioObjects[id].metric.moved(time, rate); nicholas@2538: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time, rate); nicholas@2538: console.log("slider " + id + " moved to " + rate + ' (' + time + ')'); nicholas@2538: obj.setAttribute("slider-value", convSliderPosToRate(obj)); nicholas@2538: } else { nicholas@2696: id = Number(obj.attributes.trackIndex.value); nicholas@2538: //audioEngineContext.metric.sliderPlayed(id); nicholas@2538: audioEngineContext.play(id); nicholas@2538: } nicholas@2538: interfaceContext.releaseObject(); nicholas@2538: }); nicholas@2538: nicholas@2538: $('.slider').on('touchend', null, function (event) { nicholas@2538: var obj = interfaceContext.getSelectedObject(); nicholas@2696: if (obj === null) { nicholas@2538: return; nicholas@2538: } nicholas@2538: var interfaceID = obj.parentElement.getAttribute("interfaceid"); nicholas@2538: var trackID = obj.getAttribute("trackindex"); nicholas@2696: if (interfaceContext.hasSelectedObjectMoved() === true) { nicholas@2538: var l = $(obj).css("left"); nicholas@2538: var id = obj.getAttribute('trackIndex'); nicholas@2538: var time = audioEngineContext.timer.getTestTime(); nicholas@2538: var rate = convSliderPosToRate(obj); nicholas@2538: audioEngineContext.audioObjects[id].metric.moved(time, rate); nicholas@2538: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time, rate); nicholas@2538: console.log("slider " + id + " moved to " + rate + ' (' + time + ')'); nicholas@2538: } nicholas@2538: interfaceContext.releaseObject(); nicholas@2538: }); nicholas@2538: n@2407: var interfaceList = audioHolderObject.interfaces.concat(specification.interfaces); nicholas@2538: for (var k = 0; k < interfaceList.length; k++) { nicholas@2538: for (var i = 0; i < interfaceList[k].options.length; i++) { nicholas@2538: if (interfaceList[k].options[i].type == 'show' && interfaceList[k].options[i].name == 'playhead') { n@2407: var playbackHolder = document.getElementById('playback-holder'); nicholas@2696: if (playbackHolder === null) { n@2407: playbackHolder = document.createElement('div'); n@2407: playbackHolder.id = "playback-holder"; n@2407: playbackHolder.style.width = "100%"; n@2407: playbackHolder.align = 'center'; n@2407: playbackHolder.appendChild(interfaceContext.playhead.object); n@2407: feedbackHolder.appendChild(playbackHolder); n@2407: } nicholas@2538: } else if (interfaceList[k].options[i].type == 'show' && interfaceList[k].options[i].name == 'page-count') { n@2407: var pagecountHolder = document.getElementById('page-count'); nicholas@2696: if (pagecountHolder === null) { n@2407: pagecountHolder = document.createElement('div'); n@2407: pagecountHolder.id = 'page-count'; n@2407: } nicholas@2538: pagecountHolder.innerHTML = 'Page ' + (testState.stateIndex + 1) + ' of ' + testState.stateMap.length + ''; n@2407: var inject = document.getElementById('interface-buttons'); n@2407: inject.appendChild(pagecountHolder); n@2407: } else if (interfaceList[k].options[i].type == 'show' && interfaceList[k].options[i].name == 'volume') { nicholas@2696: if (document.getElementById('master-volume-holder') === null) { n@2407: feedbackHolder.appendChild(interfaceContext.volume.object); n@2407: } n@2407: } else if (interfaceList[k].options[i].type == 'show' && interfaceList[k].options[i].name == 'comments') { nicholas@2538: interfaceContext.commentBoxes.showCommentBoxes(feedbackHolder, true); n@2407: break; n@2407: } n@2407: } n@2407: } nicholas@2538: nicholas@2538: $(audioHolderObject.commentQuestions).each(function (index, element) { nicholas@2538: var node = interfaceContext.createCommentQuestion(element); nicholas@2538: feedbackHolder.appendChild(node.holder); nicholas@2538: }); nicholas@2538: nicholas@2538: //testWaitIndicator(); nickjillings@1341: } nickjillings@1341: n@2795: function interfaceSliderHolder(interfaceObject, page) { nicholas@2538: this.sliders = []; nicholas@2538: this.metrics = []; nicholas@2538: this.id = document.getElementsByClassName("sliderCanvasDiv").length; nicholas@2538: this.name = interfaceObject.name; nicholas@2538: this.interfaceObject = interfaceObject; nicholas@2538: this.sliderDOM = document.createElement('div'); nicholas@2538: this.sliderDOM.className = 'sliderCanvasDiv'; nicholas@2538: this.sliderDOM.id = 'sliderCanvasHolder-' + this.id; nicholas@2782: this.imageHolder = (function () { nicholas@2782: var imageController = {}; nicholas@2782: imageController.root = document.createElement("div"); nicholas@2782: imageController.root.className = "imageController"; nicholas@2782: imageController.img = document.createElement("img"); nicholas@2782: imageController.root.appendChild(imageController.img); nicholas@2782: imageController.setImage = function (src) { nicholas@2782: imageController.img.src = ""; n@2795: if (typeof src !== "string" || src.length === undefined) { nicholas@2782: return; nicholas@2782: } nicholas@2782: imageController.img.src = src; n@2795: }; nicholas@2782: return imageController; nicholas@2782: })(); nicholas@2538: nicholas@2538: var pagetitle = document.createElement('div'); nicholas@2538: pagetitle.className = "pageTitle"; nicholas@2538: pagetitle.align = "center"; nicholas@2538: var titleSpan = document.createElement('span'); nicholas@2538: titleSpan.id = "pageTitle-" + this.id; nicholas@2696: if (interfaceObject.title !== undefined && typeof interfaceObject.title == "string") { nicholas@2538: titleSpan.textContent = interfaceObject.title; nicholas@2538: } else { nicholas@2538: titleSpan.textContent = "Axis " + String(this.id + 1); nicholas@2538: } nicholas@2538: pagetitle.appendChild(titleSpan); nicholas@2538: this.sliderDOM.appendChild(pagetitle); nicholas@2538: n@2795: if (interfaceObject.image !== undefined || page.audioElements.some(function (a) { n@2795: return a.image !== undefined; n@2795: })) { nicholas@2782: this.sliderDOM.appendChild(this.imageHolder.root); nicholas@2782: this.imageHolder.setImage(interfaceObject.image); nicholas@2782: } nicholas@2538: // Create the slider box to hold the slider elements nicholas@2538: this.canvas = document.createElement('div'); nicholas@2696: if (this.name !== undefined) nicholas@2538: this.canvas.id = 'slider-' + this.name; nicholas@2538: else nicholas@2538: this.canvas.id = 'slider-' + this.id; nicholas@2538: this.canvas.setAttribute("interfaceid", this.id); nicholas@2538: this.canvas.className = 'slider'; nicholas@2538: this.canvas.align = "left"; nicholas@2538: this.canvas.addEventListener('dragover', function (event) { nicholas@2538: event.preventDefault(); nicholas@2538: event.dataTransfer.effectAllowed = 'none'; nicholas@2538: event.dataTransfer.dropEffect = 'copy'; nicholas@2538: return false; nicholas@2538: }, false); nicholas@2538: this.sliderDOM.appendChild(this.canvas); nicholas@2538: nicholas@2538: // Create the div to hold any scale objects nicholas@2538: this.scale = document.createElement('div'); nicholas@2538: this.scale.className = 'sliderScale'; nicholas@2538: this.scale.id = 'sliderScaleHolder-' + this.id; nicholas@2538: this.scale.align = 'left'; nicholas@2538: this.sliderDOM.appendChild(this.scale); nicholas@2538: var positionScale = this.canvas.style.width.substr(0, this.canvas.style.width.length - 2); nicholas@2538: var offset = 50; nickjillings@1317: var dest = document.getElementById("slider-holder").appendChild(this.sliderDOM); nicholas@2696: interfaceObject.scales.forEach(function (scaleObj) { nicholas@2538: var position = Number(scaleObj.position) * 0.01; nicholas@2538: var pixelPosition = (position * $(this.canvas).width()) + offset; nicholas@2538: var scaleDOM = document.createElement('span'); nicholas@2391: scaleDOM.className = "ape-marker-text"; nicholas@2538: scaleDOM.textContent = scaleObj.text; nicholas@2696: scaleDOM.setAttribute('value', position); nicholas@2538: this.scale.appendChild(scaleDOM); nicholas@2538: scaleDOM.style.left = Math.floor((pixelPosition - ($(scaleDOM).width() / 2))) + 'px'; nicholas@2696: }, this); nicholas@2538: nicholas@2538: this.createSliderObject = function (audioObject, label) { nicholas@2538: var trackObj = document.createElement('div'); nickjillings@1317: trackObj.align = "center"; nicholas@2538: trackObj.className = 'track-slider track-slider-disabled track-slider-' + audioObject.id; nicholas@2538: trackObj.id = 'track-slider-' + this.id + '-' + audioObject.id; nicholas@2538: trackObj.setAttribute('trackIndex', audioObject.id); nicholas@2696: if (this.name !== undefined) { nicholas@2538: trackObj.setAttribute('interface-name', this.name); nicholas@2538: } else { nicholas@2538: trackObj.setAttribute('interface-name', this.id); nicholas@2538: } nicholas@2538: var offset = 50; nicholas@2538: // Distribute it randomnly nicholas@2538: var w = window.innerWidth - (offset + 8) * 2; nicholas@2538: w = Math.random() * w; nicholas@2538: w = Math.floor(w + (offset + 8)); nicholas@2538: trackObj.style.left = w + 'px'; nicholas@2538: this.canvas.appendChild(trackObj); nicholas@2538: this.sliders.push(trackObj); nicholas@2538: this.metrics.push(new metricTracker(this)); nicholas@2538: var labelHolder = document.createElement("span"); nickjillings@2168: labelHolder.textContent = label; nickjillings@2168: trackObj.appendChild(labelHolder); nicholas@2391: var rate = convSliderPosToRate(trackObj); nicholas@2538: this.metrics[this.metrics.length - 1].initialise(rate); nicholas@2538: trackObj.setAttribute("slider-value", rate); nicholas@2538: return trackObj; nicholas@2538: }; nicholas@2538: nicholas@2538: this.resize = function (event) { nicholas@2538: var sliderDiv = this.canvas; nicholas@2538: var sliderScaleDiv = this.scale; nicholas@2538: var width = $(sliderDiv).width(); nicholas@2391: var marginsize = 50; nicholas@2538: // Move sliders into new position nicholas@2696: this.sliders.forEach(function (slider, index) { nicholas@2696: var pix = Number(slider.getAttribute("slider-value")) * width; nicholas@2696: slider.style.left = (pix + marginsize) + 'px'; nicholas@2696: }); nicholas@2538: nicholas@2538: // Move scale labels nicholas@2538: for (var index = 0; index < this.scale.children.length; index++) { nicholas@2538: var scaleObj = this.scale.children[index]; nicholas@2696: var position = Number(scaleObj.attributes.value.value); nicholas@2538: var pixelPosition = (position * width) + marginsize; nicholas@2538: scaleObj.style.left = Math.floor((pixelPosition - ($(scaleObj).width() / 2))) + 'px'; nicholas@2538: } nicholas@2538: }; n@2796: n@2796: this.playing = function (id) { n@2796: var node = audioEngineContext.audioObjects.find(function (a) { n@2796: return a.id == id; n@2796: }); n@2796: if (node === undefined) { n@2796: this.imageHolder.setImage(interfaceObject.image || ""); n@2796: return; n@2796: } n@2796: var imgurl = node.specification.image || interfaceObject.image || ""; n@2796: this.imageHolder.setImage(imgurl); n@2796: } nickjillings@1341: } nickjillings@1341: nicholas@2538: function sliderObject(audioObject, interfaceObjects, index) { nicholas@2538: // Create a new slider object; nicholas@2538: this.parent = audioObject; nicholas@2538: this.trackSliderObjects = []; nicholas@2596: this.label = interfaceContext.getLabel(audioObject.specification.parent.label, index, audioObject.specification.parent.labelStart); n@2428: this.playing = false; nicholas@2538: for (var i = 0; i < interfaceContext.interfaceSliders.length; i++) { nicholas@2538: var trackObj = interfaceContext.interfaceSliders[i].createSliderObject(audioObject, this.label); nicholas@2538: this.trackSliderObjects.push(trackObj); nicholas@2538: } nickjillings@1341: nicholas@2538: // Onclick, switch playback to that track nicholas@2538: nicholas@2538: this.enable = function () { nicholas@2538: if (this.parent.state == 1) { nicholas@2538: $(this.trackSliderObjects).each(function (i, trackObj) { nicholas@2538: $(trackObj).removeClass('track-slider-disabled'); nicholas@2538: }); nicholas@2538: } nicholas@2538: }; nicholas@2538: this.updateLoading = function (progress) { nicholas@2538: if (progress != 100) { nicholas@2538: progress = String(progress); nicholas@2538: progress = progress.split('.')[0]; nicholas@2538: this.trackSliderObjects[0].children[0].textContent = progress + '%'; nicholas@2538: } else { nicholas@2538: this.trackSliderObjects[0].children[0].textContent = this.label; nicholas@2538: } nicholas@2538: }; nicholas@2538: this.startPlayback = function () { nickjillings@1360: $('.track-slider').removeClass('track-slider-playing'); nicholas@2538: var name = ".track-slider-" + this.parent.id; nickjillings@1360: $(name).addClass('track-slider-playing'); nicholas@2726: interfaceContext.commentBoxes.highlightById(audioObject.id); n@2428: $('.outside-reference').removeClass('track-slider-playing'); n@2428: this.playing = true; nicholas@2538: n@2428: if (this.parent.specification.parent.playOne || specification.playOne) { n@2428: $('.track-slider').addClass('track-slider-disabled'); n@2428: $('.outside-reference').addClass('track-slider-disabled'); n@2428: } n@2796: interfaceContext.interfaceSliders.forEach(function (ts) { n@2796: ts.playing(this.parent.id); n@2796: }, this); nickjillings@1360: }; nicholas@2538: this.stopPlayback = function () { n@2428: if (this.playing) { n@2428: this.playing = false; nicholas@2538: var name = ".track-slider-" + this.parent.id; n@2428: $(name).removeClass('track-slider-playing'); n@2428: $('.track-slider').removeClass('track-slider-disabled'); n@2428: $('.outside-reference').removeClass('track-slider-disabled'); nicholas@2726: var box = interfaceContext.commentBoxes.boxes.find(function (a) { nicholas@2726: return a.id === audioObject.id; nicholas@2726: }); nicholas@2726: if (box) { nicholas@2726: box.highlight(false); nicholas@2726: } n@2428: } nickjillings@1360: }; nicholas@2538: this.exportXMLDOM = function (audioObject) { nicholas@2538: // Called by the audioObject holding this element. Must be present nicholas@2538: var obj = []; nicholas@2538: $(this.trackSliderObjects).each(function (i, trackObj) { nicholas@2538: var node = storage.document.createElement('value'); nicholas@2538: node.setAttribute("interface-name", trackObj.getAttribute("interface-name")); nicholas@2538: node.textContent = convSliderPosToRate(trackObj); nicholas@2538: obj.push(node); nicholas@2538: }); nicholas@2538: nicholas@2538: return obj; nicholas@2538: }; nicholas@2538: this.getValue = function () { nicholas@2538: return convSliderPosToRate(this.trackSliderObjects[0]); nicholas@2538: }; nicholas@2538: this.getPresentedId = function () { nicholas@2538: return this.label; nicholas@2538: }; nicholas@2538: this.canMove = function () { nicholas@2538: return true; nicholas@2538: }; nicholas@2538: this.error = function () { nicholas@2538: // audioObject has an error!! nickjillings@2113: this.playback.textContent = "Error"; nickjillings@2113: $(this.playback).addClass("error-colour"); nicholas@2696: }; nickjillings@1341: } nickjillings@1341: nicholas@2538: function outsideReferenceDOM(audioObject, index, inject) { nicholas@2538: this.parent = audioObject; nicholas@2538: this.outsideReferenceHolder = document.createElement('div'); nicholas@2538: this.outsideReferenceHolder.id = 'outside-reference'; nicholas@2538: this.outsideReferenceHolder.className = 'outside-reference track-slider-disabled'; nicholas@2538: var outsideReferenceHolderspan = document.createElement('span'); nicholas@2538: outsideReferenceHolderspan.textContent = 'Reference'; nicholas@2538: this.outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nicholas@2538: this.outsideReferenceHolder.setAttribute('track-id', index); nicholas@2538: nicholas@2696: this.handleEvent = function (event) { nicholas@2696: audioEngineContext.play(audioObject.id); nicholas@2538: $('.track-slider').removeClass('track-slider-playing'); nickjillings@1341: $('.comment-div').removeClass('comment-box-playing'); nicholas@2696: $(this.outsideReferenceHolder).addClass('track-slider-playing'); nicholas@2538: }; nicholas@2696: this.outsideReferenceHolder.addEventListener("click", this.handleEvent); nicholas@2538: inject.appendChild(this.outsideReferenceHolder); nicholas@2538: this.enable = function () { nicholas@2538: if (this.parent.state == 1) { nicholas@2538: $(this.outsideReferenceHolder).removeClass('track-slider-disabled'); nicholas@2538: } nicholas@2538: }; nicholas@2538: this.updateLoading = function (progress) { nicholas@2538: if (progress != 100) { nicholas@2538: progress = String(progress); nicholas@2538: progress = progress.split('.')[0]; nicholas@2538: this.outsideReferenceHolder.firstChild.textContent = progress + '%'; nicholas@2538: } else { nicholas@2538: this.outsideReferenceHolder.firstChild.textContent = "Play Reference"; nicholas@2538: } nicholas@2538: }; nicholas@2538: this.startPlayback = function () { nickjillings@1360: $('.track-slider').removeClass('track-slider-playing'); nickjillings@1360: $(this.outsideReferenceHolder).addClass('track-slider-playing'); nickjillings@1360: $('.comment-div').removeClass('comment-box-playing'); nickjillings@1360: }; nicholas@2538: this.stopPlayback = function () { nickjillings@1360: $(this.outsideReferenceHolder).removeClass('track-slider-playing'); nickjillings@1360: }; nicholas@2538: this.exportXMLDOM = function (audioObject) { nicholas@2538: return null; nicholas@2538: }; nicholas@2538: this.getValue = function () { nicholas@2538: return 0; nicholas@2538: }; nicholas@2538: this.getPresentedId = function () { nicholas@2538: return 'reference'; nicholas@2538: }; nicholas@2538: this.canMove = function () { nicholas@2538: return false; nicholas@2538: }; nicholas@2538: this.error = function () { nicholas@2538: // audioObject has an error!! nickjillings@2113: this.outsideReferenceHolder.textContent = "Error"; nickjillings@2113: $(this.outsideReferenceHolder).addClass("error-colour"); nicholas@2696: }; nickjillings@1341: } nickjillings@1341: nicholas@2538: function buttonSubmitClick() { nicholas@2651: var checks = testState.currentStateMap.interfaces[0].options, nicholas@2651: canContinue = true; nickjillings@1341: nicholas@2538: // Check that the anchor and reference objects are correctly placed nicholas@2696: if (interfaceContext.checkHiddenAnchor() === false) { nicholas@2538: return; nicholas@2538: } nicholas@2696: if (interfaceContext.checkHiddenReference() === false) { nicholas@2538: return; nicholas@2538: } nicholas@2538: nicholas@2538: for (var i = 0; i < checks.length; i++) { n@2795: var checkState = true; nicholas@2538: if (checks[i].type == 'check') { nicholas@2538: switch (checks[i].name) { nicholas@2538: case 'fragmentPlayed': nicholas@2538: // Check if all fragments have been played n@2790: checkState = interfaceContext.checkAllPlayed(checks[i].errorMessage); nicholas@2538: break; nicholas@2538: case 'fragmentFullPlayback': nicholas@2538: // Check all fragments have been played to their full length n@2790: checkState = interfaceContext.checkFragmentsFullyPlayed(checks[i].errorMessage); nicholas@2538: break; nicholas@2538: case 'fragmentMoved': nicholas@2538: // Check all fragment sliders have been moved. n@2790: checkState = interfaceContext.checkAllMoved(checks[i].errorMessage); nicholas@2538: break; nicholas@2538: case 'fragmentComments': nicholas@2538: // Check all fragment sliders have been moved. n@2790: checkState = interfaceContext.checkAllCommented(checks[i].errorMessage); nicholas@2538: break; nicholas@2538: case 'scalerange': nicholas@2538: // Check the scale is used to its full width outlined by the node n@2790: checkState = interfaceContext.checkScaleRange(checks[i].errorMessage); nicholas@2538: break; nicholas@2538: default: nicholas@2538: console.log("WARNING - Check option " + checks[i].name + " is not supported on this interface"); nicholas@2538: break; nicholas@2538: } nicholas@2538: nicholas@2538: } nicholas@2703: if (checkState === false) { nicholas@2703: canContinue = false; nicholas@2538: break; nicholas@2538: } nicholas@2538: } nicholas@2538: nickjillings@1341: if (canContinue) { nicholas@2538: if (audioEngineContext.status == 1) { nicholas@2538: var playback = document.getElementById('playback-button'); nicholas@2538: playback.click(); nicholas@2538: // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options nicholas@2538: } else { nicholas@2696: if (audioEngineContext.timer.testStarted === false) { nicholas@2538: interfaceContext.lightbox.post("Warning", 'You have not started the test! Please click a fragment to begin the test!'); nicholas@2538: return; nicholas@2538: } nicholas@2538: } nicholas@2538: testState.advanceState(); nicholas@2538: } nickjillings@1341: } nickjillings@1341: nicholas@2538: function convSliderPosToRate(trackSlider) { nicholas@2538: var slider = trackSlider.parentElement; nicholas@2538: var maxPix = $(slider).width(); nicholas@2538: var marginsize = 50; nicholas@2538: var pix = trackSlider.style.left; nicholas@2538: pix = pix.substr(0, pix.length - 2); nicholas@2538: var rate = (pix - marginsize) / maxPix; nicholas@2538: return rate; nickjillings@1341: } nickjillings@1341: nicholas@2538: function resizeWindow(event) { nicholas@2538: // Function called when the window has been resized. nicholas@2538: // MANDATORY FUNCTION nicholas@2538: nicholas@2538: // Resize the slider objects nicholas@2538: for (var i = 0; i < interfaceContext.interfaceSliders.length; i++) { nicholas@2538: interfaceContext.interfaceSliders[i].resize(event); nicholas@2538: } nickjillings@1341: } nickjillings@1341: nicholas@2538: function pageXMLSave(store, pageSpecification) { nicholas@2538: // MANDATORY nicholas@2538: // Saves a specific test page nicholas@2538: // You can use this space to add any extra nodes to your XML saves nicholas@2538: // Get the current information in store (remember to appendChild your data to it) nicholas@2538: // pageSpecification is the current page node configuration nicholas@2538: // To create new XML nodes, use storage.document.createElement(); nicholas@2538: nicholas@2538: if (interfaceContext.interfaceSliders.length == 1) { nicholas@2538: // If there is only one axis, there only needs to be one metric return nicholas@2538: return; nicholas@2538: } nicholas@2538: var audioelements = store.getElementsByTagName("audioelement"); nicholas@2538: for (var i = 0; i < audioelements.length; i++) { nicholas@2538: // Have to append the metric specific nodes nicholas@2696: if (pageSpecification.outsideReference === undefined || pageSpecification.outsideReference.id != audioelements[i].id) { nicholas@2538: var inject = audioelements[i].getElementsByTagName("metric"); nicholas@2696: if (inject.length === 0) { nicholas@2538: inject = storage.document.createElement("metric"); nicholas@2538: } else { nicholas@2538: inject = inject[0]; nicholas@2538: } nicholas@2538: for (var k = 0; k < interfaceContext.interfaceSliders.length; k++) { nicholas@2538: var mrnodes = interfaceContext.interfaceSliders[k].metrics[i].exportXMLDOM(inject); nicholas@2538: for (var j = 0; j < mrnodes.length; j++) { nicholas@2538: var name = mrnodes[j].getAttribute("name"); nicholas@2538: if (name == "elementTracker" || name == "elementTrackerFull" || name == "elementInitialPosition" || name == "elementFlagMoved") { nicholas@2538: mrnodes[j].setAttribute("interface-name", interfaceContext.interfaceSliders[k].name); nicholas@2538: mrnodes[j].setAttribute("interface-id", k); nicholas@2538: } nicholas@2538: } nicholas@2538: } nicholas@2538: } nicholas@2538: } nicholas@2538: }