Эх сурвалжийг харах

Dynamic questions engine (wip)

Vijayakrishnan 3 жил өмнө
parent
commit
08e63a053d

+ 55 - 4
public/js/dq.js

@@ -50,13 +50,50 @@
 
     function resolveAllAtomicConditions(_conditions, _dataMap) {
         if(Array.isArray(_conditions)) {
-
+            for (let i = 0; i < _conditions.length; i++) {
+                resolveAllAtomicConditions(_conditions[i], _dataMap);
+            }
         }
-        else {
+        else if(typeof _conditions === 'object') {
             resolveAtomicCondition(_conditions, _dataMap);
         }
     }
 
+    function reduceConditionsIntoResolutions(_conditions) {
+        for (let i = 0; i < _conditions.length; i++) {
+            // if simple object, resolve
+            if(!Array.isArray(_conditions[i]) && typeof _conditions[i] === 'object') {
+                _conditions.splice(i, 1, _conditions[i].resolution);
+            }
+            else if(Array.isArray(_conditions[i])) {
+                reduceConditionsIntoResolutions(_conditions[i]);
+            }
+        }
+    }
+
+    function combineResolutionListsIntoSingleResolutions(_conditions) {
+        console.log('ALIX 1', _conditions);
+        for (let i = 0; i < _conditions.length; i++) {
+            if(Array.isArray(_conditions[i])) {
+                _conditions[i] = combineResolutionListsIntoSingleResolutions(_conditions[i]);
+            }
+        }
+        console.log('ALIX 2', _conditions);
+
+        // at this point, the array will have only booleans and "AND", "OR" combinators
+        let resolution = _conditions[0];
+        for (let i = 1; i < _conditions.length; i+=2) {
+            if(_conditions[i] === 'AND') {
+                resolution = resolution && _conditions[i + 1];
+            }
+            else if(_conditions[i] === 'OR') {
+                resolution = resolution || _conditions[i + 1];
+            }
+        }
+
+        return resolution;
+    }
+
     window.runDQConditions = function(_parent) {
 
         _parent.find('.dq-line.has-pre-condition').each(function() {
@@ -66,7 +103,7 @@
             resolveAllAtomicConditions(conditions, dataMap);
 
             // if object, single condition with key, op and value
-            if(!Array.isArray(conditions)) {
+            if(!Array.isArray(conditions) && typeof conditions === 'object') {
                 if(conditions.resolution) {
                     $(this).removeClass('d-none');
                 }
@@ -74,8 +111,22 @@
                     $(this).addClass('d-none');
                 }
             }
-
             // else if array - means list of conditions with 'and' or 'or' separators - array size MUST be an odd number
+            else if(Array.isArray(conditions)) {
+
+                // goal is to reduce each item in the array into a single boolean - recursively
+                reduceConditionsIntoResolutions(conditions);
+
+                conditions = combineResolutionListsIntoSingleResolutions(conditions);
+
+                if(conditions) {
+                    $(this).removeClass('d-none');
+                }
+                else {
+                    $(this).addClass('d-none');
+                }
+
+            }
 
         });
 

+ 103 - 0
resources/views/app/dq-templates/tech.json

@@ -17,6 +17,109 @@
         "key": "fe_or_be",
         "value": "Front End"
       }
+    },
+    {
+      "key": "react_version",
+      "helpText": "React Version:",
+      "resultSummary": "<b>React v</b>: {value}",
+      "fieldType": "numeric",
+      "preConditions": [
+        {
+          "key": "fe_or_be",
+          "value": "Front End"
+        },
+        "AND",
+        {
+          "key": "fav_fe_tech",
+          "value": "React"
+        }
+      ]
+    },
+    {
+      "key": "angular_version",
+      "helpText": "Angular Version:",
+      "resultSummary": "<b>Angular v</b>: {value}",
+      "fieldType": "numeric",
+      "preConditions": [
+        {
+          "key": "fe_or_be",
+          "value": "Front End"
+        },
+        "AND",
+        {
+          "key": "fav_fe_tech",
+          "value": "Angular"
+        }
+      ]
+    },
+    {
+      "key": "fav_color",
+      "helpText": "Favorite Color?",
+      "resultSummary": "<b>Color</b>: {value}",
+      "fieldType": "select",
+      "options": ["Red", "Blue", "Yellow", "Orange"]
+    },
+    {
+      "key": "condition_test_1",
+      "helpText": "So you're either a front-end angular dev - OR - you like yellow!",
+      "resultSummary": "<b>Ex: </b>: {value}",
+      "fieldType": "text",
+      "preConditions": [
+        [
+          {
+            "key": "fe_or_be",
+            "value": "Front End"
+          },
+          "AND",
+          {
+            "key": "fav_fe_tech",
+            "value": "Angular"
+          }
+        ],
+        "OR",
+        {
+          "key": "fav_color",
+          "value": "Yellow"
+        }
+      ]
+    },
+    {
+      "key": "condition_test_2",
+      "helpText": "So you're a (front-end AND (react or angular) dev) AND (you like red OR blue)!",
+      "resultSummary": "<b>Ex: </b>: {value}",
+      "fieldType": "text",
+      "preConditions": [
+        [
+          {
+            "key": "fe_or_be",
+            "value": "Front End"
+          },
+          "AND",
+          [
+            {
+              "key": "fav_fe_tech",
+              "value": "Angular"
+            },
+            "OR",
+            {
+              "key": "fav_fe_tech",
+              "value": "React"
+            }
+          ]
+        ],
+        "AND",
+        [
+          {
+            "key": "fav_color",
+            "value": "Red"
+          },
+          "OR",
+          {
+            "key": "fav_color",
+            "value": "Blue"
+          }
+        ]
+      ]
     }
   ],
   "dataMap": {