2015-09-19 13:25:35 +00:00
|
|
|
$(document).ready(function() {
|
2016-02-03 22:33:03 +00:00
|
|
|
// Start pjax
|
2015-09-20 10:03:17 +00:00
|
|
|
$(document).pjax('a[data-pjax]', '#content');
|
2015-09-19 13:25:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('ready pjax:success', function() {
|
2016-02-01 22:30:29 +00:00
|
|
|
// Auto Grow Textarea
|
|
|
|
function autoGrow(element) {
|
|
|
|
this.style.height = "5px";
|
|
|
|
this.style.height = (this.scrollHeight) + "px";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto Grow textareas after loading
|
|
|
|
$("textarea").each(autoGrow);
|
|
|
|
// Auto Grow textareas when changing its content
|
|
|
|
$('textarea').keyup(autoGrow);
|
|
|
|
// Auto Grow textareas when resizing the window
|
|
|
|
$(window).resize(function() {
|
|
|
|
$("textarea").each(autoGrow);
|
2015-10-24 09:54:10 +00:00
|
|
|
});
|
2015-09-19 13:25:35 +00:00
|
|
|
|
|
|
|
// Log out the user sending bad credentials to the server
|
2015-09-19 18:26:51 +00:00
|
|
|
$("#logout").click(function(event) {
|
|
|
|
event.preventDefault();
|
2015-09-19 13:25:35 +00:00
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: "/admin",
|
|
|
|
async: false,
|
|
|
|
username: "username",
|
|
|
|
password: "password",
|
|
|
|
headers: {
|
|
|
|
"Authorization": "Basic xxx"
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
window.location = "/";
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2015-09-20 10:29:31 +00:00
|
|
|
// Delete a file or a field in editor
|
2015-09-20 12:04:33 +00:00
|
|
|
$("body").on('click', '.delete', function(event) {
|
2015-09-20 10:29:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
button = $(this);
|
|
|
|
|
|
|
|
if (button.data("file") && confirm("Are you sure you want to delete this?")) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
url: button.data("file")
|
|
|
|
}).done(function(data) {
|
|
|
|
button.parent().parent().fadeOut();
|
|
|
|
notification({
|
|
|
|
text: button.data("message"),
|
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
} else {
|
2015-09-20 12:28:47 +00:00
|
|
|
name = button.parent().parent().attr("for") || button.parent().parent().attr("id") || button.parent().parent().parent().attr("id");
|
2015-09-20 12:04:33 +00:00
|
|
|
name = name.replace(/\[/, '\\[');
|
|
|
|
name = name.replace(/\]/, '\\]');
|
2015-09-20 10:29:31 +00:00
|
|
|
console.log(name)
|
|
|
|
|
|
|
|
$('label[for="' + name + '"]').fadeOut().remove();
|
2015-09-20 12:04:33 +00:00
|
|
|
$('#' + name).fadeOut().remove();
|
2015-09-20 10:29:31 +00:00
|
|
|
}
|
2015-09-20 12:04:33 +00:00
|
|
|
|
|
|
|
return false;
|
2015-09-20 10:29:31 +00:00
|
|
|
});
|
|
|
|
|
2015-09-20 13:43:41 +00:00
|
|
|
if ($('main').hasClass('browse')) {
|
|
|
|
$('.new').click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if ($(this).data("opened")) {
|
|
|
|
$('#new-file').fadeOut(200);
|
|
|
|
$(this).data("opened", false);
|
|
|
|
} else {
|
|
|
|
$('#new-file').fadeIn(200);
|
|
|
|
$(this).data("opened", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#new-file').on('keypress', 'input', function(event) {
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
var value = $(this).val(),
|
|
|
|
splited = value.split(":"),
|
|
|
|
filename = "",
|
2015-09-20 19:42:22 +00:00
|
|
|
archetype = "";
|
2015-09-20 13:43:41 +00:00
|
|
|
|
|
|
|
if (value == "") {
|
|
|
|
notification({
|
|
|
|
text: "You have to write something. If you want to close the box, click the button again.",
|
|
|
|
type: 'warning',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else if (splited.length == 1) {
|
|
|
|
filename = value;
|
|
|
|
} else if (splited.length == 2) {
|
|
|
|
filename = splited[0];
|
2015-09-20 19:42:22 +00:00
|
|
|
archetype = splited[1];
|
2015-09-20 13:43:41 +00:00
|
|
|
} else {
|
|
|
|
notification({
|
2015-09-20 19:42:22 +00:00
|
|
|
text: "Hmm... I don't understand you. Try writing something like 'name[:archetype]'.",
|
2015-09-20 13:43:41 +00:00
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-20 19:42:22 +00:00
|
|
|
var content = '{"filename": "' + filename + '", "archetype": "' + archetype + '"}';
|
2015-09-20 13:43:41 +00:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: window.location.pathname,
|
|
|
|
data: content,
|
|
|
|
dataType: 'json',
|
|
|
|
encode: true,
|
|
|
|
}).done(function(data) {
|
|
|
|
notification({
|
2015-09-25 20:06:29 +00:00
|
|
|
text: "File created successfully.",
|
2015-09-20 13:43:41 +00:00
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
2015-09-25 20:06:29 +00:00
|
|
|
|
|
|
|
$.pjax({
|
2015-09-26 21:12:56 +00:00
|
|
|
url: window.location.pathname.replace("browse", "edit") + filename,
|
2015-09-25 20:06:29 +00:00
|
|
|
container: '#content'
|
|
|
|
})
|
2015-09-20 13:43:41 +00:00
|
|
|
}).fail(function(data) {
|
|
|
|
// error types
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2015-09-26 11:34:53 +00:00
|
|
|
|
|
|
|
$("#upload").click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
$('.actions input[type="file"]').click();
|
2015-09-27 20:04:59 +00:00
|
|
|
return false;
|
2015-09-26 11:34:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$('input[type="file"]').on('change', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
files = event.target.files;
|
|
|
|
|
|
|
|
// Create a formdata object and add the files
|
|
|
|
var data = new FormData();
|
|
|
|
$.each(files, function(key, value) {
|
|
|
|
data.append(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: window.location.pathname,
|
|
|
|
type: 'POST',
|
|
|
|
data: data,
|
|
|
|
cache: false,
|
|
|
|
dataType: 'json',
|
|
|
|
headers: {
|
|
|
|
'X-Upload': 'true',
|
|
|
|
},
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
}).done(function(data) {
|
|
|
|
notification({
|
|
|
|
text: "File(s) uploaded successfully.",
|
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
|
|
|
|
$.pjax({
|
|
|
|
url: window.location.pathname,
|
|
|
|
container: '#content'
|
|
|
|
})
|
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
2015-09-27 20:04:59 +00:00
|
|
|
return false;
|
2015-09-26 11:34:53 +00:00
|
|
|
});
|
2015-09-20 13:43:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 13:25:35 +00:00
|
|
|
// If it's editor page
|
|
|
|
if ($(".editor")[0]) {
|
2016-02-03 22:33:03 +00:00
|
|
|
var mode = $("#source-area").data('mode');
|
2016-01-31 22:14:17 +00:00
|
|
|
var editor = ace.edit("source-area");
|
2016-02-03 22:33:03 +00:00
|
|
|
editor.getSession().setMode("ace/mode/" + mode);
|
2016-01-31 22:14:17 +00:00
|
|
|
editor.setOptions({
|
|
|
|
wrap: true,
|
|
|
|
maxLines: Infinity,
|
|
|
|
theme: "ace/theme/github",
|
|
|
|
showPrintMargin: false,
|
|
|
|
fontSize: "1em"
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-09-19 13:25:35 +00:00
|
|
|
preview = $("#preview-area");
|
|
|
|
textarea = $("#content-area");
|
|
|
|
|
2015-09-20 12:04:33 +00:00
|
|
|
$('body').on('keypress', 'input', function(event) {
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
$('input[value="Save"]').focus().click();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-03 22:33:03 +00:00
|
|
|
//TODO: reform this
|
2015-09-19 13:25:35 +00:00
|
|
|
// Submites any form in the page in JSON format
|
|
|
|
$('form').submit(function(event) {
|
|
|
|
event.preventDefault();
|
2015-09-20 20:03:18 +00:00
|
|
|
|
|
|
|
// Reset preview area and button to make sure it will
|
|
|
|
// not serialize any form inside the preview
|
|
|
|
$('#preview-area').html('').fadeOut();
|
|
|
|
$('#preview').data("previewing", "false");
|
|
|
|
$('.CodeMirror').fadeIn();
|
|
|
|
|
2015-09-29 20:54:44 +00:00
|
|
|
// Save editor values
|
2015-09-30 19:47:56 +00:00
|
|
|
if (typeof editor !== 'undefined' && editor) {
|
2015-09-29 20:54:44 +00:00
|
|
|
editor.save();
|
|
|
|
}
|
|
|
|
|
2015-09-19 13:25:35 +00:00
|
|
|
var data = JSON.stringify($(this).serializeJSON()),
|
|
|
|
button = $(this).find("input[type=submit]:focus");
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: window.location,
|
|
|
|
data: data,
|
|
|
|
headers: {
|
|
|
|
'X-Regenerate': button.data("regenerate"),
|
2015-09-25 20:06:29 +00:00
|
|
|
'X-Schedule': button.data("schedule"),
|
2015-09-19 13:25:35 +00:00
|
|
|
'X-Content-Type': button.data("type")
|
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
encode: true,
|
2015-09-29 21:12:01 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
2015-09-19 13:25:35 +00:00
|
|
|
}).done(function(data) {
|
|
|
|
notification({
|
|
|
|
text: button.data("message"),
|
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
2015-09-19 18:26:51 +00:00
|
|
|
|
2015-09-20 12:04:33 +00:00
|
|
|
return false;
|
2015-09-19 13:25:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Adds one more field to the current group
|
2015-09-20 12:04:33 +00:00
|
|
|
$("body").on('click', '.add', function(event) {
|
2015-09-19 18:26:51 +00:00
|
|
|
event.preventDefault();
|
2016-02-04 22:30:46 +00:00
|
|
|
defaultID = "lorem-ipsum-sin-dolor-amet";
|
2015-09-19 18:26:51 +00:00
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
if ($("#" + defaultID).length) {
|
2015-09-19 18:26:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
block = $(this).parent().parent();
|
|
|
|
blockType = block.data("type");
|
|
|
|
blockID = block.attr("id");
|
|
|
|
|
|
|
|
// Main add button, after all blocks
|
|
|
|
if (block.is('div') && block.hasClass("frontmatter")) {
|
|
|
|
block = $('.blocks');
|
|
|
|
block.append('<div class="block" id="' + defaultID + '"></div>');
|
|
|
|
blockType = "object";
|
2015-09-19 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
// If the Block Type is an array
|
|
|
|
if (blockType == "array") {
|
|
|
|
newID = blockID + "[]";
|
|
|
|
input = blockID;
|
2016-01-31 22:14:17 +00:00
|
|
|
input = input.replace(/\[/, '\\[');
|
|
|
|
input = input.replace(/\]/, '\\]');
|
2016-02-04 22:30:46 +00:00
|
|
|
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-01-31 22:14:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
// If the Block is an object
|
|
|
|
if (blockType == "object") {
|
|
|
|
newItem = $("#" + defaultID);
|
|
|
|
newItem.html('<input id="name-' + defaultID + '" placeholder="Write the field name and press enter..."></input>');
|
|
|
|
field = $("#name-" + defaultID);
|
2015-09-19 18:26:51 +00:00
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
// Show a notification with some information for newbies
|
2015-09-26 10:42:00 +00:00
|
|
|
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=/';
|
2015-09-20 12:04:33 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
$(field).keypress(function(event) {
|
|
|
|
// When you press enter within the new name field:
|
2015-09-19 18:26:51 +00:00
|
|
|
if (event.which == 13) {
|
|
|
|
event.preventDefault();
|
2016-02-04 22:30:46 +00:00
|
|
|
// This var should have a value of the type "name[:array, :object]"
|
|
|
|
value = field.val();
|
2015-09-19 18:26:51 +00:00
|
|
|
|
|
|
|
if (value == "") {
|
2016-01-31 22:24:04 +00:00
|
|
|
newItem.remove();
|
2015-09-20 12:04:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
elements = value.split(":")
|
|
|
|
|
|
|
|
if (elements.length > 2) {
|
|
|
|
notification({
|
|
|
|
text: "Invalid syntax. It must be 'name[:type]'.",
|
|
|
|
type: 'error'
|
|
|
|
});
|
2015-09-19 18:26:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
if (elements.length == 2 && elements[1] != "array" && elements[1] != "object") {
|
|
|
|
notification({
|
|
|
|
text: "Only arrays and objects are allowed.",
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
field.remove();
|
|
|
|
|
|
|
|
// TODO: continue here. :) 04/02/2016 10:30pm
|
2015-09-20 12:04:33 +00:00
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
if (typeof blockID === "undefined") {
|
|
|
|
blockID = elements[0];
|
2015-09-19 18:26:51 +00:00
|
|
|
} else {
|
2016-02-04 22:30:46 +00:00
|
|
|
blockID = blockID + '[' + elements[0] + ']';
|
2015-09-19 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 12:04:33 +00:00
|
|
|
if (elements.length == 1) {
|
2016-02-04 22:30:46 +00:00
|
|
|
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>');
|
2015-09-20 12:04:33 +00:00
|
|
|
} else {
|
2016-02-04 22:30:46 +00:00
|
|
|
type = "";
|
2015-09-20 12:04:33 +00:00
|
|
|
|
|
|
|
if (elements[1] == "array") {
|
2016-02-04 22:30:46 +00:00
|
|
|
type = "array";
|
2015-09-20 12:04:33 +00:00
|
|
|
} else {
|
2016-02-04 22:30:46 +00:00
|
|
|
type = "object"
|
2015-09-20 12:04:33 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 22:30:46 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
console.log('"' + blockID + '" block of type "' + type + '" added.');
|
2015-09-20 12:04:33 +00:00
|
|
|
}
|
2015-09-19 18:26:51 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-19 13:25:35 +00:00
|
|
|
return false;
|
|
|
|
});
|
2015-09-26 10:42:00 +00:00
|
|
|
|
|
|
|
// If it has a textarea
|
|
|
|
if (textarea[0]) {
|
|
|
|
options = {
|
|
|
|
mode: textarea.data("mode"),
|
2015-09-28 20:11:52 +00:00
|
|
|
theme: 'ttcn',
|
2015-09-26 10:42:00 +00:00
|
|
|
lineWrapping: true,
|
|
|
|
lineNumbers: true,
|
|
|
|
scrollbarStyle: null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textarea.data("mode") == "markdown") {
|
|
|
|
options.lineNumbers = false
|
|
|
|
}
|
|
|
|
|
|
|
|
editor = CodeMirror.fromTextArea(textarea[0], options);
|
|
|
|
codemirror = $('.CodeMirror');
|
|
|
|
|
|
|
|
// Toggles between preview and editing mode
|
|
|
|
$("#preview").click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// If it currently in the preview mode, hide the preview
|
|
|
|
// and show the editor
|
|
|
|
if ($(this).data("previewing") == "true") {
|
|
|
|
preview.hide();
|
|
|
|
codemirror.fadeIn();
|
|
|
|
$(this).data("previewing", "false");
|
|
|
|
notification({
|
|
|
|
text: "Think, relax and do the better you can!",
|
|
|
|
type: 'information',
|
|
|
|
timeout: 2000
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Copy the editor content to texteare
|
|
|
|
editor.save()
|
|
|
|
|
|
|
|
// If it's in editing mode, convert the markdown to html
|
|
|
|
// and show it
|
|
|
|
var converter = new showdown.Converter(),
|
|
|
|
text = textarea.val(),
|
|
|
|
html = converter.makeHtml(text);
|
|
|
|
|
|
|
|
// Hide the editor and show the preview
|
|
|
|
codemirror.hide();
|
|
|
|
preview.html(html).fadeIn();
|
|
|
|
|
|
|
|
$(this).data("previewing", "true");
|
|
|
|
notification({
|
|
|
|
text: "This is how your post looks like.",
|
|
|
|
type: 'information',
|
|
|
|
timeout: 2000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2015-09-19 13:25:35 +00:00
|
|
|
}
|
2016-01-31 22:14:17 +00:00
|
|
|
});
|