<%namespace name="components" file="/components.html"/>


<%def name="smallrow(tile, page)">
    <div class="tile smallrow">
        ${h.page.link(page)|n}
    </div>
</%def>

<%! from adhocracy.lib import sorting %>

<%def name="row(tile, page, level=0)">
    <li class="content_box">
        <div class="level-${str(level)}">
            %if c.is_hierarchical and level == 0 and len(page.subpages) > 0 and not page.is_sectionpage():
            <div class="more only-js">
                <a href="#" class="showhide_button"
                   data-target="#${page.id}-sub"
                   data-toggle-class="less"
                   data-target-speed="fast"
                   data-toggle-text="${_('hide sub-topics')}">
                ${_('show sub-topics')}</a>
            </div>
            %endif
            <h3>
                <a class="link" href="${h.entity_url(page)}">${page.title}</a>
            </h3>
            <div class="meta noclear">
                %if page.function == page.NORM:
                    <% selection_count = page.selection_count(recursive=page.sectionpage) %>
                    ${ungettext("%s proposal", "%s proposals", selection_count) % selection_count}
                    ·
                %endif
                <% comment_count = page.comment_count(recursive=page.sectionpage) %>
                ${ungettext("%s comment", "%s comments", comment_count) % comment_count}
                ·
                <% latest_comment_time = page.find_latest_comment_time(recursive=page.sectionpage)%>
                ${_("latest <b>%s</b>") % h.datetime_tag(latest_comment_time)|n}
            </div>
        </div>

        %if c.is_hierarchical and len(page.subpages) > 0 and not page.is_sectionpage():
        <ul id="${'%s-sub' % page.id if level == 0 else ''}"
            class="content_list ${'only-no-js' if level == 0 else ''}">
            %for page in sorting.delegateable_title(page.subpages):
            ${row(None, page, level=level + 1)}
            %endfor
        </ul>
        %endif
    </li>


</%def>

<%def name="select_page(tile, field_name, select, exclude, functions,
list_limit, initial_option=None, allow_empty=True, include_sectionpages=False)">
    %if list_limit is not None and model.Page.count(instance=c.instance, exclude=exclude, functions=functions) > list_limit:
        <input id="${field_name}" name="${field_name}" />
    %else:
        <select id="${field_name}" name="${field_name}">
        %if allow_empty:
            <option value="">${_("(none)")}</option>
        %endif
        <%
        pages = model.Page.all(instance=c.instance, exclude=exclude, functions=functions, include_sectionpages=include_sectionpages)
        pages = lib.sorting.delegateable_full_title(pages)
        %>
        %for page in pages:
            <option value="${page.id}" ${'selected="selected"' if select and select.id == page.id else "" | n}>${page.full_title}</option>
        %endfor
        </select>
    %endif
</%def>



<%def name="inline(tile, page, text, subpages_pager=None, hide_discussion=False)">
    ## FIXME: better text, translate
    ${tiles.text.minimal(text, missing_translation=_('No text.'))}
</%def>


<%def name="header(tile, page, active, text, variant)">
    <div class="top_actions title">

        %if can.norm.create() and active == 'view':
            <a class="button title admin ttip" title="${_('Create a new norm.')}"
                href="/page/new">${_("new norm")}</a>
        %endif

        %if can.variant.edit(page, "") and page.has_variants and active == 'diff':
            <a class="button title add ttip" title="${_('Propose alternative language.')}"
                href="${h.entity_url(text, member='branch')}">${_("branch new variant")}</a>
        %endif

        %if variant==model.Text.HEAD and can.variant.edit(page, variant):
            <a class="button title admin ttip" title="${_('Edit the status quo.')}"
                href="${h.entity_url(text, member='edit')}">${_("edit")}</a>
        %elif can.variant.edit(page, variant):
            <a class="button title edit ttip" title="${_('Edit this variant.')}"
                href="${h.entity_url(text, member='edit')}">${_("edit")}</a>
        %endif

        %if can.variant.delete(page, variant) and active == 'diff' and c.variant:
            <a class="button title delete ttip" title="${_('Delete this variant.')}"
                href="${h.entity_url(text, member='ask_purge')}">${_("purge variant")}</a>
        %endif

        %if can.page.delete(page) and active != 'diff':
            <a class="button title delete"
                href="${h.entity_url(page, member='ask_delete')}">${_("delete")}</a>
        %endif

        ${components.watch(page)}
    </div>

    <div style="clear: both;"></div>
    <h1 class="page_title">
    %if text.variant != text.HEAD:
        %if c.variant:
            ${_("Variant %s of %s") % (text.variant_html, text.title)|n}
        %else:
            ${_("Variant of %s") % (text.title,)}
        %endif
    %else:
        ${text.title}
    %endif
    </h1>
</%def>


