Commit b5461999 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Sous template

parent 35c2c119
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace UnicaenRenderer;

use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment;
use UnicaenRenderer\View\Helper\TemplateInsertViewHelper;
use UnicaenPrivilege\Guard\PrivilegeController;
use UnicaenRenderer\Controller\TemplateController;
use UnicaenRenderer\Controller\TemplateControllerFactory;
@@ -153,4 +154,9 @@ return [
            TemplateController::class => TemplateControllerFactory::class,
        ]
    ],
    'view_helpers' => [
        'invokables' => [
            'templateInsert' => TemplateInsertViewHelper::class,
        ],
    ],
];
+21 −1
Original line number Diff line number Diff line
@@ -14,8 +14,28 @@
}

.macro-select {
  width: 30rem;
  width: 15rem;
}
.macro-selection-placeholder{
  display: none;
}

#template-insert-widget-content{
  display: none;
}
.template-popover{
  display: none;
}
.template-insert-widget {
  position: relative;
  top: 32px;
  left: -5px;
  margin-left: 15px;
}

.template-select {
  width: 15rem;
}
.template-selection-placeholder{
  display: none;
}
 No newline at end of file
+83 −0
Original line number Diff line number Diff line
@@ -92,3 +92,86 @@ function installMacrosWidgets(macros)
        });
    });
}


/**
 * Parcours des textareas "template compatibles" pour y installer le widget de recherche/insertion de template.
 * @param templates array
 */
function installTemplatesWidgets(templates)
{
    $(".template-compatible").each(function (index, element) {
        const $textarea = $(element);
        // Pour éviter un chevauchement éventuelle d'input ayant la croix permettant de les vider
        $textarea.parent().find(".form-control-clear").detach();
        $textarea.parent().removeClass('has-clear');

        const id = 'template-insert-widget-' + index;
        const $widget = $($("#template-insert-widget-content").html()).attr('id', id);

        // insertion du widget
        $(this).before($widget);

        // installation du popover sur le bouton
        const $popoverContent = $widget.find(".template-popover-content").attr('id', 'template-popover-content-' + index);
        const $popoverBtn = $widget.find(".template-btn").popover({
            html: true,
            sanitize: false,
            content: $popoverContent,
        });

        // installation du select2 dans le popover
        let $templateSelect2;
        const escapeHTML = function(html) {
            return document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML;
        };
        const templateSelectTemplate = function (template) {
            if (!template) return "Aucun template sélectionné.";
            return '<strong class="template-id">' + template.text + '</strong> : ' + '<code class="template-value">' + escapeHTML(template.value) + '</code><br>' +
                '<div class="template-description text-secondary">' + template.description + '</div>';
        }
        try {
            $templateSelect2 = $widget.find(".template-select").select2({
                data: templates,
                dropdownParent: $popoverContent,
                placeholder: "Sélectionnez un template...",
                search: true,
                templateResult: template => $(templateSelectTemplate(template))
            });
        } catch (e) {
            alert("Impossible d'activer Select2 ! : " + e);
            console.error(e);
        }

        // bouton déclenchant l'insertion de template sélectionné dans le textarea
        const $templateInsertBtn = $popoverContent.find(".template-insert-btn").on("click", function () {
            // debugger;
            const template = $templateSelect2.select2('data')[0];
            if (!template) {
                return;
            }
            if (tinyMCE && ($ed = tinyMCE.get($textarea.prop('id')))) {
                $ed.selection.setContent(template.value);
            } else {
                const caretPos = $textarea.prop('selectionStart');
                if (caretPos === undefined) {
                    $textarea.val($textarea.val() + ' ' + template.value);
                } else {
                    $textarea.val($textarea.val().substring(0, caretPos) + template.value + $textarea.val().substring(caretPos));
                }
            }
        });

        // bouton de fermeture du popover
        const $templateCancelBtn = $popoverContent.find(".template-close-btn").on("click", function () {
            $popoverBtn.popover('hide');
        });

        // lorsqu'une template est sélectionnée dans le select, affichage de ses détails
        $templateSelect2.on("select2:select", function (e) {
            const template = e.params.data;
            $popoverContent.find(".template-selection-placeholder").html(templateSelectTemplate(template)).show();
            $templateInsertBtn.removeAttr("disabled");
        });
    });
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ class TemplateController extends AbstractActionController
            'form' => $form,
            'template' => $template,
            'macrosJsonValue' => $this->getMacroService()->generateMacrosJsonValueForTemplate($template),
            'templatesJsonValue' => $this->getTemplateService()->generateTemplatesJsonValues($template),
        ]);

        return $vm;
@@ -107,6 +108,7 @@ class TemplateController extends AbstractActionController
            'form' => $form,
            'template' => $template,
            'macrosJsonValue' => $this->getMacroService()->generateMacrosJsonValueForTemplate($template),
            'templatesJsonValue' => $this->getTemplateService()->generateTemplatesJsonValues($template),
        ]);
    }

+1 −1
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ class TemplateForm extends Form
            ],
            'attributes' => [
                'id' => 'corps',
                'class' => 'form-control corps macro-compatible',
                'class' => 'form-control corps macro-compatible template-compatible',
                'rows' => 10,
            ]
        ]);
Loading