diff --git a/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js b/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js index c015d81f4f7e035217db1b4abecf3d856063fcdf..b561f4e1ba8aa7cd862741e26612a4ed8c715799 100644 --- a/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js +++ b/src/vitis/client/javascript/externs/formReader/formReaderDrtv.js @@ -405,11 +405,6 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc */ scope['isFieldPresent'] = function (oField, oTab, bCheckButtons) { - // Valeurs dynamiques - if (goog.isDefAndNotNull(oField['dynamic_value'])) { - this['setDynamicValue'](oField); - } - bCheckButtons = goog.isDefAndNotNull(bCheckButtons) ? bCheckButtons : false; // Visible @@ -491,7 +486,11 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc } }; - + /** + * Set the filed value using dynamic_value + * + * @param {object} oField + */ scope['setDynamicValue'] = function (oField) { var value = formReaderService['getFormValue'](oField['name']); @@ -504,7 +503,52 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc console.error('error with ternary : ' + oField["dynamic_value"]); } } + } + /** + * Ajoute des watchers pour déclencher les dynamic values + * + */ + scope['setFormDynamicValues'] = function () { + + var aAttrs; + var oFormDefinition = scope['oFormDefinition']; + var sFormDefinitionName = scope['sFormDefinitionName']; + + // Nettoyage automatique des watchers + if (goog.isArray(this.dynamicWatchers_)) { + for (var i = this.dynamicWatchers_.length - 1; i >= 0; i--) { + this.dynamicWatchers_[i](); + this.dynamicWatchers_.pop(); + } + } else { + this.dynamicWatchers_ = []; + } + if (goog.isDefAndNotNull(oFormDefinition[sFormDefinitionName])) { + + var aFormElementDefinitions = formReaderService['getAllFormElementDefinition'](sFormDefinitionName, oFormDefinition) + + for (var i = 0; i < aFormElementDefinitions.length; i++) { + if (goog.isDefAndNotNull(aFormElementDefinitions[i]['dynamic_value'])) { + + // Ajoute les watchers sur les champs présents dans l'expression dynamic_value + if(formReaderService["isCustomTernaryString"](aFormElementDefinitions[i]["dynamic_value"], false)) { + aAttrs = formReaderService['getCustomTernaryStringAttrs'](aFormElementDefinitions[i]["dynamic_value"]); + + for (var ii = 0; ii < aAttrs.length; ii++) { + if (goog.isDefAndNotNull(aAttrs[ii][1])) { + + this.dynamicWatchers_.push(scope.$watch('oFormValues[sFormDefinitionName].' + aAttrs[ii][1], angular.bind(this, function(oField, newVal, oldVal, scope){ + + // Set la nouvelle valeur + this['setDynamicValue'](oField); + }, angular.copy(aFormElementDefinitions[i])), true)); + } + } + } + } + } + } } /** @@ -675,8 +719,12 @@ formReader.formReaderDirective = function ($q, formReaderService, propertiesSrvc // Lors du chargement du formulaire scope.$on('loadForm', function () { + // Vide les file inputs scope['resetFileInputs'](); + + // Trigger dynamic values + scope['setFormDynamicValues'](); }); } }; diff --git a/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js b/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js index 5d9017b0c2d91dddfff92a5d6fadeb29c21663ca..c4dfddeb1f64979e0a7e8cd0529c290ab78eab81 100644 --- a/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js +++ b/src/vitis/client/javascript/externs/formReader/formReaderSrvc.js @@ -696,7 +696,6 @@ formReader.formReaderService = function ($translate, $rootScope, $q, $log, $time return false; } }, - /** * translateCustomTernaryString function * Traduit une chaine ternaire customisée par une chaine ternaire exploitable @@ -722,12 +721,7 @@ formReader.formReaderService = function ($translate, $rootScope, $q, $log, $time 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); - } + var aAttrs = this['getCustomTernaryStringAttrs'](sString); // Remplace les attributs var sReplacer; @@ -762,6 +756,24 @@ formReader.formReaderService = function ($translate, $rootScope, $q, $log, $time return sString; }, + /** + * Cherche les attributs dans l'expression ternaire + * + * @param {string} sString + * @return {array} + */ + "getCustomTernaryStringAttrs": function (sString) { + + // Cherche les attributs + var aAttrs = []; + var attrRegex = RegExp('{{(\\w*?)}}','g'); + var attrRegexResult; + while ((attrRegexResult = attrRegex.exec(sString)) !== null) { + aAttrs.push(attrRegexResult); + } + + return aAttrs; + }, /** * selectFirstOption function. * Sélectionne la 1ere option d'un <select>.