1
use super::{Cell, Choice, Input};
2

            
3
/// The values of a choice cell, each with its label.
4
type Options = &'static [(&'static str, &'static str)];
5

            
6
/// The submit button of each form.
7
const SUBMITS: &[(&str, &str)] = &[
8
    ("transaction-create", "Create transaction"),
9
    ("transaction-edit", "Save"),
10
    ("transaction-query", "Show transactions"),
11
    ("report-balance", "Generate"),
12
    ("report-activity", "Generate"),
13
    ("account-merge", "Review merge"),
14
];
15

            
16
/// The add and remove buttons of each kind of row.
17
const ROW_BUTTONS: &[(&str, &str, &str)] = &[
18
    ("splits", "Add split", "Remove split"),
19
    ("tags", "Add tag", "Remove tag"),
20
    ("split-tags", "Add split tag", "Remove split tag"),
21
];
22

            
23
493
pub(super) fn submit_text(form: &str) -> String {
24
493
    let text = SUBMITS
25
493
        .iter()
26
2100
        .find(|(name, _)| *name == form)
27
493
        .map_or("Submit", |(_, text)| text);
28
493
    t!(text).to_string()
29
493
}
30

            
31
1637
fn row_buttons(rows: &str) -> (&'static str, &'static str) {
32
1637
    ROW_BUTTONS
33
1637
        .iter()
34
3555
        .find(|(kind, _, _)| *kind == rows)
35
1637
        .map_or(("Add row", "Remove"), |(_, add, remove)| (add, remove))
36
1637
}
37

            
38
627
pub(super) fn add_text(rows: &str) -> String {
39
627
    t!(row_buttons(rows).0).to_string()
40
627
}
41

            
42
1010
pub(super) fn remove_text(rows: &str) -> String {
43
1010
    t!(row_buttons(rows).1).to_string()
44
1010
}
45

            
46
591
fn choices(options: Options, value: &str) -> Vec<Choice> {
47
591
    options
48
591
        .iter()
49
        .map(|&(option, label)| Choice {
50
1731
            value: option,
51
1731
            label: t!(label).to_string(),
52
1731
            selected: option == value,
53
1731
        })
54
        .collect()
55
591
}
56

            
57
/// A descriptor choice: the default stands in for an empty value, and an
58
/// optional field without a default gets an empty first option.
59
591
pub(super) fn field_choice(
60
591
    options: Options,
61
591
    default: Option<&str>,
62
591
    required: bool,
63
591
    value: &str,
64
591
) -> Input {
65
591
    let value = if value.is_empty() {
66
481
        default.unwrap_or_default()
67
    } else {
68
110
        value
69
    };
70
591
    let blank = Choice {
71
591
        value: "",
72
591
        label: String::new(),
73
591
        selected: value.is_empty(),
74
591
    };
75
591
    let blank = (default.is_none() && !required).then_some(blank);
76
591
    Input::Choice(blank.into_iter().chain(choices(options, value)).collect())
77
591
}
78

            
79
7049
pub(super) fn mark(rows: &str, key: &str, cell: &mut Cell) {
80
7049
    let Cell::Control(control) = cell else { return };
81
5776
    if let (Input::Text, ("", "filter")) = (&control.input, (rows, key)) {
82
74
        control.input = Input::Filter(format!("\n{}", control.value));
83
5702
    }
84
7049
}