{% from "macros/form.html" import m_field %}

{% macro m_tags(entity, ns=None) %}
  {%- set tags_form = tags.entity_tags_form(entity)(obj=entity, ns=ns) %}
  {{ tags_form['tags'].render_view() }}
{% endmacro %}

{# display tags from a search result #}
{% macro m_tags_from_hit(hit) %}
  {%- for tag in tags.tags_from_hit(hit['tag_ids']) %}
    <span class="badge">{{ tag.label }}</span>
  {%- endfor %}
{% endmacro %}

{% macro m_tags_form(entity, ns=None) %}
  {%- set ctx = tags.get_form_context(entity) %}
  <form class="form-inline" action="{{ ctx.url }}" method="POST" enctype="multipart/form-data" role="form">
    {%- for field in ctx.form %}
      {{ m_field(field) }}
    {%- endfor %}
    <div class="form-group form-actions">
      {%- for button in ctx.buttons %}
        {{ button.render() }}
      {%- endfor %}
    </div>
  </form>
{% endmacro %}

{# show tags with an edit button, that replaces content by edit form #}
{% macro m_tags_toggle_edit(entity, ns=None) %}
  <div>
    <div class="tags-view">
      {{ m_tags(entity, ns) }}
      {%- set target_id = 'collapse-%s' | format(g.id_generator.next()) %}
      <button class="btn btn-default" data-toggle="collapse" data-target="#{{ target_id }}">
        <i class="fa fa-edit"></i>
      </button>
    </div>
    <div class="collapse" id="{{ target_id }}">
      {{ m_tags_form(entity, ns) }}
    </div>
  </div>
{% endmacro %}
