{#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
#
# Invenio 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 2 of the
# License, or (at your option) any later version.
#
# Invenio 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 Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#}

{# Pagination Macro
Leverages Twitter-Bootstrap paginate class and Flask-SQLAlchemy Pagination object

Args:
  obj: Flask-SQLAlchemy Pagination object
  endpoint: redirect enpoint
  small: wheather should be rendered as small or not

Returns:
  Pagination component

It assumes that page exists in the current context.
#}
{%- macro paginate(obj, small) %}
{%- set args = request.args.copy().to_dict() -%}
{%- do args.update(request.view_args.copy()) -%}
{%- set endpoint = request.endpoint -%}
<div>
  <ul class="pagination {{ 'pagination-sm' if small }}">
    <li {% if not obj.has_prev -%} class="disabled" {%- endif %}}>
      {%- set new_args = args.copy() -%}
      {%- do new_args.update({'page': 1}) -%}
      {%- if not obj.has_prev %}
      <span title="first">&laquo;</span>
      {% else %}
      <a title="first" href="{{ url_for(endpoint, **new_args) if obj.has_prev}}">&laquo;</a>
      {%- endif %}
    </li>
    <li {% if not obj.has_prev -%} class="disabled" {%- endif %}}>
      {%- do args.update({'page': obj.page-1 }) -%}
      {%- if not obj.has_prev %}
      <span title="prev">&lsaquo;</span>
      {% else %}
      <a title="prev" href="{{ url_for(endpoint, **args) if obj.has_prev }}">&lsaquo;</a>
      {%- endif %}
    </li>
    {%- for page_p in obj.iter_pages() %}
      {%- if page_p %}
        {%- set new_args = args.copy() -%}
        {%- do new_args.update({'page': page_p}) -%}
        <li {% if page_p == obj.page -%} class="active" {%- endif %}>
          <a title="current" href="{{ url_for(endpoint, **new_args) }}">{{ page_p }}</a>
        </li>
      {%- else %}
        <li class="disabled"><a href="#">&hellip;</a></li>
      {%- endif %}
    {%- endfor %}
    <li {% if not obj.has_next -%} class="disabled" {%- endif %}}>
      {%- set new_args = args.copy() -%}
      {%- do new_args.update({'page': obj.page+1}) -%}
      {%- if not obj.has_next %}
      <span title ="next">&rsaquo;</span>
      {% else %}
      <a title ="next" href="{{ url_for(endpoint, **new_args) if obj.has_next }}">&rsaquo;</a>
      {%- endif %}
    </li>
    <li {% if not obj.has_next -%} class="disabled" {%- endif %}}>
      {%- set new_args = args.copy() -%}
      {%- do new_args.update({'page': obj.pages }) -%}
      {%- if not obj.has_next %}
      <span title="last">&raquo;</span>
      {% else %}
      <a title="last" href="{{ url_for(endpoint, **new_args) if obj.has_next }}">&raquo;</a>
      {%- endif %}
    </li>
  </ul>
</div>
{%- endmacro %}


{# List Status Indicator Macro

Args:
  obj: Flask-SQLAlchemy Pagination object

Returns:
  List Status Indicator component

It assumes that page and per_page exists in the current context.
#}
{%- macro list_status(obj) %}
<span class="text-muted">
  {{ _("Displaying items %(start)d - %(stop)d out of %(total)d", start=(page - 1) * per_page, stop=((page - 1) * per_page) + obj.items|count, total=obj.total) }}
</span>
{%- endmacro %}
