Skip to content

Commit

Permalink
show excerpt in a modal popup
Browse files Browse the repository at this point in the history
  • Loading branch information
gythaogg committed May 21, 2024
1 parent 2f0ed62 commit 2869c32
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
17 changes: 17 additions & 0 deletions apis_ontology/static/scripts/show_popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function showPopup(recordId, renderStyle) {
renderStyle = "tei";
fetch(`/apis/excerpts/${recordId}/${renderStyle}`)
.then(response => response.text())
.then(data => {
// Display the data in the popup
document.getElementById('popupContent').innerHTML = data;
document.getElementById('popupModal').style.display = 'block';
})
.catch(error => {
console.error('Error fetching dynamic content:', error);
});
}

function closePopup() {
document.getElementById('popupModal').style.display = 'none';
}
16 changes: 1 addition & 15 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,8 @@ def render_tei_refs(self, value):
links = []
for xml_id in xml_ids:
true_id = xml_id.replace('"', "").replace("xml:id=", "").strip()
excerpt_xml = "" # Fallback blank excerpt
try:
excerpt_xml = Excerpts.objects.get(xml_id=true_id).xml_content

except Excerpts.DoesNotExist:
logger.error(
"Could not find excerpt with id %s in the database.", true_id
)
except Exception as e:
logger.error(repr(e))
encoded_excerpt_xml = escape(
excerpt_xml.replace("'", "\\'").replace("\n", "")
)
# Append the link with the onclick event
links.append(
f'<a href="#" onclick="alert(\'{encoded_excerpt_xml}\'); return false;">{true_id}</a>'
f"""<a href="#" onclick="showPopup('{true_id}'); return false;">{true_id}</a>"""
)

return mark_safe("<br />".join(links))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
{% endblock col-zero %}

{% block col-one %}
{% include "excerpts/popup.html" %}

<h4>Relations</h4>
{% related_entity_types as related_entity_types %}
<div class="container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load apiscore %}
{% load relationsng %}
{% load relations %}
{% include "excerpts/popup.html" %}

<h4>Relations</h4> <!-- Edit View -->
{% related_entity_types as related_entity_types %}
Expand Down
19 changes: 19 additions & 0 deletions apis_ontology/templates/excerpts/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script src="/static/scripts/show_popup.js"></script>

<div class="modal" id="popupModal" style="display:none;" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Excerpt</h5>
</div>
<div class="modal-body">

<span id="popupContent"></span>
</div>
<div class="modal-footer">

<button onclick="closePopup()">Close</button>
</div>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions apis_ontology/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from apis_ontology.views import ExcerptsView
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView
Expand All @@ -13,6 +14,11 @@
path("accounts/", include("django.contrib.auth.urls")),
path("entity/<int:pk>/", GetEntityGeneric.as_view(), name="GetEntityGenericRoot"),
path("", TemplateView.as_view(template_name="base.html")),
path(
"apis/excerpts/<str:xml_id>/<str:render_style>/",
ExcerptsView.as_view(),
name="excerpts_view",
),
]

urlpatterns += staticfiles_urlpatterns()
Expand Down
19 changes: 19 additions & 0 deletions apis_ontology/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.http import HttpResponse
from django.views import View
from django.shortcuts import get_object_or_404
from .models import Excerpts


class ExcerptsView(View):
def get(self, request, xml_id, render_style, *args, **kwargs):
print("You are here!")
record = get_object_or_404(Excerpts, xml_id=xml_id)

# Generate dynamic content based on link_type
if render_style == "tei":
# TODO: render correctly with xslt
content = f"{record.xml_content}"
else:
content = f"{record.xml_content}"

return HttpResponse(content)

0 comments on commit 2869c32

Please sign in to comment.