diff --git a/app/models/logic_jump.server.model.js b/app/models/logic_jump.server.model.js index 7bc51101..650d1fe8 100644 --- a/app/models/logic_jump.server.model.js +++ b/app/models/logic_jump.server.model.js @@ -8,33 +8,6 @@ var mongoose = require('mongoose'), _ = require('lodash'), math = require('mathjs'); -/** - * Calculate a 32 bit FNV-1a hash - * Found here: https://gist.github.com/vaiorabbit/5657561 - * Ref.: http://isthe.com/chongo/tech/comp/fnv/ - * - * @param {string} str the input value - * @param {boolean} [asString=false] set to true to return the hash value as - * 8-digit hex string instead of an integer - * @param {integer} [seed] optionally pass the hash of the previous chunk - * @returns {integer | string} - */ -function hashFnv32a(str, asString, seed) { - /*jshint bitwise:false */ - var i, l, - hval = (seed === undefined) ? 0x811c9dc5 : seed; - - for (i = 0, l = str.length; i < l; i++) { - hval ^= str.charCodeAt(i); - hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); - } - if( asString ){ - // Convert to 8 digit hex string - return ("0000000" + (hval >>> 0).toString(16)).substr(-8); - } - return hval >>> 0; -} - var schemaOptions = { toObject: { virtuals: true @@ -48,8 +21,18 @@ var LogicJumpSchema = new Schema({ expressionString: { type: String, enum: [ - 'a == b', - 'a !== b' + 'field == static', + 'field != static', + 'field > static', + 'field >= static', + 'field <= static', + 'field < static', + 'field contains static', + 'field !contains static', + 'field begins static', + 'field !begins static', + 'field ends static', + 'field !ends static', ] }, fieldA: { @@ -93,21 +76,6 @@ var LogicJumpSchema = new Schema({ */ -LogicJumpSchema.virtual('result').get(function () { - if (this.expressionString && this.fieldA.fieldValue && this.valueB) { - var valA = hashFnv32a(String(this.fieldA.fieldValue)); - var valB = hashFnv32a(String(this.valueB)); - var scope = { - 'a': valA, - 'b': valB - }; - return math.eval(this.expressionString, scope); - - } else { - return null; - } -}); - mongoose.model('LogicJump', LogicJumpSchema); module.exports = LogicJumpSchema; diff --git a/bower.json b/bower.json index c0b23fa5..7f96231e 100755 --- a/bower.json +++ b/bower.json @@ -38,7 +38,8 @@ "angular-translate": "~2.11.0", "ng-device-detector": "~3.0.1", "ng-translate": "*", - "mathjs": "^3.4.1" + "mathjs": "^3.4.1", + "jsep": "^0.3.1" }, "resolutions": { "angular-bootstrap": "^0.14.0", diff --git a/public/form_modules/forms/base/directives/submit-form.client.directive.js b/public/form_modules/forms/base/directives/submit-form.client.directive.js index 15709759..23d4439b 100644 --- a/public/form_modules/forms/base/directives/submit-form.client.directive.js +++ b/public/form_modules/forms/base/directives/submit-form.client.directive.js @@ -1,5 +1,14 @@ 'use strict'; +//FIXME: Should find an appropriate place for this +//Setting up jsep +jsep.addBinaryOp("contains", 10); +jsep.addBinaryOp("!contains", 10); +jsep.addBinaryOp("begins", 10); +jsep.addBinaryOp("!begins", 10); +jsep.addBinaryOp("ends", 10); +jsep.addBinaryOp("!ends", 10); + /** * Calculate a 32 bit FNV-1a hash * Found here: https://gist.github.com/vaiorabbit/5657561 @@ -121,15 +130,62 @@ angular.module('view-form').directive('submitFormDirective', ['$http', 'TimeCoun */ var evaluateLogicJump = function(field){ console.log('evaluateLogicJump'); + console.log(field.fieldValue); var logicJump = field.logicJump; - if (logicJump.expressionString && logicJump.valueB) { - var valA = hashFnv32a(String(field.fieldValue)); - var valB = hashFnv32a(String(logicJump.valueB)); - var scope = { - 'a': valA, - 'b': valB - }; - return math.eval(logicJump.expressionString, scope); + + if (logicJump.expressionString && logicJump.valueB && field.fieldValue) { + var parse_tree = jsep(logicJump.expressionString); + var left, right; + + console.log(parse_tree); + + if(parse_tree.left.name === 'field'){ + left = field.fieldValue; + right = logicJump.valueB + } else { + left = logicJump.valueB; + right = field.fieldValue; + } + + if(field.fieldType === 'number' || field.fieldType === 'scale' || field.fieldType === 'rating'){ + switch(parse_tree.operator) { + case '==': + return (parseInt(left) === parseInt(right)); + case '!==': + return (parseInt(left) !== parseInt(right)); + case '>': + return (parseInt(left) > parseInt(right)); + case '>=': + return (parseInt(left) > parseInt(right)); + case '<': + return (parseInt(left) < parseInt(right)); + case '<=': + return (parseInt(left) <= parseInt(right)); + default: + return false; + } + } else { + switch(parse_tree.operator) { + case '==': + return (left === right); + case '!==': + return (left !== right); + case 'contains': + return (left.indexOf(right) > -1); + case '!contains': + return !(left.indexOf(right) > -1); + case 'begins': + return left.startsWith(right); + case '!begins': + return !left.startsWith(right); + case 'ends': + return left.endsWith(right); + case '!ends': + return left.endsWith(right); + default: + return false; + } + } } }; @@ -210,7 +266,6 @@ angular.module('view-form').directive('submitFormDirective', ['$http', 'TimeCoun $rootScope.nextField = $scope.nextField = function(){ var currField = $scope.myform.visible_form_fields[$scope.selected.index]; - console.log(currField.logicJump); if($scope.selected && $scope.selected.index > -1){ //Jump to logicJump's destination if it is true diff --git a/public/modules/forms/admin/views/directiveViews/form/edit-form.client.view.html b/public/modules/forms/admin/views/directiveViews/form/edit-form.client.view.html index b5bbdb39..9ed68310 100644 --- a/public/modules/forms/admin/views/directiveViews/form/edit-form.client.view.html +++ b/public/modules/forms/admin/views/directiveViews/form/edit-form.client.view.html @@ -308,10 +308,10 @@ -
+

Logic Jump

-
+
{{ 'ADD_LOGIC_JUMP' | translate }}