structure changes

This commit is contained in:
Henrique Dias
2016-03-06 20:37:49 +00:00
parent 3f36200862
commit 1375038a66
43 changed files with 405 additions and 392 deletions

View File

@@ -0,0 +1,53 @@
$(document).ready(function() {
// Log out the user sending bad credentials to the server
$("#logout").click(function(event) {
event.preventDefault();
$.ajax({
type: "GET",
url: "/admin",
async: false,
username: "username",
password: "password",
headers: {
"Authorization": "Basic xxx"
}
}).fail(function() {
window.location = "/";
});
return false;
});
$(document).pjax('a[data-pjax]', '#content');
});
$(document).on('ready pjax:end', function() {
$('#content').off();
// Update the title
document.title = document.getElementById('site-title').innerHTML;
//TODO: navbar titles changing effect when changing page
// Auto Grow Textarea
function autoGrow() {
this.style.height = '5px';
this.style.height = this.scrollHeight + 'px';
}
$("textarea").each(autoGrow);
$('textarea').keyup(autoGrow);
$(window).resize(function() {
$("textarea").each(autoGrow);
});
if ($('main').hasClass('browse')) {
$(document).trigger("page:browse");
}
if ($(".editor")[0]) {
$(document).trigger("page:editor");
}
return false;
});

View File

@@ -0,0 +1,358 @@
$(document).on('page:browse', function() {
var foreground = '#foreground';
/* DELETE FILE */
var remove = new Object();
remove.selector = 'form#delete';
remove.form = $(remove.selector);
remove.row = '';
remove.button = '';
remove.url = '';
$('#content').on('click', '.delete', function(event) {
event.preventDefault();
remove.button = $(this);
remove.row = $(this).parent().parent();
$(foreground).fadeIn(200);
remove.url = remove.row.find('.filename').text();
remove.form.find('span').text(remove.url);
remove.form.fadeIn(200);
return false;
});
$('#content').on('submit', remove.selector, function(event) {
event.preventDefault();
var request = new XMLHttpRequest();
request.open("DELETE", remove.button.data("file"));
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
var response = JSON.parse(request.responseText),
type = "success",
timeout = 5000;
$(foreground).fadeOut(200);
remove.form.fadeOut(200);
remove.row.fadeOut(200);
if (request.status != 200) {
type = "error";
timeout = false;
}
notification({
text: response.message,
type: type,
timeout: timeout
});
}
}
return false;
});
/* FILE UPLOAD */
$('#content').on('change', 'input[type="file"]', 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);
});
return false;
});
$('#content').on('click', '#upload', function(event) {
event.preventDefault();
$('.actions input[type="file"]').click();
return false;
});
/* NEW FILE */
var create = new Object();
create.selector = 'form#new';
create.form = $(create.selector);
create.input = create.selector + ' input[type="text"]';
create.button = '';
create.url = '';
$('#content').on('click', '.new', function(event) {
event.preventDefault();
create.button = $(this);
$(foreground).fadeIn(200);
create.form.fadeIn(200);
return false;
});
$('#content').on('keypress', create.input, function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$(create.form).submit();
return false;
}
});
$('#content').on('submit', create.selector, function(event) {
event.preventDefault();
var value = create.form.find('input[type="text"]').val(),
splited = value.split(":"),
filename = "",
archetype = "";
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];
archetype = splited[1];
} else {
notification({
text: "Hmm... I don't understand you. Try writing something like 'name[:archetype]'.",
type: 'error'
});
return false;
}
var content = {
filename: filename,
archetype: archetype
}
var request = new XMLHttpRequest();
request.open("POST", window.location.pathname);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify(content));
request.onreadystatechange = function() {
if (request.readyState == 4) {
var response = JSON.parse(request.responseText);
var type = "success";
var timeout = 5000;
if (request.status != 200) {
type = "error";
timeout = false;
}
notification({
text: response.message,
type: type,
timeout: timeout
});
if (request.status == 200) {
$.pjax({
url: data.Location,
container: '#content'
})
}
}
}
return false;
});
/* RENAME FILE */
var rename = new Object();
rename.selector = 'form#rename';
rename.form = $(rename.selector);
rename.input = rename.selector + ' input[type="text"]';
rename.button = '';
rename.url = '';
$('#content').on('click', '.rename', function(event) {
event.preventDefault();
rename.button = $(this);
$(foreground).fadeIn(200);
rename.url = $(this).parent().parent().find('.filename').text();
rename.form.fadeIn(200);
rename.form.find('span').text(rename.url);
rename.form.find('input[type="text"]').val(rename.url);
return false;
});
$('#content').on('keypress', rename.input, function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$(rename.form).submit();
return false;
}
});
$('#content').on('submit', rename.selector, function(event) {
event.preventDefault();
var filename = rename.form.find('input[type="text"]').val();
if (filename === "") {
return false;
}
if (filename.substring(0, 1) != "/") {
filename = window.location.pathname.replace("/admin/browse/", "") + '/' + filename;
}
var content = {
filename: filename
};
var request = new XMLHttpRequest();
request.open("PUT", rename.url);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify(content));
request.onreadystatechange = function() {
if (request.readyState == 4) {
var response = JSON.parse(request.responseText),
type = "success",
timeout = 5000;
if (request.status != 200) {
type = "error";
timeout = false;
}
$.pjax({
url: window.location.pathname,
container: '#content'
});
notification({
text: response.message,
type: type,
timeout: timeout
});
}
}
return false;
});
/* GIT ACTIONS */
var git = new Object();
git.selector = 'form#git';
git.form = $(git.selector);
git.input = git.selector + ' input[type="text"]';
$('#content').on('click', 'button.git', function(event) {
event.preventDefault();
$(foreground).fadeIn(200);
git.form.fadeIn(200);
return false;
});
$('#content').on('keypress', git.input, function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$(git.form).submit();
return false;
}
});
$('#content').on('submit', git.selector, function(event) {
event.preventDefault();
var value = git.form.find('input[type="text"]').val();
if (value == "") {
notification({
text: "You have to write something. If you want to close the box, click outside of the box.",
type: 'warning',
timeout: 5000
});
return false;
}
var request = new XMLHttpRequest();
request.open("POST", "/admin/git");
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify({
command: value
}));
request.onreadystatechange = function() {
if (request.readyState == 4) {
var data = JSON.parse(request.responseText);
if (request.status == 200) {
notification({
text: data.message,
type: "success"
});
} else {
notification({
text: data.message,
type: "error"
});
}
}
}
return false;
});
/* $(foreground) AND STUFF */
$('#content').on('click', '.close', function(event) {
event.preventDefault();
$(this).parent().parent().fadeOut(200);
$(foreground).click();
return false;
});
$('#content').on('click', foreground, function(event) {
event.preventDefault();
$(foreground).fadeOut(200);
create.form.fadeOut(200);
rename.form.fadeOut(200);
remove.form.fadeOut(200);
git.form.fadeOut(200);
return false;
});
});

View File

@@ -0,0 +1,257 @@
$(document).on('page:editor', function() {
var container = $('.editor');
var preview = $('#editor-preview');
var editor = $('#editor-source');
if (container.hasClass('complete')) {
// Change title field when editing the header
$('#content').on('keyup', '#site-title', function() {
$('.frontmatter #title').val($(this).val());
});
}
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,
fontSize: "1em",
minLines: 20
});
$('#content').on('click', '#see-source', function(event) {
event.preventDefault();
preview.hide();
editor.fadeIn();
$(this).addClass('active');
$("#see-preview").removeClass('active');
$("#see-preview").data("previewing", "false");
})
// Toggles between preview and editing mode
$('#content').on('click', '#see-preview', 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();
editor.fadeIn();
$(this).removeClass('active');
$("#see-source").addClass('active');
$(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);
// Hide the editor and show the preview
editor.hide();
preview.html(html).fadeIn();
$(this).addClass('active');
$("#see-source").removeClass('active');
$(this).data("previewing", "true");
}
return false;
});
}
$('#content').on('keypress', 'input', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$('input[value="Save"]').focus().click();
return false;
}
});
// Submites any form in the page in JSON format
$('#content').on('submit', 'form', function(event) {
event.preventDefault();
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();
}
var data = JSON.stringify($(this).serializeJSON()),
button = $(this).find("input[type=submit]:focus");
var request = new XMLHttpRequest();
request.open("POST", window.location);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.setRequestHeader("X-Regenerate", button.data("regenerate"));
request.setRequestHeader("X-Schedule", button.data("schedule"));
request.setRequestHeader("X-Content-Type", button.data("type"));
request.send(data);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
notification({
text: button.data("message"),
type: 'success',
timeout: 5000
});
} else {
notification({
text: 'Something went wrong.',
type: 'error'
});
console.log(request.responseText);
}
}
}
return false;
});
// Adds one more field to the current group
$("#content").on('click', '.add', function(event) {
event.preventDefault();
defaultID = "lorem-ipsum-sin-dolor-amet";
// Remove if there is an incomplete new item
newItem = $("#" + defaultID);
if (newItem.length) {
newItem.remove();
}
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">&#8722;</button></span></div></div>');
console.log('New array item added.');
}
// Main add button, after all blocks
if (block.is('div') && block.hasClass("frontmatter")) {
block = $('.blocks');
blockType = "object";
}
// If the Block is an object
if (blockType == "object") {
block.append('<div class="block" id="' + defaultID + '"></div>');
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">&#8722;</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\">&#43;</button> <button class=\"delete\">&#8722;</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.');
}
return false;
}
});
}
return false;
});
$("#content").on('click', '.delete', function(event) {
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(/\]/, '\\]');
console.log(name)
$('label[for="' + name + '"]').fadeOut().remove();
$('#' + name).fadeOut().remove();
return false;
});
});

View File

@@ -0,0 +1,82 @@
$.noty.themes.admin = {
name: 'admin',
helpers: {},
modal: {
css: {
position: 'fixed',
width: '100%',
height: '100%',
backgroundColor: '#000',
zIndex: 10000,
opacity: 0.6,
display: 'none',
left: 0,
top: 0
}
}
};
$.noty.defaults = {
layout: 'topRight',
theme: 'admin',
dismissQueue: true,
animation: {
open: 'animated bounceInRight',
close: 'animated fadeOut',
easing: 'swing',
speed: 500 // opening & closing animation speed
},
timeout: false, // delay for closing event. Set false for sticky notifications
force: false, // adds notification to the beginning of queue when set to true
modal: false,
maxVisible: 5, // you can set max visible notification for dismissQueue true option,
killer: false, // for close all notifications before show
closeWith: ['click'], // ['click', 'button', 'hover', 'backdrop'] // backdrop click will close all notifications
callback: {
onShow: function() {},
afterShow: function() {},
onClose: function() {},
afterClose: function() {},
onCloseClick: function() {},
},
buttons: false // an array of buttons
};
notification = function(options) {
var icon;
switch (options.type) {
case "success":
icon = '<i class="fa fa-check"></i>';
break;
case "error":
icon = '<i class="fa fa-times"></i>';
break;
case "warning":
icon = '<i class="fa fa-exclamation"></i>';
break;
case "information":
icon = '<i class="fa fa-info"></i>';
break;
default:
icon = '<i class="fa fa-bell"></i>';
}
var defaults = {
template: '<div class="noty_message"><span class="noty_icon">' + icon + '</span><span class="noty_text"></span></div>'
}
options = $.extend({}, defaults, options);
noty(options);
if (!document.cookie.replace(/(?:(?:^|.*;\s*)stickynoties\s*\=\s*([^;]*).*$)|^.*$/, "$1") && !options.timeout) {
var date = new Date();
date.setDate(date.getDate() + 365);
document.cookie = 'stickynoties=true; expires=' + date.toUTCString + '; path=/';
notification({
text: "Some notifications are sticky. If it doesn't go away, click to dismiss it.",
type: 'information'
});
}
}