/* * Custom LightGallery plugin to add some buttons for administration * * This file is part of photo21 * Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay * SPDX-License-Identifier: GPL-3.0-or-later */ class lgAdmin { constructor(instance, $LG) { this.core = instance; this.$LG = $LG; this.isStaff = instance.settings.isStaff; this.csrfToken = instance.settings.csrfToken; this.photoId = 0; return this; } init() { const adminIcon = ""; const deleteIcon = "";; const reportIcon = ""; // Add button linking to Django admin page 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) { 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", redirect: "manual", // do not load gallery again body: data, credentials: 'same-origin', }).then(res => { console.log("Deletion complete, response:", res); // Remove HTML element document.querySelectorAll(`[data-slide-name='${this.photoId}']`)[0].remove() this.core.closeGallery(); 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", redirect: "manual", // do not load gallery again body: data, credentials: 'same-origin', }).then(res => { 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.closeGallery(); this.core.refresh(); } else { location.reload(); } }); } } // Plugins must have destroy prototype destroy() { } }