kirche-im-netz.Startodon-Hub/src/utils/starterkit-utils.js

119 lines
4.9 KiB
JavaScript
Raw Normal View History

import { fetchProfile } from './profile-utils.js';
import { createRecommendationPopup } from './popup-utils.js';
2024-12-14 15:34:09 +01:00
import { getServerClass, extractHostname } from './instances.js';
2024-12-14 22:10:17 +01:00
import { showConsentPopup, getUserConsent, setUserConsent } from './consent-utils.js';
export function createStarterKitElement(kit) {
const template = document.getElementById('starterkit-template').content.cloneNode(true);
2024-12-12 22:30:51 +01:00
// Titel und Beschreibung
const kitElement = template.querySelector('.starterkit');
kitElement.querySelector('h3').textContent = kit.name;
kitElement.querySelector('p').textContent = kit.description;
// Aufteilen der Profilanzahl in zwei Zeilen
const profileCount = kitElement.querySelector('.profile-count');
2024-12-14 22:10:17 +01:00
profileCount.querySelector('.profile-line2').textContent = `${kit.accounts.length}`;
2024-12-19 20:40:34 +01:00
// Hinzufügen der Autoren mit 'created by'
const authorsContainer = kitElement.querySelector('.authors-container');
if (!authorsContainer) {
console.error('Fehlendes Element: .authors-container im Template. Bitte prüfen!');
return kitElement; // Gibt das unvollständige Element zurück
}
authorsContainer.innerHTML = ''; // Sicherstellen, dass der Container leer ist
if (kit.authors && kit.authors.length > 0) {
const authorsToShow = kit.authors.slice(0, 2); // Maximal zwei Autoren
const additionalAuthorsCount = kit.authors.length - authorsToShow.length;
authorsToShow.forEach(author => {
const authorLink = document.createElement('a');
authorLink.href = author;
authorLink.classList.add('account');
authorLink.target = '_blank'; // Damit die Links wie im Template ein neues Fenster öffnen
fetchProfile(author, { updateElement: authorLink }).then(() => {
const avatarImage = authorLink.querySelector('.account-avatar');
if (avatarImage) {
authorLink.textContent = '';
authorLink.appendChild(avatarImage);
// Hinzufügen der server-class für den Avatar-Rahmen
const hostname = extractHostname(author);
const serverClass = getServerClass(hostname);
if (serverClass) {
avatarImage.classList.add(serverClass);
}
}
});
authorsContainer.appendChild(authorLink);
});
// Hinweis auf zusätzliche Autoren
if (additionalAuthorsCount > 0) {
const additionalAuthorsText = document.createElement('span');
additionalAuthorsText.classList.add('additional-authors');
additionalAuthorsText.textContent = `+ ${additionalAuthorsCount}`;
authorsContainer.appendChild(additionalAuthorsText);
}
2024-12-19 20:40:34 +01:00
}
kitElement.dataset.accounts = JSON.stringify(kit.accounts);
return kitElement;
}
export function enhanceStarterKits() {
const starterkits = document.querySelectorAll('.starterkit');
starterkits.forEach(kit => {
kit.addEventListener('click', function () {
2024-12-14 22:10:17 +01:00
const userConsent = getUserConsent(); // Hole den aktuellen Zustimmungsstatus
if (userConsent === null) {
// Zeige das Popup, da der Nutzer noch nicht zugestimmt oder abgelehnt hat
showConsentPopup(
() => {
// Bestätigen-Callback: Zustimmung speichern und Profile laden
setUserConsent(true);
loadStarterKitProfiles(kit);
},
() => {
// Ablehnen-Callback: Ablehnung speichern
setUserConsent(false);
console.log('Nutzer hat die Datenabfrage abgelehnt.');
}
);
} else if (userConsent === true) {
// Nutzer hat zugestimmt: Lade die StarterKit-Profile
loadStarterKitProfiles(kit);
} else {
// Nutzer hat abgelehnt: Popup erneut anzeigen
showConsentPopup(
() => {
// Bestätigen-Callback: Zustimmung speichern und Profile laden
setUserConsent(true);
loadStarterKitProfiles(kit);
},
() => {
// Ablehnen-Callback: Ablehnung speichern und nichts tun
setUserConsent(false);
console.log('Nutzer hat die Datenabfrage erneut abgelehnt.');
}
);
}
});
});
}
2024-12-14 22:10:17 +01:00
// Funktion zum Laden der StarterKit-Profile
function loadStarterKitProfiles(kit) {
const title = kit.querySelector('h3').textContent;
const accounts = JSON.parse(kit.dataset.accounts || '[]');
const popup = createRecommendationPopup(accounts, title);
document.body.appendChild(popup);
}