-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ReferenceOnListView): add form to add new reference
This commit introduces a `ReferenceNewForm`, which is a basic form for References. This form is added to the ReferenceOnListView. The form is also included in the ReferenceOnListView template. It uses htmx, so the page does not have to reload if a new entry is added. To make this work, there is a `reinit_select2.html` partial which works around bugs with the dal/select2 implementation in and htmx. Another fix is in the `fix_select2_bootstrap_focus.html` partial: if we want to load a view with a select2 form via a modal, we have to override a function to prevent the bootstrap modal from stealing the focus.
- Loading branch information
Showing
6 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apis_bibsonomy/templates/apis_bibsonomy/partials/fix_select2_bootstrap_focus.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% comment %} | ||
This is a workaround for select2 and bootstrap getting into each others way. | ||
Copied from: | ||
* https://stackoverflow.com/questions/18487056/select2-doesnt-work-when-embedded-in-a-bootstrap-modal | ||
|
||
See also | ||
* https://github.com/yourlabs/django-autocomplete-light/issues/715 | ||
{% endcomment %} | ||
<script> | ||
$(document).ready(function () { | ||
$.fn.modal.Constructor.prototype._enforceFocus = function() {}; | ||
}); | ||
</script> |
7 changes: 7 additions & 0 deletions
7
apis_bibsonomy/templates/apis_bibsonomy/partials/fix_select2_bootstrap_overflow.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<style> | ||
.select2-selection__rendered{ | ||
word-wrap: break-word !important; | ||
text-overflow: inherit !important; | ||
white-space: normal !important; | ||
} | ||
</style> |
29 changes: 26 additions & 3 deletions
29
apis_bibsonomy/templates/apis_bibsonomy/partials/reference_list.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,33 @@ | ||
<ul> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<div id="referencelist"> | ||
{% include "apis_bibsonomy/partials/fix_select2_bootstrap_overflow.html" %} | ||
<ul class="list-group list-group-flush"> | ||
{% for reference in object_list %} | ||
<li> | ||
<a href="{{ reference.get_absolute_url }}">{{ reference }} ({{ reference.id }})</a> | ||
<li class="list-group-item justify-content-between align-items-center d-flex"> | ||
<a href="{{ reference.get_absolute_url }}">{{ reference }} ({{ reference.id }})</a> | ||
<a href="{% url "apis_bibsonomy:referencedelete" reference.id %}?redirect={{ request.path }}" | ||
hx-delete="{% url "apis_bibsonomy:referencedelete" reference.id %}" | ||
hx-confirm="Are your sure you want to delete reference {{ reference }} for {{ reference.referenced_object }}" | ||
hx-target="closest li" | ||
hx-swap="outerHTML swap:1s">Delete</a> | ||
</li> | ||
{% empty %} | ||
<li>No references yet.</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
{% if form %} | ||
{% load crispy_forms_tags %} | ||
<form method="post" hx-post="{{ request.path }}" hx-target="#referencelist" hx-swap="outerHTML" class="mt-4"> | ||
{% crispy form %} | ||
</form> | ||
{% endif %} | ||
|
||
<script> | ||
document.body.addEventListener('htmx:configRequest', (event) => { | ||
event.detail.headers['X-CSRFToken'] = '{{ csrf_token }}'; | ||
}) | ||
</script> | ||
{% include "apis_bibsonomy/partials/reinit_select2.html" %} | ||
{% include "apis_bibsonomy/partials/fix_select2_bootstrap_focus.html" %} | ||
</div> |
32 changes: 32 additions & 0 deletions
32
apis_bibsonomy/templates/apis_bibsonomy/partials/reinit_select2.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<script> | ||
{# this is a simplified copy of dal upstreams `template` method #} | ||
function tohtml(item) { | ||
var $result = $('<span>'); | ||
$result.html(item.text); | ||
return $result; | ||
} | ||
{% comment %} | ||
We add our own Select2 reinitialization function, | ||
becaus there is none from upstream yet: | ||
https://github.com/yourlabs/django-autocomplete-light/issues/1311 | ||
and | ||
https://github.com/yourlabs/django-autocomplete-light/issues/1221 | ||
|
||
The above mentioned solutions did not work in our case. | ||
{% endcomment %} | ||
function reinitSelect2(something) { | ||
$('#id_bibs_url').select2({ | ||
ajax: { | ||
url: $('#id_bibs_url').data('autocomplete-light-url'), | ||
}, | ||
templateResult: tohtml, | ||
templateSelection: tohtml, | ||
}); | ||
$('.select2-selection').addClass("form-control"); | ||
} | ||
|
||
{# If htmx is available, we reinitialize the #bibs_url field after htmx events #} | ||
htmx.on("htmx:afterSettle", function(evt) { | ||
reinitSelect2(evt.detail.elt); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters