From 3ed38ca817e8814faef32753a8d727eb3c26d135 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 23 May 2024 14:02:23 +0200 Subject: [PATCH] feat: allow script code changes in profile form_fields segments --- README.md | 9 +++-- src/model/data_form.rs | 73 ++++++++++++++++++++++++++++++++++++++ src/model/mod.rs | 8 +++-- src/model/unterformular.rs | 73 ++++++++++++++++++++++++++++++++++++++ src/profile.rs | 29 ++++++++++++--- 5 files changed, 182 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5f072c4..f84ce7f 100644 --- a/README.md +++ b/README.md @@ -198,11 +198,14 @@ In ihr sind die durchzuführenden Änderungen definiert. Eine Profildatei hat di ``` forms: - name: "ExampleForm" - form_field: + form_fields: - name: "formularfeld" hide: true - name: "otherformfield" default_value: "T" + scripts_code: | + // Beispielcode + console.log(getFieldValue('ref_first_mtb')); form_references: - name: "ref_first_mtb" referenced_data_form: "Formularverweis.Variante" @@ -222,8 +225,8 @@ Hierzu wird die Anwendung angewiesen im Formular "ExampleForm" den Formularverwe * den Verweis auf das Formular "Formularverweis.Variante" zu setzen * die Anzeige im Auswahlmenü auf "Referenziertes Formular vom: {Datum}" zu setzen * die Anzeige unterhalb des Auswahlmenüs auf "Datum im referenzierten Formular: {Datum}" zu setzen -* den Code zur Ausführung "nach Aktualisierung" für das Formularfeld auf die angegebene, mehrzeilige Zeichenkette - anzupassen +* den Code zur Ausführung "nach Aktualisierung" für Formularfelder werden auf die angegebene, mehrzeilige Zeichenkette + anzupassen. Dies kann in `form_fields`, als auch `form_references` geschehen. und dabei die vorhandenen Angaben für den Formularverweis zu ersetzen. diff --git a/src/model/data_form.rs b/src/model/data_form.rs index 06453c6..f994340 100644 --- a/src/model/data_form.rs +++ b/src/model/data_form.rs @@ -696,6 +696,7 @@ mod tests { use std::str::FromStr; use crate::model::onkostar_editor::OnkostarEditor; + use crate::model::Script; use crate::profile::Profile; #[test] @@ -822,4 +823,76 @@ mod tests { _ => panic!("Test failed: MenuCategory not found!"), } } + + #[test] + fn should_change_dataform_entry_scripts_code_with_form_fields() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + form_fields: + - name: Auswahl + scripts_code: |- + // Example code + console.log(42); + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!( + onkostar_editor.editor.data_form[0].entries.entry[2].scripts, + None + ); + + onkostar_editor.apply_profile(&profile); + + assert_eq!( + onkostar_editor.editor.data_form[0].entries.entry[2].scripts, + Some(Script { + code: "// Example code console.log(42);".into(), + valid: true + }) + ); + } + + #[test] + fn should_change_dataform_entry_scripts_code_with_form_references() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Hauptformular' + form_fields: + - name: Auswahl + scripts_code: |- + // Example code + console.log(42); + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!( + onkostar_editor.editor.data_form[0].entries.entry[2].scripts, + None + ); + + onkostar_editor.apply_profile(&profile); + + assert_eq!( + onkostar_editor.editor.data_form[0].entries.entry[2].scripts, + Some(Script { + code: "// Example code console.log(42);".into(), + valid: true + }) + ); + } } diff --git a/src/model/mod.rs b/src/model/mod.rs index 2ef1e1f..84a5308 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -30,7 +30,7 @@ use std::hash::{Hash, Hasher}; use serde::{Deserialize, Serialize}; use crate::model::requirements::Requires; -use crate::profile::{FormField, FormReference, Profile}; +use crate::profile::{FormField, FormReference, Profile, WithScriptsCode}; pub mod data_catalogue; pub mod data_form; @@ -40,7 +40,7 @@ pub mod property_catalogue; pub mod requirements; pub mod unterformular; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] #[serde(deny_unknown_fields)] pub struct Script { #[serde(rename = "Code")] @@ -337,12 +337,14 @@ where if entry.get_name() == form_field.name && form_field.hide { entry.hide() } - if entry.get_name() == form_field.name { if let Some(new_default_value) = &form_field.default_value { entry.update_default_value(new_default_value.to_string()) } } + if let Some(scripts_code) = &form_field.escaped_scripts_code() { + entry.update_scripts_code(scripts_code.clone()); + } } pub trait FormEntryContainer { diff --git a/src/model/unterformular.rs b/src/model/unterformular.rs index 604ac2f..cf3f5cc 100644 --- a/src/model/unterformular.rs +++ b/src/model/unterformular.rs @@ -692,6 +692,7 @@ mod tests { use std::str::FromStr; use crate::model::onkostar_editor::OnkostarEditor; + use crate::model::Script; use crate::profile::Profile; #[test] @@ -790,4 +791,76 @@ mod tests { .menu_category .is_none()); } + + #[test] + fn should_change_dataform_entry_scripts_code_with_form_fields() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Unterformular' + form_fields: + - name: Termin + scripts_code: |- + // Example code + console.log(42); + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!( + onkostar_editor.editor.unterformular[0].entries.entry[1].scripts, + None + ); + + onkostar_editor.apply_profile(&profile); + + assert_eq!( + onkostar_editor.editor.unterformular[0].entries.entry[1].scripts, + Some(Script { + code: "// Example code console.log(42);".into(), + valid: true + }) + ); + } + + #[test] + fn should_change_dataform_entry_scripts_code_with_form_references() { + let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc")); + + assert!(onkostar_editor.is_ok()); + let mut onkostar_editor = onkostar_editor.unwrap(); + + let profile = "forms: + - name: 'Unterformular' + form_fields: + - name: Termin + scripts_code: |- + // Example code + console.log(42); + "; + + let profile = Profile::from_str(profile); + assert!(profile.is_ok()); + let profile = profile.unwrap(); + + assert_eq!( + onkostar_editor.editor.unterformular[0].entries.entry[1].scripts, + None + ); + + onkostar_editor.apply_profile(&profile); + + assert_eq!( + onkostar_editor.editor.unterformular[0].entries.entry[1].scripts, + Some(Script { + code: "// Example code console.log(42);".into(), + valid: true + }) + ); + } } diff --git a/src/profile.rs b/src/profile.rs index 83652f8..0488815 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -69,6 +69,10 @@ pub struct Form { pub menu_category: Option, } +pub trait WithScriptsCode { + fn escaped_scripts_code(&self) -> Option; +} + #[derive(Deserialize)] pub struct FormReference { pub name: String, @@ -78,8 +82,8 @@ pub struct FormReference { scripts_code: Option, } -impl FormReference { - pub fn escaped_scripts_code(&self) -> Option { +impl WithScriptsCode for FormReference { + fn escaped_scripts_code(&self) -> Option { self.scripts_code.as_ref().map(|code| escape_script(code)) } } @@ -90,6 +94,13 @@ pub struct FormField { #[serde(default)] pub hide: bool, pub default_value: Option, + scripts_code: Option, +} + +impl WithScriptsCode for FormField { + fn escaped_scripts_code(&self) -> Option { + self.scripts_code.as_ref().map(|code| escape_script(code)) + } } #[derive(Deserialize)] @@ -101,7 +112,7 @@ pub struct MenuCategory { #[cfg(test)] mod tests { - use crate::profile::Profile; + use crate::profile::{Profile, WithScriptsCode}; use std::str::FromStr; #[test] @@ -250,17 +261,27 @@ mod tests { hide: false - name: formularfeld_to_hide hide: true + - name: formularfeld_to_mod + scripts_code: |- + // Example code + console.log(42); "; match Profile::from_str(content) { Ok(profile) => { assert_eq!(profile.forms.len(), 1); assert_eq!(profile.forms[0].name, "DNPM Therapieplan"); - assert_eq!(profile.forms[0].form_fields.len(), 2); + assert_eq!(profile.forms[0].form_fields.len(), 3); assert_eq!(profile.forms[0].form_fields[0].name, "formularfeld_to_keep"); assert!(!profile.forms[0].form_fields[0].hide); assert_eq!(profile.forms[0].form_fields[1].name, "formularfeld_to_hide"); assert!(profile.forms[0].form_fields[1].hide); + assert_eq!(profile.forms[0].form_fields[2].name, "formularfeld_to_mod"); + assert!(!profile.forms[0].form_fields[2].hide); + assert_eq!( + profile.forms[0].form_fields[2].escaped_scripts_code(), + Some("// Example code console.log(42);".to_string()) + ); } Err(e) => panic!("Cannot deserialize profile: {}", e), }