improvements

pull/144/head
Henrique Dias 2016-06-27 13:22:07 +01:00
parent c5d9803bae
commit 1c55147faa
6 changed files with 357 additions and 4 deletions

View File

@ -637,3 +637,6 @@ i.spin {
#editor textarea[name="content"] {
display: none;
}
#editor fieldset fieldset {
margin-left: 1em;
}

View File

@ -477,8 +477,135 @@ var handleEditorPage = function () {
});
}
let deleteFrontMatterItemButtons = document.getElementsByClassName('delete');
Array.from(deleteFrontMatterItemButtons).forEach(button => {
button.addEventListener('click', deleteFrontMatterItem);
});
return false;
}
var deleteFrontMatterItem = function(event) {
event.preventDefault();
document.getElementById(this.dataset.delete).remove();
}
var addFrontMatterItem = 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>');
}
// 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();
}
return false;
}
});
}
return false;
*/
}

View File

@ -0,0 +1,141 @@
/**
Author: Jhan Mateo
Date Started: 7/29/2015
Date Ended: 7/30/2015
Description: Using native javascript (no js framework), this application will serializes from form data to Json format.
The MIT License (MIT)
Copyright (c) 2015 Jhan Mateo
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
**/
'use strict';
function formToJson(form){
if('form'!==form.nodeName.toLowerCase() && 1!==form.nodeType){
console.log('Form error');
return false;
}
var json_data = {}, new_arr_obj=null, index=null, key=null, input_name=null, new_obj=null;
for(var i=0,n=form.length; i<n; i++){
if(form[i].type!=='submit' || form[i].nodeName.toLowerCase()!=='fieldset' || form[i].nodeName.toLowerCase()!=='reset'){
if(
(form[i]!==undefined && form[i]!==null) &&
(form[i].type==='checkbox' && form[i].checked) ||
(form[i].type==='radio' && form[i].checked) ||
(form[i].type==='text' && form[i].value.length>0) ||
(form[i].type==='range' && form[i].value.length>0) ||
(form[i].type==='select-one' && form[i].options[form[i].selectedIndex].value.length>0) ||
(form[i].type==='select-multiple' && form[i].selectedOptions.length>0) ||
(form[i].type==='textarea' && form[i].value.length>0) ||
(form[i].type==='number' && form[i].value.length>0) ||
(form[i].type==='date' && form[i].value.length>0) ||
(form[i].type==='color' && form[i].value.length>0) ||
(form[i].type==='month' && form[i].value.length>0) ||
(form[i].type==='week' && form[i].value.length>0) ||
(form[i].type==='time' && form[i].value.length>0) ||
(form[i].type==='datetime' && form[i].value.length>0) ||
(form[i].type==='datetime-local' && form[i].value.length>0) ||
(form[i].type==='email' && form[i].value.length>0) ||
(form[i].type==='search' && form[i].value.length>0) ||
(form[i].type==='tel' && form[i].value.length>0) ||
(form[i].type==='url' && form[i].value.length>0) ||
(form[i].type==='image' && form[i].value.length>0) ||
(form[i].type==='file' && form[i].value.length>0)
){
/*get the name of the current input*/
input_name = form[i].name;
/*array/object*/
if(input_name.match(/\[.*\]/g)){
if(input_name.match(/\[.+\]/g)){
/*array object, Object[][name]*/
if(input_name.match(/\[.+\]/g)[0].match(/\[[0-9]\]/)!==null){
new_arr_obj = input_name.replace(/\[.+\]/g,''); //get object name
index = input_name.match(/[0-9]/g)[0]; //get index group
key = input_name.match(/\[.+\]/g)[0].replace(/(\[|\]|[0-9])/g,'');
/*create an array in an object*/
if(typeof json_data[new_arr_obj]==='undefined'){
json_data[new_arr_obj] = [];
}
/*create an object inside array*/
if(typeof json_data[new_arr_obj][index]==='undefined'){
json_data[new_arr_obj][index] = {};
}
json_data[new_arr_obj][index][key] = form[i].value;
}else if(input_name.match(/\[.+\]/g)!==null){
//to object
//Object[name]
/*get object name*/
new_obj = input_name.replace(/\[.+\]/g,'');
/*set new object*/
if(typeof json_data[new_obj]==='undefined'){
json_data[new_obj] = {};
}
/*assign a key name*/
key = input_name.match(/\[.+\]/g)[0].replace(/(\[|\])/g,'');
/*set key and form value*/
json_data[new_obj][key] = form[i].value;
}else{}
}else{
/*to array, Object[]*/
key = input_name.replace(/\[.*\]/g, '');
if(form[i].type==='select-multiple'){
for(var j=0, m=form[i].selectedOptions.length; j<m; j++){
if(form[i].selectedOptions[j].value.length>0){
if(typeof json_data[key]==='undefined'){
json_data[key] = [];
}
json_data[key].push(form[i].selectedOptions[j].value);
}
}
}else{
if(typeof json_data[key]==='undefined'){
json_data[key] = [];
}
json_data[key].push(form[i].value);
}
}
}else{
/*basic info*/
key = form[i].name.replace(/\[.*\]/g, '');
json_data[key] = form[i].value;
}
}
}
}//endfor
document.getElementById('json_result').innerHTML = JSON.stringify(json_data);
console.log("Result: ",json_data);
return false;
}//endfunc

View File

@ -4,7 +4,7 @@
{{ if eq $value.Parent.Type "array" }}
<div id="{{ $value.Name }}-{{ $key }}" data-type="array-item">
{{ template "value" $value }}
<div class="action delete">
<div class="action delete" data-delete="{{ $value.Name }}-{{ $key }}">
<i class="material-icons">close</i>
</div>
</div>
@ -12,7 +12,7 @@
<div class="block" id="block-{{ $value.Name }}" data-content="{{ $value.Name }}">
<label for="{{ $value.Name }}">{{ SplitCapitalize $value.Title }}</label>
{{ template "value" $value }}
<div class="action delete">
<div class="action delete" data-delete="block-{{ $value.Name }}">
<i class="material-icons">close</i>
</div>
</div>
@ -27,6 +27,15 @@
{{ range $key, $value := .Objects }}
{{ template "fielset" $value }}
{{ end }}
<template id="arrayItem">
</template>
<template id="objectItem">
</template>
{{ end }}
{{ define "value" }}
@ -46,10 +55,9 @@
<div class="action add">
<i class="material-icons">add</i>
</div>
<div class="action delete">
<div class="action delete" data-delete="{{ .Name }}">
<i class="material-icons">close</i>
</div>
{{ template "blocks" .Content }}
</fieldset>
{{ end }}

View File

@ -73,6 +73,12 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
}
return fi.ServeAsHTML(w, r, c)
case http.MethodPut:
if fi.IsDir {
return http.StatusNotAcceptable, nil
}
// Update a file
return fi.Update(w, r, c)
case http.MethodPost:
// Upload a new file
if r.Header.Get("Upload") == "true" {

68
internal/file/update.go Normal file
View File

@ -0,0 +1,68 @@
package file
import (
"net/http"
"github.com/hacdias/caddy-filemanager/internal/config"
)
// Update is
func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
/*
// POST handles the POST method on editor page
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
var data info
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
fmt.Println(string(rawBuffer.Bytes()))
if err != nil {
return server.RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
}
// Initializes the file content to write
var file []byte
var code int
switch data.ContentType {
case "frontmatter-only":
file, code, err = parseFrontMatterOnlyFile(data, filename)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
case "content-only":
// The main content of the file
mainContent := data.Content["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
file, code, err = parseCompleteFile(data, filename, c)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
default:
return server.RespondJSON(w, &response{"Invalid content type."}, http.StatusBadRequest, nil)
}
// Write the file
err = ioutil.WriteFile(filename, file, 0666)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, http.StatusInternalServerError, err)
}
if data.Regenerate {
go hugo.Run(c, false)
}
return server.RespondJSON(w, nil, http.StatusOK, nil)
}
*/
return 0, nil
}