{% extends "html/base_authenticated.html" %}
{#
INPUT:
- pub -- a boolean stating whether to show in public mode.
- typ -- a string stating what kind of worksheets this listing shows
- worksheets -- list of Worksheet objects
- readonly -- a boolean stating whether the user is read only
#}
{% if pub %}
{% set worksheet_heading=gettext('Published Worksheets') %}
{% elif typ == 'trash' %}
{% set worksheet_heading=gettext('Deleted Worksheets') %}
{% elif typ == 'active' %}
{% set worksheet_heading=gettext('Active Worksheets') %}
{% else %}
{% set worksheet_heading=gettext('Archived Worksheets') %}
{% endif %}  

{% block title %}
{{ worksheet_heading }}
{% endblock %}

{% block page_id %}worksheet-listing-page{% endblock %}
{% block body_classes %}{% if pub %}public{% endif %}{% endblock %}

{% block javascript %}
{% if not pub %}
<link rel="stylesheet" href="/javascript/jqueryui/css/sage/jquery-ui-1.10.4.custom.css" />
<script type="text/javascript" src="/javascript/jqueryui/js/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript" src="/javascript/jquery/plugins/form/jquery.form.min.js"></script>
<script type="text/javascript" src="/javascript/jquery/plugins/jquery.bgiframe.min.js"></script>
<script type="text/javascript">
    var worksheet_filenames = {{ worksheet_filenames|tojson| safe }}; 
</script>
<script type="text/javascript" src="/javascript/sage/js/notebook_lib.js"></script>
<script type="text/javascript" src="/javascript/dynamic/notebook_dynamic.js"></script>
{% else %}
<script type="text/javascript" src="/javascript/sage/js/ws_list.js"></script>
{% endif %}
{% endblock %}


{% block main %}
{% if readonly is defined and readonly %}
<h1>{{ gettext('Account is read only.  You may download or delete worksheets or data.') }}</h1>
{% endif %}
<div id="user-main-controls" class="user-controls">
    {% if pub is not defined or not pub %}
    <a href="/new_worksheet" target="_blank">{{ gettext('New Worksheet') }}</a>
    <a href="/upload">{{ gettext('Upload') }}</a>
    <a href="/download_worksheets.zip">{{ gettext('Download All Active') }}</a>
    {% endif %}

    <div id="search-area">
        <form action="." method="GET">
            <input type="hidden" value="{{ typ if not pub else 'pub' }}" name="typ" />
            <input id="search-worksheets" size="20" value="{{ search if search else "" }}" name="search" />
            <button class="add_new_worksheet_menu" id="search-worksheets-button" type="submit">{{ gettext('Search Worksheets') }}</button>
        </form>
    </div>
</div>       
<div id="worksheet-list-controls" class="controls">
    {% if not pub %}
    <div class="action-buttons">
        {% if typ == 'archive' %}
        <button onClick="make_active_button();" title="{{ gettext('Unarchive selected worksheets so it appears in the default worksheet list') }}">{{ gettext('Unarchive') }}</button>
        {% else %}
        <button onClick="archive_button();" title="{{ gettext('Archive selected worksheets so they do not appear in the default worksheet list') }}">{{ gettext('Archive') }}</button>
        {% endif %}

        {% if typ != 'trash' %}
        <button onClick="delete_button();" title="{{ gettext('Move the selected worksheets to the trash') }}">{{ gettext('Delete') }}</button>
        {% else %}
        <button onClick="make_active_button();" title="{{ gettext('Move the selected worksheets out of the trash') }}">{{ gettext('Undelete') }}</button>
        {% endif %}

        <button onClick="stop_worksheets_button();" title="{{ gettext('Stop selected worksheets') }}">{{ gettext('Stop') }}</button>
        <button onClick="download_worksheets_button();" title="{{ gettext('Download selected worksheets') }}">{{ gettext('Download') }}</button>
    </div>
    <div class="folders">
        {{ gettext('Current Folder') }}:
        <a class="{{ 'bold' if typ == 'active' else '' }}usercontrol" href=".">{{ gettext('Active') }}</a>
        <a class="{{ 'bold' if typ == 'archive' else '' }}usercontrol" href=".?typ=archive">{{ gettext('Archived') }}</a>
        <a class="{{ 'bold' if typ == 'trash' else '' }}usercontrol" href=".?typ=trash">{{ gettext('Trash') }}</a>

        {% if typ == 'trash' %}
        <form method="POST" action="/emptytrash" id="empty-trash-form">
            <a class="boldusercontrol" onClick="empty_trash(this); return false" href="#">({{ gettext('Empty Trash') }})</a>
        </form>
        {% endif %}
    </div>
    {% endif %}
</div>

<table cellspacing="0" cellpadding="0" id="worksheet-list">
    <thead>
        <tr>
            {% if not pub %}
            <td class="checkbox">
                <input id="controlbox" onClick="set_worksheet_list_checks();" class="entry" type="checkbox" />
            </td>
            {% else %}
            <td><a class="listcontrol" href=".?sort=rating">{{ gettext('Rating') }}</a></td>
            {% endif %}

            <td>
                <a class="listcontrol" href=".?typ={{ typ }}&sort=name{{ '' if sort != 'name' or reverse else '&reverse=True' }}">
                    {{ worksheet_heading }}
                </a>
            </td>
            
            <td>
                <a class="listcontrol" href=".?typ={{ typ }}&sort=owner{{ '' if sort != 'owner' or reverse else '&reverse=True' }}">
                    {{ gettext('Owner') }} {{ '' if pub else ' / '+gettext('Collaborators') }}
                </a>
            </td>

            <td>
                <a class="listcontrol" href=".?typ={{ typ }}{{ '' if sort != 'last_edited' or reverse else '&reverse=True' }}">
                    {{ gettext('Last Edited') }}
                </a>
            </td>
        </tr>
    </thead>
    <tbody>
        {% if not worksheets %}
        {% if pub %}
        <tr>
            <td colspan="5">
                {{ gettext('There are no published worksheets.') }}
            </td>
        </tr>
        {% elif typ == 'active' %}
        <tr>
            <td colspan="5" id="welcome-message">
                {{ gettext('Welcome to Sage! You can <a href="/new_worksheet">create a new worksheet</a>, view <a href="/pub/">published worksheets</a>, or read the <a href="/help" target="_new">documentation</a>.') }}
            </td>
        </tr>
        {% endif %}
        {% else %}

        {% for worksheet in worksheets %}
        {% set name = worksheet.filename() %}
        <tr>
            <td class="entry">
                {% if pub %}
                
                <a class="worksheet_edit" href="/home/{{ name }}/rating_info">
                    {% if worksheet.rating() < 0 %}
                    ----
                    {% else %}
                    {{ worksheet.rating() }}
                    {% endif %}
                </a>
                
                {% else %}
                
                <input type="checkbox" unchecked id="{{ name|css_escape }}" />
                {# I'm removing this select since it is a massive performance killer and these 
                serve no real purpose at all.  Plus google docs got rid of the analogous menu.
                <select onchange="go_option(this);" class="worksheet_edit">
                    <option value="" title="File options" selected>File</option>
                    <option value="list_rename_worksheet('{{ name }}','{{ worksheet.name() }}');" title="Change the name of this worksheet.">
                        Rename...
                    </option>            
                    <option value="list_edit_worksheet('{{ name }}');" title="Open this worksheet and edit it">Edit</option>
                    <option value="list_copy_worksheet('{{ name }}');" title="Copy this worksheet">Copy Worksheet</option>
                    <option value="list_share_worksheet('{{ name }}');" title="Share this worksheet with others">Collaborate</option>
                    <option value="list_publish_worksheet('{{ name }}');" title="Publish this worksheet on the internet">Publish</option>
                    <option value="list_revisions_of_worksheet('{{ name }}');" title="See all revisions of this worksheet">Revisions</option>
                </select>
                #}      
                {% endif %}
            </td>

            <td class="worksheet_link">
                <a title="{{ worksheet.name() }}" id="name-{{ name|css_escape }}" class="worksheetname" href="/home/{{ name }}/">
                    {% if worksheet.compute_process_has_been_started() %}({{ gettext('running') }}){% endif %}
                    {{ worksheet.truncated_name(50) }}
                </a>
                
                {% if not pub and worksheet.is_published() %}(Published){% endif %}
            </td>
            <td class="owner_collab">

                {% if not pub %}
                {{ worksheet.owner() }}
                {% else %}
                {{worksheet.worksheet_that_was_published().owner()}}
                {% endif %}
                
                {% if not pub and typ != 'trash' %}

                {% set shared = False %}

                {% if worksheet.collaborator_names() %}
                / {{ worksheet.collaborator_names(5) }}
                {% set shared = True %}
                {% endif %}

                {% if worksheet.viewer_names() %}
                / {{ worksheet.viewer_names(5) }}
                {% set shared = True %}
                {% endif %}

                {% if (worksheet.owner() != username) or username == 'admin' %}
                {% set shared = False %}
                {% endif %}
                
                {% if shared %}
                <a class="share" href="/home/{{ worksheet.filename() }}/share">{{ gettext('Add or Delete') }}</a>
                {% else %}
                <a class="share" href="/home/{{ worksheet.filename() }}/share">{{ gettext('Share now') }}</a>
                {% endif %}

                {% if worksheet.has_published_version() %}
                <a href="/home/{{ worksheet.published_version().filename() }}">
                    ({{ gettext('published') }})
                </a>
                {% endif %}
                
                {% endif %}
            </td>
            <td>
                {{ worksheet.html_time_nice_edited(username) | safe }}
            </td>
        </tr>
        {% endfor %}
        {% endif %}
    </tbody>
</table>
{% endblock %}
