diff --git a/photologue/static/lightgallery/plugins/admin/lg-admin.js b/photologue/static/lightgallery/plugins/admin/lg-admin.js index 9f8695c..3faef11 100644 --- a/photologue/static/lightgallery/plugins/admin/lg-admin.js +++ b/photologue/static/lightgallery/plugins/admin/lg-admin.js @@ -9,6 +9,8 @@ class lgAdmin { this.core = instance; this.$LG = $LG; this.isStaff = instance.settings.isStaff; + this.csrfToken = instance.settings.csrfToken; + this.photoId = 0; return this; } @@ -18,24 +20,78 @@ class lgAdmin { const reportIcon = ""; // Add button linking to Django admin page - this.core.$toolbar.append(`${adminIcon}`); + this.core.$toolbar.append(`${adminIcon}`); document.getElementById("lg-admin").style.display = this.isStaff ? 'block' : 'none'; // Add button to delete photo this.core.$toolbar.append(`${deleteIcon}`); document.getElementById("lg-delete").style.display = this.isStaff ? 'block' : 'none'; + document.getElementById("lg-delete").addEventListener('click', this.onDelete.bind(this)); // Add button to report photo this.core.$toolbar.append(`${reportIcon}`); + document.getElementById("lg-report").addEventListener('click', this.onReport.bind(this)); this.core.LGel.on("lgAfterSlide.admin", this.onAfterSlide.bind(this)); } + // Event called when showing a new slide onAfterSlide(event) { - const photoId = this.core.galleryItems[event.detail.index].slideName; - document.getElementById("lg-admin").href = `https://photos.crans.org/admin/photologue/photo/${photoId}/change/`; - document.getElementById("lg-delete").href = `https://photos.crans.org/admin/photologue/photo/${photoId}/delete/`; - document.getElementById("lg-report").href = `mailto:photos@crans.org?subject=[ABUS] Photo ${photoId}&body=${encodeURIComponent(window.location.href)}`; + this.photoId = this.core.galleryItems[event.detail.index].slideName; + document.getElementById("lg-admin").href = `/admin/photologue/photo/${this.photoId}/change/`; + } + + // Event called when user click on delete button + onDelete(event) { + event.preventDefault(); + if(confirm("Are you sure to delete this photo?")) { + // Build form request + let data = new FormData(); + data.append('csrfmiddlewaretoken', this.csrfToken); + fetch(`/photo/${this.photoId}/delete/`, { + method: "POST", + body: data, + credentials: 'same-origin', + }).then(res => { + if(res.ok) { + console.log("Deletion complete, response:", res); + + // Remove HTML element + document.querySelectorAll(`[data-slide-name='${this.photoId}']`)[0].remove() + this.core.goToNextSlide(); + this.core.refresh(); + } + }); + } + } + + // Event called when user click on report button + onReport(event) { + event.preventDefault(); + if(confirm("Are you sure to report this photo?")) { + // Build form request + let data = new FormData(); + data.append('csrfmiddlewaretoken', this.csrfToken); + fetch(`/photo/${this.photoId}/report/`, { + method: "POST", + body: data, + credentials: 'same-origin', + }).then(res => { + if(res.ok) { + console.log("Report complete, response:", res); + + // Update HTML element + const thumbnail = document.querySelectorAll(`[data-slide-name='${this.photoId}']`)[0]; + if (!this.isStaff) { + thumbnail.remove() + this.core.goToNextSlide(); + this.core.refresh(); + } else { + location.reload(); + } + } + }); + } } // Plugins must have destroy prototype diff --git a/photologue/templates/photologue/gallery_detail.html b/photologue/templates/photologue/gallery_detail.html index 35d3538..0d14bac 100644 --- a/photologue/templates/photologue/gallery_detail.html +++ b/photologue/templates/photologue/gallery_detail.html @@ -23,6 +23,7 @@ SPDX-License-Identifier: GPL-3.0-or-later plugins: [lgAdmin, lgHash, lgThumbnail, lgZoom], customSlideName: true, isStaff: {{ request.user.is_staff|yesno:"true,false" }}, + csrfToken: "{{ csrf_token }}", }); {% endblock %} diff --git a/photologue/templates/photologue/photo_confirm_report.html b/photologue/templates/photologue/photo_confirm_report.html new file mode 100644 index 0000000..e682b9b --- /dev/null +++ b/photologue/templates/photologue/photo_confirm_report.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} + +{% block title %}{% trans "Report confirmation" %}{% endblock %} + +{% block content %} +