changeset 2684:f9cba2241c35

Syntax tidy of specification.js
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Sat, 11 Mar 2017 08:04:05 +0000
parents 15104a43089d
children e4e7211842ad
files js/specification.js
diffstat 1 files changed, 82 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/js/specification.js	Wed Mar 01 17:10:24 2017 +0000
+++ b/js/specification.js	Sat Mar 11 08:04:05 2017 +0000
@@ -1,3 +1,4 @@
+/* globals document, console */
 function Specification() {
     // Handles the decoding of the project specification XML into a simple JavaScript Object.
 
@@ -28,11 +29,11 @@
     this.processAttribute = function (attribute, schema, schemaRoot) {
         // attribute is the string returned from getAttribute on the XML
         // schema is the <xs:attribute> node
-        if (schema.getAttribute('name') == undefined && schema.getAttribute('ref') != undefined) {
+        if (schema.getAttribute('name') === undefined && schema.getAttribute('ref') !== undefined) {
             schema = schemaRoot.getAllElementsByName(schema.getAttribute('ref'))[0];
         }
         var defaultOpt = schema.getAttribute('default');
-        if (attribute == null) {
+        if (attribute === null) {
             attribute = defaultOpt;
         }
         var dataType = schema.getAttribute('type');
@@ -51,7 +52,7 @@
                 dataType = "string";
             }
         }
-        if (attribute == null) {
+        if (attribute === null) {
             return attribute;
         }
         switch (dataType) {
@@ -87,7 +88,8 @@
         var schemaSetup = this.schema.getAllElementsByName('setup')[0];
         // First decode the attributes
         var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
-        for (var i = 0; i < attributes.length; i++) {
+        var i;
+        for (i = 0; i < attributes.length; i++) {
             var attributeName = attributes[i].getAttribute('name') || attributes[i].getAttribute('ref');
             var projectAttr = setupNode.getAttribute(attributeName);
             projectAttr = this.processAttribute(projectAttr, attributes[i], this.schema);
@@ -114,7 +116,7 @@
 
         // Now process the survey node options
         var survey = setupNode.getElementsByTagName('survey');
-        for (var i = 0; i < survey.length; i++) {
+        for (i = 0; i < survey.length; i++) {
             var location = survey[i].getAttribute('location');
             switch (location) {
                 case 'pre':
@@ -135,7 +137,7 @@
             this.errors.push("Only one <interface> node in the <setup> node allowed! Others except first ingnored!");
         }
         this.interfaces = new this.interfaceNode(this);
-        if (interfaceNode.length != 0) {
+        if (interfaceNode.length !== 0) {
             interfaceNode = interfaceNode[0];
             this.interfaces.decode(this, interfaceNode, this.schema.getAllElementsByName('interface')[1]);
         }
@@ -143,7 +145,7 @@
         // Page tags
         var pageTags = projectXML.getElementsByTagName('page');
         var pageSchema = this.schema.getAllElementsByName('page')[0];
-        for (var i = 0; i < pageTags.length; i++) {
+        for (i = 0; i < pageTags.length; i++) {
             var node = new this.page(this);
             node.decode(this, pageTags[i], pageSchema);
             this.pages.push(node);
@@ -162,7 +164,7 @@
         var attributes = schemaSetup.getAllElementsByTagName('xs:attribute');
         for (var i = 0; i < attributes.length; i++) {
             var name = attributes[i].getAttribute("name");
-            if (name == undefined) {
+            if (name === undefined) {
                 name = attributes[i].getAttribute("ref");
             }
             if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {
@@ -171,7 +173,7 @@
         }
         root.appendChild(setup);
         // Survey node
-        if (this.exitText != null) {
+        if (this.exitText !== null) {
             var exitTextNode = RootDocument.createElement('exitText');
             exitTextNode.textContent = this.exitText;
             setup.appendChild(exitTextNode);
@@ -180,9 +182,9 @@
         setup.appendChild(this.postTest.encode(RootDocument));
         setup.appendChild(this.metrics.encode(RootDocument));
         setup.appendChild(this.interfaces.encode(RootDocument));
-        for (var page of this.pages) {
+        this.pages.forEach(function (page) {
             root.appendChild(page.encode(RootDocument));
-        }
+        });
         return RootDocument;
     };
 
@@ -210,8 +212,9 @@
             this.decode = function (parent, child) {
                 this.schema = specification.schema.getAllElementsByName(child.nodeName)[0];
                 var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
-                for (var i in attributeMap) {
-                    if (isNaN(Number(i)) == true) {
+                var i;
+                for (i in attributeMap) {
+                    if (isNaN(Number(i)) === true) {
                         break;
                     }
                     var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
@@ -239,13 +242,13 @@
                 this.statement = child.getElementsByTagName('statement')[0].textContent;
                 if (this.type == "checkbox" || this.type == "radio") {
                     var children = child.getElementsByTagName('option');
-                    if (children.length == null) {
+                    if (children.length === null) {
                         console.log('Malformed' + child.nodeName + 'entry');
                         this.statement = 'Malformed' + child.nodeName + 'entry';
                         this.type = 'statement';
                     } else {
                         this.options = [];
-                        for (var i = 0; i < children.length; i++) {
+                        for (i = 0; i < children.length; i++) {
                             this.options.push({
                                 name: children[i].getAttribute('name'),
                                 text: children[i].textContent
@@ -265,14 +268,14 @@
                     }
                 }
                 var conditionElements = child.getElementsByTagName("conditional");
-                for (var i = 0; i < conditionElements.length; i++) {
+                for (i = 0; i < conditionElements.length; i++) {
                     var condition = conditionElements[i];
                     var obj = {
                         check: condition.getAttribute("check"),
                         value: condition.getAttribute("value"),
                         jumpToOnPass: condition.getAttribute("jumpToOnPass"),
                         jumpToOnFail: condition.getAttribute("jumpToOnFail")
-                    }
+                    };
                     this.conditions.push(obj);
                 }
             };
@@ -283,24 +286,24 @@
                 statement.textContent = this.statement;
                 node.appendChild(statement);
                 node.id = this.id;
-                if (this.name != undefined) {
+                if (this.name !== undefined) {
                     node.setAttribute("name", this.name);
                 }
-                if (this.mandatory != undefined) {
+                if (this.mandatory !== undefined) {
                     node.setAttribute("mandatory", this.mandatory);
                 }
                 node.id = this.id;
-                if (this.name != undefined) {
+                if (this.name !== undefined) {
                     node.setAttribute("name", this.name);
                 }
                 switch (this.type) {
                     case "checkbox":
-                        if (this.min != undefined) {
+                        if (this.min !== undefined) {
                             node.setAttribute("min", this.min);
                         } else {
                             node.setAttribute("min", "0");
                         }
-                        if (this.max != undefined) {
+                        if (this.max !== undefined) {
                             node.setAttribute("max", this.max);
                         } else {
                             node.setAttribute("max", "undefined");
@@ -315,27 +318,27 @@
                         }
                         break;
                     case "number":
-                        if (this.min != undefined) {
+                        if (this.min !== undefined) {
                             node.setAttribute("min", this.min);
                         }
-                        if (this.max != undefined) {
+                        if (this.max !== undefined) {
                             node.setAttribute("max", this.max);
                         }
                         break;
                     case "question":
-                        if (this.boxsize != undefined) {
+                        if (this.boxsize !== undefined) {
                             node.setAttribute("boxsize", this.boxsize);
                         }
-                        if (this.mandatory != undefined) {
+                        if (this.mandatory !== undefined) {
                             node.setAttribute("mandatory", this.mandatory);
                         }
                         break;
                     case "video":
-                        if (this.mandatory != undefined) {
+                        if (this.mandatory !== undefined) {
                             node.setAttribute("mandatory", this.mandatory);
                         }
                     case "youtube":
-                        if (this.url != undefined) {
+                        if (this.url !== undefined) {
                             node.setAttribute("url", this.url);
                         }
                         break;
@@ -355,14 +358,14 @@
                     default:
                         break;
                 }
-                for (var condition of this.conditions) {
+                this.conditions.forEach(function (condition) {
                     var conditionDOM = doc.createElement("conditional");
                     conditionDOM.setAttribute("check", condition.check);
                     conditionDOM.setAttribute("value", condition.value);
                     conditionDOM.setAttribute("jumpToOnPass", condition.jumpToOnPass);
                     conditionDOM.setAttribute("jumpToOnFail", condition.jumpToOnFail);
                     node.appendChild(conditionDOM);
-                }
+                });
                 return node;
             };
         };
@@ -374,14 +377,14 @@
             } else if (this.location == 'after') {
                 this.location = 'post';
             }
-            var child = xml.firstElementChild
+            var child = xml.firstElementChild;
             while (child) {
                 var node = new this.OptionNode(this.specification);
                 node.decode(parent, child);
                 this.options.push(node);
                 child = child.nextElementSibling;
             }
-            if (this.options.length == 0) {
+            if (this.options.length === 0) {
                 console.log("Empty survey node");
                 console.log(this);
             }
@@ -413,16 +416,17 @@
             // Extract interfaceoption node schema
             var interfaceOptionNodeSchema = this.schema.getAllElementsByName('interfaceoption')[0];
             var attributeMap = interfaceOptionNodeSchema.getAllElementsByTagName('xs:attribute');
-            for (var i = 0; i < interfaceOptionNodes.length; i++) {
+            var i, j;
+            for (i = 0; i < interfaceOptionNodes.length; i++) {
                 var ioNode = interfaceOptionNodes[i];
                 var option = {};
-                for (var j = 0; j < attributeMap.length; j++) {
+                for (j = 0; j < attributeMap.length; j++) {
                     var attributeName = attributeMap[j].getAttribute('name') || attributeMap[j].getAttribute('ref');
                     var projectAttr = ioNode.getAttribute(attributeName);
                     if (parent.processAttribute) {
-                        parent.processAttribute(projectAttr, attributeMap[j], parent.schema)
+                        parent.processAttribute(projectAttr, attributeMap[j], parent.schema);
                     } else {
-                        parent.parent.processAttribute(projectAttr, attributeMap[j], parent.parent.schema)
+                        parent.parent.processAttribute(projectAttr, attributeMap[j], parent.parent.schema);
                     }
                     switch (typeof projectAttr) {
                         case "number":
@@ -442,7 +446,7 @@
             if (scaleParent.length == 1) {
                 scaleParent = scaleParent[0];
                 var scalelabels = scaleParent.getAllElementsByTagName('scalelabel');
-                for (var i = 0; i < scalelabels.length; i++) {
+                for (i = 0; i < scalelabels.length; i++) {
                     this.scales.push({
                         text: scalelabels[i].textContent,
                         position: Number(scalelabels[i].getAttribute('position'))
@@ -453,27 +457,27 @@
 
         this.encode = function (doc) {
             var node = doc.createElement("interface");
-            if (typeof name == "string" && name.length > 0)
+            if (typeof this.name == "string" && this.name.length > 0)
                 node.setAttribute("name", this.name);
             if (typeof this.title == "string") {
                 var titleNode = doc.createElement("title");
                 titleNode.textContent = this.title;
                 node.appendChild(titleNode);
             }
-            for (var option of this.options) {
+            this.options.forEach(function (option) {
                 var child = doc.createElement("interfaceoption");
                 child.setAttribute("type", option.type);
                 child.setAttribute("name", option.name);
                 node.appendChild(child);
-            }
-            if (this.scales.length != 0) {
+            });
+            if (this.scales.length !== 0) {
                 var scales = doc.createElement("scales");
-                for (var scale of this.scales) {
+                this.scales.forEach(function (scale) {
                     var child = doc.createElement("scalelabel");
                     child.setAttribute("position", scale.position);
                     child.textContent = scale.text;
                     scales.appendChild(child);
-                }
+                });
                 node.appendChild(scales);
             }
             return node;
@@ -485,16 +489,16 @@
         this.decode = function (parent, xml) {
             var children = xml.getElementsByTagName('metricenable');
             for (var i in children) {
-                if (isNaN(Number(i)) == true) {
+                if (isNaN(Number(i)) === true) {
                     break;
                 }
                 this.enabled.push(children[i].textContent);
             }
-        }
+        };
         this.encode = function (doc) {
             var node = doc.createElement('metric');
             for (var i in this.enabled) {
-                if (isNaN(Number(i)) == true) {
+                if (isNaN(Number(i)) === true) {
                     break;
                 }
                 var child = doc.createElement('metricenable');
@@ -502,8 +506,8 @@
                 node.appendChild(child);
             }
             return node;
-        }
-    }
+        };
+    };
 
     this.page = function (specification) {
         this.presentedId = undefined;
@@ -531,7 +535,8 @@
         this.decode = function (parent, xml) {
             this.parent = parent;
             var attributeMap = this.schema.getAllElementsByTagName('xs:attribute');
-            for (var i = 0; i < attributeMap.length; i++) {
+            var i, node;
+            for (i = 0; i < attributeMap.length; i++) {
                 var attributeName = attributeMap[i].getAttribute('name') || attributeMap[i].getAttribute('ref');
                 var projectAttr = xml.getAttribute(attributeName);
                 projectAttr = parent.processAttribute(projectAttr, attributeMap[i], parent.schema);
@@ -548,20 +553,20 @@
 
             // Get the title
             var title = xml.getElementsByTagName('title');
-            if (title.length != 0 && title[0].parentElement == xml) {
+            if (title.length !== 0 && title[0].parentElement == xml) {
                 this.title = title[0].textContent;
             }
 
             // Get the Comment Box Prefix
             var CBP = xml.getElementsByTagName('commentboxprefix');
-            if (CBP.length != 0 && CBP[0].parentElement == xml) {
+            if (CBP.length !== 0 && CBP[0].parentElement == xml) {
                 this.commentBoxPrefix = CBP[0].textContent;
             }
 
             // Now decode the interfaces
             var interfaceNode = xml.getElementsByTagName('interface');
-            for (var i = 0; i < interfaceNode.length; i++) {
-                var node = new parent.interfaceNode(this.specification);
+            for (i = 0; i < interfaceNode.length; i++) {
+                node = new parent.interfaceNode(this.specification);
                 node.decode(this, interfaceNode[i], parent.schema.getAllElementsByName('interface')[1]);
                 this.interfaces.push(node);
             }
@@ -569,17 +574,17 @@
             // Now process the survey node options
             var survey = xml.getElementsByTagName('survey');
             var surveySchema = parent.schema.getAllElementsByName('survey')[0];
-            for (var i = 0; i < survey.length; i++) {
+            for (i = 0; i < survey.length; i++) {
                 var location = survey[i].getAttribute('location');
                 if (location == 'pre' || location == 'before') {
-                    if (this.preTest != null) {
+                    if (this.preTest !== null) {
                         this.errors.push("Already a pre/before test survey defined! Ignoring second!!");
                     } else {
                         this.preTest = new parent.surveyNode(this.specification);
                         this.preTest.decode(parent, survey[i], surveySchema);
                     }
                 } else if (location == 'post' || location == 'after') {
-                    if (this.postTest != null) {
+                    if (this.postTest !== null) {
                         this.errors.push("Already a post/after test survey defined! Ignoring second!!");
                     } else {
                         this.postTest = new parent.surveyNode(this.specification);
@@ -590,19 +595,19 @@
 
             // Now process the audioelement tags
             var audioElements = xml.getElementsByTagName('audioelement');
-            for (var i = 0; i < audioElements.length; i++) {
-                var node = new this.audioElementNode(this.specification);
-                node.decode(this, audioElements[i]);
-                this.audioElements.push(node);
+            for (i = 0; i < audioElements.length; i++) {
+                var audioNode = new this.audioElementNode(this.specification);
+                audioNode.decode(this, audioElements[i]);
+                this.audioElements.push(audioNode);
             }
 
             // Now decode the commentquestions
             var cqNode = xml.getElementsByTagName('commentquestions');
-            if (cqNode.length != 0) {
+            if (cqNode.length !== 0) {
                 cqNode = cqNode[0];
                 var commentQuestion = cqNode.firstElementChild;
                 while (commentQuestion) {
-                    var node = new this.commentQuestionNode(this.specification);
+                    node = new this.commentQuestionNode(this.specification);
                     node.decode(parent, commentQuestion);
                     this.commentQuestions.push(node);
                     commentQuestion = commentQuestion.nextElementSibling;
@@ -614,16 +619,17 @@
             var AHNode = root.createElement("page");
             // First decode the attributes
             var attributes = this.schema.getAllElementsByTagName('xs:attribute');
-            for (var i = 0; i < attributes.length; i++) {
+            var i;
+            for (i = 0; i < attributes.length; i++) {
                 var name = attributes[i].getAttribute("name");
-                if (name == undefined) {
+                if (name === undefined) {
                     name = attributes[i].getAttribute("ref");
                 }
                 if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {
                     eval("AHNode.setAttribute('" + name + "',this." + name + ")");
                 }
             }
-            if (this.loudness != null) {
+            if (this.loudness !== null) {
                 AHNode.setAttribute("loudness", this.loudness);
             }
             // <commentboxprefix>
@@ -631,15 +637,15 @@
             commentboxprefix.textContent = this.commentBoxPrefix;
             AHNode.appendChild(commentboxprefix);
 
-            for (var i = 0; i < this.interfaces.length; i++) {
+            for (i = 0; i < this.interfaces.length; i++) {
                 AHNode.appendChild(this.interfaces[i].encode(root));
             }
 
-            for (var i = 0; i < this.audioElements.length; i++) {
+            for (i = 0; i < this.audioElements.length; i++) {
                 AHNode.appendChild(this.audioElements[i].encode(root));
             }
             // Create <CommentQuestion>
-            for (var i = 0; i < this.commentQuestions.length; i++) {
+            for (i = 0; i < this.commentQuestions.length; i++) {
                 AHNode.appendChild(this.commentQuestions[i].encode(root));
             }
 
@@ -692,12 +698,12 @@
                     this.min = Number(xml.getAttribute("min"));
                     this.max = Number(xml.getAttribute("max"));
                     this.step = Number(xml.getAttribute("step"));
-                    if (this.step == undefined) {
+                    if (this.step === undefined) {
                         this.step = 1;
                     }
                     this.value = Number(xml.getAttribute("value"));
-                    if (this.value == undefined) {
-                        this.value = min;
+                    if (this.value === undefined) {
+                        this.value = this.min;
                     }
                     this.leftText = xml.getElementsByTagName("minText");
                     if (this.leftText && this.leftText.length > 0) {
@@ -733,19 +739,19 @@
                 }
                 node.id = this.id;
                 node.setAttribute("type", this.type);
-                if (this.name != undefined) {
+                if (this.name !== undefined) {
                     node.setAttribute("name", this.name);
                 }
                 var statement = root.createElement("statement");
                 statement.textContent = this.statement;
                 node.appendChild(statement);
                 if (this.type == "radio" || this.type == "checkbox") {
-                    for (var option of this.options) {
+                    this.options.forEach(function (option) {
                         var child = root.createElement("option");
                         child.setAttribute("name", option.name);
                         child.textContent = option.text;
                         node.appendChild(child);
-                    }
+                    });
                 }
                 if (this.type == "slider") {
                     node.setAttribute("min", this.min);
@@ -785,7 +791,7 @@
             this.stopTime = undefined;
             this.sampleRate = undefined;
             this.alternatives = [];
-            this.schema = specification.schema.getAllElementsByName('audioelement')[0];;
+            this.schema = specification.schema.getAllElementsByName('audioelement')[0];
             this.parent = null;
             this.decode = function (parent, xml) {
                 this.parent = parent;
@@ -822,7 +828,7 @@
                 var attributes = this.schema.getAllElementsByTagName('xs:attribute');
                 for (var i = 0; i < attributes.length; i++) {
                     var name = attributes[i].getAttribute("name");
-                    if (name == undefined) {
+                    if (name === undefined) {
                         name = attributes[i].getAttribute("ref");
                     }
                     if (eval("this." + name + " != undefined") || attributes[i].getAttribute("use") == "required") {