181 lines
6.8 KiB
JavaScript
181 lines
6.8 KiB
JavaScript
/* global thalimNL, tinymce */
|
|
(function ($) {
|
|
'use strict';
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Drag-and-drop reordering of items within each category list.
|
|
// The submit order of checkboxes follows DOM order, and the exporter
|
|
// renders each section in stored order — so reordering rows here is enough
|
|
// for the new order to appear in the generated HTML after saving.
|
|
// -------------------------------------------------------------------------
|
|
function makeSortable($container, itemSelector) {
|
|
if (!$.fn.sortable) return;
|
|
$container.sortable({
|
|
items: itemSelector,
|
|
handle: '.thalim-nl-drag-handle',
|
|
axis: 'y',
|
|
containment: 'parent',
|
|
tolerance: 'pointer',
|
|
cursor: 'grabbing',
|
|
opacity: 0.9,
|
|
placeholder: 'thalim-nl-sortable-placeholder',
|
|
forcePlaceholderSize: true,
|
|
});
|
|
}
|
|
|
|
function initSortable() {
|
|
var $root = $('#thalim-nl-sections-wrap');
|
|
|
|
$root.find('.thalim-nl-section-body').each(function () {
|
|
var $body = $(this);
|
|
if ($body.find('.thalim-nl-seminar-group').length) {
|
|
// Seminars: reorder whole seminars (séances stay chronological).
|
|
makeSortable($body, '> .thalim-nl-seminar-group');
|
|
} else {
|
|
// Normal categories: reorder post rows.
|
|
makeSortable($body, '> .thalim-nl-post-row');
|
|
}
|
|
});
|
|
}
|
|
|
|
// A click on the drag handle must not toggle its enclosing checkbox label.
|
|
$(document).on('click', '.thalim-nl-drag-handle', function (e) {
|
|
e.preventDefault();
|
|
});
|
|
|
|
$(initSortable);
|
|
|
|
// -------------------------------------------------------------------------
|
|
// 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);
|
|
initSortable();
|
|
|
|
// 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));
|