changeset 3034:49eca76782d2

Fix #218
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 26 Sep 2017 09:34:38 +0100
parents 21e3777edf58
children 1620cbee9111
files js/core.js js/specification.js
diffstat 2 files changed, 46 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/js/core.js	Mon Sep 25 11:57:42 2017 +0100
+++ b/js/core.js	Tue Sep 26 09:34:38 2017 +0100
@@ -1083,12 +1083,12 @@
         this.popupResponse.innerHTML = "";
         this.popupTitle.innerHTML = "";
         var strings = node.specification.statement.split("\n");
-        strings.forEach(function(e,i,a){
+        strings.forEach(function (e, i, a) {
             a[i] = e.trim();
         });
         node.specification.statement = strings.join("\n");
         var statementElements = p.parseFromString(converter.makeHtml(node.specification.statement), "text/html").querySelector("body").children;
-        while(statementElements.length > 0) {
+        while (statementElements.length > 0) {
             this.popupTitle.appendChild(statementElements[0]);
         }
         if (node.specification.type == 'question') {
@@ -2640,6 +2640,12 @@
             this.textArea.style.width = boxwidth - 6 + "px";
         };
         this.resize();
+        this.check = function () {
+            if (this.specification.mandatory && this.textArea.value.length == 0) {
+                return false;
+            }
+            return true;
+        }
     };
 
     this.radioBox = function (commentQuestion) {
@@ -2715,6 +2721,17 @@
             this.holder.style.width = boxwidth + "px";
         };
         this.resize();
+        this.check = function () {
+            if (this.specification.mandatory) {
+                var selected = this.options.find(function (o) {
+                    return o.checked;
+                });
+                if (selected === undefined) {
+                    return false;
+                }
+            }
+            return true;
+        };
     };
 
     this.checkboxBox = function (commentQuestion) {
@@ -2781,6 +2798,17 @@
             this.holder.style.width = boxwidth + "px";
         };
         this.resize();
+        this.check = function () {
+            if (this.specification.mandatory) {
+                var selected = this.options.filter(function (o) {
+                    return o.checked;
+                });
+                if (selected.length === 0) {
+                    return false;
+                }
+            }
+            return true;
+        };
     };
 
     this.sliderBox = function (commentQuestion) {
@@ -2838,6 +2866,9 @@
             this.slider.style.width = boxwidth - 24 + "px";
         };
         this.resize();
+        this.check = function () {
+            return true;
+        };
     };
 
     this.createCommentQuestion = function (element) {
@@ -2859,6 +2890,17 @@
         this.commentQuestions = [];
     };
 
+    this.checkCommentQuestions = function () {
+        var failed = this.commentQuestions.filter(function (a) {
+            return a.check() === false;
+        });
+        if (failed.length === 0) {
+            return true;
+        }
+        this.lightbox.post("Error", "Please answer the questions on the page.");
+        return false;
+    }
+
     this.outsideReferenceDOM = function (audioObject, index, inject) {
         this.parent = audioObject;
         this.outsideReferenceHolder = document.createElement('button');
--- a/js/specification.js	Mon Sep 25 11:57:42 2017 +0100
+++ b/js/specification.js	Tue Sep 26 09:34:38 2017 +0100
@@ -718,10 +718,12 @@
             this.name = undefined;
             this.type = undefined;
             this.statement = undefined;
+            this.mandatory = undefined;
             this.schema = schemaRoot.querySelector('[name=commentquestion]');
             this.decode = function (parent, xml) {
                 this.id = xml.id;
                 this.name = xml.getAttribute('name');
+                this.mandatory = xml.getAttribute("mandatory") == "true";
                 if (this.name === null) {
                     this.name = undefined;
                 }