170 lines
7.0 KiB
JavaScript
170 lines
7.0 KiB
JavaScript
/* jshint esversion: 6 */
|
||
jQuery(function ($) {
|
||
'use strict';
|
||
|
||
// ── Color Picker ──────────────────────────────────────────────────────
|
||
$('.wmw-color-picker').wpColorPicker();
|
||
|
||
// ── Emoji Picker ─────────────────────────────────────────────────────
|
||
$(document).on('click', '.wmw-emoji-btn', function () {
|
||
var emoji = $(this).data('emoji');
|
||
$('#wmw_icon').val(emoji);
|
||
});
|
||
|
||
// ── Live Filter – Wiki Grid ───────────────────────────────────────────
|
||
$('#wmw-filter-wikis').on('input', function () {
|
||
var q = $(this).val().toLowerCase();
|
||
$('.wmw-wiki-card').each(function () {
|
||
var title = $(this).data('title') || '';
|
||
$(this).toggle(title.includes(q));
|
||
});
|
||
});
|
||
|
||
// ── Live Filter – Article Table ───────────────────────────────────────
|
||
$('#wmw-filter-articles').on('input', function () {
|
||
var q = $(this).val().toLowerCase();
|
||
$('.wmw-article-table tbody tr').each(function () {
|
||
var title = $(this).data('title') || '';
|
||
$(this).toggle(title.includes(q));
|
||
});
|
||
});
|
||
|
||
// ── Save Wiki ─────────────────────────────────────────────────────────
|
||
$('#wmw-save-wiki').on('click', function () {
|
||
var $btn = $(this);
|
||
var id = $btn.data('id');
|
||
|
||
var data = {
|
||
action: 'wmw_save_wiki',
|
||
nonce: wmwAdmin.nonce,
|
||
id: id,
|
||
title: $('#wmw_title').val().trim(),
|
||
description: '',
|
||
icon: $('#wmw_icon').val(),
|
||
color: $('#wmw_color').val(),
|
||
version: $('[name="wmw_version"]').val(),
|
||
status: $('[name="wmw_status"]').val(),
|
||
};
|
||
|
||
// Get TinyMCE content if available
|
||
if (typeof tinymce !== 'undefined' && tinymce.get('wmw_description')) {
|
||
data.description = tinymce.get('wmw_description').getContent();
|
||
} else {
|
||
data.description = $('[name="wmw_description"]').val() || '';
|
||
}
|
||
|
||
if (!data.title) {
|
||
alert('Bitte gib einen Titel ein.');
|
||
return;
|
||
}
|
||
|
||
showSaving();
|
||
$.post(wmwAdmin.ajaxUrl, data, function (res) {
|
||
hideSaving();
|
||
if (res.success) {
|
||
if (!id) {
|
||
window.location.href = ajaxurl.replace('admin-ajax.php','') + 'admin.php?page=wmw-edit-wiki&id=' + res.data.id + '&wmw_saved=1';
|
||
} else {
|
||
showNotice('✅ Wiki gespeichert!', 'success');
|
||
}
|
||
} else {
|
||
showNotice('❌ Fehler: ' + (res.data && res.data.message ? res.data.message : 'Unbekannter Fehler'), 'error');
|
||
}
|
||
});
|
||
});
|
||
|
||
// ── Save Article ──────────────────────────────────────────────────────
|
||
$('#wmw-save-article').on('click', function () {
|
||
var $btn = $(this);
|
||
var id = $btn.data('id');
|
||
|
||
var cats = [];
|
||
$('[name="wmw_categories[]"]:checked').each(function () { cats.push($(this).val()); });
|
||
|
||
var content = '';
|
||
if (typeof tinymce !== 'undefined' && tinymce.get('wmw_content')) {
|
||
content = tinymce.get('wmw_content').getContent();
|
||
} else {
|
||
content = $('[name="wmw_content"]').val() || '';
|
||
}
|
||
|
||
var data = {
|
||
action: 'wmw_save_article',
|
||
nonce: wmwAdmin.nonce,
|
||
id: id,
|
||
title: $('#wmw_a_title').val().trim(),
|
||
content: content,
|
||
excerpt: $('#wmw_a_excerpt').val(),
|
||
wiki_id: $('#wmw_a_wiki_id').val(),
|
||
categories: cats,
|
||
tags: $('#wmw_a_tags').val(),
|
||
status: $('#wmw_a_status').val(),
|
||
order: $('#wmw_a_order').val(),
|
||
};
|
||
|
||
if (!data.title) { alert('Bitte gib einen Titel ein.'); return; }
|
||
if (!data.wiki_id) { alert('Bitte wähle ein Wiki aus.'); return; }
|
||
|
||
showSaving();
|
||
$.post(wmwAdmin.ajaxUrl, data, function (res) {
|
||
hideSaving();
|
||
if (res.success) {
|
||
if (!id) {
|
||
var base = wmwAdmin.ajaxUrl.replace('admin-ajax.php', '');
|
||
window.location.href = base + 'admin.php?page=wmw-edit-article&id=' + res.data.id + '&wmw_saved=1';
|
||
} else {
|
||
showNotice('✅ Artikel gespeichert!', 'success');
|
||
}
|
||
} else {
|
||
showNotice('❌ ' + (res.data && res.data.message ? res.data.message : 'Fehler'), 'error');
|
||
}
|
||
});
|
||
});
|
||
|
||
// ── Delete Wiki ───────────────────────────────────────────────────────
|
||
$(document).on('click', '.wmw-delete-wiki', function (e) {
|
||
e.preventDefault();
|
||
var $btn = $(this);
|
||
var id = $btn.data('id');
|
||
var title = $btn.data('title');
|
||
if (!confirm('Wiki "' + title + '" und ALLE Artikel darin löschen?')) return;
|
||
|
||
$.post(wmwAdmin.ajaxUrl, { action: 'wmw_delete_wiki', nonce: wmwAdmin.nonce, id: id }, function (res) {
|
||
if (res.success) {
|
||
$btn.closest('.wmw-wiki-card').fadeOut(300, function () { $(this).remove(); });
|
||
} else {
|
||
alert('Fehler beim Löschen.');
|
||
}
|
||
});
|
||
});
|
||
|
||
// ── Delete Article ────────────────────────────────────────────────────
|
||
$(document).on('click', '.wmw-delete-article', function (e) {
|
||
e.preventDefault();
|
||
if (!confirm(wmwAdmin.confirm_delete)) return;
|
||
var $el = $(this);
|
||
var id = $el.data('id');
|
||
|
||
$.post(wmwAdmin.ajaxUrl, { action: 'wmw_delete_article', nonce: wmwAdmin.nonce, id: id }, function (res) {
|
||
if (res.success) {
|
||
$el.closest('tr').fadeOut(300, function () { $(this).remove(); });
|
||
}
|
||
});
|
||
});
|
||
|
||
// ── Helpers ───────────────────────────────────────────────────────────
|
||
function showSaving() {
|
||
if (!$('#wmw-saving-overlay').length) {
|
||
$('body').append('<div id="wmw-saving-overlay" style="position:fixed;inset:0;background:rgba(0,0,0,.3);z-index:9999;display:flex;align-items:center;justify-content:center"><div style="background:#fff;border-radius:10px;padding:24px 36px;font-size:15px;font-weight:600;box-shadow:0 10px 40px rgba(0,0,0,.2)">⏳ Speichern…</div></div>');
|
||
}
|
||
}
|
||
function hideSaving() { $('#wmw-saving-overlay').remove(); }
|
||
|
||
function showNotice(msg, type) {
|
||
var cls = type === 'success' ? 'notice-success' : 'notice-error';
|
||
var $note = $('<div class="notice ' + cls + ' is-dismissible"><p>' + msg + '</p></div>');
|
||
$('.wmw-admin-header').after($note);
|
||
setTimeout(function () { $note.fadeOut(400, function () { $(this).remove(); }); }, 3000);
|
||
}
|
||
});
|