{#  This file was part of Flask-Bootstrap and was modified under the terms of
 its BSD License. Copyright (c) 2013, Marc Brinkmann. All rights reserved. #}

{# This file was part of Bootstrap-Flask and was modified under the terms of
 its MIT License. Copyright (c) 2018 Grey Li. All rights reserved.#}

{% from 'fomantic/utils.html' import arg_url_for %}

{% macro render_ui_pager(pagination,
                      fragment='',
                      prev='left chevron',
                      next='right chevron',
                      extra_classes=None,
                      color_active_item=None) -%}
<nav aria-label="Page navigation">
<div class="ui pagination{% if extra_classes %} {{extra_classes}}{%endif%} menu">
    <a class="{% if pagination.has_prev %}active {% if color_active_item %}{{color_active_item}} {%endif %}{% else %}disabled {%endif%}item" href="{{ url_for(request.endpoint, page=pagination.prev_num, **kwargs) + fragment if pagination.has_prev else '#' }}">
        <i class="{{ prev }} icon"></i>Prev
    </a>
    <div class="disabled item">...</div>
    <a class="{% if pagination.has_next %}active {% if color_active_item %}{{color_active_item}} {%endif %}{% else %}disabled {%endif%}item" href="{{ url_for(request.endpoint, page=pagination.next_num, **kwargs) + fragment if pagination.has_next else '#' }}">
        Next<i class="{{ next }} icon"></i>
    </a>
</div>
</nav>
{%- endmacro %}

{% macro render_ui_pagination(pagination,
                           endpoint=None,
                           prev='left chevron',
                           next='right chevron',
                           ellipses='…',
                           args={},
                           fragment='',
                           extra_classes=None,
                           color_active_item=None
                           )-%}
    {% if fragment != '' and not fragment.startswith('#') %}
        {% set fragment = '#' + fragment %}
    {% endif %}
    {% with url_args = {} %}
        {%- do url_args.update(request.view_args if not endpoint else {}),
       url_args.update(request.args if not endpoint else {}),
       url_args.update(args) -%}
        {% with endpoint = endpoint or request.endpoint %}
        <nav aria-label="Page navigation">
            <div class="ui pagination{% if extra_classes %} {{extra_classes}}{%endif%} menu">
                
                    {# prev and next are only show if a symbol has been passed. #}
                    {% if prev != None -%}
                        <a class="item {% if not pagination.has_prev %}disabled{% endif %}" href="{{ arg_url_for(endpoint, url_args, page=pagination.prev_num) if pagination.has_prev else '#' }}{{ fragment }}">
                        <i class="{{ prev }} icon"></i>
                        </a>
                    {%- endif -%}

                    {%- for page in pagination.iter_pages() %}
                        {% if page %}
                            {% if page != pagination.page %}
                                <a class="item" href="{{ arg_url_for(endpoint, url_args, page=page) }}{{ fragment }}">{{ page }}</a>
                            {% else %}
                                <a class="active{% if color_active_item %} {{color_active_item}}{%endif%} item" href="#">{{ page }}</a>
                            {% endif %}
                        {% elif ellipses != None %}
                            <div class="disabled item">
                                {{ ellipses }}
                            </div>
                        {% endif %}
                    {%- endfor %}

                    {% if next != None -%}
                        <a class="{% if not pagination.has_next %}disabled{% endif %} item" href="{{ arg_url_for(endpoint, url_args, page=pagination.next_num) if pagination.has_next else '#' }}{{ fragment }}"><i class="{{ next }} icon"></i></a>
                    {%- endif -%}
            </div>
        {% endwith %}
        </nav>
    {% endwith %}
{% endmacro %}
