<%def name="namedpager(pager)">
<div class="pager_wrapper">
    %if (pager.enable_pages and len(pager.items)) or hasattr(pager.sorts, 'render') or (len(pager.sorts.keys()) > 1 and pager.enable_sorts):
        <div class="floatbox">
            %if pager.enable_pages:
                <span class="pager_items">
                    %if len(pager.items):
                    ${_("%(start)s-%(end)s out of %(total)s") % dict(start=pager.offset+1, end=pager.offset+len(pager.items), total=pager.total_num_items())}
                    %endif
                </span>
            %endif
            %if hasattr(pager.sorts, 'render'):
            ## New style NamedSorts-object
            ${pager.sorts.render()}
            %elif len(pager.sorts.keys()) > 1 and pager.enable_sorts:
            ## Old style index based sort
            ${index_based_sort_dropdown(pager.sorts, pager)}
            %endif
        </div>
    %endif
    %if pager.enable_pages or pager.enable_sorts:
    <ul class="content_list ${pager.name}_pager">
    %endif
    %if not (pager.enable_pages or pager.enable_sorts):
    <ul class="content_list ${pager.name}_pager without_paging">
    %endif
    %if not len(pager.items):
        <li class="infobox">${_("No entries.")}</li>
    %endif
    %for item in pager.items:
        ${pager.itemfunc(item)}
    %endfor
      </ul>
    ${pager.render_pages_nav()}
</div>
</%def>


<%def name="pages_nav(pager)">
%if pager.enable_pages:
    %if pager.pages > 1:
    <div class="pager">
        <div class="prev_page">
            %if pager.page > 1:
            <a href="${pager.build_url(page=pager.page-1)|n}#top"
               target="_self">&laquo; ${_("previous")}</a>
            %else:
            <span>&laquo; ${_("previous")}</span>
            %endif
        </div>
        %for page in pager.pages_items():
            %if page['seperator']:
                <span class="seperator">...</span>
            %else:
                %if page['current']:
                    <span class="current">${page['label']}</span>
                %else:
                    <a href="${page['url']|n}#top"
                       target="_self"
                       class="${page['class']}">${page['label']}</a>
                %endif
            %endif
        %endfor
        <div class="next_page">
            %if pager.page < pager.pages:
            <a href="${pager.build_url(page=pager.page+1)|n}#top"
               target="_self">${_("next")} &raquo;</a>
            %else:
            <span>${_("next")} &raquo;</span>
            %endif
        </div>
    </div>
    %endif
    %if pager.page_sizes():
    <div class="pager_sorts bottom">
        %for size in pager.page_sizes():
        %if size['current']:
        ${size['size']}
        %else:
        <a href="${size['url']|n}#top" rel="nofollow" target="_self">${size['size']}</a>
        %endif
        ${'|' if not size['last'] else ''}
        %endfor
        ${_("entries")}
    </div>
    %endif
%endif
</%def>


<%def name="facets(pager, cls=None, exclude=[])">
<% is_admin = can.badge.edit_global() or can.badge.edit_instance() %>
%if any(facet.available(is_admin) and facet.name not in exclude for facet in pager.facets):
<div id="facets" ${'class=%s'%cls if cls is not None else ''}>
<h3 id="facets-heading">${_("Filter list")}</h3>

  %for facet in pager.facets:
    %if facet.available(is_admin) and facet.name not in exclude:
    <div class="facet facet-${facet.name}">
        ${facet.render()}
    </div>
    %endif
  %endfor
</div>
%endif
</%def>

<%def name="facet(facet)">
<h4>
    ${facet.title}
    %if facet.used:
    <a class="remove"
       href="${facet.unselect_all_link()|n}"
       rel="nofollow">(${_("reset")})</a>
    %endif

    %if facet.badge_type is not None and can.badge.manage_instance():
    <a class="button_small edit"
        href="${h.base_url('/badge/%s' % facet.badge_type)}"
        rel="#overlay-url">${_(u"Edit")}</a>
    %endif
</h4>
%if facet.description:
<p>${facet.description}</p>
%endif

%if facet.name == "delegateabletags" and h.config.get_bool('adhocracy.tagcloud_facet'):
    ${facet_cloud_tag(facet.current_items)}
%else:
    ${facet_items(facet.current_items)}
%endif
</%def>


<%def name="facet_cloud_tag(items)">
%if items:
<ul class="tagcloud_facet">
    %for item in sorted(items, key=lambda i: i['link_text']):
    %if item['visible']:
    <li>
        %if item['disabled']:
        <span class="tag_link disabled" role="link" aria-disabled="true"
              style="font-size: ${h.tag.solr_tag_size(item, items)}%;">
            ${item['link_text']}</span>
        %else:
        <a class="tag_link ${'selected' if item['selected'] else ''}"
           style="font-size: ${h.tag.solr_tag_size(item, items)}%;"
           href="${item['url']|n}"
           title="${ungettext(u'%i item', u'%i items', item['current_count']) % item['current_count']}"
           rel="nofollow">
            ${item['link_text']}</a>
        %endif
    </li>
    %endif
    %endfor
</ul>
%else:
${_(u'None')}
%endif
</%def>


<%def name="facet_items(items, indent=True)">
<ul${'' if indent else ' class="noindent"' | n}>
    %if not items:
    ${_(u'None')}
    %endif
    %for item in items:
    <li class="facet-item-level-${item['level']}${' expanded' if item['open'] else ''}">
        %if item['visible']:
        %if item['disabled']:
        <span class="disabled">
            %if not item['hide_checkbox']:
            <input type="checkbox" disabled="disabled" />
            %endif
            <span class="facet_item_text">${item['link_text']}</span>
            <span>(${item['current_count']})</span>
        </span>
        %else:
            %if item['selected']:
            <span class="selected">
                <input type="checkbox" class="facet_check" checked="checked" />
                <a class="facet_item_text remove" href="${item['url']|n}"
                  rel="nofollow">${item['link_text']}</a>
                <span>(${item['current_count']})</span>
            </span>
            %else:
            <span class="selected">
              <input type="checkbox" class="facet_check" />
              <a class="facet_item_text ${item['selected']}"
                 href="${item['url']|n}"
                 rel="nofollow">${item['link_text']}</a>
               <span>(${item['current_count']})</span>
            </span>
            %endif
        %endif

        ${h.literal(item['thumbnail'])}

        %if item["children"]:
        <a class="expand_arrow" href="#"></a>
        ${facet_items(item["children"], indent=not item['hide_checkbox'])}
        %endif
        %endif

    </li>
    %endfor
</ul>
</%def>

<%def name="overlay_pager(render_facets=True)">

<div>${pager.render_pager()}</div>
%if render_facets:
<div>${pager.render_facets()}</div>
%endif
</%def>


<%def name="index_based_sort_dropdown(sorts, pager)">

%if len(sorts.keys()):

<%
zipped = zip(pager.sorts.keys(), xrange(1, len(pager.sorts.keys()) + 1))
help = lib.pager.help_strings()
%>

<div class="filter_box">
    <div class="only-js">
        ${_('Sort:')}
        <select class="sort_options autoreload"
                name="${pager.sort_param}">
            %for sort, i in zipped:
            <% selected = i == pager.selected_sort %>
            <option value="${i}"
                    %if selected:
                    selected="selected"
                    %endif
                    title='${help.get(sort, '')}'
                    data-url="${pager.build_url(sort=i)|n}">
                ${sort}
            </option>
            %endfor
        </select>
    </div>
</div>
%endif

</%def>

<%def name="sort_dropdown(sorts)">
<%
help = lib.pager.help_strings()
%>
%if len(sorts):
<div class="filter_box">
    <div class="only-js">
        ${_('Sort:')}
        <% selected = sorts.selected() %>
        <select class="sort_options autoreload"
                name="${sorts.pager.sort_param}">
            %for (group, options) in sorts.grouped_options():
            %if group is not None:
            <optgroup label="${group}">
            %endif
                %for sort in options:
                <%
                selected_attr = ''
                if (sort == selected):
                    selected_attr = 'selected="selected"'
                %>
                <option value="${sort.value}"
                        ${selected_attr|n}
                        title='${help.get(sort.label, '')}'
                        data-url="${sorts.pager.build_url(sort=sort.value)}">
                    ${sort.label}
                </option>
                %endfor
            %if group is not None:
            </optgroup>
            %endif
            %endfor
        </select>
    </div>
</div>
%endif
</%def>


<%def name="sort_slidedown(sorts)">
%if len(sorts):
<%
selected = sorts.selected()
param = sorts.pager.sort_param
%>
<div class="filter_box">
    <div class="only-js">
        ${_('Sort:')}
        <span class="btn-group"
             id="${param}_btn_group">
            <a id="${param}_btn"
               class="button_round showhide_button control"
               data-target="#${param}_options"
               aria-controls="${param}_options"
               data-target-speed="fast"
               data-toggle-class="less"
               href="#">
                ${selected.label}
            </a>
        </span>
    </div>
</div>
<div id="${param}_options" style="display: none">
    %for (group, options) in sorts.grouped_options():
    <div class="sort_group">
        %if group is not None:
        <h6><em>${group}</em></h6>
        %else:
        <h6>&nbsp;</h6>
        %endif
        <ul class="nobullet">
            %for sort in options:
            <li data-sort-key="${sort.value}"
                %if sort is selected:
                class="active_sort"
                %endif
            >
                %if sort is selected:
                <em>${sort.label}</em>
                %else:
                <a href="${sorts.pager.build_url(sort=sort.value)}"
                   title="${sort.description or ''}"
                   class="${sort.description and 'ttip' or ''}">
                ${sort.label}
                </a>
                %endif
            </li>
        %endfor
        </ul>
    </div>
    %endfor
</div>

%endif
</%def>

