Files
2026-05-12 23:34:00 +02:00

137 lines
5.0 KiB
JavaScript

/* global thalimNL, tinymce */
(function ($) {
'use strict';
// -------------------------------------------------------------------------
// Month change → AJAX reload sections + update TinyMCE intro
// -------------------------------------------------------------------------
$('#thalim-nl-month').on('change', function () {
var month = $(this).val();
var $wrap = $('#thalim-nl-sections-wrap');
var $spin = $('.thalim-nl-spinner');
if (!month) return;
$spin.addClass('is-active');
$wrap.css('opacity', '0.5');
$.post(thalimNL.ajaxUrl, {
action: 'thalim_nl_load_month',
month: month,
_wpnonce: thalimNL.nonce,
})
.done(function (response) {
if (!response.success) return;
$wrap.html(response.data.html);
// Update TinyMCE intro — do NOT re-render wp_editor
var introEditor = typeof tinymce !== 'undefined' && tinymce.get('thalim_nl_intro');
if (introEditor) {
introEditor.setContent(response.data.intro || '');
} else {
$('#thalim_nl_intro').val(response.data.intro || '');
}
// Update TinyMCE conclusion
var conclusionEditor = typeof tinymce !== 'undefined' && tinymce.get('thalim_nl_conclusion');
if (conclusionEditor) {
conclusionEditor.setContent(response.data.conclusion || '');
} else {
$('#thalim_nl_conclusion').val(response.data.conclusion || '');
}
// Update subscribe/unsubscribe URLs
$('#thalim-nl-subscribe').val(response.data.subscribe_url || '');
$('#thalim-nl-unsubscribe').val(response.data.unsubscribe_url || '');
})
.fail(function () {
$wrap.html('<p class="thalim-nl-empty">Erreur lors du chargement.</p>');
})
.always(function () {
$spin.removeClass('is-active');
$wrap.css('opacity', '1');
});
});
// -------------------------------------------------------------------------
// Fetch newsletter HTML (shared by download & preview)
// -------------------------------------------------------------------------
function fetchNewsletterHTML(postId) {
return $.post(thalimNL.ajaxUrl, {
action: 'thalim_nl_export_html',
post_id: postId,
_wpnonce: thalimNL.nonce,
});
}
// -------------------------------------------------------------------------
// Download HTML file
// -------------------------------------------------------------------------
$(document).on('click', '.thalim-nl-download', function () {
var $btn = $(this);
var postId = $btn.data('post-id');
var filename = $btn.data('filename') || 'newsletter-THALIM.html';
var origText = $btn.text();
$btn.prop('disabled', true).text('…');
fetchNewsletterHTML(postId)
.done(function (response) {
if (!response.success) {
alert('Erreur : ' + (response.data || 'Impossible d\'exporter le HTML.'));
return;
}
var blob = new Blob([response.data.html], { type: 'text/html;charset=utf-8' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
})
.fail(function () {
alert('Erreur réseau lors de l\'export.');
})
.always(function () {
$btn.prop('disabled', false).text(origText);
});
});
// -------------------------------------------------------------------------
// Preview in new window
// -------------------------------------------------------------------------
$(document).on('click', '.thalim-nl-preview', function () {
var $btn = $(this);
var postId = $btn.data('post-id');
var origText = $btn.text();
$btn.prop('disabled', true).text('…');
fetchNewsletterHTML(postId)
.done(function (response) {
if (!response.success) {
alert('Erreur : ' + (response.data || 'Impossible de charger la prévisualisation.'));
return;
}
var win = window.open('', '_blank');
if (win) {
win.document.open();
win.document.write(response.data.html);
win.document.close();
} else {
alert('Le navigateur a bloqué l\'ouverture de la fenêtre. Autorisez les pop-ups pour ce site.');
}
})
.fail(function () {
alert('Erreur réseau lors du chargement.');
})
.always(function () {
$btn.prop('disabled', false).text(origText);
});
});
}(jQuery));