{#
    This template is used by other templates (show.html, etc.) to format the
    numerous links within the Item Views panel in the theme's header or sidebar.

    Some links may be suppressed because of item ACLs.
    Several links are suppressed if the item or revision is deleted.
    Comment and Transclusion links may be hidden by Javascript if item lacks content.
#}

{% set item = item %}
{% set not_trash =  not item_is_deleted if item_is_deleted is defined else True %}
{% set icon_classes = theme_supp.get_endpoint_iconmap() %}

{%- macro a_label(icon_class, label) %}
    <i class="{{ icon_class }}"></i>
    <span>{{ label }}</span>
{%- endmacro %}

<ul class="moin-itemviews panel">
    <li class="moin-panel-heading">{{ _('Item Views') }}</li>
    {%- for endpoint, label, title, check_exists in cfg.item_views if not endpoint in cfg.endpoints_excluded %}
        {% set icon_class = icon_classes[endpoint] %}
        {%- if endpoint in [
            'frontend.show_item',
            'frontend.show_item_meta',
            'frontend.history',
            'frontend.download_item',
        ] %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if not_trash and endpoint in [
            'frontend.index',
            'frontend.refs',
            'frontend.sitemap',
            'frontend.similar_names',
            'frontend.copy_item',
        ] %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint == 'frontend.highlight_item' and item and ('text' in item.contenttype or 'docbook' in item.contenttype) %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint == 'frontend.modify_item' and may.write and not_trash %}
            <li>
                <a class="moin-modify-button" href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint in [
            'frontend.rename_item', 'frontend.delete_item',
        ] and may.write and not_trash %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint in [
            'frontend.convert_item',
        ] and may.write and item and theme_supp.is_markup_or_text(item.contenttype) and not_trash %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint == 'frontend.destroy_item' and may.destroy %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint == 'frontend.quicklink_item' and user.valid and not_trash %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">
                    {%- if user.is_quicklinked_to([fqname]) %}
                        {{ a_label(icon_class, _('Remove Link')) }}
                    {%- else %}
                        {{ a_label(icon_class, _('Add Link')) }}
                    {%- endif %}
                </a>
            </li>
        {%- endif %}

        {%- if endpoint == 'frontend.subscribe_item' and user.valid and not_trash %}
            <li>
                <a href="{{ url_for(endpoint, item_name = fqname) }}" title="{{ title }}" rel="nofollow">
                    {%- if user.is_subscribed_to(item) %}
                        {{ a_label(icon_class, _('Unsubscribe')) }}
                    {%- else %}
                        {{ a_label(icon_class, _('Subscribe')) }}
                    {%- endif %}
                </a>
            </li>
        {%- endif %}

        {%- if endpoint == 'special.comments' and not_trash %}
            <li class="moin-toggle-comments-button">
                <a class="moin-toggle-comments-tooltip" href="#" title="{{ title }}">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {% if endpoint == 'special.transclusions' and not_trash -%}
            <li class="moin-transclusions-button">
                <a class="moin-transclusions-tooltip" href="#" title="{{ title }}">{{ a_label(icon_class, label) }}</a>
            </li>
        {%- endif %}

        {%- if endpoint == 'special.supplementation' and fqname %}
            {%- for sub_item_name in cfg.supplementation_item_names %}
                {%- set current_sub = item_name.rsplit('/', 1)[-1] %}
                {%- if not current_sub in cfg.supplementation_item_names %}
                    {%- set supp_name = '%s/%s' % (fqname, sub_item_name) %}
                    {%- if not_trash and (storage.has_item(supp_name) or user.may.write(supp_name)) %}
                        {% set title = _('Discuss contents of this item') %}
                        <li>
                            <a class="moin-subitem-discuss" href="{{ url_for('frontend.show_item', item_name = supp_name) }}"
                                title="{{ title }}" rel="nofollow">{{ a_label(icon_class, _(sub_item_name)) }}
                            </a>
                        </li>
                    {%- endif %}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
    {%- endfor %}
</ul>
