{#
/**
 * Copyright © 2003-2024 The Galette Team
 *
 * This file is part of Galette (https://galette.eu).
 *
 * Galette is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Galette is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
 */
#}
{% extends 'page.html.twig' %}

{% block content %}
        <div class="ui stackable pointing inverted menu">
{% for m in models %}
            <a href="{{ url_for('pdfModels', {'id': m.id}) }}" class="item{% if m.id == model.id %} active{% endif %}" data-tab="{{ m.id }}">{{ m.name }}</a>
{% endfor %}
        </div>
{% for m in models %}
        <div class="ui {% if m.id == model.id %} active{% endif %} tab segment" data-tab="{{ loop.index }}">
    {% if m.id == model.id %}
        {% include 'elements/edit_pdf_models.html.twig' %}
    {% endif %}
        </div>
{% endfor %}
{% endblock %}

{% block javascripts %}
        <script type="text/javascript">
            $(function() {
                $('.pointing.menu .item').tab({
                    auto: true,
                    alwaysRefresh: true,
                    path: '{{ url_for('pdfModels', {'id': ''}) }}',
                    onLoad: function(tabPath, parameterArray, historyEvent) {
                        $('.ui.tab.segment:not(.active)').empty();
                        $('.note-editor, .btnlegend').remove();
                        _initCodeEditor();
                        _bindSubmit();
                        _addLegendButton('.field:not(.nolegend) .ui.corner.label');
                        _handleLegend(tabPath);
                        _bindFomanticComponents();
                    }
                });
            });

            function _initCodeEditor() {
                $('textarea.codeeditor').summernote({
                    lang: '{{ i18n.getID()|replace({'_': '-'}) }}',
                    height: 240,
                    toolbar: [
                        ['view', ['codeview']]
                    ]
                });
                // Disable wysiwyg edition
                $('textarea.codeeditor').next().find('.note-editable').attr('contenteditable', false);
            }
            _initCodeEditor();

            function _bindSubmit() {
                $('button.action').click(function(event) {
                    event.preventDefault();
                    $('.note-editor').each(function(index) {
                        var _this= $(this);
                        // Because wysiwyg edition is disabled, it is required to toggle
                        // codeview before submit, otherwise modifications will not persist.
                        if (_this.hasClass('codeview')) {
                            var _editor = _this.prev();
                            _editor.summernote('codeview.toggle');
                        }
                    });
                    $('.form').submit();
                });
            }
            _bindSubmit();

            _addLegendButton('.field:not(.nolegend) .ui.corner.label');
            _handleLegend();
        </script>
{% endblock %}
