2016-02-06 16:08:48 +00:00
|
|
|
$(document).on('page:editor', function() {
|
2016-02-07 09:55:16 +00:00
|
|
|
var container = $('.editor');
|
2016-02-07 09:47:44 +00:00
|
|
|
var preview = $('#editor-preview');
|
|
|
|
var editor = $('#editor-source');
|
|
|
|
|
2016-02-07 09:55:16 +00:00
|
|
|
if (container.hasClass('complete')) {
|
|
|
|
// Change title field when editing the header
|
2016-02-08 08:32:03 +00:00
|
|
|
$('#content').on('keyup', '#site-title', function() {
|
2016-02-07 09:55:16 +00:00
|
|
|
$('.frontmatter #title').val($(this).val());
|
|
|
|
});
|
|
|
|
}
|
2016-02-07 09:47:44 +00:00
|
|
|
|
2016-02-07 09:55:16 +00:00
|
|
|
if (!container.hasClass('frontmatter-only')) {
|
|
|
|
// Setup ace editor
|
|
|
|
var mode = $("#editor-source").data('mode');
|
|
|
|
var textarea = $('textarea[name="content"]').hide();
|
|
|
|
var aceEditor = ace.edit('editor-source');
|
|
|
|
aceEditor.getSession().setMode("ace/mode/" + mode);
|
|
|
|
aceEditor.getSession().setValue(textarea.val());
|
|
|
|
aceEditor.getSession().on('change', function() {
|
|
|
|
textarea.val(aceEditor.getSession().getValue());
|
|
|
|
});
|
|
|
|
aceEditor.setOptions({
|
|
|
|
wrap: true,
|
|
|
|
maxLines: Infinity,
|
|
|
|
theme: "ace/theme/github",
|
|
|
|
showPrintMargin: false,
|
2016-02-09 19:52:48 +00:00
|
|
|
fontSize: "1em",
|
|
|
|
minLines: 20
|
2016-02-07 09:55:16 +00:00
|
|
|
});
|
2016-02-07 09:34:28 +00:00
|
|
|
|
2016-02-08 08:32:03 +00:00
|
|
|
$('#content').on('click', '#see-source', function(event) {
|
2016-02-07 09:55:16 +00:00
|
|
|
event.preventDefault();
|
2016-02-07 09:34:28 +00:00
|
|
|
preview.hide();
|
|
|
|
editor.fadeIn();
|
2016-02-08 20:15:55 +00:00
|
|
|
$(this).addClass('active');
|
|
|
|
$("#see-preview").removeClass('active');
|
2016-02-07 09:55:16 +00:00
|
|
|
$("#see-preview").data("previewing", "false");
|
|
|
|
})
|
2016-02-07 09:34:28 +00:00
|
|
|
|
2016-02-07 09:55:16 +00:00
|
|
|
// Toggles between preview and editing mode
|
2016-02-08 08:32:03 +00:00
|
|
|
$('#content').on('click', '#see-preview', function(event) {
|
2016-02-07 09:55:16 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// If it currently in the preview mode, hide the preview
|
|
|
|
// and show the editor
|
|
|
|
if ($(this).data("previewing") == "true") {
|
|
|
|
preview.hide();
|
|
|
|
editor.fadeIn();
|
2016-02-08 20:15:55 +00:00
|
|
|
$(this).removeClass('active');
|
|
|
|
$("#see-source").addClass('active');
|
2016-02-07 09:55:16 +00:00
|
|
|
$(this).data("previewing", "false");
|
|
|
|
} else {
|
|
|
|
// If it's in editing mode, convert the markdown to html
|
|
|
|
// and show it
|
|
|
|
var converter = new showdown.Converter(),
|
|
|
|
text = aceEditor.getValue(),
|
|
|
|
html = converter.makeHtml(text);
|
2016-02-07 09:34:28 +00:00
|
|
|
|
2016-02-07 09:55:16 +00:00
|
|
|
// Hide the editor and show the preview
|
|
|
|
editor.hide();
|
|
|
|
preview.html(html).fadeIn();
|
2016-02-08 20:15:55 +00:00
|
|
|
$(this).addClass('active');
|
|
|
|
$("#see-source").removeClass('active');
|
2016-02-07 09:55:16 +00:00
|
|
|
$(this).data("previewing", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-08 08:32:03 +00:00
|
|
|
$('#content').on('keypress', 'input', function(event) {
|
2016-02-07 09:55:16 +00:00
|
|
|
if (event.keyCode == 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
$('input[value="Save"]').focus().click();
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-07 09:34:28 +00:00
|
|
|
});
|
|
|
|
|
2016-03-13 07:57:21 +00:00
|
|
|
var submitActor = null;
|
|
|
|
|
|
|
|
$('#content').on('click', 'form input[type=submit]', function(event) {
|
|
|
|
submitActor = this;
|
|
|
|
});
|
|
|
|
|
2016-02-06 16:08:48 +00:00
|
|
|
// Submites any form in the page in JSON format
|
2016-02-08 08:32:03 +00:00
|
|
|
$('#content').on('submit', 'form', function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2016-02-07 09:55:16 +00:00
|
|
|
if (!container.hasClass('frontmatter-only')) {
|
|
|
|
// Reset preview area and button to make sure it will
|
|
|
|
// not serialize any form inside the preview
|
|
|
|
preview.html('').fadeOut();
|
|
|
|
$("#see-preview").data("previewing", "false");
|
|
|
|
editor.fadeIn();
|
|
|
|
}
|
2016-02-06 16:08:48 +00:00
|
|
|
|
2016-03-13 07:57:21 +00:00
|
|
|
var button = $(submitActor);
|
2016-03-11 20:38:57 +00:00
|
|
|
var data = {
|
|
|
|
content: $(this).serializeJSON(),
|
2016-03-12 09:25:15 +00:00
|
|
|
contentType: button.data("type"),
|
2016-03-11 20:38:57 +00:00
|
|
|
schedule: button.data("schedule"),
|
|
|
|
regenerate: button.data("regenerate")
|
|
|
|
}
|
2016-02-06 16:08:48 +00:00
|
|
|
|
2016-06-07 17:15:55 +00:00
|
|
|
console.log(JSON.stringify(data));
|
|
|
|
|
2016-03-06 13:06:30 +00:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open("POST", window.location);
|
|
|
|
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
2016-03-11 20:38:57 +00:00
|
|
|
request.send(JSON.stringify(data));
|
2016-03-06 13:06:30 +00:00
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if (request.readyState == 4) {
|
2016-03-11 20:38:57 +00:00
|
|
|
var response = JSON.parse(request.responseText),
|
|
|
|
type = "success",
|
|
|
|
timeout = 5000;
|
|
|
|
|
2016-03-06 13:06:30 +00:00
|
|
|
if (request.status == 200) {
|
2016-03-11 20:38:57 +00:00
|
|
|
response.message = button.data("message");
|
2016-03-06 13:06:30 +00:00
|
|
|
}
|
2016-03-11 20:38:57 +00:00
|
|
|
|
|
|
|
if (request.status != 200) {
|
|
|
|
type = "error";
|
|
|
|
timeout = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
notification({
|
|
|
|
text: response.message,
|
|
|
|
type: type,
|
|
|
|
timeout: timeout
|
|
|
|
});
|
2016-03-06 13:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-06 16:08:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Adds one more field to the current group
|
2016-02-08 08:32:03 +00:00
|
|
|
$("#content").on('click', '.add', function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
|
|
|
defaultID = "lorem-ipsum-sin-dolor-amet";
|
|
|
|
|
2016-02-08 19:25:40 +00:00
|
|
|
// Remove if there is an incomplete new item
|
|
|
|
newItem = $("#" + defaultID);
|
|
|
|
if (newItem.length) {
|
|
|
|
newItem.remove();
|
2016-02-06 16:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block = $(this).parent().parent();
|
|
|
|
blockType = block.data("type");
|
|
|
|
blockID = block.attr("id");
|
|
|
|
|
|
|
|
// If the Block Type is an array
|
|
|
|
if (blockType == "array") {
|
|
|
|
newID = blockID + "[]";
|
|
|
|
input = blockID;
|
|
|
|
input = input.replace(/\[/, '\\[');
|
|
|
|
input = input.replace(/\]/, '\\]');
|
|
|
|
block.append('<div id="' + newID + '-' + $('#' + input + ' > div').length + '" data-type="array-item"><input name="' + newID + ':auto" id="' + newID + '"></input><span class="actions"> <button class="delete">−</button></span></div></div>');
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:25:40 +00:00
|
|
|
// Main add button, after all blocks
|
|
|
|
if (block.is('div') && block.hasClass("frontmatter")) {
|
|
|
|
block = $('.blocks');
|
2016-02-07 16:16:42 +00:00
|
|
|
blockType = "object";
|
|
|
|
}
|
|
|
|
|
2016-02-06 16:08:48 +00:00
|
|
|
// If the Block is an object
|
|
|
|
if (blockType == "object") {
|
2016-02-08 19:25:40 +00:00
|
|
|
block.append('<div class="block" id="' + defaultID + '"></div>');
|
|
|
|
|
2016-02-06 16:08:48 +00:00
|
|
|
newItem = $("#" + defaultID);
|
|
|
|
newItem.html('<input id="name-' + defaultID + '" placeholder="Write the field name and press enter..."></input>');
|
|
|
|
field = $("#name-" + defaultID);
|
|
|
|
|
|
|
|
// Show a notification with some information for newbies
|
|
|
|
if (!document.cookie.replace(/(?:(?:^|.*;\s*)placeholdertip\s*\=\s*([^;]*).*$)|^.*$/, "$1")) {
|
|
|
|
var date = new Date();
|
|
|
|
date.setDate(date.getDate() + 365);
|
|
|
|
document.cookie = 'placeholdertip=true; expires=' + date.toUTCString + '; path=/';
|
|
|
|
|
|
|
|
notification({
|
|
|
|
text: 'Write the field name and then press enter. If you want to create an array or an object, end the name with ":array" or ":object".',
|
|
|
|
type: 'information'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(field).keypress(function(event) {
|
|
|
|
// When you press enter within the new name field:
|
|
|
|
if (event.which == 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
// This var should have a value of the type "name[:array, :object]"
|
|
|
|
value = field.val();
|
|
|
|
|
|
|
|
if (value == "") {
|
|
|
|
newItem.remove();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
elements = value.split(":")
|
|
|
|
|
|
|
|
if (elements.length > 2) {
|
|
|
|
notification({
|
|
|
|
text: "Invalid syntax. It must be 'name[:type]'.",
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elements.length == 2 && elements[1] != "array" && elements[1] != "object") {
|
|
|
|
notification({
|
|
|
|
text: "Only arrays and objects are allowed.",
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
field.remove();
|
|
|
|
|
|
|
|
if (typeof blockID === "undefined") {
|
|
|
|
blockID = elements[0];
|
|
|
|
} else {
|
|
|
|
blockID = blockID + '[' + elements[0] + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elements.length == 1) {
|
|
|
|
newItem.attr('id', 'block-' + blockID);
|
|
|
|
newItem.append('<input name="' + blockID + ':auto" id="' + blockID + '"></input><br>');
|
|
|
|
newItem.prepend('<label for="' + blockID + '">' + value + '</label> <span class="actions"><button class="delete">−</button></span>');
|
|
|
|
} else {
|
|
|
|
type = "";
|
|
|
|
|
|
|
|
if (elements[1] == "array") {
|
|
|
|
type = "array";
|
|
|
|
} else {
|
|
|
|
type = "object"
|
|
|
|
}
|
|
|
|
|
|
|
|
template = "<fieldset id=\"${blockID}\" data-type=\"${type}\"> <h3>${elements[0]}</h3> <span class=\"actions\"> <button class=\"add\">+</button> <button class=\"delete\">−</button> </span> </fieldset>"
|
|
|
|
template = template.replace("${blockID}", blockID);
|
|
|
|
template = template.replace("${elements[0]}", elements[0]);
|
|
|
|
template = template.replace("${type}", type);
|
|
|
|
newItem.after(template);
|
|
|
|
newItem.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-08 08:32:03 +00:00
|
|
|
$("#content").on('click', '.delete', function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
|
|
|
button = $(this);
|
|
|
|
|
|
|
|
name = button.parent().parent().attr("for") || button.parent().parent().attr("id") || button.parent().parent().parent().attr("id");
|
|
|
|
name = name.replace(/\[/, '\\[');
|
|
|
|
name = name.replace(/\]/, '\\]');
|
|
|
|
|
|
|
|
$('label[for="' + name + '"]').fadeOut().remove();
|
|
|
|
$('#' + name).fadeOut().remove();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|