{% extends "home_base.html" %}

{% load seahub_tags avatar_tags i18n %}


{% block sub_title %}{% trans "Personal Wiki" %} - {% endblock %}

{% block cur_personal_wiki %}tab-cur{% endblock %}

{% comment %}
{% block left_panel %}
    {{ block.super }}
    {% if wiki_exists %}
    <div class="wiki-index">
        {% if not wiki_index_exists %}
        <h3 class="hd">{% trans "Wiki Index" %}</h3>
        <p>{% trans "No index now." %}</p>
        <button id="create-index">{% trans "Create" %}</button>
        {% else %}
        <div class="hd ovhd">
            <h3 class="fleft">{% trans "Wiki Index" %}</h3>
            <a id="edit-index" class="wiki-index-edit-icon op-icon icon-pencil fright" title="{% trans "Edit" %}" aria-label="{% trans "Edit" %}" href="#"></a>
        </div>
        <div id="wiki-index-con"></div>
        {% endif %}
    </div>
    {% endif %}
{% endblock %}
{% endcomment %}

{% block right_panel %}
    {% if not wiki_exists %}
    <div class="empty-tips">
        <h2 class="alc">{% trans "You do not have personal wiki" %}</h2>
        <p>{% trans "Seafile Wiki enables you to organize your knowledge in a simple way. The contents of wiki is stored in a normal library with pre-defined file/folder structure. This enables you to edit your wiki in your desktop and then sync back to the server." %}</p>
        <a id="wiki-create" href="#">{% trans "Create Wiki Now" %}</a>
        {% if owned_repos %} 
        <br /><a id="wiki-use-lib" href="#">{% trans "Choose a library as wiki" %}</a>
        {% endif %} 
    </div>
    <form id="wiki-create-form" action="" method="post" class="hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "Create Wiki" %}</h3>
        <label for="wiki-name">{% trans "Name" %}</label><br/>
        <input type="text" name="repo_name" value="{% trans "personal-wiki" %}" maxlength="{{max_file_name}}" class="input" id="wiki-name" /><br />
        <label for="wiki-desc">{% trans "Description" %}</label><br/>
        <textarea name="repo_desc" class="textarea" id="wiki-desc">{% trans "Personal Wiki Pages" %}</textarea><br/>
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
    </form>

    <form id="wiki-use-lib-form" action="{% url 'personal_wiki_use_lib' %}" method="post" class="file-choose-form hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "Choose a library as wiki:" %}</h3>
        <div class="dir-tree-cont">
            <div id="repos-dirs"></div>
        </div>
        <input type="hidden" name="dst_repo" value="" />
        <p class="error hide">{% trans "Please click and choose a library."%}</p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
        <button class="simplemodal-close">{% trans "Cancel"%}</button>
    </form>

    {% else %}
    <div class="wiki-top ovhd">
        <ul class="wiki-nav fleft">
            <li class="item{% if page == 'home' %} cur-item{% endif %}"><a href="{% url 'personal_wiki' %}" class="link">{% trans "Home" %}</a></li>
            <li class="item"><a href="{% url 'personal_wiki_pages' %}" class="link">{% trans "Pages" %}</a></li>
            <li class="item"><a href="{% url 'repo_history' repo_id %}" class="link" id="wiki-history">{% trans "Wiki History" %}</a></li>
        </ul>

        <button id="page-create" class="btn-white">{% trans "New Page" %}</button>
        <button id="page-delete" class="btn-white" data-url="{% url 'personal_wiki_page_delete' page %}">{% trans "Delete Page" %}</button>
        <a class="sf-btn-link btn-white" href="{% url 'personal_wiki_page_edit' page %}">{% trans "Edit Page" %}</a>
        <a class="sf-btn-link btn-white" href="{% url 'file_revisions' repo_id %}?p={{path|urlencode}}" id="page-history">{% trans "Page History" %}</a>
    </div>

    <div id="wiki-area" class="article">
        <h1 class="bold">{{ page|capfirst }}</h1>
        <div id="wiki-content"></div><!--content will be offered by js-->
        <p id="wiki-last-modified">{% blocktrans with modifier=latest_contributor|email2nickname modify_time=last_modified|translate_seahub_time %}Last modified by {{modifier}}, {{modify_time}}{% endblocktrans %}
        </p>
    </div>

    <form id="page-create-form" action="{% url 'personal_wiki_page_new' %}" method="post" class="hide">{% csrf_token %}
        <h3 id="dialogTitle">{% trans "New Page"%}</h3>
        <label for="page-name">{% trans "Name"%}</label><br/>
        <input id="page-name" type="text" name="page_name" value="" maxlength="{{max_file_name}}" class="input" /><br />
        <p class="error hide"></p>
        <input type="submit" value="{% trans "Submit"%}" class="submit" />
    </form>
    {% endif %}

{% endblock %}

{% block extra_script %}{{block.super}}
<script type="text/javascript" src="{{MEDIA_URL}}js/editormd/lib/marked.min.js"></script>
<script type="text/javascript">
{% if wiki_exists %}
var renderer = new marked.Renderer();
// remove 'id' attribute for headings
renderer.heading = function (text, level) {
    return '<h' + level + '>' + text + '</h' + level + '>';
};
marked.setOptions({
    renderer: renderer,
    breaks: true, // keep linebreak
    smartLists: true,
    sanitize: true // Ignore any HTML that has been input
});
$('#wiki-content')
.html(addAnchorsToHeaders(marked('{{ content|escapejs }}')))
.find('h1, h2')
.on('mouseenter', function() {
    $('.anchor', $(this)).removeClass('vh');
})  
.on('mouseleave', function() {
    $('.anchor', $(this)).addClass('vh');
});  

$('#page-create').on('click', function() {
    $('#page-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
});
$('#page-create-form').on('submit', function() {
    var page_name = $.trim($('[name="page_name"]', $(this)).val());
    if (!page_name) {
        return false;
    }
});

addConfirmTo($('#page-delete'), {
    'title': "{% trans "Delete Page" %}",
    'con': "{% trans "Are you sure you want to delete this page?" %}"
});
$('a.wiki-page-missing').on('click', function() {
    $('#page-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
    $('#page-name').val($(this).text());
    return false;
});

{% else %}
$('#wiki-create').on('click', function() {
    $('#wiki-create-form').modal({appendTo: '#main'});
    $('#simplemodal-container').css({'height':'auto'});
    return false;
});
$('#wiki-create-form').on('submit', function () {
    var form = $(this),
        form_id = form.attr('id'),
        repo_name = $('[name="repo_name"]', form).val(),
        repo_desc = $('[name="repo_desc"]', form).val();
    if (!$.trim(repo_name)) {
         apply_form_error(form_id, "{% trans "Name is required." %}");
         return false;
    }
    if (!$.trim(repo_desc)) {
         apply_form_error(form_id, "{% trans "Description is required." %}");
         return false;
    }

    var submit_btn = $('[type="submit"]', form);
    disable(submit_btn);

    $.ajax({
        url: '{% url 'personal_wiki_create' %}',
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {
            'repo_name': repo_name,
            'repo_desc': repo_desc
        },
        success: function(data) {
            location.href = data['href'];
        },
        error: function(xhr, textStatus, jqXHR) {
            var error_msg = prepareAjaxErrorMsg(xhr);
            apply_form_error(form_id, error_msg);
            enable(submit_btn);
        }
    });

    return false;
});

{% if owned_repos %}
var all_repos = [];
{% for a_repo in owned_repos %}
    all_repos.push({
        'text': HTMLescape('{{ a_repo.props.name|escapejs }}'),
        'data': {
            'repo_id': '{{ a_repo.props.id }}',
            'path': '/'
        }
    });
{% endfor %}
$('#wiki-use-lib').on('click', function() {
    var $form = $('#wiki-use-lib-form');
    $form.modal({appendTo:'#main', autoResize:true, focus:false});
    $('#simplemodal-container').css({'width':'auto', 'height':'auto'});
    FileTree.renderDirTree($('#repos-dirs').data('site_root', '{{SITE_ROOT}}'), $form, all_repos);
    return false;
});
$('#wiki-use-lib-form').on('submit', function() {
    var dst_repo = $('[name="dst_repo"]', $(this)).val();
    if (!$.trim(dst_repo)) {
        $('.error', $(this)).removeClass('hide');
        return false;
    }
});
{% endif %}

{% endif %}                     // END wiki_exists

{% comment %}
{% if wiki_exists %}
{% if wiki_index_exists %}
$('#wiki-index-con').html(marked('{{ index_content|escapejs }}'));
{% with page_name='index' %}
$('#edit-index').attr('href', "{% url 'personal_wiki_page_edit' page_name %}");
{% endwith %}
{% else %}
$('#create-index').on('click', function() {
    var $form = $('#page-create-form');
    $('[name="page_name"]', $form).val('index');
    $form.trigger('submit');
});
{% endif %}
{% endif %}
{% endcomment %}

// for 'go back'
var $pageHistory = $('#page-history');
$pageHistory.attr('href', $pageHistory.attr('href') + '&referer=' + encodeURIComponent(location.href));
var $wikiHistory = $('#wiki-history');
$wikiHistory.attr('href', $wikiHistory.attr('href') + '?referer=' + encodeURIComponent(location.href));
</script>
{% endblock %}
