image-rights-html-generator/js/image-rights-html-generator.js

84 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2024-06-03 21:21:41 +00:00
(function($) {
$(document).ready(function() {
2024-06-05 08:55:48 +00:00
// Event-Listener für das Öffnen des Modals
2024-06-03 21:21:41 +00:00
$(document).on('click', '#openModal', function() {
$('#modal').removeClass('hidden');
// Auslesen der bestehenden Beschriftung
const captionFields = [
$('#attachment-details-two-column-caption'),
$('#attachment-details-caption'),
$('#attachment_caption')
];
let currentCaption = '';
for (const field of captionFields) {
if (field.length && field.val()) {
currentCaption = field.val();
break;
}
}
// Extrahieren der Werte aus der bestehenden Beschriftung und ins Formular einfügen
const imageTitle = currentCaption.match(/<a href="[^"]*" target="_blank" rel="noreferrer noopener">([^<]*)<\/a>/);
const imageSourceUrl = currentCaption.match(/<a href="([^"]*)" target="_blank" rel="noreferrer noopener">[^<]*<\/a>/);
const imageAuthor = currentCaption.match(/von: <a href="[^"]*" target="_blank" rel="noreferrer noopener">([^<]*)<\/a>/);
const authorImageUrl = currentCaption.match(/von: <a href="([^"]*)" target="_blank" rel="noreferrer noopener">[^<]*<\/a>/);
const imageLicense = currentCaption.match(/Lizenz\/Rechte: <a href="[^"]*" target="_blank" rel="noreferrer noopener">([^<]*)<\/a>/);
const licenseUrl = currentCaption.match(/Lizenz\/Rechte: <a href="([^"]*)" target="_blank" rel="noreferrer noopener">[^<]*<\/a>/);
// Füllen der Formularfelder mit den extrahierten Werten
$('#imageTitle').val(imageTitle ? imageTitle[1] : '');
$('#imageSourceUrl').val(imageSourceUrl ? imageSourceUrl[1] : '');
$('#imageAuthor').val(imageAuthor ? imageAuthor[1] : '');
$('#authorImageUrl').val(authorImageUrl ? authorImageUrl[1] : '');
$('#imageLicense').val(imageLicense ? imageLicense[1] : '');
$('#licenseUrl').val(licenseUrl ? licenseUrl[1] : '');
});
// Event-Listener für das Schließen des Modals
$(document).on('click', '#closeModal', function() {
$('#modal').addClass('hidden');
});
// Event-Listener für das Speichern der Daten und das Einfügen in das Beschriftungsfeld
$('#metaDataForm').on('submit', function(event) {
event.preventDefault();
// Sammeln der Werte aus den Formularfeldern
const imageTitle = $('#imageTitle').val();
const imageSourceUrl = $('#imageSourceUrl').val();
const imageAuthor = $('#imageAuthor').val();
const authorImageUrl = $('#authorImageUrl').val();
const imageLicense = $('#imageLicense').val();
const licenseUrl = $('#licenseUrl').val();
// Erstellen des neuen HTML-Snippets für die Bildbeschriftung
2024-06-04 08:24:26 +00:00
let newCaption = `<a href="${imageSourceUrl}" target="_blank" rel="noreferrer noopener">${imageTitle}</a> |`;
2024-06-03 21:21:41 +00:00
if (imageAuthor) {
2024-06-04 08:24:26 +00:00
newCaption += ` von: <a href="${authorImageUrl}" target="_blank" rel="noreferrer noopener">${imageAuthor}</a> |`;
2024-06-03 21:21:41 +00:00
}
newCaption += ` Lizenz/Rechte: <a href="${licenseUrl}" target="_blank" rel="noreferrer noopener">${imageLicense}</a>`;
// Einfügen des neuen HTML-Snippets in das passende Beschriftungsfeld
const captionFields = [
$('#attachment-details-two-column-caption'),
$('#attachment-details-caption'),
$('#attachment_caption')
];
for (const field of captionFields) {
if (field.length) {
field.val(newCaption);
break;
}
}
// Schließen des Modals
$('#modal').addClass('hidden');
});
});
})(jQuery);