filebrowser/assets/src/js/app.js

203 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-09-19 13:25:35 +00:00
$(document).ready(function() {
$(document).pjax('a', '#main');
});
$(document).on('ready pjax:success', function() {
// Starts the perfect scroolbar plugin
$('.scroll').perfectScrollbar();
// 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;
});
// If it's editor page
if ($(".editor")[0]) {
editor = false;
preview = $("#preview-area");
textarea = $("#content-area");
2015-09-19 18:26:51 +00:00
// If it has a textarea
2015-09-19 13:25:35 +00:00
if (textarea[0]) {
options = {
2015-09-19 13:47:59 +00:00
mode: textarea.data("mode"),
2015-09-19 13:25:35 +00:00
theme: 'mdn-like',
lineWrapping: true,
lineNumbers: true,
scrollbarStyle: null
}
2015-09-19 13:47:59 +00:00
if (textarea.data("mode") == "markdown") {
2015-09-19 13:25:35 +00:00
options.lineNumbers = false
}
editor = CodeMirror.fromTextArea(textarea[0], options);
2015-09-19 18:26:51 +00:00
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
}
// Submites any form in the page in JSON format
$('form').submit(function(event) {
event.preventDefault();
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"),
'X-Content-Type': button.data("type")
},
dataType: 'json',
encode: true,
}).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-19 13:25:35 +00:00
});
// Adds one more field to the current group
2015-09-19 18:26:51 +00:00
$(".add").click(function(event) {
event.preventDefault();
if ($("#new").length) {
return false;
}
title = $(this).parent().parent();
fieldset = title.parent();
type = fieldset.data("type");
name = fieldset.data("name");
if (title.is('h1')) {
fieldset = $('.sidebar .content');
fieldset.prepend('<div id="ghost"></div>');
title = $('#ghost');
type = "object";
}
if (type == "object") {
title.after('<input id="new" placeholder="Write the field name and press enter..."></input>');
element = $("#new");
$(element).keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
value = element.val();
element.remove();
if (value == "") {
return false;
}
if (name == "undefined") {
name = value
} else {
name = name + '[' + value + ']';
}
title.after('<input name="' + name + ':auto" id="' + name + '"></input><br>');
title.after('<label for="' + name + '">' + value + ' <span class="actions"><button class="delete"><i class="fa fa-minus"></i></button></span></label>');
return false;
}
});
}
if (type == "array") {
name = name + "[]";
title.after('<input name="' + name + ':auto" id="' + name + '"></input><br>');
}
2015-09-19 13:25:35 +00:00
return false;
});
2015-09-19 18:53:01 +00:00
$(".delete").click(function(event) {
event.preventDefault();
2015-09-19 18:54:52 +00:00
name = $(this).parent().parent().attr("for") || $(this).parent().parent().parent().attr("id");
2015-09-19 18:53:01 +00:00
console.log(name)
2015-09-19 18:54:52 +00:00
$('#' + name).fadeOut().remove();
$('label[for="' + name + '"]').fadeOut().remove();
2015-09-19 18:53:01 +00:00
});
2015-09-19 18:26:51 +00:00
$('body').on('keypress', 'input', function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$('input[value="Save"]').focus().click();
return false;
}
});
2015-09-19 13:25:35 +00:00
}
});
$(document).on('pjax:send', function() {
2015-09-19 18:26:51 +00:00
$('#loading').fadeIn();
});
2015-09-19 13:25:35 +00:00
$(document).on('pjax:complete', function() {
2015-09-19 18:26:51 +00:00
$('#loading').fadeOut();
});