{% extends "home_base.html" %}

{% load seahub_tags avatar_tags i18n %}
{% load url from future %}

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

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

{% block left_panel %}
    {{ block.super }}
    <div class="wiki-index">
        <h3>{% trans "Wiki Index" %}</h3>
        {% if not wiki_index_exists %}
        <p>{% trans "You do not have index now." %}</p>
        <button id="create-index">{% trans "Create" %}</button>
        {% endif %}
    </div>
{% endblock %}

{% 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>{% trans "Create Wiki" %}</h3>
        <label>{% trans "Name" %}</label><br/>
        <input type="text" name="repo_name" value="{% trans "personal-wiki" %}" maxlength="{{max_file_name}}" class="input" /><br />
        <label>{% trans "Description" %}</label><br/>
        <textarea name="repo_desc" class="textarea">{% 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>{% 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 w100 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 %}" target="_blank" class="link">{% trans "Wiki History" %}</a></li>
        </ul>
        
        <button id="page-create" class="op-btn">{% trans "New Page" %}</button>
        <button id="page-delete" class="op-btn" data-url="{% url 'personal_wiki_page_delete' page %}">{% trans "Delete Page" %}</button>
        <button id="page-edit" class="op-btn">{% trans "Edit Page" %}</button>
        <button id="page-history" class="op-btn">{% trans "Page History" %}</button>
    </div>

    <div id="wiki-area" class="article">
        <h1>{{ 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>{% trans "New Page"%}</h3>
        <label>{% 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" id="page-create-submit" value="{% trans "Submit"%}" class="submit"/>
    </form>
    {% endif %}

{% endblock %}

{% block extra_script %}{{block.super}}
<script type="text/javascript" src="{{MEDIA_URL}}js/Markdown.Converter.js"></script>
<script type="text/javascript" src="{{MEDIA_URL}}js/Markdown.Extra.js"></script>
<script type="text/javascript">
{% if wiki_exists %}
var converter = new Markdown.Converter();
{% include 'snippets/md_xss.html' %}
Markdown.Extra.init(converter, {extensions: ["fenced_code_gfm", "tables", "def_list"]});
$('#wiki-content')
.html(addAnchorsToHeaders(converter.makeHtml('{{ content|escapejs }}')))
.find('h1, h2').hover(
    function() {
        $('.anchor', $(this)).removeClass('vh');
    },  
    function() {
        $('.anchor', $(this)).addClass('vh');
    }
);  

$('#page-create').click(function() {
    $('#page-create-form').modal({appendTo: '#main', autoResize: true});
})

$('#page-edit').click(function() {
    location.href = ("{% url 'personal_wiki_page_edit' page %}");
})

$('#page-list').click(function () {
    location.href = ("{% url 'personal_wiki_pages' %}");
});

$('#page-history').click(function () {
    window.open("{% url 'file_revisions' repo_id %}" + "?p=" + "{{path|urlencode}}");
});

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

{% else %}
$('#wiki-create').click(function() {
    $('#wiki-create-form').modal({appendTo: '#main', autoResize: true});
    return false;
});
$('#wiki-create-form').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(data, textStatus, jqXHR) {
            var errors = $.parseJSON(data.responseText);
            $.each(errors, function(index, value) {
                apply_form_error(form_id, value);
            });
            enable(submit_btn);
        }
    });

    return false;
});

{% if owned_repos %}
var all_repos = [];
{% for a_repo in owned_repos %}
    all_repos.push({
        'data': '{{ a_repo.props.name }}',
        'attr': {'repo_id': '{{ a_repo.props.id }}', 'root_node': true},
    });
{% endfor %}
$('#wiki-use-lib').click(function() {
    var form = $('#wiki-use-lib-form'),
        file_tree = new FileTree();
    form.modal({appendTo:'#main', autoResize:true, focus:false});
    file_tree.renderDirTree($('#repos-dirs').data('site_root', '{{SITE_ROOT}}'), form, all_repos);
    return false;
});
$('#wiki-use-lib-form').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

{% if wiki_index_exists %}
$('.wiki-index').append(converter.makeHtml('{{ index_content|escapejs }}'));
{% else %}
$('#create-index').click(function() {
    var form = $('#page-create-form');
    $('[name="page_name"]', form).val('index');
    form.submit();
});
{% endif %}
</script>
{% endblock %}
