From 93a4771b1c4f99a3eb7f6119565eb93ae0333209 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 27 Jan 2025 01:06:34 +0100 Subject: [PATCH] Force user to apply or revert changes in edit view before switching its display mode. --- ledger.py | 65 ++++++++++++++++++---------------- templates/_macros.tmpl | 23 ++++++++++++ templates/edit_raw.tmpl | 12 ++++--- templates/edit_structured.tmpl | 9 ++--- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/ledger.py b/ledger.py index a3bc7af..a998b97 100755 --- a/ledger.py +++ b/ledger.py @@ -245,45 +245,48 @@ class Handler(PlomHttpHandler): redir_path = Path('/') if self.pagename.startswith('edit_'): id_ = int(self.path_toks[2]) - redir_path = Path('/').joinpath('booking').joinpath(str(id_)) + redir_path = Path('/').joinpath(self.pagename).joinpath(str(id_)) if self.pagename == 'file': if 'reload' in self.postvars.as_dict: self.server.load() elif 'save' in self.postvars.as_dict: self.server.save() elif self.pagename == 'edit_structured': - line_keys = self.postvars.keys_prefixed('line_') - lineno_to_inputs: dict[int, list[str]] = {} - for key in line_keys: - toks = key.split('_', maxsplit=2) - lineno = int(toks[1]) - if lineno not in lineno_to_inputs: - lineno_to_inputs[lineno] = [] - lineno_to_inputs[lineno] += [toks[2]] - new_dat_lines = [] - indent = ' ' - for lineno, input_names in lineno_to_inputs.items(): - data = '' - comment = self.postvars.first(f'line_{lineno}_comment') - for name in input_names: - input_ = self.postvars.first(f'line_{lineno}_{name}') - if name == 'date': - data = input_ - elif name == 'target': - data += f' {input_}' - elif name == 'error': - data = f'{indent}{input_}' - elif name == 'account': - data = f'{indent}{input_}' - elif name in {'amount', 'currency'}: - data += f' {input_}' - new_dat_lines += [ + if self.postvars.first('apply'): + line_keys = self.postvars.keys_prefixed('line_') + lineno_to_inputs: dict[int, list[str]] = {} + for key in line_keys: + toks = key.split('_', maxsplit=2) + lineno = int(toks[1]) + if lineno not in lineno_to_inputs: + lineno_to_inputs[lineno] = [] + lineno_to_inputs[lineno] += [toks[2]] + new_dat_lines = [] + indent = ' ' + for lineno, input_names in lineno_to_inputs.items(): + data = '' + comment = self.postvars.first(f'line_{lineno}_comment') + for name in input_names: + input_ = self.postvars.first(f'line_{lineno}_{name}') + if name == 'date': + data = input_ + elif name == 'target': + data += f' {input_}' + elif name == 'error': + data = f'{indent}{input_}' + elif name == 'account': + data = f'{indent}{input_}' + elif name in {'amount', 'currency'}: + data += f' {input_}' + new_dat_lines += [ DatLine(f'{data} ; {comment}' if comment else data)] - self.server.rewrite_booking(id_, new_dat_lines) + self.server.rewrite_booking(id_, new_dat_lines) elif self.pagename == 'edit_raw': - new_dat_lines = [DatLine(line) for line - in self.postvars.first('booking').splitlines()] - self.server.rewrite_booking(id_, new_dat_lines) + if self.postvars.first('apply'): + new_dat_lines = [ + DatLine(line) for line + in self.postvars.first('booking').splitlines()] + self.server.rewrite_booking(id_, new_dat_lines) self.redirect(redir_path) def do_GET(self) -> None: diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl index dd45db6..d09643b 100644 --- a/templates/_macros.tmpl +++ b/templates/_macros.tmpl @@ -51,3 +51,26 @@ td.invalid, tr.warning td.invalid { background-color: #ff0000; } {% endfor %} {% endmacro %} + +{% macro taint_js() %} +function taint() { + const els = document.getElementsByClassName("enable_on_change"); + for (let i = 0; i < els.length; i++) { + els[i].disabled = false; + } + const a = document.getElementById("switch_link"); + const link_text = a.textContent; + const span = a.parentNode; + span.innerHTML = ""; + const del = document.createElement("del"); + span.appendChild(del); + del.textContent = link_text; +} +{% endmacro %} + +{% macro edit_bar(target, id) %} + + +switch to {{target}} +
+{% endmacro %} diff --git a/templates/edit_raw.tmpl b/templates/edit_raw.tmpl index ad344a8..6e3702d 100644 --- a/templates/edit_raw.tmpl +++ b/templates/edit_raw.tmpl @@ -6,13 +6,17 @@ {{ macros.css_errors() }} {% endblock %} + +{% block script %} +{{ macros.taint_js() }} +{% endblock %} + + {% block content %} -edit structured -
- -
{% endblock %} diff --git a/templates/edit_structured.tmpl b/templates/edit_structured.tmpl index af0c634..a320088 100644 --- a/templates/edit_structured.tmpl +++ b/templates/edit_structured.tmpl @@ -10,6 +10,8 @@ {% block script %} var dat_lines = {{dat_lines|tojson|safe}}; +{{ macros.taint_js() }} + function update_form() { const table = document.getElementById("dat_lines"); table.innerHTML = ""; @@ -19,7 +21,7 @@ function update_form() { btn.textContent = text; btn.type = "button"; // otherwise will act as form submit btn.disabled = disable; - btn.onclick = function() {f(); update_form();}; + btn.onclick = function() {f(); update_form(); taint(); }; } function add_td(tr, colspan=1) { const td = document.createElement("td"); @@ -37,6 +39,7 @@ function update_form() { td.appendChild(input); input.name = `line_${i}_${name}` input.value = value.trim(); + input.onchange = taint; if (dat_line.error) { td.classList.add("invalid"); } @@ -90,11 +93,9 @@ window.onload = update_form; {% block content %} -edit raw -
+{{ macros.edit_bar("raw", id) }}
-
{% endblock %} -- 2.30.2