2014-10-03 21:49:18 +03:00
|
|
|
var $entry_markdown_header = $("#entry-markdown-header");
|
|
|
|
var $entry_preview_header = $("#entry-preview-header");
|
|
|
|
var $entry_markdown = $(".entry-markdown");
|
|
|
|
var $entry_preview = $(".entry-preview");
|
2014-10-21 01:27:38 +03:00
|
|
|
var $page_name = $("#page-name");
|
|
|
|
var $page_message = $("#page-message");
|
2014-10-03 21:49:18 +03:00
|
|
|
|
|
|
|
// Tabs
|
|
|
|
$entry_markdown_header.click(function(){
|
|
|
|
$entry_markdown.addClass('active');
|
|
|
|
$entry_preview.removeClass('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
$entry_preview_header.click(function(){
|
|
|
|
$entry_preview.addClass('active');
|
|
|
|
$entry_markdown.removeClass('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('shaMismatch', function() {
|
|
|
|
bootbox.dialog({
|
|
|
|
title: "Page has changed",
|
|
|
|
message: "This page has changed and differs from your draft. What do you want to do?",
|
|
|
|
buttons: {
|
|
|
|
ignore: {
|
|
|
|
label: "Ignore",
|
|
|
|
className: "btn-default",
|
|
|
|
callback: function() {
|
|
|
|
var info = aced.info();
|
|
|
|
info['ignore'] = true;
|
|
|
|
aced.info(info);
|
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
},
|
2014-10-03 21:49:18 +03:00
|
|
|
discard: {
|
|
|
|
label: "Discard Draft",
|
|
|
|
className: "btn-danger",
|
|
|
|
callback: function() {
|
|
|
|
aced.discard();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
changes: {
|
|
|
|
label: "Show Diff",
|
|
|
|
className: "btn-primary",
|
|
|
|
callback: function() {
|
|
|
|
bootbox.alert("Draft diff not done! Sorry");
|
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-03 21:49:18 +03:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
$(function(){
|
|
|
|
$("#discard-draft-btn").click(function() {
|
|
|
|
aced.discard();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".entry-markdown .floatingheader").click(function(){
|
|
|
|
aced.editor.focus();
|
|
|
|
});
|
|
|
|
|
2014-11-11 20:46:07 +02:00
|
|
|
$("#delete-page-btn").click(function() {
|
2015-09-28 07:57:56 +03:00
|
|
|
bootbox.confirm('Are you sure you want to delete this page?', function(result) {
|
|
|
|
if (result) {
|
|
|
|
deletePage();
|
|
|
|
}
|
|
|
|
});
|
2014-10-03 21:49:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-09-28 07:57:56 +03:00
|
|
|
var deletePage = function() {
|
|
|
|
var pageName = $page_name.val();
|
|
|
|
var path = Config['RELATIVE_PATH'] + '/' + pageName;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
url: path,
|
|
|
|
}).done(function(data) {
|
|
|
|
var msg = 'Deleted page: ' + pageName;
|
|
|
|
bootbox.alert(msg, function() {
|
|
|
|
location.href = '/';
|
|
|
|
});
|
|
|
|
}).fail(function(data, status, error) {
|
|
|
|
bootbox.alert('Error deleting page!');
|
|
|
|
});
|
|
|
|
};
|
2016-08-10 06:20:06 +03:00
|
|
|
var last_imports = '';
|
2016-08-10 04:45:07 +03:00
|
|
|
var partials = [];
|
2014-10-03 21:49:18 +03:00
|
|
|
var aced = new Aced({
|
|
|
|
editor: $('#entry-markdown-content').find('.editor').attr('id'),
|
2016-08-09 07:52:08 +03:00
|
|
|
renderer: function(md) {
|
|
|
|
var doc = metaMarked(md);
|
2016-08-10 03:14:29 +03:00
|
|
|
if (doc.meta && 'import' in doc.meta) {
|
2016-08-10 04:45:07 +03:00
|
|
|
// If the imports have changed, refresh them from the server
|
2016-08-10 06:20:06 +03:00
|
|
|
if (doc.meta['import'].toString() != last_imports) {
|
|
|
|
last_imports = doc.meta['import'].toString();
|
2016-08-10 04:45:07 +03:00
|
|
|
$.getJSON('/_partials', {'imports': doc.meta['import']}, function (response) {
|
|
|
|
partials = response['partials'];
|
2016-08-10 03:14:29 +03:00
|
|
|
// TODO: Better way to force update of the preview here than this fake signal?
|
|
|
|
aced.editor.session.doc._signal('change',
|
2016-08-10 04:45:07 +03:00
|
|
|
{'action': 'insert', 'lines': [], 'start': {'row': 0}, 'end': {'row': 0}});
|
2016-08-10 03:14:29 +03:00
|
|
|
});
|
2016-08-09 07:52:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MDR.convert(md, partials)
|
|
|
|
},
|
2014-10-03 21:49:18 +03:00
|
|
|
info: Commit.info,
|
|
|
|
submit: function(content) {
|
|
|
|
var data = {
|
2015-12-10 01:41:15 +02:00
|
|
|
name: $page_name.val().replace(/^\/*/g, "").replace(/\/+/g, "/"),
|
2014-10-21 01:27:38 +03:00
|
|
|
message: $page_message.val(),
|
2014-10-03 21:49:18 +03:00
|
|
|
content: content
|
2014-10-02 01:14:54 +03:00
|
|
|
};
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2015-09-28 02:14:34 +03:00
|
|
|
// If renaming an existing page, use the old page name for the URL to PUT to
|
2015-12-10 01:41:15 +02:00
|
|
|
var subPath = (PAGE_NAME) ? PAGE_NAME : data['name'];
|
2015-09-28 02:14:34 +03:00
|
|
|
var path = Config['RELATIVE_PATH'] + '/' + subPath;
|
|
|
|
var newPath = Config['RELATIVE_PATH'] + '/' + data['name'];
|
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
var type = (Commit.info['sha']) ? "PUT" : "POST";
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: type,
|
|
|
|
url: path,
|
|
|
|
data: data,
|
|
|
|
dataType: 'json'
|
|
|
|
}).always(function(data, status, error) {
|
2014-11-11 20:46:07 +02:00
|
|
|
var res = data['responseJSON'];
|
|
|
|
if (res && res['error']) {
|
2014-10-21 01:27:38 +03:00
|
|
|
$page_name.addClass('parsley-error');
|
2014-11-11 20:46:07 +02:00
|
|
|
bootbox.alert("<h3>" + res['message'] + "</h3>");
|
2014-10-21 01:27:38 +03:00
|
|
|
} else {
|
2015-09-28 02:14:34 +03:00
|
|
|
location.href = newPath;
|
2014-10-21 01:27:38 +03:00
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
});
|
|
|
|
}
|
2014-11-11 20:46:07 +02:00
|
|
|
});
|