2023-08-21 06:46:25 +02:00
|
|
|
import {
|
2023-08-21 06:55:28 +02:00
|
|
|
eventSource,
|
|
|
|
this_chid,
|
|
|
|
characters,
|
|
|
|
callPopup,
|
2023-08-21 06:46:25 +02:00
|
|
|
} from "../../../script.js";
|
|
|
|
import { selected_group } from "../../group-chats.js";
|
2023-08-21 17:21:32 +02:00
|
|
|
import { loadFileToDocument } from "../../utils.js";
|
2023-08-21 06:46:25 +02:00
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
const extensionName = "gallery";
|
|
|
|
const extensionFolderPath = `scripts/extensions/${extensionName}/`;
|
|
|
|
let firstTime = true;
|
2023-08-21 06:46:25 +02:00
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
|
2023-08-21 06:46:25 +02:00
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a list of gallery items based on a given URL. This function calls an API endpoint
|
|
|
|
* to get the filenames and then constructs the item list.
|
|
|
|
*
|
|
|
|
* @param {string} url - The base URL to retrieve the list of images.
|
|
|
|
* @returns {Promise<Array>} - Resolves with an array of gallery item objects, rejects on error.
|
|
|
|
*/
|
|
|
|
async function getGalleryItems(url) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
$.get(`/listimgfiles/${url}`, function (files) {
|
|
|
|
const items = files.map((file) => ({
|
|
|
|
src: `user/images/${url}/${file}`,
|
|
|
|
srct: `user/images/${url}/${file}`,
|
|
|
|
title: "", // Optional title for each item
|
|
|
|
}));
|
|
|
|
resolve(items);
|
|
|
|
}).fail(reject);
|
|
|
|
});
|
|
|
|
}
|
2023-08-21 06:46:25 +02:00
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
/**
|
|
|
|
* Displays a character gallery. The gallery is initialized using the nanogallery2 library.
|
|
|
|
* This function takes care of preparing the gallery items, loading necessary resources,
|
|
|
|
* and ensuring body position is correctly set.
|
|
|
|
*/
|
2023-08-21 06:46:25 +02:00
|
|
|
async function showCharGallery() {
|
2023-08-21 06:55:28 +02:00
|
|
|
// Load necessary files if it's the first time calling the function
|
2023-08-21 06:46:25 +02:00
|
|
|
if (firstTime) {
|
2023-08-21 17:21:32 +02:00
|
|
|
await loadFileToDocument(
|
2023-08-21 06:55:28 +02:00
|
|
|
`${extensionFolderPath}nanogallery2.woff.min.css`,
|
|
|
|
"css"
|
|
|
|
);
|
2023-08-21 17:21:32 +02:00
|
|
|
await loadFileToDocument(
|
2023-08-21 06:55:28 +02:00
|
|
|
`${extensionFolderPath}jquery.nanogallery2.min.js`,
|
|
|
|
"js"
|
|
|
|
);
|
|
|
|
firstTime = false;
|
2023-08-21 06:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-08-21 06:55:28 +02:00
|
|
|
let url = selected_group || this_chid;
|
|
|
|
if (!selected_group && this_chid) {
|
|
|
|
const char = characters[this_chid];
|
|
|
|
url = char.avatar.replace(".png", "");
|
2023-08-21 06:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const items = await getGalleryItems(url);
|
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
let close = callPopup('<div id="my-gallery"></div>', "text");
|
2023-08-21 06:46:25 +02:00
|
|
|
if ($("body").css("position") === "fixed") {
|
|
|
|
$("body").css("position", "static");
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
$("#my-gallery").nanogallery2({
|
|
|
|
items: items,
|
|
|
|
thumbnailHeight: 150,
|
|
|
|
thumbnailWidth: 150,
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
close.then(() => {
|
2023-08-21 06:55:28 +02:00
|
|
|
$("#my-gallery").nanogallery2("destroy");
|
2023-08-21 06:46:25 +02:00
|
|
|
if ($("body").css("position") === "static") {
|
|
|
|
$("body").css("position", "fixed");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
$(document).ready(function () {
|
|
|
|
// Register an event listener
|
|
|
|
eventSource.on("charManagementDropdown", (selectedOptionId) => {
|
|
|
|
if (selectedOptionId === "show_char_gallery") {
|
|
|
|
showCharGallery();
|
|
|
|
}
|
2023-08-21 06:46:25 +02:00
|
|
|
});
|
|
|
|
|
2023-08-21 06:55:28 +02:00
|
|
|
// Add an option to the dropdown
|
|
|
|
$("#char-management-dropdown").append(
|
|
|
|
$("<option>", {
|
|
|
|
id: "show_char_gallery",
|
|
|
|
text: "Show Gallery",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|