1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 11:46:50 +00:00

feat: allow script code changes in profile form_fields segments

This commit is contained in:
Paul-Christian Volkmer 2024-05-23 14:02:23 +02:00
parent 4b2ede0d80
commit 3ed38ca817
5 changed files with 182 additions and 10 deletions

View File

@ -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.

View File

@ -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
})
);
}
}

View File

@ -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 {

View File

@ -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
})
);
}
}

View File

@ -69,6 +69,10 @@ pub struct Form {
pub menu_category: Option<MenuCategory>,
}
pub trait WithScriptsCode {
fn escaped_scripts_code(&self) -> Option<String>;
}
#[derive(Deserialize)]
pub struct FormReference {
pub name: String,
@ -78,8 +82,8 @@ pub struct FormReference {
scripts_code: Option<String>,
}
impl FormReference {
pub fn escaped_scripts_code(&self) -> Option<String> {
impl WithScriptsCode for FormReference {
fn escaped_scripts_code(&self) -> Option<String> {
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<String>,
scripts_code: Option<String>,
}
impl WithScriptsCode for FormField {
fn escaped_scripts_code(&self) -> Option<String> {
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&#10;console.log(42);".to_string())
);
}
Err(e) => panic!("Cannot deserialize profile: {}", e),
}