From 232fb2a7df237267fbd1705678e6ca05e1e22c06 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Thu, 15 Dec 2022 11:29:38 +0100
Subject: [PATCH] feat: add FormulaireDefinition.parseSelectDefaultValue()

refs #585
---
 .../formulaire/definition/form-definition.ts  | 19 +++++++++++++++++++
 src/app/formulaire/definition/form-section.ts | 14 ++------------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/app/formulaire/definition/form-definition.ts b/src/app/formulaire/definition/form-definition.ts
index 475c630c8..f2141323a 100644
--- a/src/app/formulaire/definition/form-definition.ts
+++ b/src/app/formulaire/definition/form-definition.ts
@@ -25,6 +25,7 @@ import { ServiceFactory } from "../../services/service-factory";
 import { SelectEntry } from "../elements/select/select-entry";
 import { SelectField } from "../elements/select/select-field";
 import { DeepSelectFieldIterator } from "../form-iterator/deep-selectfield-iterator";
+import { ConfigParser } from "./config-parser";
 
 /**
  * classe de base pour tous les formulaires
@@ -146,6 +147,24 @@ export abstract class FormulaireDefinition extends FormulaireNode implements Obs
         Session.getInstance().deleteNub(sn);
     }
 
+    /**
+     * parse calculator JSON configuration for select default value
+     * @param selectId select id, ie. "id" field value
+     * @return select "default" field value
+     */
+    protected parseSelectDefaultValue(json: {}, selectId: string): string {
+        const jp = new ConfigParser(json);
+        for (const fs of jp.forAll("fieldset")) {
+            const fsp = new ConfigParser(fs["fields"]);
+            for (const sel of fsp.forAll("select")) {
+                if (sel["id"] === selectId) {
+                    return sel["default"];
+                }
+            }
+        }
+        return undefined;
+    }
+
     /**
      * prepare options parsing
      */
diff --git a/src/app/formulaire/definition/form-section.ts b/src/app/formulaire/definition/form-section.ts
index 7ef1534e1..8f8be0af9 100644
--- a/src/app/formulaire/definition/form-section.ts
+++ b/src/app/formulaire/definition/form-section.ts
@@ -3,7 +3,6 @@ import { FieldSet } from "../elements/fieldset";
 import { ServiceFactory } from "../../services/service-factory";
 
 import { IObservable, Session, SectionNub, Props, CalculatorType, Prop_NullParameters, acSection } from "jalhyd";
-import { ConfigParser } from "./config-parser";
 import { SectionType } from "jalhyd";
 
 export class FormulaireSection extends FormulaireFixedVar {
@@ -16,17 +15,8 @@ export class FormulaireSection extends FormulaireFixedVar {
      */
     private parseDefaultSectionType() {
         if (this._defaultSectionType === undefined) {
-            const jp = new ConfigParser(this._jsonConfig);
-            for (const fs of jp.forAll("fieldset")) {
-                const fsp = new ConfigParser(fs["fields"]);
-                for (const sel of fsp.forAll("select")) {
-                    if (sel["id"] === "select_section") {
-                        const st = sel["default"];
-                        this._defaultSectionType = SectionType[st];
-                        return;
-                    }
-                }
-            }
+            const def = this.parseSelectDefaultValue(this._jsonConfig, "select_section");
+            this._defaultSectionType = SectionType[def];
         }
     }
 
-- 
GitLab