1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-07-03 01:02:55 +00:00

feat: add profile option 'remove_filter' to remove filter for form entries

This commit is contained in:
2024-05-23 15:04:19 +02:00
parent 3ed38ca817
commit 218e6aff15
6 changed files with 281 additions and 14 deletions

View File

@ -662,6 +662,10 @@ impl FormEntry for Entry {
});
self.speichern = "0".into()
}
fn remove_filter(&mut self) {
self.filter = None;
}
}
impl Sortable for Entry {
@ -696,7 +700,7 @@ mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::Script;
use crate::model::{Filter, RefEntries, Script};
use crate::profile::Profile;
#[test]
@ -895,4 +899,140 @@ mod tests {
})
);
}
#[test]
fn should_remove_dataform_entry_filter_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
remove_filter: true
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[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[1].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[3].filter,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[1].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[3].filter,
None
);
}
#[test]
fn should_remove_dataform_entry_filter_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
remove_filter: true
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[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[1].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[3].filter,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[1].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].filter,
None
);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[3].filter,
None
);
}
}

View File

@ -197,7 +197,7 @@ pub struct FeldWert {
punkte: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Filter {
#[serde(rename = "Condition")]
@ -209,7 +209,7 @@ pub struct Filter {
ref_entries: Option<RefEntries>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct RefEntries {
#[serde(rename = "RefEntry")]
@ -327,6 +327,9 @@ where
if let Some(scripts_code) = &form_reference.escaped_scripts_code() {
entry.update_scripts_code(scripts_code.clone());
}
if form_reference.remove_filter {
entry.remove_filter()
}
}
}
@ -334,16 +337,19 @@ fn apply_profile_to_form_field<E>(entry: &mut E, form_field: &FormField)
where
E: FormEntry,
{
if entry.get_name() == form_field.name && form_field.hide {
entry.hide()
}
if entry.get_name() == form_field.name {
if form_field.hide {
entry.hide()
}
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());
if let Some(scripts_code) = &form_field.escaped_scripts_code() {
entry.update_scripts_code(scripts_code.clone());
}
if form_field.remove_filter {
entry.remove_filter()
}
}
}
@ -391,6 +397,7 @@ pub trait FormEntry {
fn update_scripts_code(&mut self, value: String);
fn update_default_value(&mut self, value: String);
fn hide(&mut self);
fn remove_filter(&mut self);
}
pub trait FolderContent {

View File

@ -657,6 +657,10 @@ impl FormEntry for Entry {
});
self.speichern = "0".into()
}
fn remove_filter(&mut self) {
self.filter = None;
}
}
impl Sortable for Entry {
@ -692,7 +696,7 @@ mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::Script;
use crate::model::{Filter, RefEntries, Script};
use crate::profile::Profile;
#[test]
@ -793,7 +797,7 @@ mod tests {
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_fields() {
fn should_change_unterformular_entry_scripts_code_with_form_fields() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
@ -829,7 +833,7 @@ mod tests {
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_references() {
fn should_change_unterformular_entry_scripts_code_with_form_references() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
@ -863,4 +867,100 @@ mod tests {
})
);
}
#[test]
fn should_remove_unterformular_entry_filter_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
remove_filter: true
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[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
#[test]
fn should_remove_unterformular_entry_filter_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
remove_filter: true
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[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
Some(Filter {
condition: "getGlobalSetting('mehrere_mtb_in_mtbepisode') = 'true'".into(),
valid: true,
ref_entries: Some(RefEntries { ref_entry: None })
})
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[0].filter,
None
);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].filter,
None
);
}
}

View File

@ -79,6 +79,8 @@ pub struct FormReference {
pub referenced_data_form: Option<String>,
pub anzeige: Option<String>,
pub anzeige_auswahl: Option<String>,
#[serde(default)]
pub remove_filter: bool,
scripts_code: Option<String>,
}
@ -94,6 +96,8 @@ pub struct FormField {
#[serde(default)]
pub hide: bool,
pub default_value: Option<String>,
#[serde(default)]
pub remove_filter: bool,
scripts_code: Option<String>,
}
@ -154,6 +158,7 @@ mod tests {
referenced_data_form: 'OS.Tumorkonferenz.VarianteUKW'
anzeige: 'Datum: {Datum}'
anzeige_auswahl: 'TK vom {Datum}'
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
@ -177,6 +182,7 @@ mod tests {
profile.forms[0].form_references[0].anzeige_auswahl,
Some("TK vom {Datum}".to_string())
);
assert!(profile.forms[0].form_references[0].remove_filter);
assert_eq!(
profile.forms[0].form_references[0].escaped_scripts_code(),
Some("// Example code&#10;console.log(42);".to_string())
@ -262,6 +268,7 @@ mod tests {
- name: formularfeld_to_hide
hide: true
- name: formularfeld_to_mod
remove_filter: true
scripts_code: |-
// Example code
console.log(42);
@ -278,6 +285,7 @@ mod tests {
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!(profile.forms[0].form_fields[2].remove_filter);
assert_eq!(
profile.forms[0].form_fields[2].escaped_scripts_code(),
Some("// Example code&#10;console.log(42);".to_string())