changeset 2624:a33b66ca3baa

#135: Added user-modifiable interfaces/interfaces.json. Users just need to update this file to add 3rd party interfaces
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Wed, 18 Jan 2017 14:54:12 +0000
parents d4707c4a8b98
children e3cf3c24149e
files interfaces/interfaces.json js/core.js
diffstat 2 files changed, 68 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/interfaces/interfaces.json	Wed Jan 18 14:54:12 2017 +0000
@@ -0,0 +1,35 @@
+{
+    "interfaces": [
+        {
+            "name": "APE",
+            "scripts": ["interfaces/ape.js"],
+            "css": ["interfaces/ape.css"]
+        },
+        {
+            "name": "MUSHRA",
+            "scripts": ["interfaces/mushra.js"],
+            "css": ["interfaces/mushra.css"]
+        },
+        {
+            "name": "horizontal",
+            "scripts": ["interfaces/horizontal-sliders.js"],
+            "css": ["interfaces/horizontal-sliders.css"]
+        }, {
+            "name": "discrete",
+            "scripts": ["interfaces/discrete.js"],
+            "css": ["interfaces/discrete.css"]
+        }, {
+            "name": "AB",
+            "scripts": ["interfaces/AB.js"],
+            "css": ["interfaces/AB.css"]
+        }, {
+            "name": "ABX",
+            "scripts": ["interfaces/ABX.js"],
+            "css": ["interfaces/ABX.css"]
+        }, {
+            "name": "timeline",
+            "scripts": ["interfaces/timeline.js"],
+            "css": ["interfaces/timeline.css"]
+        }
+    ]
+}
--- a/js/core.js	Thu Dec 08 14:33:01 2016 +0000
+++ b/js/core.js	Wed Jan 18 14:54:12 2017 +0000
@@ -331,98 +331,40 @@
         }
     }
 
-    // Detect the interface to use and load the relevant javascripts.
-    var interfaceJS = document.createElement('script');
-    interfaceJS.setAttribute("type", "text/javascript");
-    switch (specification.interface) {
-        case "APE":
-            interfaceJS.setAttribute("src", "interfaces/ape.js");
-
-            // APE comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/ape.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-
-        case "MUSHRA":
-            interfaceJS.setAttribute("src", "interfaces/mushra.js");
-
-            // MUSHRA comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/mushra.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-
-        case "AB":
-            interfaceJS.setAttribute("src", "interfaces/AB.js");
-
-            // AB comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/AB.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-
-        case "ABX":
-            interfaceJS.setAttribute("src", "interfaces/ABX.js");
-
-            // AB comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/ABX.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-
-        case "Bipolar":
-        case "ACR":
-        case "DCR":
-        case "CCR":
-        case "ABC":
-            // Above enumerate to horizontal sliders
-            interfaceJS.setAttribute("src", "interfaces/horizontal-sliders.js");
-
-            // horizontal-sliders comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/horizontal-sliders.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-        case "discrete":
-        case "likert":
-            // Above enumerate to horizontal discrete radios
-            interfaceJS.setAttribute("src", "interfaces/discrete.js");
-
-            // horizontal-sliders comes with a css file
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/discrete.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
-        case "timeline":
-            interfaceJS.setAttribute("src", "interfaces/timeline.js");
-            var css = document.createElement('link');
-            css.rel = 'stylesheet';
-            css.type = 'text/css';
-            css.href = 'interfaces/timeline.css';
-
-            document.getElementsByTagName("head")[0].appendChild(css);
-            break;
+    var getInterfaces = new XMLHttpRequest();
+    getInterfaces.open("GET", "interfaces/interfaces.json");
+    getInterfaces.onerror = function (e) {
+        throw (e);
     }
-    document.getElementsByTagName("head")[0].appendChild(interfaceJS);
+    getInterfaces.onload = function () {
+        if (getInterfaces.status !== 200) {
+            throw (new Error(getInterfaces.status));
+        }
+        // Get the current interface
+        var name = specification.interface,
+            head = document.getElementsByTagName("head")[0],
+            data = JSON.parse(getInterfaces.responseText),
+            interfaceObject = data.interfaces.find(function (e) {
+                return e.name == name;
+            });
+        if (!interfaceObject) {
+            throw ("Cannot load desired interface");
+        }
+        interfaceObject.scripts.forEach(function (v) {
+            var script = document.createElement("script");
+            script.setAttribute("type", "text/javascript");
+            script.setAttribute("src", v);
+            head.appendChild(script);
+        });
+        interfaceObject.css.forEach(function (v) {
+            var css = document.createElement("link");
+            css.setAttribute("rel", "stylesheet");
+            css.setAttribute("type", "text/css");
+            css.setAttribute("href", v);
+            head.appendChild(css);
+        });
+    }
+    getInterfaces.send();
 
     if (gReturnURL != undefined) {
         console.log("returnURL Overide from " + specification.returnURL + " to " + gReturnURL);