jQuery(document).ready(function($) {
// Medienbibliothek für neues Teammitglied
var newTeamcardFrame;
$('#new-teamcard-bild-button').on('click', function(e) {
e.preventDefault();
if (newTeamcardFrame) {
newTeamcardFrame.open();
return;
}
newTeamcardFrame = wp.media({
title: 'Bild auswählen',
button: { text: 'Verwenden' },
multiple: false
});
newTeamcardFrame.on('select', function() {
var attachment = newTeamcardFrame.state().get('selection').first().toJSON();
$('#new-teamcard-bild-id').val(attachment.id);
$('#new-teamcard-bild-vorschau').attr('src', attachment.url).show();
});
newTeamcardFrame.open();
});
// Medienbibliothek für vorhandene Teammitglieder
var teamcardFrame;
$(document).on('click', '.teamcard-bild-button', function(e) {
e.preventDefault();
var button = $(this);
var teamcardId = button.data('id');
if (teamcardFrame) {
teamcardFrame.open();
return;
}
teamcardFrame = wp.media({
title: 'Bild auswählen',
button: { text: 'Verwenden' },
multiple: false
});
teamcardFrame.on('select', function() {
var attachment = teamcardFrame.state().get('selection').first().toJSON();
$.ajax({
url: teamcard_data.ajax_url,
type: 'POST',
data: {
action: 'update_teamcard_image',
id: teamcardId,
bild_id: attachment.id,
nonce: teamcard_data.nonce
},
success: function(response) {
if (response.success) {
var container = button.siblings('.image-preview-container');
container.html('');
showMessage('Bild erfolgreich aktualisiert', 'success');
}
}
});
});
teamcardFrame.open();
});
// Neues Teammitglied hinzufügen
$('#add-teamcard-button').on('click', function() {
var name = $('#new-teamcard-name').val();
var funktion = $('#new-teamcard-funktion').val();
var zustaendigkeit = $('#new-teamcard-zustaendigkeit').val();
var bildId = $('#new-teamcard-bild-id').val();
if (!name) {
showMessage('Bitte gib einen Namen ein', 'error');
return;
}
$.ajax({
url: teamcard_data.ajax_url,
type: 'POST',
data: {
action: 'add_teamcard',
name: name,
funktion: funktion,
zustaendigkeit: zustaendigkeit,
bild_id: bildId,
nonce: teamcard_data.nonce
},
success: function(response) {
if (response.success) {
var bildHtml = '';
if (response.data.bild_url) {
bildHtml = '
';
}
var newRow = '
' + message + '
').show(); setTimeout(function() { messageElement.fadeOut(); }, 3000); } });