filebrowser/assets/embed/public/js/application.js

912 lines
29 KiB
JavaScript
Raw Normal View History

2016-06-28 09:01:00 +00:00
'use strict';
2016-06-14 15:13:38 +00:00
2016-08-21 18:08:08 +00:00
var tempID = "_fm_internal_temporary_id"
2016-06-14 15:13:38 +00:00
var selectedItems = [];
2016-07-05 16:46:45 +00:00
var token = "";
2016-06-14 15:13:38 +00:00
2016-06-28 09:01:00 +00:00
/* * * * * * * * * * * * * * * *
* *
* GENERAL FUNCTIONS *
* *
* * * * * * * * * * * * * * * */
// Removes an element, if exists, from an array
Array.prototype.removeElement = function(element) {
var i = this.indexOf(element);
if (i != -1) {
this.splice(i, 1);
}
2016-06-14 19:33:59 +00:00
}
2016-06-28 09:01:00 +00:00
// Replaces an element inside an array by another
Array.prototype.replaceElement = function(begin, end) {
var i = this.indexOf(begin);
if (i != -1) {
this[i] = end;
}
2016-06-21 13:46:59 +00:00
}
2016-06-28 09:01:00 +00:00
// Sends a costum event to itself
Document.prototype.sendCostumEvent = function(text) {
this.dispatchEvent(new CustomEvent(text));
}
2016-06-28 09:01:00 +00:00
// Gets the content of a cookie
Document.prototype.getCookie = function(name) {
var re = new RegExp("(?:(?:^|.*;\\s*)" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$");
return document.cookie.replace(re, "$1");
2016-06-21 13:46:59 +00:00
}
// Changes a button to the loading animation
2016-06-28 09:01:00 +00:00
Element.prototype.changeToLoading = function() {
let element = this;
let originalText = element.innerHTML;
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
element.style.opacity = 0;
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
setTimeout(function() {
element.innerHTML = '<i class="material-icons spin">autorenew</i>';
element.style.opacity = 1;
}, 200);
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
return originalText;
2016-06-21 13:46:59 +00:00
}
// Changes an element to done animation
2016-06-28 09:01:00 +00:00
Element.prototype.changeToDone = function(error, html) {
this.style.opacity = 0;
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
let thirdStep = () => {
this.innerHTML = html;
2016-09-29 20:46:37 +00:00
this.style.opacity = null;
2016-06-21 13:46:59 +00:00
2016-06-30 19:12:43 +00:00
if (selectedItems.length == 0 && document.getElementById('listing')) {
2016-06-28 09:01:00 +00:00
document.sendCostumEvent('changed-selected');
}
}
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
let secondStep = () => {
this.style.opacity = 0;
setTimeout(thirdStep, 200);
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
let firstStep = () => {
if (error) {
this.innerHTML = '<i class="material-icons">close</i>';
} else {
this.innerHTML = '<i class="material-icons">done</i>';
}
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
this.style.opacity = 1;
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
setTimeout(secondStep, 1000);
}
2016-06-21 13:46:59 +00:00
2016-06-28 09:01:00 +00:00
setTimeout(firstStep, 200);
return false;
2016-06-21 13:46:59 +00:00
}
2016-10-18 14:41:04 +00:00
var toWebDavURL = function(url) {
url = url.replace("/", "/webdav/")
return window.location.origin + url
}
2016-06-28 09:01:00 +00:00
// Handles the open file button event
var openEvent = function(event) {
if (this.classList.contains('disabled')) {
return false;
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
if (selectedItems.length) {
window.open(selectedItems[0] + '?raw=true');
return false;
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
window.open(window.location + '?raw=true');
return false;
2016-06-15 07:47:33 +00:00
}
2016-06-14 15:13:38 +00:00
2016-06-28 09:01:00 +00:00
// Handles the delete button event
var deleteEvent = function(event) {
let single = false;
2016-06-14 20:23:54 +00:00
2016-06-28 09:01:00 +00:00
if (!selectedItems.length) {
selectedItems = [window.location];
single = true;
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
if (selectedItems.length) {
Array.from(selectedItems).forEach(link => {
let html = document.getElementById("delete").changeToLoading();
let request = new XMLHttpRequest();
2016-10-18 14:41:04 +00:00
request.open('DELETE', toWebDavURL(link));
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-28 09:01:00 +00:00
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
2016-10-18 14:41:04 +00:00
if (request.status == 204) {
2016-06-28 09:01:00 +00:00
if (single) {
window.location.pathname = RemoveLastDirectoryPartOf(window.location.pathname);
} else {
document.getElementById(link).remove();
selectedItems.removeElement(link);
}
}
2016-10-18 14:41:04 +00:00
document.getElementById('delete').changeToDone((request.status != 204), html);
2016-06-28 09:01:00 +00:00
}
}
});
2016-06-14 20:23:54 +00:00
2016-06-28 09:01:00 +00:00
return false;
}
return false;
2016-06-14 19:33:59 +00:00
}
2016-06-28 09:01:00 +00:00
// Prevent Default event
var preventDefault = function(event) {
event.preventDefault();
2016-06-14 15:13:38 +00:00
}
2016-06-28 09:01:00 +00:00
// Download file event
var downloadEvent = function(event) {
if (this.classList.contains('disabled')) {
return false;
}
if (selectedItems.length) {
Array.from(selectedItems).forEach(item => {
window.open(item + "?download=true");
});
return false;
}
2016-06-28 09:01:00 +00:00
window.open(window.location + "?download=true");
return false;
}
2016-06-14 20:23:54 +00:00
2016-06-28 09:01:00 +00:00
// Remove the last directory of an url
var RemoveLastDirectoryPartOf = function(url) {
var arr = url.split('/');
arr.pop();
return (arr.join('/'));
}
2016-06-14 20:23:54 +00:00
2016-07-05 16:46:45 +00:00
// Get the current token
var updateToken = function() {
token = document.getElementById("token").innerHTML;
}
2016-06-28 09:01:00 +00:00
/* * * * * * * * * * * * * * * *
* *
* LISTING SPECIFIC FUNCTIONS *
* *
* * * * * * * * * * * * * * * */
2016-09-29 20:46:37 +00:00
var reloadListing = function(callback) {
2016-06-30 21:37:52 +00:00
let request = new XMLHttpRequest();
request.open('GET', window.location);
request.setRequestHeader('Minimal', 'true');
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-30 21:37:52 +00:00
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
document.querySelector('body main').innerHTML = request.responseText;
// Handle date times
let timeList = document.getElementsByTagName("time");
Array.from(timeList).forEach(localizeDatetime);
// Add action to checkboxes
let checkboxes = document.getElementsByClassName('checkbox');
Array.from(checkboxes).forEach(link => {
link.addEventListener('click', itemClickEvent);
});
2016-09-29 20:46:37 +00:00
updateToken();
2016-09-29 20:46:37 +00:00
if (typeof callback == 'function') {
callback();
}
2016-06-30 21:37:52 +00:00
}
}
}
}
2016-06-21 13:46:59 +00:00
// Rename file event
2016-06-28 09:01:00 +00:00
var renameEvent = function(event) {
if (this.classList.contains('disabled') || !selectedItems.length) {
return false;
2016-06-22 20:03:48 +00:00
}
2016-06-28 09:01:00 +00:00
// This mustn't happen
if (selectedItems.length > 1) {
alert("Something went wrong. Please refresh the page.");
location.refresh();
2016-06-22 20:03:48 +00:00
}
2016-06-28 09:01:00 +00:00
Array.from(selectedItems).forEach(link => {
let item = document.getElementById(link);
let span = item.getElementsByTagName('span')[0];
let name = span.innerHTML;
item.addEventListener('click', preventDefault);
item.removeEventListener('click', itemClickEvent);
span.setAttribute('contenteditable', 'true');
span.focus();
let keyDownEvent = (event) => {
if (event.keyCode == 13) {
let newName = span.innerHTML;
2016-10-18 14:41:04 +00:00
let newLink = toWebDavURL(link).replace(name, newName)
2016-06-28 09:01:00 +00:00
let html = document.getElementById('rename').changeToLoading();
let request = new XMLHttpRequest();
2016-10-18 14:41:04 +00:00
request.open('MOVE', toWebDavURL(link));
request.setRequestHeader('Destination', newLink);
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-28 09:01:00 +00:00
request.send();
request.onreadystatechange = function() {
2016-10-18 14:41:04 +00:00
// TODO: redirect if it's moved to another folder
2016-06-28 09:01:00 +00:00
if (request.readyState == 4) {
2016-10-18 14:41:04 +00:00
if (request.status != 201 && request.status != 204) {
2016-06-28 09:01:00 +00:00
span.innerHTML = name;
} else {
2016-09-29 20:46:37 +00:00
let newLink = encodeURI(link.replace(name, newName));
2016-10-18 14:41:04 +00:00
console.log(request.body)
2016-09-29 20:46:37 +00:00
reloadListing(() => {
let newLink = encodeURI(link.replace(name, newName));
selectedItems = [newLink];
document.getElementById(newLink).classList.add("selected")
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
});
2016-06-28 09:01:00 +00:00
}
2016-10-18 14:41:04 +00:00
document.getElementById('rename').changeToDone((request.status != 201 && request.status != 204), html);
2016-06-28 09:01:00 +00:00
}
}
}
if (event.KeyCode == 27) {
span.innerHTML = name;
}
if (event.keyCode == 13 || event.keyCode == 27) {
span.setAttribute('contenteditable', 'false');
span.removeEventListener('keydown', keyDownEvent);
item.removeEventListener('click', preventDefault);
item.addEventListener('click', itemClickEvent);
event.preventDefault();
}
return false;
}
span.addEventListener('keydown', keyDownEvent);
span.addEventListener('blur', (event) => {
span.innerHTML = name;
span.setAttribute('contenteditable', 'false');
span.removeEventListener('keydown', keyDownEvent);
item.removeEventListener('click', preventDefault);
});
});
return false;
2016-06-28 09:01:00 +00:00
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
// Upload files
var handleFiles = function(files) {
let button = document.getElementById("upload");
let html = button.changeToLoading();
let data = new FormData();
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
for (let i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
2016-06-22 20:03:48 +00:00
2016-06-28 09:01:00 +00:00
let request = new XMLHttpRequest();
request.open('POST', window.location.pathname);
request.setRequestHeader("Upload", "true");
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-28 09:01:00 +00:00
request.send(data);
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
2016-06-30 21:37:52 +00:00
reloadListing();
2016-06-28 09:01:00 +00:00
}
button.changeToDone((request.status != 200), html);
}
}
return false;
2016-06-14 15:13:38 +00:00
}
2016-06-28 09:01:00 +00:00
// Handles the back button event
var backEvent = function(event) {
var items = document.getElementsByClassName('item');
Array.from(items).forEach(link => {
link.classList.remove('selected');
});
selectedItems = [];
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
return false;
2016-06-14 15:13:38 +00:00
}
2016-06-28 09:01:00 +00:00
// Handles the click event
var itemClickEvent = function(event) {
2016-06-30 20:28:44 +00:00
var url = this.dataset.href;
var el = document.getElementById(url);
2016-06-28 09:01:00 +00:00
if (selectedItems.length != 0) event.preventDefault();
if (selectedItems.indexOf(url) == -1) {
2016-06-30 20:28:44 +00:00
el.classList.add('selected');
2016-06-28 09:01:00 +00:00
selectedItems.push(url);
} else {
2016-06-30 20:28:44 +00:00
el.classList.remove('selected');
2016-06-28 09:01:00 +00:00
selectedItems.removeElement(url);
}
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
return false;
2016-06-23 07:22:39 +00:00
}
2016-06-28 09:01:00 +00:00
// Handles the datetimes present on the document
var localizeDatetime = function(e, index, ar) {
if (e.textContent === undefined) {
return;
}
var d = new Date(e.getAttribute('datetime'));
if (isNaN(d)) {
d = new Date(e.textContent);
if (isNaN(d)) {
return;
}
}
e.textContent = d.toLocaleString();
}
// Toggles the view mode
var viewEvent = function(event) {
let cookie = document.getCookie('view-list');
let listing = document.getElementById('listing');
if (cookie != 'true') {
document.cookie = 'view-list=true';
} else {
document.cookie = 'view-list=false';
}
handleViewType(document.getCookie('view-list'));
return false;
2016-06-14 20:23:54 +00:00
}
2016-06-28 09:01:00 +00:00
// Handles the view mode change
var handleViewType = function(viewList) {
let listing = document.getElementById('listing');
let button = document.getElementById('view');
2016-06-14 15:13:38 +00:00
2016-06-28 09:01:00 +00:00
if (viewList == "true") {
listing.classList.add('list');
button.innerHTML = '<i class="material-icons" title="Switch View">view_module</i> <span>Switch view</span>';
2016-06-28 09:01:00 +00:00
return false;
}
2016-06-14 15:13:38 +00:00
button.innerHTML = '<i class="material-icons" title="Switch View">view_list</i> <span>Switch view</span>';
2016-06-28 09:01:00 +00:00
listing.classList.remove('list');
return false;
}
2016-06-14 15:13:38 +00:00
var addNewDirEvents = function() {
document.getElementById('new').addEventListener('click', event => {
let newdir = document.getElementById('newdir');
newdir.classList.add('enabled');
newdir.focus();
});
document.getElementById('newdir').addEventListener('blur', event => {
document.getElementById('newdir').classList.remove('enabled');
});
document.getElementById('newdir').addEventListener('keydown', newDirEvent);
}
2016-06-29 14:00:30 +00:00
// Handles the new directory event
var newDirEvent = function(event) {
if (event.keyCode == 27) {
document.getElementById('newdir').classList.toggle('enabled');
setTimeout(() => {
document.getElementById('newdir').value = '';
}, 200);
}
if (event.keyCode == 13) {
event.preventDefault();
let button = document.getElementById('new');
let html = button.changeToLoading();
let request = new XMLHttpRequest();
request.open("POST", window.location);
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-29 14:00:30 +00:00
request.setRequestHeader('Filename', document.getElementById('newdir').value);
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
button.changeToDone((request.status != 200), html);
reloadListing(() => {
addNewDirEvents();
});
2016-06-29 14:00:30 +00:00
}
}
}
return false;
2016-06-29 14:00:30 +00:00
}
2016-06-28 09:01:00 +00:00
// Handles the event when there is change on selected elements
document.addEventListener("changed-selected", function(event) {
var toolbar = document.getElementById("toolbar");
var selectedNumber = selectedItems.length;
document.getElementById("selected-number").innerHTML = selectedNumber;
2016-06-14 15:13:38 +00:00
2016-06-28 09:01:00 +00:00
if (selectedNumber) {
toolbar.classList.add("enabled");
2016-06-14 15:13:38 +00:00
2016-06-28 09:01:00 +00:00
if (selectedNumber > 1) {
document.getElementById("open").classList.add("disabled");
document.getElementById("rename").classList.add("disabled");
}
2016-06-15 07:47:33 +00:00
2016-06-28 09:01:00 +00:00
if (selectedNumber == 1) {
document.getElementById("open").classList.remove("disabled");
document.getElementById("rename").classList.remove("disabled");
}
2016-06-28 09:01:00 +00:00
return false;
}
2016-06-19 22:34:16 +00:00
2016-06-28 09:01:00 +00:00
toolbar.classList.remove("enabled");
return false;
2016-06-15 07:47:33 +00:00
});
2016-06-24 12:17:55 +00:00
2016-06-30 21:24:38 +00:00
var searchEvent = function(event) {
let value = this.value;
let box = document.querySelector('#search div');
if (value.length == 0) {
box.innerHTML = "Write one of yours suported commands: " + user.Commands.join(", ") + ".";
2016-06-30 21:24:38 +00:00
return;
}
let pieces = value.split(' ');
let supported = false;
2016-06-30 21:24:38 +00:00
user.Commands.forEach(function(cmd) {
if (cmd == pieces[0]) {
supported = true;
}
});
if (!supported) {
2016-06-30 21:24:38 +00:00
box.innerHTML = "Command not supported."
return;
}
box.innerHTML = "Press enter to continue."
if (event.keyCode == 13) {
2016-06-30 21:37:52 +00:00
box.innerHTML = '<i class="material-icons spin">autorenew</i>';
2016-06-30 21:24:38 +00:00
let request = new XMLHttpRequest();
request.open('POST', window.location);
request.setRequestHeader('Command', value);
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-30 21:24:38 +00:00
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 501) {
box.innerHTML = "Command not implemented."
}
if (request.status == 500) {
box.innerHTML = "Something went wrong."
}
if (request.status == 200) {
let text = request.responseText;
text = text.substring(1, text.length - 1);
text = text.replace('\\n', "\n");
box.innerHTML = text;
2016-06-30 21:37:52 +00:00
reloadListing();
2016-06-30 21:24:38 +00:00
}
}
}
}
}
2016-06-28 09:01:00 +00:00
document.addEventListener('listing', event => {
// Handle date times
let timeList = document.getElementsByTagName("time");
Array.from(timeList).forEach(localizeDatetime);
2016-06-28 09:01:00 +00:00
// Handles the current view mode and adds the event to the button
handleViewType(document.getCookie("view-list"));
document.getElementById("view").addEventListener("click", viewEvent);
2016-06-25 20:57:10 +00:00
2016-06-28 09:01:00 +00:00
// Add event to items
2016-07-01 09:55:08 +00:00
let checkboxes = document.getElementsByClassName('checkbox');
Array.from(checkboxes).forEach(link => {
2016-06-28 09:01:00 +00:00
link.addEventListener('click', itemClickEvent);
});
2016-06-25 20:57:10 +00:00
2016-06-28 09:01:00 +00:00
// Add event to back button and executes back event on ESC
document.getElementById("back").addEventListener("click", backEvent)
document.addEventListener('keydown', (event) => {
if (event.keyCode == 27) {
backEvent(event);
}
2016-06-27 12:22:07 +00:00
});
2016-06-25 20:57:10 +00:00
if (user.AllowCommands) {
2016-08-21 19:10:12 +00:00
document.querySelector('#search input').addEventListener('focus', event => {
document.getElementById('search').classList.add('active');
});
2016-06-30 21:24:38 +00:00
2016-08-21 19:10:12 +00:00
document.querySelector('#search input').addEventListener('blur', event => {
document.getElementById('search').classList.remove('active');
document.querySelector('#search input').value = '';
});
2016-06-30 21:24:38 +00:00
document.querySelector('#search div').innerHTML = "Write one of yours suported commands: " + user.Commands.join(", ") + ".";
2016-08-21 19:10:12 +00:00
document.querySelector('#search input').addEventListener('keyup', searchEvent);
}
2016-06-30 21:24:38 +00:00
if (user.AllowEdit) {
// Enables rename button
document.getElementById("rename").addEventListener("click", renameEvent);
}
if (user.AllowNew) {
2016-08-21 19:10:12 +00:00
// Enables upload button
document.getElementById("upload").addEventListener("click", (event) => {
document.getElementById("upload-input").click();
});
2016-06-25 20:57:10 +00:00
addNewDirEvents();
2016-06-27 17:57:54 +00:00
2016-08-21 19:10:12 +00:00
// Drag and Drop
let items = document.getElementsByClassName('item');
document.addEventListener("dragover", function(event) {
event.preventDefault();
}, false);
2016-06-25 20:57:10 +00:00
2016-08-21 19:10:12 +00:00
document.addEventListener("dragover", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 0.5;
});
}, false);
2016-06-28 09:01:00 +00:00
2016-08-21 19:10:12 +00:00
document.addEventListener("dragleave", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 1;
});
}, false);
2016-06-28 09:01:00 +00:00
2016-08-21 19:10:12 +00:00
document.addEventListener("drop", function(event) {
event.preventDefault();
var dt = event.dataTransfer;
var files = dt.files;
handleFiles(files);
}, false);
}
2016-06-28 09:01:00 +00:00
});
/* * * * * * * * * * * * * * * *
* *
* EDITOR SPECIFIC FUNCTIONS *
* *
* * * * * * * * * * * * * * * */
// auto grow textareas
var textareaAutoGrow = function() {
let autogrow = function() {
this.style.height = '5px';
this.style.height = this.scrollHeight + 'px';
}
let textareas = document.getElementsByTagName('textarea');
let addAutoGrow = () => {
Array.from(textareas).forEach(textarea => {
autogrow.bind(textarea)();
textarea.addEventListener('keyup', autogrow);
});
}
addAutoGrow();
window.addEventListener('resize', addAutoGrow)
2016-06-24 12:17:55 +00:00
}
2016-06-27 12:22:07 +00:00
var deleteFrontMatterItem = function(event) {
event.preventDefault();
document.getElementById(this.dataset.delete).remove();
}
var addFrontMatterItem = function(event) {
event.preventDefault();
2016-06-27 20:40:51 +00:00
let temp = document.getElementById(tempID)
if (temp) {
temp.remove();
2016-06-27 12:22:07 +00:00
}
2016-06-27 17:57:54 +00:00
let block = this.parentNode;
let type = block.dataset.type;
let id = block.id;
// If the block is an array
if (type === "array") {
let fieldID = id + "[]"
let input = fieldID
let count = block.querySelectorAll('.group > div').length
input = input.replace(/\[/, '\\[');
input = input.replace(/\]/, '\\]');
2016-07-06 10:25:00 +00:00
let fieldsets = block.getElementsByTagName("fieldset");
if (fieldsets.length > 0) {
let newtype = fieldsets[0].dataset.type;
let bid = id + "[" + fieldsets.length + "]";
let name = fieldsets.length;
let template = `<fieldset id="${bid}" data-type="${newtype}">
<h3>${name}</h3>
<div class="action add">
<i class="material-icons">add</i>
</div>
<div class="action delete" data-delete="${bid}">
<i class="material-icons">close</i>
</div>
<div class="group">
</div>
</fieldset>`;
block.insertAdjacentHTML('beforeend', template);
document.querySelector(`div[data-delete="${bid}"]`).addEventListener('click', deleteFrontMatterItem);
document.getElementById(bid).querySelector('.action.add').addEventListener('click', addFrontMatterItem);
} else {
block.querySelector('.group').insertAdjacentHTML('beforeend', `<div id="${fieldID}-${count}" data-type="array-item">
<input name="${fieldID}" id="${fieldID}" type="text" data-parent-type="array"></input>
<div class="action delete" data-delete="${fieldID}-${count}">
<i class="material-icons">close</i>
</div>
</div>`);
document.getElementById(`${fieldID}-${count}`).querySelector('input').focus();
document.querySelector(`div[data-delete="${fieldID}-${count}"]`).addEventListener('click', deleteFrontMatterItem);
}
2016-06-27 12:22:07 +00:00
}
2016-06-27 20:40:51 +00:00
if (type == "object" || type == "parent") {
let template = `<div class="group temp" id="${tempID}">
<div class="block" id="${tempID}">
<label>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".</label>
<input name="${tempID}" type="text" placeholder="Write the field name and press enter.."></input>
</div></div>`;
if (type == "parent") {
document.querySelector('div.button.add').insertAdjacentHTML('beforebegin', template);
} else {
block.querySelector('.delete').insertAdjacentHTML('afterend', template);
}
2016-06-27 12:22:07 +00:00
2016-06-27 20:40:51 +00:00
let temp = document.getElementById(tempID);
let input = temp.querySelector('input');
input.focus();
input.addEventListener('keydown', (event) => {
if (event.keyCode == 27) {
event.preventDefault();
temp.remove();
2016-06-27 12:22:07 +00:00
}
2016-06-27 20:40:51 +00:00
if (event.keyCode == 13) {
event.preventDefault();
let value = input.value;
if (value === '') {
temp.remove();
return true;
}
let name = value.substring(0, value.lastIndexOf(':'));
let newtype = value.substring(value.lastIndexOf(':') + 1, value.length);
if (newtype !== "" && newtype !== "array" && newtype !== "object") {
name = value;
}
name = name.replace(' ', '_');
let bid = name;
if (id != '') {
bid = id + "." + bid;
}
temp.remove();
switch (newtype) {
case "array":
case "object":
let template = `<fieldset id="${bid}" data-type="${newtype}">
<h3>${name}</h3>
<div class="action add">
<i class="material-icons">add</i>
</div>
<div class="action delete" data-delete="${bid}">
<i class="material-icons">close</i>
</div>
<div class="group">
</div>
</fieldset>`;
if (type == "parent") {
document.querySelector('div.button.add').insertAdjacentHTML('beforebegin', template);
} else {
block.insertAdjacentHTML('beforeend', template);
}
document.querySelector(`div[data-delete="${bid}"]`).addEventListener('click', deleteFrontMatterItem);
document.getElementById(bid).querySelector('.action.add').addEventListener('click', addFrontMatterItem);
break;
default:
let group = block.querySelector('.group');
if (group == null) {
block.insertAdjacentHTML('afterbegin', '<div class="group"></div>');
group = block.querySelector('.group');
}
group.insertAdjacentHTML('beforeend', `<div class="block" id="block-${bid}" data-content="${bid}">
2016-06-27 20:40:51 +00:00
<label for="${bid}">${name}</label>
<input name="${bid}" id="${bid}" type="text" data-parent-type="object"></input>
<div class="action delete" data-delete="block-${bid}">
<i class="material-icons">close</i>
</div>
</div>`);
document.getElementById(bid).focus();
document.querySelector(`div[data-delete="block-${bid}"]`).addEventListener('click', deleteFrontMatterItem);
}
}
});
2016-06-27 12:22:07 +00:00
}
2016-06-28 09:01:00 +00:00
return false;
}
document.addEventListener("editor", (event) => {
textareaAutoGrow();
let container = document.getElementById('editor');
let button = document.querySelector('#submit span:first-child');
let kind = container.dataset.kind;
if (kind != 'frontmatter-only') {
let editor = document.getElementById('editor-source');
let mode = editor.dataset.mode;
let textarea = document.querySelector('textarea[name="content"]');
let aceEditor = ace.edit('editor-source');
aceEditor.getSession().setMode("ace/mode/" + mode);
aceEditor.getSession().setValue(textarea.value);
aceEditor.getSession().on('change', function() {
textarea.value = aceEditor.getSession().getValue();
});
aceEditor.setOptions({
wrap: true,
maxLines: Infinity,
theme: "ace/theme/github",
showPrintMargin: false,
fontSize: "1em",
minLines: 20
});
}
let deleteFrontMatterItemButtons = document.getElementsByClassName('delete');
Array.from(deleteFrontMatterItemButtons).forEach(button => {
button.addEventListener('click', deleteFrontMatterItem);
});
let addFrontMatterItemButtons = document.getElementsByClassName('add');
Array.from(addFrontMatterItemButtons).forEach(button => {
button.addEventListener('click', addFrontMatterItem);
});
2016-09-17 07:25:09 +00:00
2016-09-16 10:22:24 +00:00
let saveContent = function() {
2016-06-28 09:01:00 +00:00
let data = form2js(document.querySelector('form'));
let html = button.changeToLoading();
let request = new XMLHttpRequest();
request.open("PUT", window.location);
request.setRequestHeader('Kind', kind);
2016-07-05 16:46:45 +00:00
request.setRequestHeader('Token', token);
2016-06-28 09:01:00 +00:00
request.send(JSON.stringify(data));
request.onreadystatechange = function() {
if (request.readyState == 4) {
button.changeToDone((request.status != 200), html);
}
}
2016-09-16 10:22:24 +00:00
}
document.querySelector('form').addEventListener('submit', (event) => {
event.preventDefault();
saveContent();
2016-06-28 09:01:00 +00:00
});
2016-09-17 07:25:09 +00:00
2016-09-16 10:22:24 +00:00
window.addEventListener('keydown', (event) => {
if (event.ctrlKey || event.metaKey) {
switch (String.fromCharCode(event.which).toLowerCase()) {
2016-09-17 07:25:09 +00:00
case 's':
event.preventDefault();
saveContent();
break;
2016-09-16 10:22:24 +00:00
}
}
2016-09-17 07:25:09 +00:00
});
2016-06-28 09:01:00 +00:00
return false;
});
/* * * * * * * * * * * * * * * *
* *
* BOOTSTRAP *
* *
* * * * * * * * * * * * * * * */
2016-07-01 09:01:31 +00:00
2016-06-28 09:01:00 +00:00
document.addEventListener("DOMContentLoaded", function(event) {
// Add event to logout button
document.getElementById("logout").addEventListener("click", event => {
let request = new XMLHttpRequest();
request.open('GET', window.location.pathname, true, "username", "password");
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
window.location = "/";
}
}
});
2016-06-27 20:40:51 +00:00
2016-07-05 16:46:45 +00:00
// Updates the token
updateToken();
2016-06-28 09:01:00 +00:00
// Enables open, delete and download buttons
document.getElementById("open").addEventListener("click", openEvent);
2016-08-21 19:10:12 +00:00
if (user.AllowEdit) {
2016-08-21 19:10:12 +00:00
document.getElementById("delete").addEventListener("click", deleteEvent);
}
2016-06-28 09:01:00 +00:00
document.getElementById("download").addEventListener("click", downloadEvent);
2016-07-01 09:01:31 +00:00
document.getElementById("open-nav").addEventListener("click", event => {
2016-07-01 09:46:09 +00:00
document.querySelector("header > div:nth-child(2)").classList.toggle("active");
});
document.getElementById("overlay").addEventListener("click", event => {
document.querySelector("header > div:nth-child(2)").classList.toggle("active");
2016-07-01 09:01:31 +00:00
});
2016-06-28 09:01:00 +00:00
if (document.getElementById('listing')) {
document.sendCostumEvent('listing');
}
if (document.getElementById('editor')) {
document.sendCostumEvent('editor');
}
2016-06-27 17:57:54 +00:00
return false;
2016-10-18 14:41:04 +00:00
});