From f66a6d18567a48a6d8f4e8eb0da1af0c178c17a6 Mon Sep 17 00:00:00 2001
From: Armand Bahi <armand.bahi@veremes.com>
Date: Fri, 28 Dec 2018 14:51:58 +0100
Subject: [PATCH] Ajout des champs customTernary pour afficher ou cacher les
 champs

---
 .../externs/formReader/formReaderDrtv.js      | 34 ++++++-
 .../externs/formReader/formReaderSrvc.js      | 88 +++++++++++++++++++
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js b/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js
index 48e7a37a..8bfa3d57 100644
--- a/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js
+++ b/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js
@@ -420,9 +420,21 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc
                     if (formReaderService["isFunctionCall"](oField["visible"])) {
                         isVisible = formReaderService["callFunction"](oField["visible"]);
                     } else if(formReaderService["isTernaryString"](oField["visible"])) {
-                        isVisible = scope.$eval(oField["visible"]);
+                        try {
+                            isVisible = scope.$eval(oField["visible"]);
+                        } catch (e) {
+                            isVisible = false;
+                        }
+                    } else if(formReaderService["isCustomTernaryString"](oField["visible"])) {
+                        try {
+                            isVisible = scope.$eval(formReaderService["translateCustomTernaryString"](oField["visible"], scope['oFormValues'], scope['sFormDefinitionName']));
+                        } catch (e) {
+                            isVisible = false;
+                        }
                     } else if (goog.isBoolean(oField["visible"])) {
                         isVisible = oField["visible"];
+                    } else if (oField["visible"] == true || oField["visible"] == false || oField["visible"] == "true" || oField["visible"] == "false") {
+                        isVisible = oField["visible"];
                     } else {
                         isVisible = false;
                     }
@@ -614,6 +626,26 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc
                 });
             };
 
+            /**
+             * Transforme en int
+             *
+             * @param  {string} sField
+             * @return {number}
+             */
+            scope['int'] = scope['parseInt'] = function (sField) {
+                return parseInt(sField);
+            }
+
+            /**
+             * Transforme en float
+             *
+             * @param  {string} sField
+             * @return {number}
+             */
+            scope['float'] = scope['parseFloat'] = function (sField) {
+                return parseFloat(sField);
+            }
+
             // Attend la suppression du scope.
             scope.$on("$destroy", function () {
                 // Supprime toutes les données du formulaire.
diff --git a/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js b/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js
index f174cfef..f3baa43a 100644
--- a/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js
+++ b/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js
@@ -642,6 +642,9 @@ formReader.formReaderService = function ($translate, $rootScope, $q, $log, $time
                 bCheckFunction = true;
             var bReturn = false;
             if (typeof (sString) === "string") {
+                if (sString.substr(0, 1) === '=') {
+                    return false;
+                }
                 aMatchResult = sString.match(/(.+)\((.*)\)/);
                 if (aMatchResult !== null) {
                     if (bCheckFunction) {
@@ -678,6 +681,91 @@ formReader.formReaderService = function ($translate, $rootScope, $q, $log, $time
                 return false;
             }
         },
+        /**
+         * isCustomTernaryString function.
+         * La chaine de caractère passée est une expression ternaire définie par l'administrateur
+         * ex : "= {{puissance}} == 500"
+         * @param {string} sString Chaine de caractère.
+         * @return {boolean}
+         */
+        "isCustomTernaryString": function (sString) {
+            if (goog.isDefAndNotNull(this['translateCustomTernaryString'](sString))) {
+                return true;
+            } else {
+                return false;
+            }
+        },
+
+        /**
+         * translateCustomTernaryString function
+         * Traduit une chaine ternaire customisée par une chaine ternaire exploitable
+         * ex : "= {{puissance}} == 500" devient "oFormValues[sFormDefinitionName].puissance == 500 ? true : false"
+         * @param  {string} sString
+         * @param  {object} oFormValues
+         * @param  {string} sFormDefinitionName
+         * @return {string}
+         */
+        "translateCustomTernaryString": function (sString, oFormValues, sFormDefinitionName) {
+
+            // Verif type
+            if (!goog.isString(sString)) {
+                return null;
+            }
+
+            // Vérification chaine
+            if (sString.substr(0, 1) !== '=') {
+                return null;
+            }
+
+            if (!goog.isDefAndNotNull(RegExp('==').exec(sString))) {
+                return null;
+            }
+
+            // Enlève le premier signe "="
+            sString = sString.substr(1);
+
+            // Cherche les attributs
+            var aAttrs = [];
+            var attrRegex = RegExp('{{(\\w*?)}}','g');
+            var attrRegexResult;
+            while ((attrRegexResult = attrRegex.exec(sString)) !== null) {
+                aAttrs.push(attrRegexResult);
+            }
+
+            // Remplace les attributs
+            var sReplacer;
+            for (var i = 0; i < aAttrs.length; i++) {
+                if (goog.isDefAndNotNull(aAttrs[i][0]) && goog.isDefAndNotNull(aAttrs[i][1])) {
+
+                    // Pour les valeurs simples
+                    sReplacer = 'oFormValues[sFormDefinitionName].' + aAttrs[i][1];
+
+                    // Pour valeurs dans les listes déroulantes
+                    if (goog.isDefAndNotNull(oFormValues)) {
+                        if (goog.isDefAndNotNull(oFormValues[sFormDefinitionName])) {
+                            if (goog.isDefAndNotNull(oFormValues[sFormDefinitionName][aAttrs[i][1]])) {
+                                if (goog.isDefAndNotNull(oFormValues[sFormDefinitionName][aAttrs[i][1]]['selectedOption'])) {
+                                    if (goog.isDefAndNotNull(oFormValues[sFormDefinitionName][aAttrs[i][1]]['selectedOption']['value'])) {
+                                        sReplacer = 'oFormValues[sFormDefinitionName].' + aAttrs[i][1] + '.selectedOption.value';
+                                    }
+                                    $log.log(aAttrs[i][0] + ': ' + oFormValues[sFormDefinitionName][aAttrs[i][1]]['selectedOption']['value']);
+                                } else {
+                                    $log.log(aAttrs[i][0] + ': ' + oFormValues[sFormDefinitionName][aAttrs[i][1]]);
+                                }
+                            }
+                        }
+                    }
+
+
+                    sString = sString.replace(aAttrs[i][0], sReplacer);
+                }
+            }
+
+            // Ajoute le retour true/false
+            sString = sString + ' ? true : false';
+
+            return sString;
+        },
         /**
          * selectFirstOption function.
          * Sélectionne la 1ere option d'un <select>.
-- 
GitLab