From 1c55147faaa19ee8c1f829c1eb4617e98e8cbaeb Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 27 Jun 2016 13:22:07 +0100 Subject: [PATCH] improvements --- assets/public/css/styles.css | 3 + assets/public/js/application.js | 127 +++++++++++++++++++++++++++ assets/public/js/form-to-json.js | 141 ++++++++++++++++++++++++++++++ assets/templates/frontmatter.tmpl | 16 +++- filemanager.go | 6 ++ internal/file/update.go | 68 ++++++++++++++ 6 files changed, 357 insertions(+), 4 deletions(-) create mode 100644 assets/public/js/form-to-json.js create mode 100644 internal/file/update.go diff --git a/assets/public/css/styles.css b/assets/public/css/styles.css index e79d5785..7fcd44e8 100644 --- a/assets/public/css/styles.css +++ b/assets/public/css/styles.css @@ -637,3 +637,6 @@ i.spin { #editor textarea[name="content"] { display: none; } +#editor fieldset fieldset { + margin-left: 1em; +} diff --git a/assets/public/js/application.js b/assets/public/js/application.js index b1758541..555dba61 100644 --- a/assets/public/js/application.js +++ b/assets/public/js/application.js @@ -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('
'); + } + + // 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('
'); + + newItem = $("#" + defaultID); + newItem.html(''); + 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('
'); + newItem.prepend(' '); + } else { + type = ""; + + if (elements[1] == "array") { + type = "array"; + } else { + type = "object" + } + + template = "

${elements[0]}

" + 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; + */ +} diff --git a/assets/public/js/form-to-json.js b/assets/public/js/form-to-json.js new file mode 100644 index 00000000..778cf848 --- /dev/null +++ b/assets/public/js/form-to-json.js @@ -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; i0) || + (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; j0){ + 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 diff --git a/assets/templates/frontmatter.tmpl b/assets/templates/frontmatter.tmpl index 0659bca5..d8af3c50 100644 --- a/assets/templates/frontmatter.tmpl +++ b/assets/templates/frontmatter.tmpl @@ -4,7 +4,7 @@ {{ if eq $value.Parent.Type "array" }}
{{ template "value" $value }} -
+
close
@@ -12,7 +12,7 @@
{{ template "value" $value }} -
+
close
@@ -27,6 +27,15 @@ {{ range $key, $value := .Objects }} {{ template "fielset" $value }} {{ end }} + + + + + {{ end }} {{ define "value" }} @@ -46,10 +55,9 @@
add
-
+
close
{{ template "blocks" .Content }} - {{ end }} diff --git a/filemanager.go b/filemanager.go index d071d31f..dd4168df 100644 --- a/filemanager.go +++ b/filemanager.go @@ -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" { diff --git a/internal/file/update.go b/internal/file/update.go new file mode 100644 index 00000000..e30e548b --- /dev/null +++ b/internal/file/update.go @@ -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 +}