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();
n@3009: window.APE = undefined;
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: // 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
n@3009: window.APE = new ape();
nicholas@2538: testState.initialise();
nicholas@2538: testState.advanceState();
nickjillings@1341: }
nickjillings@1341:
nicholas@2538: function loadTest(audioHolderObject) {
n@3009: APE.clear();
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: // Delete outside reference
nicholas@2538: document.getElementById("outside-reference-holder").innerHTML = "";
nicholas@2538:
nicholas@2651: var interfaceObj = interfaceContext.getCombinedInterfaces(audioHolderObject);
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:
n@3009: APE.initialisePage(audioHolderObject);
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@3009: function ape() {
n@3009: var axis = []
n@3009: var DOMRoot = document.getElementById("slider-holder");
n@3009: var AOIs = [];
n@3009: var page = undefined;
n@3009:
n@3009: function audioObjectInterface(audioObject, parent) {
n@3009: // The audioObject communicates with this object
n@3009: var playing = false;
n@3009: var sliders = [];
n@3009: this.enable = function () {
n@3009: sliders.forEach(function (s) {
n@3009: s.enable();
n@3009: })
n@3009: }
n@3009:
n@3009: this.updateLoading = function (p) {
n@3009: sliders.forEach(function (s) {
n@3009: s.updateLoading(p);
n@3009: })
n@3009: }
n@3009:
n@3009: this.startPlayback = function () {
n@3009: playing = true;
n@3009: sliders.forEach(function (s) {
n@3009: s.playing();
n@3009: });
n@3009: }
n@3009:
n@3009: this.stopPlayback = function () {
n@3009: playing = false;
n@3009: sliders.forEach(function (s) {
n@3009: s.stopped();
n@3009: });
n@3009: }
n@3009:
n@3009: this.getValue = function () {
n@3009: return sliders[0].value();
n@3009: }
n@3009:
n@3009: this.getPresentedId = function () {
n@3009: return sliders[0].label;
n@3009: }
n@3009:
n@3009: this.canMove = function () {
n@3009: return true;
n@3009: }
n@3009:
n@3009: this.exportXMLDOM = function (audioObject) {
n@3009: var elements = [];
n@3009: sliders.forEach(function (s) {
n@3009: elements.push(s.exportXMLDOM());
n@3009: });
n@3009: return elements;
n@3009: }
n@3009:
n@3009: this.error = function () {
n@3009: sliders.forEach(function (s) {
n@3009: s.error();
n@3009: });
n@3009: }
n@3009:
n@3009: this.addSlider = function (s) {
n@3009: sliders.push(s);
n@3009: }
n@3009:
n@3009: this.clicked = function (event) {
n@3009: if (!playing) {
n@3009: audioEngineContext.play(audioObject.id);
n@3009: } else {
n@3009: audioEngineContext.stop();
n@3009: }
n@3009: playing = !playing;
n@3009: }
n@3009:
n@3009: this.pageXMLSave = function (store) {
n@3009: var inject = audioObject.storeDOM.getElementsByTagName("metric")[0];
n@3009: sliders.forEach(function (s) {
n@3009: s.pageXMLSave(inject);
n@3009: });
n@3009: }
n@3009:
n@3009: }
n@3009:
n@3009: function axisObject(interfaceObject, parent) {
n@3009:
n@3009: function sliderInterface(AOI, axisInterface) {
n@3009: var trackObj = document.createElement('div');
n@3009: var labelHolder = document.createElement("span");
n@3009: var label = "";
n@3009: var metric = new metricTracker(this);
n@3009: trackObj.align = "center";
n@3009: trackObj.className = 'track-slider track-slider-disabled';
n@3009: trackObj.appendChild(labelHolder);
n@3009: trackObj.style.left = (Math.random() * $(sliderRail).width()) - 50 + "px";
n@3009: axisInterface.sliderRail.appendChild(trackObj);
n@3009: metric.initialise(this.value);
n@3009: this.setLabel = function (s) {
n@3009: label = s;
n@3009: }
n@3009: this.resize = function (event) {
n@3009: var width = $(axisInterface.sliderRail).width();
n@3009: var w = Number(value * width + 50);
n@3009: trackObj.style.left = String(w) + "px";
n@3009: }
n@3009: this.playing = function () {
n@3009: trackObj.classList.add("track-slider-playing");
n@3009: }
n@3009: this.stopped = function () {
n@3009: trackObj.classList.remove("track-slider-playing");
n@3009: }
n@3009: this.enable = function () {
n@3009: trackObj.addEventListener("mousedown", this);
n@3009: trackObj.addEventListener("mouseup", this);
n@3009: trackObj.addEventListener("touchstart", this);
n@3009: trackObj.classList.remove("track-slider-disabled");
n@3009: labelHolder.textContent = label;
n@3009: }
n@3009: this.updateLoading = function (progress) {
n@3009: labelHolder.textContent = progress + "%";
n@3009: }
n@3009: this.exportXMLDOM = function () {
n@3009: var node = storage.document.createElement('value');
n@3009: node.setAttribute("interface-name", axisInterface.name)
n@3009: node.textContent = this.value();
n@3009: return node;
n@3009: }
n@3009: this.error = function () {
n@3009: trackObj.classList.add("error-colour");
n@3009: trackObj.removeEventListener("mousedown");
n@3009: trackObj.removeEventListener("mouseup");
n@3009: trackObj.removeEventListener("touchstart");
n@3009: }
n@3009: var timing = undefined;
n@3009: this.handleEvent = function (e) {
n@3009: // This is only for the mousedown / touchdown
n@3009: if (e.preventDefault) {
n@3009: e.preventDefault();
n@3009: }
n@3009: if (e.type == "mousedown" || e.type == "touchstart") {
n@3009: axisInterface.mousedown(this);
n@3009: } else if (e.type == "mouseup") {
n@3009: axisInterface.mouseup(this);
n@3009: }
n@3009: }
n@3009: this.clicked = function (e) {
n@3009: AOI.clicked();
n@3009: }
n@3009: this.pageXMLSave = function (inject) {
n@3009: var nodes = metric.exportXMLDOM(inject);
n@3009: nodes.forEach(function (elem) {
n@3009: var name = elem.getAttribute("name");
n@3009: if (name == "elementTracker" || name == "elementTrackerFull" || name == "elementInitialPosition" || name == "elementFlagMoved") {
n@3009: mrnodes[j].setAttribute("interface-name", axisInterface.name);
n@3009: }
n@3009: });
n@3009: }
n@3009: Object.defineProperties(this, {
n@3009: "DOM": {
n@3009: "value": trackObj
n@3009: },
n@3009: "value": {
n@3009: "value": function () {
n@3009: var maxPix = $(axisInterface.sliderRail).width();
n@3009: var pix = trackObj.style.left.substr(0, trackObj.style.left.length - 2);
n@3009: return (pix - 50) / maxPix;
n@3009: }
n@3009: },
n@3009: "moveToPixel": {
n@3009: "value": function (pix) {
n@3009: var t = audioEngineContext.timer.getTestTime();
n@3009: trackObj.style.left = String(pix) + "px";
n@3009: metric.moved(t, this.value);
n@3009: }
n@3009: },
n@3009: "label": {
n@3009: "get": function () {
n@3009: return label;
n@3009: },
n@3009: "set": function () {}
n@3009: }
n@3009: });
n@3009: }
n@3009:
n@3009: function createScaleMarkers(interfaceObject, root, w) {
n@3009: interfaceObject.scales.forEach(function (scaleObj) {
n@3009: var position = Number(scaleObj.position) * 0.01;
n@3009: var pixelPosition = (position * w) + 50;
n@3009: var scaleDOM = document.createElement('span');
n@3009: scaleDOM.className = "ape-marker-text";
n@3009: scaleDOM.textContent = scaleObj.text;
n@3009: scaleDOM.setAttribute('value', position);
n@3009: root.appendChild(scaleDOM);
n@3009: scaleDOM.style.left = Math.floor((pixelPosition - ($(scaleDOM).width() / 2))) + 'px';
n@3009: }, this);
n@3009: }
n@3009: var sliders = [];
n@3009: var UI = {
n@3009: selected: undefined,
n@3009: startTime: undefined
n@3009: }
n@3009: this.name = interfaceObject.name;
n@3009: var DOMRoot = document.createElement("div");
n@3009: DOMRoot.className = "sliderCanvasDiv";
n@3009: DOMRoot.id = "sliderCanvasHolder-" + this.name;
n@3009: var sliders = [];
n@3009:
n@3009: var axisTitle = document.createElement("div");
n@3009: axisTitle.className = "pageTitle";
n@3009: axisTitle.align = "center";
n@3009: var titleSpan = document.createElement('span');
n@3009: titleSpan.id = "pageTitle-" + this.name;
n@3009: if (interfaceObject.title !== undefined && typeof interfaceObject.title == "string") {
n@3009: titleSpan.textContent = interfaceObject.title;
n@3009: } else {
n@3009: titleSpan.textContent = "Axis " + String(this.id + 1);
n@3009: }
n@3009: axisTitle.appendChild(titleSpan);
n@3009: DOMRoot.appendChild(axisTitle);
n@3009:
n@3009: var imageHolder = (function () {
n@3009: var imageController = {};
n@3009: imageController.root = document.createElement("div");
n@3009: imageController.root.className = "imageController";
n@3009: imageController.img = document.createElement("img");
n@3009: imageController.root.appendChild(imageController.img);
n@3009: imageController.setImage = function (src) {
n@3009: imageController.img.src = "";
n@3009: if (typeof src !== "string" || src.length === undefined) {
n@3009: return;
n@3009: }
n@3009: imageController.img.src = src;
n@3009: };
n@3009: return imageController;
n@3009: })();
n@3009: if (interfaceObject.image !== undefined || page.audioElements.some(function (a) {
n@3009: return a.image !== undefined;
n@3009: })) {
n@3009: DOMRoot.appendChild(imageHolder.root);
n@3009: imageHolder.setImage(interfaceObject.image);
n@3009: }
n@3009:
n@3009: // Now create the slider box to hold the fragment sliders
n@3009: var sliderRail = document.createElement("div");
n@3009: sliderRail.id = "sliderrail-" + this.name;
n@3009: sliderRail.className = "slider";
n@3009: sliderRail.align = "left";
n@3009: DOMRoot.appendChild(sliderRail);
n@3009:
n@3009: // Create the div to hold any scale objects
n@3009: var scale = document.createElement("div");
n@3009: scale.className = "sliderScale";
n@3009: scale.id = "slider-scale-holder-" + this.name;
n@3009: scale.slign = "left";
n@3009: DOMRoot.appendChild(scale);
n@3009: createScaleMarkers(interfaceObject, scale, $(sliderRail).width());
n@3009:
n@3009: parent.getDOMRoot().appendChild(DOMRoot);
n@3009:
n@3009: this.resize = function (event) {
n@3009: var w = $(sliderRail).width();
n@3009: var marginsize = 50;
n@3009: sliders.forEach(function (s) {
n@3009: s.resize();
n@3009: });
n@3009: scale.innerHTML = "";
n@3009: createScaleMarkers(interfaceObject, scale, $(sliderRail).width());
n@3009: }
n@3009: this.playing = function (id) {
n@3009: var node = audioEngineContext.audioObjects.find(function (a) {
n@3009: return a.id == id;
n@3009: });
n@3009: if (node === undefined) {
n@3009: this.imageHolder.setImage(interfaceObject.image || "");
nicholas@2782: return;
nicholas@2782: }
n@3009: var imgurl = node.specification.image || interfaceObject.image || "";
n@3009: this.imageHolder.setImage(imgurl);
nicholas@2538: }
n@3009: this.stopped = function () {
n@3009: var imgurl = interfaceObject.image || "";
n@3009: this.imageHolder.setImage(imgurl);
nicholas@2538: }
n@3009: this.addSlider = function (aoi) {
n@3009: var node = new sliderInterface(aoi, this);
n@3009: sliders.push(node);
n@3009: return node;
n@2796: }
n@3009: this.mousedown = function (sliderUI) {
n@3009: UI.selected = sliderUI;
n@3009: UI.startTime = new Date();
nicholas@2538: }
n@3009: this.mouseup = function (sliderUI) {
n@3009: var delta = new Date() - UI.startTime;
n@3009: if (delta < 200) {
n@3009: UI.selected.clicked();
n@3009: }
n@3009: UI.selected = undefined;
n@3009: UI.startTime = undefined;
nicholas@2538: }
n@3009: this.handleEvent = function (event) {
n@3009: if (event.preventDefault) {
n@3009: event.preventDefault();
n@3009: }
n@3009: if (UI.selected === undefined) {
n@3009: return;
n@3009: }
n@3009: if (event.type == "mousemove") {
n@3009: var move = event.clientX - 6;
n@3009: var w = $(sliderRail).width();
n@3009: move = Math.max(50, move);
n@3009: move = Math.min(w + 50, move);
n@3009: UI.selected.moveToPixel(move);
n@3009: } else if (event.type == "touchmove") {
n@3009: var move = event.originalEvent.targetTouches[0].clientX - 6;
n@3009: var w = $(event.currentTarget).width();
n@3009: move = Math.max(50, move);
n@3009: move = Math.min(w + 50, move);
n@3009: UI.selected.moveToPixel(move);
nicholas@2726: }
n@2428: }
n@3009: sliderRail.addEventListener("mousemove", this);
n@3009: sliderRail.addEventListener("touchmove", this);
n@3009: Object.defineProperties(this, {
n@3009: "sliderRail": {
n@3009: "value": sliderRail
nicholas@2820: }
nicholas@2538: });
n@3009: }
n@3009: this.getDOMRoot = function () {
n@3009: return DOMRoot;
n@3009: }
n@3009: this.getPage = function () {
n@3009: return page;
n@3009: }
n@3009: this.clear = function () {
n@3009: page = undefined;
n@3009: axis = [];
n@3009: AOIs = [];
n@3009: DOMRoot.innerHTML = "";
n@3009: }
n@3009: this.initialisePage = function (page_init) {
n@3009: this.clear();
n@3009: page = page_init;
n@3009: var interfaceObj = interfaceContext.getCombinedInterfaces(page);
n@3009: // Create each of the interface axis
n@3009: interfaceObj.forEach(function (i) {
n@3009: var node = new axisObject(i, this);
n@3009: axis.push(node);
n@3009: }, this);
nicholas@2538:
n@3009: // Create the audioObject interface objects for each aO.
n@3009: page.audioElements.forEach(function (element, index) {
n@3009: var audioObject = audioEngineContext.newTrack(element);
n@3009: if (element.type == 'outside-reference') {
n@3009: // Construct outside reference;
n@3009: var orNode = new outsideReferenceDOM(audioObject, index, document.getElementById("outside-reference-holder"));
n@3009: audioObject.bindInterface(orNode);
n@3009: } else {
n@3009: var aoi = new audioObjectInterface(audioObject, this);
n@3009: var label = interfaceContext.getLabel(page.label, index, page.labelStart);
n@3009: axis.forEach(function (a) {
n@3009: var node = a.addSlider(aoi);
n@3009: node.setLabel(label);
n@3009: aoi.addSlider(node);
n@3009: audioObject.bindInterface(aoi);
n@3009: });
n@3009: }
n@3009: });
n@3009: }
n@3009: this.pageXMLSave = function (store, pageSpecification) {
n@3009: AOIs.forEach(function (ao) {
n@3009: ao.pageXMLSave(store);
n@3009: });
n@3009: }
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@2825: if (interfaceContext.checkFragmentMinPlays() === false) {
nicholas@2825: return;
nicholas@2825: }
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:
n@3009: APE.pageXMLSave(store, pageSpecification);
nicholas@2538: }