From a2f2104a08743610d85df3f0c30055ddc8a62813 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 14 Mar 2022 10:42:53 -0700 Subject: [PATCH 1/4] Create non-functional UI for editing array fields --- .../templates/book/edit/edit_book_form.html | 70 ++++++++++++------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/bookwyrm/templates/book/edit/edit_book_form.html b/bookwyrm/templates/book/edit/edit_book_form.html index 38a7fe35..d886d12f 100644 --- a/bookwyrm/templates/book/edit/edit_book_form.html +++ b/bookwyrm/templates/book/edit/edit_book_form.html @@ -22,7 +22,7 @@ {% trans "Title:" %} - + {% include 'snippets/form_errors.html' with errors_list=form.title.errors id="desc_title" %} @@ -31,7 +31,7 @@ {% trans "Subtitle:" %} - + {% include 'snippets/form_errors.html' with errors_list=form.subtitle.errors id="desc_subtitle" %} @@ -40,7 +40,7 @@ {% trans "Description:" %} {{ form.description }} - + {% include 'snippets/form_errors.html' with errors_list=form.description.errors id="desc_description" %} @@ -51,7 +51,7 @@ {% trans "Series:" %} - + {% include 'snippets/form_errors.html' with errors_list=form.series.errors id="desc_series" %} @@ -61,7 +61,7 @@ {% trans "Series number:" %} {{ form.series_number }} - + {% include 'snippets/form_errors.html' with errors_list=form.series_number.errors id="desc_series_number" %} @@ -75,21 +75,41 @@ {% trans "Separate multiple values with commas." %} - + {% include 'snippets/form_errors.html' with errors_list=form.languages.errors id="desc_languages" %} -
-
@@ -106,7 +126,7 @@ {% trans "Separate multiple values with commas." %} - + {% include 'snippets/form_errors.html' with errors_list=form.publishers.errors id="desc_publishers" %} @@ -115,7 +135,7 @@ {% trans "First published date:" %} - + {% include 'snippets/form_errors.html' with errors_list=form.first_published_date.errors id="desc_first_published_date" %} @@ -124,7 +144,7 @@ {% trans "Published date:" %} - + {% include 'snippets/form_errors.html' with errors_list=form.published_date.errors id="desc_published_date" %} @@ -193,7 +213,7 @@ - + {% include 'snippets/form_errors.html' with errors_list=form.cover.errors id="desc_cover" %} @@ -214,7 +234,7 @@
{{ form.physical_format }}
- + {% include 'snippets/form_errors.html' with errors_list=form.physical_format.errors id="desc_physical_format" %} @@ -224,7 +244,7 @@ {% trans "Format details:" %} {{ form.physical_format_detail }} - + {% include 'snippets/form_errors.html' with errors_list=form.physical_format_detail.errors id="desc_physical_format_detail" %} @@ -235,7 +255,7 @@ {% trans "Pages:" %} {{ form.pages }} - + {% include 'snippets/form_errors.html' with errors_list=form.pages.errors id="desc_pages" %} @@ -251,7 +271,7 @@ {% trans "ISBN 13:" %} {{ form.isbn_13 }} - + {% include 'snippets/form_errors.html' with errors_list=form.isbn_13.errors id="desc_isbn_13" %} @@ -260,7 +280,7 @@ {% trans "ISBN 10:" %} {{ form.isbn_10 }} - + {% include 'snippets/form_errors.html' with errors_list=form.isbn_10.errors id="desc_isbn_10" %} @@ -269,7 +289,7 @@ {% trans "Openlibrary ID:" %} {{ form.openlibrary_key }} - + {% include 'snippets/form_errors.html' with errors_list=form.openlibrary_key.errors id="desc_openlibrary_key" %} @@ -278,7 +298,7 @@ {% trans "Inventaire ID:" %} {{ form.inventaire_id }} - + {% include 'snippets/form_errors.html' with errors_list=form.inventaire_id.errors id="desc_inventaire_id" %} @@ -287,7 +307,7 @@ {% trans "OCLC Number:" %} {{ form.oclc_number }} - + {% include 'snippets/form_errors.html' with errors_list=form.oclc_number.errors id="desc_oclc_number" %} @@ -296,7 +316,7 @@ {% trans "ASIN:" %} {{ form.asin }} - + {% include 'snippets/form_errors.html' with errors_list=form.ASIN.errors id="desc_ASIN" %} From 35e6dede098b46c1d0b851281657b5e03c82061d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 14 Mar 2022 11:34:14 -0700 Subject: [PATCH 2/4] Script to remove input fields --- bookwyrm/static/js/forms.js | 17 +++++++++++++++++ .../templates/book/edit/edit_book_form.html | 8 ++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bookwyrm/static/js/forms.js b/bookwyrm/static/js/forms.js index 7d946d14..99887389 100644 --- a/bookwyrm/static/js/forms.js +++ b/bookwyrm/static/js/forms.js @@ -1,6 +1,19 @@ (function () { "use strict"; + /** + * Remoev input field + * + * @param {event} the button click event + */ + function removeInput(event) { + const trigger = event.currentTarget; + const input_id = trigger.dataset.remove; + const input = document.getElementById(input_id); + + input.remove(); + } + /** * Duplicate an input field * @@ -29,4 +42,8 @@ document .querySelectorAll("[data-duplicate]") .forEach((node) => node.addEventListener("click", duplicateInput)); + + document + .querySelectorAll("[data-remove]") + .forEach((node) => node.addEventListener("click", removeInput)); })(); diff --git a/bookwyrm/templates/book/edit/edit_book_form.html b/bookwyrm/templates/book/edit/edit_book_form.html index d886d12f..fc15d26a 100644 --- a/bookwyrm/templates/book/edit/edit_book_form.html +++ b/bookwyrm/templates/book/edit/edit_book_form.html @@ -87,12 +87,16 @@ -
+
-
{% endfor %} - + {% include 'snippets/form_errors.html' with errors_list=form.subjects.errors id="desc_subjects" %}
@@ -186,7 +201,12 @@ {% endfor %} - + + + From a37f83c458594219efc9722c1fe182ddf6520a77 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 14 Mar 2022 14:55:41 -0700 Subject: [PATCH 4/4] Get the field working --- bookwyrm/forms/books.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bookwyrm/forms/books.py b/bookwyrm/forms/books.py index 91d9b8f0..72df1371 100644 --- a/bookwyrm/forms/books.py +++ b/bookwyrm/forms/books.py @@ -14,6 +14,14 @@ class CoverForm(CustomForm): help_texts = {f: None for f in fields} +class ArrayWidget(forms.widgets.TextInput): + # pylint: disable=unused-argument + # pylint: disable=no-self-use + def value_from_datadict(self, data, files, name): + """get all values for this name""" + return [i for i in data.getlist(name) if i] + + class EditionForm(CustomForm): class Meta: model = models.Edition @@ -41,12 +49,10 @@ class EditionForm(CustomForm): "series_number": forms.TextInput( attrs={"aria-describedby": "desc_series_number"} ), + "subjects": ArrayWidget(), "languages": forms.TextInput( attrs={"aria-describedby": "desc_languages_help desc_languages"} ), - "subjects": forms.TextInput( - attrs={"aria-describedby": "desc_subjects"} - ), "publishers": forms.TextInput( attrs={"aria-describedby": "desc_publishers_help desc_publishers"} ),