diff --git a/bookwyrm/migrations/0122_alter_annualgoal_year.py b/bookwyrm/migrations/0122_alter_annualgoal_year.py
new file mode 100644
index 00000000..90af5fcc
--- /dev/null
+++ b/bookwyrm/migrations/0122_alter_annualgoal_year.py
@@ -0,0 +1,19 @@
+# Generated by Django 3.2.5 on 2022-01-04 18:59
+
+import bookwyrm.models.user
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("bookwyrm", "0121_user_summary_keys"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="annualgoal",
+ name="year",
+ field=models.IntegerField(default=bookwyrm.models.user.get_current_year),
+ ),
+ ]
diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py
index deec2a44..bd340b01 100644
--- a/bookwyrm/models/user.py
+++ b/bookwyrm/models/user.py
@@ -415,12 +415,17 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
return activity_object
+def get_current_year():
+ """sets default year for annual goal to this year"""
+ return timezone.now().year
+
+
class AnnualGoal(BookWyrmModel):
"""set a goal for how many books you read in a year"""
user = models.ForeignKey("User", on_delete=models.PROTECT)
goal = models.IntegerField(validators=[MinValueValidator(1)])
- year = models.IntegerField(default=timezone.now().year)
+ year = models.IntegerField(default=get_current_year)
privacy = models.CharField(
max_length=255, default="public", choices=fields.PrivacyLevels.choices
)
diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js
index 6d21c207..cf3944b3 100644
--- a/bookwyrm/static/js/bookwyrm.js
+++ b/bookwyrm/static/js/bookwyrm.js
@@ -41,6 +41,7 @@ let BookWyrm = new (class {
document
.querySelectorAll("[data-duplicate]")
.forEach((node) => node.addEventListener("click", this.duplicateInput.bind(this)));
+
document
.querySelectorAll("details.dropdown")
.forEach((node) =>
@@ -60,6 +61,9 @@ let BookWyrm = new (class {
.querySelectorAll('input[type="file"]')
.forEach(bookwyrm.disableIfTooLarge.bind(bookwyrm));
document.querySelectorAll("[data-copytext]").forEach(bookwyrm.copyText.bind(bookwyrm));
+ document
+ .querySelectorAll(".modal.is-active")
+ .forEach(bookwyrm.handleActiveModal.bind(bookwyrm));
});
}
@@ -405,7 +409,7 @@ let BookWyrm = new (class {
}
/**
- * Handle the modal component.
+ * Handle the modal component with a button trigger.
*
* @param {Event} event - Event fired by an element
* with the `data-modal-open` attribute
@@ -416,6 +420,7 @@ let BookWyrm = new (class {
* for information about using the modal.
*/
handleModalButton(event) {
+ const { handleFocusTrap } = this;
const modalButton = event.currentTarget;
const targetModalId = modalButton.dataset.modalOpen;
const htmlElement = document.querySelector("html");
@@ -427,6 +432,8 @@ let BookWyrm = new (class {
// Helper functions
function handleModalOpen(modalElement) {
+ event.preventDefault();
+
htmlElement.classList.add("is-clipped");
modalElement.classList.add("is-active");
modalElement.getElementsByClassName("modal-card")[0].focus();
@@ -455,42 +462,48 @@ let BookWyrm = new (class {
modalButton.focus();
}
- function handleFocusTrap(event) {
- if (event.key !== "Tab") {
- return;
- }
-
- const focusableEls = event.currentTarget.querySelectorAll(
- [
- "a[href]:not([disabled])",
- "button:not([disabled])",
- "textarea:not([disabled])",
- 'input:not([type="hidden"]):not([disabled])',
- "select:not([disabled])",
- "details:not([disabled])",
- '[tabindex]:not([tabindex="-1"]):not([disabled])',
- ].join(",")
- );
- const firstFocusableEl = focusableEls[0];
- const lastFocusableEl = focusableEls[focusableEls.length - 1];
-
- if (event.shiftKey) {
- /* Shift + tab */ if (document.activeElement === firstFocusableEl) {
- lastFocusableEl.focus();
- event.preventDefault();
- }
- } /* Tab */ else {
- if (document.activeElement === lastFocusableEl) {
- firstFocusableEl.focus();
- event.preventDefault();
- }
- }
- }
-
// Open modal
handleModalOpen(modal);
}
+ /**
+ * Handle the modal component when opened at page load.
+ *
+ * @param {Element} modalElement - Active modal element
+ * @return {undefined}
+ *
+ */
+ handleActiveModal(modalElement) {
+ if (!modalElement) {
+ return;
+ }
+
+ const { handleFocusTrap } = this;
+
+ modalElement.getElementsByClassName("modal-card")[0].focus();
+
+ const closeButtons = modalElement.querySelectorAll("[data-modal-close]");
+
+ closeButtons.forEach((button) => {
+ button.addEventListener("click", function () {
+ handleModalClose(modalElement);
+ });
+ });
+
+ document.addEventListener("keydown", function (event) {
+ if (event.key === "Escape") {
+ handleModalClose(modalElement);
+ }
+ });
+
+ modalElement.addEventListener("keydown", handleFocusTrap);
+
+ function handleModalClose(modalElement) {
+ modalElement.removeEventListener("keydown", handleFocusTrap);
+ history.back();
+ }
+ }
+
/**
* Display pop up window.
*
diff --git a/bookwyrm/templates/author/author.html b/bookwyrm/templates/author/author.html
index 66ecb062..2f3ed6f0 100644
--- a/bookwyrm/templates/author/author.html
+++ b/bookwyrm/templates/author/author.html
@@ -92,10 +92,11 @@
{% trans "View on OpenLibrary" %}
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
- {% with controls_text="ol_sync" controls_uid=author.id %}
- {% include 'snippets/toggle/toggle_button.html' with text=button_text focus="modal_title_ol_sync" class="is-small" icon_with_text="download" %}
- {% include "author/sync_modal.html" with source="openlibrary.org" source_name="OpenLibrary" %}
- {% endwith %}
+
+ {% include "author/sync_modal.html" with source="openlibrary.org" source_name="OpenLibrary" id="openlibrary_sync" %}
{% endif %}
{% endif %}
@@ -107,10 +108,11 @@
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
- {% with controls_text="iv_sync" controls_uid=author.id %}
- {% include 'snippets/toggle/toggle_button.html' with text=button_text focus="modal_title_iv_sync" class="is-small" icon_with_text="download" %}
- {% include "author/sync_modal.html" with source="inventaire.io" source_name="Inventaire" %}
- {% endwith %}
+
+ {% include "author/sync_modal.html" with source="inventaire.io" source_name="Inventaire" id="inventaire_sync" %}
{% endif %}
{% endif %}
diff --git a/bookwyrm/templates/author/sync_modal.html b/bookwyrm/templates/author/sync_modal.html
index a061ada8..a6e032cb 100644
--- a/bookwyrm/templates/author/sync_modal.html
+++ b/bookwyrm/templates/author/sync_modal.html
@@ -19,12 +19,8 @@
{% endblock %}
{% block modal-footer %}
-
-
-{% trans "Cancel" as button_text %}
-{% include 'snippets/toggle/toggle_button.html' with text=button_text %}
+
+
{% endblock %}
{% block modal-form-close %}{% endblock %}
diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html
index 4903da87..19dbccbd 100644
--- a/bookwyrm/templates/book/book.html
+++ b/bookwyrm/templates/book/book.html
@@ -63,7 +63,11 @@
{% if not book.cover %}
{% if user_authenticated %}
-
-{% include 'snippets/delete_readthrough_modal.html' with controls_text="delete_readthrough" controls_uid=readthrough.id no_body=True %}
+{% join "delete_readthrough" readthrough.id as modal_id %}
+{% include 'book/delete_readthrough_modal.html' with id=modal_id %}
diff --git a/bookwyrm/templates/book/sync_modal.html b/bookwyrm/templates/book/sync_modal.html
index d80bf25f..6e5df0c0 100644
--- a/bookwyrm/templates/book/sync_modal.html
+++ b/bookwyrm/templates/book/sync_modal.html
@@ -19,12 +19,8 @@
{% endblock %}
{% block modal-footer %}
-
- {% trans "Confirm" %}
-
-
-{% trans "Cancel" as button_text %}
-{% include 'snippets/toggle/toggle_button.html' with text=button_text %}
+{% trans "Confirm" %}
+{% trans "Cancel" %}
{% endblock %}
{% block modal-form-close %}{% endblock %}
diff --git a/bookwyrm/templates/components/modal.html b/bookwyrm/templates/components/modal.html
index 2eabd2e2..3e1cc759 100644
--- a/bookwyrm/templates/components/modal.html
+++ b/bookwyrm/templates/components/modal.html
@@ -1,40 +1,33 @@
{% load i18n %}
-
- {# @todo Implement focus traps to prevent tabbing out of the modal. #}
-
- {% trans "Close" as label %}
-
-
-
+
+
+
+
+
+
- {% if static %}
- {{ label }}
- {% else %}
- {% include 'snippets/toggle/toggle_button.html' with label=label class="delete" nonbutton=True %}
- {% endif %}
+
+
{% block modal-form-open %}{% endblock %}
- {% if not no_body %}
{% block modal-body %}{% endblock %}
- {% endif %}
{% block modal-form-close %}{% endblock %}
- {% if static %}
-
{{ label }}
- {% else %}
- {% include 'snippets/toggle/toggle_button.html' with label=label class="modal-close is-large" nonbutton=True %}
- {% endif %}
+
-
diff --git a/bookwyrm/templates/components/new_modal.html b/bookwyrm/templates/components/new_modal.html
deleted file mode 100644
index c4927e52..00000000
--- a/bookwyrm/templates/components/new_modal.html
+++ /dev/null
@@ -1,31 +0,0 @@
-{% load i18n %}
-
-
-
-
-
-
- {% block modal-title %}{% endblock %}
-
-
-
-
-
- {% block modal-body %}{% endblock %}
-
-
-
-
-
diff --git a/bookwyrm/templates/groups/delete_group_modal.html b/bookwyrm/templates/groups/delete_group_modal.html
index 4bb87ed9..592a861f 100644
--- a/bookwyrm/templates/groups/delete_group_modal.html
+++ b/bookwyrm/templates/groups/delete_group_modal.html
@@ -14,8 +14,7 @@
{% trans "Delete" %}
- {% trans "Cancel" as button_text %}
- {% include 'snippets/toggle/toggle_button.html' with text=button_text controls_text="delete_group" controls_uid=group.id %}
+ {% trans "Cancel" %}
{% endblock %}
diff --git a/bookwyrm/templates/groups/edit_form.html b/bookwyrm/templates/groups/edit_form.html
index 1c58dc77..a83ad9fb 100644
--- a/bookwyrm/templates/groups/edit_form.html
+++ b/bookwyrm/templates/groups/edit_form.html
@@ -9,5 +9,5 @@
-{% include "groups/delete_group_modal.html" with controls_text="delete_group" controls_uid=group.id %}
+{% include "groups/delete_group_modal.html" with id="delete_group" %}
{% endblock %}
diff --git a/bookwyrm/templates/groups/form.html b/bookwyrm/templates/groups/form.html
index 21c525ee..2b3dd635 100644
--- a/bookwyrm/templates/groups/form.html
+++ b/bookwyrm/templates/groups/form.html
@@ -14,21 +14,20 @@
-
-
-
-
- {% include 'snippets/privacy_select_no_followers.html' with current=group.privacy %}
-
-
- {% trans "Save" %}
-
-
-
+
{% if group.id %}
-
- {% trans "Delete group" as button_text %}
- {% include 'snippets/toggle/toggle_button.html' with class="is-danger" text=button_text icon_with_text="x" controls_text="delete_group" controls_uid=group.id focus="modal_title_delete_group" %}
+
+
+ {% trans "Delete group" %}
+
{% endif %}
+
+
+ {% include 'snippets/privacy_select_no_followers.html' with current=group.privacy %}
+
+
+ {% trans "Save" %}
+
+
diff --git a/bookwyrm/templates/lists/delete_list_modal.html b/bookwyrm/templates/lists/delete_list_modal.html
index 7861e78c..aad87952 100644
--- a/bookwyrm/templates/lists/delete_list_modal.html
+++ b/bookwyrm/templates/lists/delete_list_modal.html
@@ -1,4 +1,4 @@
-{% extends 'components/new_modal.html' %}
+{% extends 'components/modal.html' %}
{% load i18n %}
{% block modal-title %}{% trans "Delete this list?" %}{% endblock %}
diff --git a/bookwyrm/templates/snippets/progress_field.html b/bookwyrm/templates/snippets/progress_field.html
index d61956ba..4ceed04c 100644
--- a/bookwyrm/templates/snippets/progress_field.html
+++ b/bookwyrm/templates/snippets/progress_field.html
@@ -5,7 +5,7 @@
type="number"
name="progress"
class="input"
- id="id_progress_{{ readthrough.id }}{{ controls_uid }}"
+ id="{{ field_id }}"
value="{{ readthrough.progress }}"
{% if progress_required %}required{% endif %}
>
diff --git a/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html b/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html
index 3e0fd45e..919d2100 100644
--- a/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html
+++ b/bookwyrm/templates/snippets/reading_modals/progress_update_modal.html
@@ -1,5 +1,6 @@
{% extends 'snippets/reading_modals/layout.html' %}
{% load i18n %}
+{% load utilities %}
{% block modal-title %}
{% trans "Update progress" %}
@@ -12,8 +13,9 @@
{% endblock %}
{% block reading-dates %}
-
-{% include "snippets/progress_field.html" with progress_required=True %}
+{% join "id_progress" uuid as field_id %}
+
+{% include "snippets/progress_field.html" with progress_required=True id=field_id %}
{% endblock %}
{% block form %}
diff --git a/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html b/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html
index 60c7a89e..861f118f 100644
--- a/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html
+++ b/bookwyrm/templates/snippets/reading_modals/start_reading_modal.html
@@ -11,7 +11,7 @@ Start "
{{ book_title }}"
{% block modal-form-open %}
{# Only show progress for editing existing readthroughs #}
{% if readthrough.id and not readthrough.finish_date %}
-