2016-02-06 16:08:48 +00:00
|
|
|
$(document).on('page:browse', function() {
|
2016-02-07 19:37:01 +00:00
|
|
|
var foreground = $('.foreground');
|
2016-02-06 17:18:30 +00:00
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
/* FILE UPLOAD */
|
2016-02-06 17:18:30 +00:00
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
$('input[type="file"]').on('change', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
files = event.target.files;
|
2016-02-06 17:18:30 +00:00
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
// Create a formdata object and add the files
|
|
|
|
var data = new FormData();
|
|
|
|
$.each(files, function(key, value) {
|
|
|
|
data.append(key, value);
|
|
|
|
});
|
2016-02-06 17:18:30 +00:00
|
|
|
|
|
|
|
$.ajax({
|
2016-02-07 19:37:01 +00:00
|
|
|
url: window.location.pathname,
|
|
|
|
type: 'POST',
|
|
|
|
data: data,
|
|
|
|
cache: false,
|
2016-02-06 17:18:30 +00:00
|
|
|
dataType: 'json',
|
2016-02-07 19:37:01 +00:00
|
|
|
headers: {
|
|
|
|
'X-Upload': 'true',
|
|
|
|
},
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
2016-02-06 17:18:30 +00:00
|
|
|
}).done(function(data) {
|
|
|
|
notification({
|
2016-02-07 19:37:01 +00:00
|
|
|
text: "File(s) uploaded successfully.",
|
2016-02-06 17:18:30 +00:00
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
2016-02-07 19:37:01 +00:00
|
|
|
|
|
|
|
$.pjax({
|
|
|
|
url: window.location.pathname,
|
|
|
|
container: '#content'
|
|
|
|
})
|
2016-02-06 17:18:30 +00:00
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
$("#upload").click(function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
2016-02-07 19:37:01 +00:00
|
|
|
$('.actions input[type="file"]').click();
|
2016-02-06 16:08:48 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
/* NEW FILE */
|
|
|
|
|
|
|
|
var create = new Object();
|
|
|
|
create.form = $('form#new');
|
|
|
|
create.button = '';
|
|
|
|
create.url = '';
|
|
|
|
|
2016-02-06 17:18:30 +00:00
|
|
|
$('.new').off('click').click(function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
2016-02-07 19:37:01 +00:00
|
|
|
create.button = $(this);
|
2016-02-06 16:08:48 +00:00
|
|
|
|
|
|
|
if ($(this).data("opened")) {
|
2016-02-07 19:37:01 +00:00
|
|
|
foreground.fadeOut(200);
|
|
|
|
create.form.fadeOut(200);
|
|
|
|
create.button.data("opened", false);
|
2016-02-06 16:08:48 +00:00
|
|
|
} else {
|
2016-02-07 19:37:01 +00:00
|
|
|
foreground.fadeIn(200);
|
|
|
|
create.form.fadeIn(200);
|
|
|
|
create.button.data("opened", true);
|
2016-02-06 16:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
create.form.find('input[type="text"]').off('keypress').keypress(function(event) {
|
2016-02-07 17:49:20 +00:00
|
|
|
if (event.keyCode == 13) {
|
|
|
|
event.preventDefault();
|
2016-02-07 19:37:01 +00:00
|
|
|
$(create.form).submit();
|
2016-02-07 17:49:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
create.form.submit(function(event) {
|
2016-02-07 17:16:09 +00:00
|
|
|
event.preventDefault();
|
2016-02-07 19:37:01 +00:00
|
|
|
|
|
|
|
var value = create.form.find('input[type="text"]').val(),
|
2016-02-07 17:16:09 +00:00
|
|
|
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'
|
2016-02-06 16:08:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-07 17:16:09 +00:00
|
|
|
|
|
|
|
var content = '{"filename": "' + filename + '", "archetype": "' + archetype + '"}';
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: window.location.pathname,
|
|
|
|
data: content,
|
|
|
|
dataType: 'json',
|
|
|
|
encode: true,
|
|
|
|
}).done(function(data) {
|
|
|
|
notification({
|
|
|
|
text: "File created successfully.",
|
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
|
|
|
|
$.pjax({
|
|
|
|
url: window.location.pathname.replace("browse", "edit") + filename,
|
|
|
|
container: '#content'
|
|
|
|
})
|
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
2016-02-06 16:08:48 +00:00
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
/* RENAME FILE */
|
|
|
|
|
|
|
|
var rename = new Object();
|
|
|
|
rename.form = $('form#rename');
|
|
|
|
rename.button = '';
|
|
|
|
rename.url = '';
|
|
|
|
|
|
|
|
$('.rename').off('click').click(function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
2016-02-07 19:37:01 +00:00
|
|
|
rename.button = $(this);
|
|
|
|
|
|
|
|
if ($(this).data("opened")) {
|
|
|
|
foreground.fadeOut(200);
|
|
|
|
rename.form.fadeOut(200);
|
|
|
|
rename.button.data("opened", false);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
rename.button.data("opened", true);
|
|
|
|
}
|
|
|
|
|
2016-02-06 16:08:48 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
rename.form.find('input[type="text"]').off('keypress').keypress(function(event) {
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
$(rename.form).submit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rename.form.off('submit').submit(function(event) {
|
2016-02-06 16:08:48 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
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 + '"}';
|
2016-02-06 16:08:48 +00:00
|
|
|
|
|
|
|
$.ajax({
|
2016-02-07 19:37:01 +00:00
|
|
|
type: 'PUT',
|
|
|
|
url: rename.url,
|
|
|
|
data: content,
|
2016-02-06 16:08:48 +00:00
|
|
|
dataType: 'json',
|
2016-02-07 19:37:01 +00:00
|
|
|
encode: true
|
2016-02-06 16:08:48 +00:00
|
|
|
}).done(function(data) {
|
2016-02-07 19:37:01 +00:00
|
|
|
$.pjax({
|
|
|
|
url: window.location.pathname,
|
|
|
|
container: '#content'
|
|
|
|
});
|
2016-02-06 16:08:48 +00:00
|
|
|
notification({
|
2016-02-07 19:37:01 +00:00
|
|
|
text: rename.button.data("message"),
|
2016-02-06 16:08:48 +00:00
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
2016-02-07 19:37:01 +00:00
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
2016-02-06 16:08:48 +00:00
|
|
|
|
2016-02-07 19:37:01 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
$('body').off('click', '.delete').on('click', '.delete', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
button = $(this);
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
url: button.data("file")
|
|
|
|
}).done(function(data) {
|
|
|
|
button.parent().parent().fadeOut();
|
|
|
|
notification({
|
|
|
|
text: button.data("message"),
|
|
|
|
type: 'success',
|
|
|
|
timeout: 5000
|
|
|
|
});
|
2016-02-06 16:08:48 +00:00
|
|
|
}).fail(function(data) {
|
|
|
|
notification({
|
|
|
|
text: 'Something went wrong.',
|
|
|
|
type: 'error'
|
|
|
|
});
|
|
|
|
console.log(data);
|
|
|
|
});
|
2016-02-07 19:37:01 +00:00
|
|
|
|
2016-02-06 16:08:48 +00:00
|
|
|
return false;
|
|
|
|
});
|
2016-02-07 19:37:01 +00:00
|
|
|
|
|
|
|
/* FOREGROUND */
|
|
|
|
|
|
|
|
foreground.off('click').click(function() {
|
|
|
|
foreground.fadeOut(200);
|
|
|
|
create.form.fadeOut(200);
|
|
|
|
rename.form.fadeOut(200);
|
|
|
|
create.button.data("opened", false);
|
|
|
|
rename.button.data("opened", false);
|
|
|
|
});
|
2016-02-06 16:08:48 +00:00
|
|
|
});
|