

{% macro bs_btn(label=None, icon=None, style="default", size=None, block=False, active=False, tag_name="button") -%}
  {% set class_ = "btn btn-" + style + " " + kwargs.pop("class_", "") %}
  {% if size %}{% set class_ = class_ + " btn-" + size %}{% endif %}
  {% if block %}{% set class_ = class_ + " btn-block" %}{% endif %}
  {% if active %}{% set class_ = class_ + " active" %}{% endif %}
  {% if "add_class" in kwargs %}{% set class_ = class_ + " " + kwargs.pop("add_class") %}{% endif %}
  {% if tag_name == "button" %}{% do kwargs.setdefault("type", "button") %}{% endif %}
  {{ html_tag(tag_name, class_=class_, **kwargs) }}
    {% if icon %}{{ ico(icon) }}{% endif %}
    {% if not label is none %}{{ label }}{% else %}{{ caller() }}{% endif %}
  </{{ tag_name }}>
{%- endmacro %}


{% macro bs_linkbtn(url, label=None, disabled=False) -%}
  {% set c = caller %}
  {% do kwargs.setdefault("role", "button") %}
  {% if disabled %}{% do kwargs.update(add_class="disabled " + kwargs.get("add_class", "")) %}{% endif %}
  {% call bs_btn(tag_name="a", href=url, **kwargs) %}
    {% if label %}{{ label }}{% else %}{{ c() }}{% endif %}
  {% endcall %}
{%- endmacro %}


{% macro bs_flash_messages() -%}
  {% flash_messages %}
    <div class="alert alert-{{ category }} alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
      {{ message }}
    </div>
  {% endflash %}
{%- endmacro %}


{% macro glyphi(name, class_="") -%}
  {{ html_tag("span", class_="glyphicon glyphicon-%s %s" % (name, class_), **kwargs) }}</span>
{%- endmacro %}


{% macro fa(name, size=None, class_="") -%}
  {% set size = ("fa-" + size) if size else "" %}
  {{ html_tag("i", class_="fa fa-%s %s %s" % (name, size, class_), **kwargs) }}</i>
{%- endmacro %}


{% macro ico(name) -%}
  {% if name.startswith("fa-") %}{{ fa(name[3:], **kwargs) }}{% else %}{{ glyphi(name, **kwargs) }}{% endif %}
{%- endmacro %}


{% macro bs_navbar(brand=None, logo=None, url="/", collapse_id="bs-navbar-collapse", style="default", position=None, class_="", fluid=True) -%}
  {% set navclass = "navbar navbar-%s" % style %}
  {% if position %}{% set navclass = navclass + " navbar-" + position %}{% endif %}
  {% do kwargs.setdefault('role', 'navigation') %}
  {{ html_tag('nav', class_=navclass + " " + class_, **kwargs) }}
    <div class="container{% if fluid %}-fluid{% endif %}">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#{{ collapse_id }}">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="{{ url }}">
          {% if logo %}<img src="{{ logo }}" alt="{{ brand or "" }}">
          {% else %}{{ brand }}{% endif %}
        </a>
      </div>
      <div class="collapse navbar-collapse" id="{{ collapse_id }}">
        {% if caller %}{{ caller() }}{% endif %}
      </div>
    </div>
  </nav>
{%- endmacro %}


{% macro bs_pagination(page, pages, prev_page=None, next_page=None, endpoint=None, request_args=None, size=None) -%}
  {% set endpoint = endpoint or request.endpoint %}
  {% set request_args = request_args or request.args %}
  <ul class="pagination{% if size %} pagination-{{ size }}{% endif %}">
    <li{% if not prev_page %} class="disabled"{% endif %}>
      <a href="{{ url_for(endpoint, **real_dict(request_args, page=prev_page)) if prev_page else '#' }}">&laquo;</a>
    </li>
    {% for p in pages %}
    <li class="{% if p and p == page %}active{% elif not p %}disabled{% endif %}">
      {% if p %}
        <a href="{{ url_for(endpoint, **real_dict(request_args, page=p)) }}">
          {{ p }} {% if p == page %}<span class="sr-only">(current)</span>{% endif %}
        </a>
      {% else %}
        <span>…</span>
      {% endif %}
    </li>
    {% endfor %}
    <li{% if not next_page %} class="disabled"{% endif %}>
      <a href="{{ url_for(endpoint, **real_dict(request_args, page=next_page)) if next_page else '#' }}">&raquo;</a>
    </li>
  </ul>
{%- endmacro %}


{% macro bs_pagination_obj(pagination) -%}
  {% if pagination.nb_pages > 1 %}
    {{ bs_pagination(pagination.page, pagination.iter_pages(), pagination.prev_page, pagination.next_page, **kwargs) }}
  {% endif %}
{%- endmacro %}


{% macro bs_label(label=None, style="default") -%}
  {% set class_ = "label label-" + style + " " + kwargs.pop("class_", "") %}
  {{ html_tag("span", class_=class_, **kwargs) }}
    {% if label %}{{ label }}{% else %}{{ caller() }}{% endif %}
  </span>
{%- endmacro %}


{% macro bs_page_header(title=None, subtext=None, url=None, icon=None) -%}
  <div class="page-header">
    <h1>
      {% if caller %}
        {{ caller() }}
      {% else %}
        {% if url %}<a href="{{ url }}">{% endif %}
        {% if icon %}{{ ico(icon) }}{% endif %}
        {{ title }}{% if subtext %} <small>{{ subtext }}</small>{% endif %}
        {% if url %}</a>{% endif %}
      {% endif %}
    </h1>
  </div>
{%- endmacro %}


{% macro bs_thumbnail(src, alt="") -%}
  {% if caller %}
    {% set tag_name = "div" %}
  {% else %}
    {% set tag_name = "a" %}
    {% do kwargs.setdefault("href", "#") %}
  {% endif %}
  {{ html_tag(tag_name, class_="thumbnail " + kwargs.pop("class_", ""), **kwargs) }}
    <img src="{{ src }}" alt="{{ alt }}">
    {% if caller %}<div class="caption">{{ caller() }}</div>{% endif %}
  </{{ tag_name }}>
{%- endmacro %}


{% macro bs_alert(content=None, style="success", dismissible=True, close_lbl="Close") -%}
  {% set class_ = "alert alert-" + style + " " + kwargs.pop("class_", "") %}
  {% if dismissible %}{% set class_ = class_ + " alert-dismissible" %}{% endif %}
  {{ html_tag("div", class_=class_, **kwargs) }}
    {% if dismissible %}
      <button type="button" class="close" data-dismiss="alert">
        <span aria-hidden="true">&times;</span><span class="sr-only">{{ close_lbl }}</span>
      </button>
    {% endif %}
    {% if content %}{{ content }}{% else %}{{ caller() }}{% endif %}
  </div>
{%- endmacro %}


{% macro bs_progress(pct, style=None, striped=False, active=False, with_label=False) -%}
  <div class="progress">
    <div class="progress-bar{% if style %} progress-bar-{{ style }}{% endif %}{% if striped %} progress-bar-striped{% endif %}{% if active %} active{% endif %}"
      role="progressbar" aria-valuenow="{{ pct }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ pct }}%;">
      {% if with_label %}{{ pct }}%
      {% else %}<span class="sr-only">{{ pct }}% Complete</span>{% endif %}
    </div>
  </div>
{%- endmacro %}


{% macro bs_media(title, src=None, href="#", alt="", img_position="left", tag_name="div") -%}
  {{ html_tag(tag_name, class_="media " + kwargs.pop("class_", ""), **kwargs) }}
    {% if src %}
      <a class="pull-{{ img_position }}" href="{{ href }}">
        <img class="media-object" src="{{ src }}" alt="{{ alt }}">
      </a>
    {% endif %}
    <div class="media-body">
      <h4 class="media-heading">{{ title }}</h4>
      {{ caller() }}
    </div>
  </{{ tag_name }}>
{%- endmacro %}


{% macro bs_media_li(title) -%}
  {% set c = caller %}
  {% call bs_media(title, tag_name="li", **kwargs) %}
    {{ c() }}
  {% endcall %}
{%- endmacro %}


{% macro bs_panel(title=None, style="default", heading=None, footer=None) -%}
  <div class="panel panel-{{ style }}">
    {% if heading or title %}
    <div class="panel-heading">
      {% if heading %}{{ heading|safe }}
      {% elif title %}<h3 class="panel-title">{{ title|safe }}</h3>{% endif %}
    </div>
    {% endif %}
    <div class="panel-body">
      {{ caller() }}
    </div>
    {% if footer %}
    <div class="panel-footer">{{ footer|safe }}</div>
    {% endif %}
  </div>
{%- endmacro %}