working settings

This commit is contained in:
Henrique Dias 2015-09-14 18:18:26 +01:00
parent 4d07761bd5
commit a388b6a025
11 changed files with 187 additions and 65 deletions

View File

@ -50,7 +50,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
page := new(page.Page) page := new(page.Page)
page.Title = "Edit" page.Title = "Edit"
page.Body = inf page.Body = inf
return page.Render(w, "edit") return page.Render(w, r, "edit")
} }
return 200, nil return 200, nil

View File

@ -24,12 +24,12 @@ func Pretty(content []byte, language string) (interface{}, error) {
return []string{}, err return []string{}, err
} }
//log.Print(c)
return rawToPretty(c, ""), nil return rawToPretty(c, ""), nil
} }
type frontmatter struct { type frontmatter struct {
Name string Name string
Tag string
Content interface{} Content interface{}
SubContent bool SubContent bool
} }
@ -55,7 +55,6 @@ func rawToPretty(config interface{}, master string) interface{} {
for index := range names { for index := range names {
c := new(frontmatter) c := new(frontmatter)
c.Name = names[index] c.Name = names[index]
c.Tag = master + "_" + names[index]
c.SubContent = false c.SubContent = false
i := config.(map[string]interface{})[names[index]] i := config.(map[string]interface{})[names[index]]

View File

@ -1,4 +1,4 @@
//go:generate go-bindata -pkg assets -o assets/assets.go static/css/ static/js/ templates/ //go:generate go-bindata -pkg assets -o assets/assets.go static/css/ static/js/ templates/ static/
package hugo package hugo
@ -31,8 +31,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
if middleware.Path(r.URL.Path).Matches("/admin") { if middleware.Path(r.URL.Path).Matches("/admin") {
page := parseComponents(r)[1] page := parseComponents(r)[1]
if page == "assets" { if page == "static" {
filename := strings.Replace(r.URL.Path, "/admin/assets", "static", 1) filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
file, err := assets.Asset(filename) file, err := assets.Asset(filename)
if err != nil { if err != nil {

View File

@ -24,8 +24,13 @@ type Page struct {
} }
// Render the page // Render the page
func (p *Page) Render(w http.ResponseWriter, templates ...string) (int, error) { func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...string) (int, error) {
templates = append(templates, "base_full") if r.URL.Query().Get("minimal") == "true" {
templates = append(templates, "base_minimal")
} else {
templates = append(templates, "base_full")
}
var tpl *template.Template var tpl *template.Template
for i, t := range templates { for i, t := range templates {

View File

@ -1,6 +1,8 @@
package settings package settings
import ( import (
"bytes"
"encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -8,20 +10,46 @@ import (
"github.com/hacdias/caddy-hugo/frontmatter" "github.com/hacdias/caddy-hugo/frontmatter"
"github.com/hacdias/caddy-hugo/page" "github.com/hacdias/caddy-hugo/page"
"github.com/spf13/hugo/commands"
) )
type test struct {
Test string
}
// Execute the page // Execute the page
func Execute(w http.ResponseWriter, r *http.Request) (int, error) { func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
language := getConfigFrontMatter()
if language == "" {
log.Print("Configuration frontmatter can't be defined")
return 500, nil
}
if r.Method == "POST" { if r.Method == "POST" {
err := os.Remove("config." + language)
} else { if err != nil {
language := getConfigFrontMatter() log.Print(err)
if language == "" {
log.Print("Configuration frontmatter can't be defined")
return 500, nil return 500, nil
} }
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
raw := buf.Bytes()
content := new(bytes.Buffer)
json.Indent(content, raw, "", " ")
err = ioutil.WriteFile("config.json", content.Bytes(), 0666)
if err != nil {
log.Print(err)
return 500, err
}
commands.Execute()
} else {
content, err := ioutil.ReadFile("config." + language) content, err := ioutil.ReadFile("config." + language)
if err != nil { if err != nil {
@ -39,7 +67,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
page := new(page.Page) page := new(page.Page)
page.Title = "Settings" page.Title = "Settings"
page.Body = f page.Body = f
return page.Render(w, "settings", "frontmatter") return page.Render(w, r, "settings", "frontmatter")
} }
return 200, nil return 200, nil
} }

View File

@ -1,36 +1,76 @@
body { body {
font-family: 'Roboto', sans-serif; font-family: 'Roboto', sans-serif;
padding-top: 3em; color: #212121;
height: 100%;
width: 100%;
} }
header {
/* SITE HEADER */ position: fixed;
top: 0;
.site-header { left: 0;
height: 3em; height: 3em;
width: 100%; width: 100%;
background-color: #263238; background-color: #212121;
color: #fff; padding: 0 2em;
padding: 0 2em; box-sizing: border-box;
box-sizing: border-box; z-index: 999;
position: fixed; color: #fff;
top: 0; }
left: 0; header nav {} header nav ul {
margin: 0;
padding: 0;
}
header nav ul li {
list-style-type: none;
display: inline-block;
vertical-align: middle;
}
header nav img {
height: 2em;
}
header nav ul li a {
padding: 0.5em 0.5em;
line-height: 2em;
display: block;
text-decoration: none;
color: inherit;
transition: .5s ease background-color;
}
header nav ul li a:hover {
background-color: rgba(255, 255, 255, 0.3);
}
main {
top: 3em;
position: relative;
} }
.content { .content {
margin: 1.5em auto; margin: 1.5em auto;
width: 80%; width: 80%;
max-width: 960px; max-width: 800px;
} }
textarea { textarea {
width: 100%; width: 100%;
min-height: 50em; min-height: 50em;
resize: vertical; resize: vertical;
border: 0; border: 0;
font-family: inherit; font-family: inherit;
} }
.block { /* FORMS */
margin: 1em 0;
form {
} }
form input {
color: rgba(0, 0, 0, 0.41);width: 15em;line-height: 1.25em;margin: .5em 0;border: 1px solid #fff;transition: .5s ease-out all;}
form input:focus {
color: inherit;
outline: 0;
border-bottom: 1px solid #2196F3;
}
form label {
width: 10.5em;display: inline-block;}

BIN
static/hugo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -0,0 +1,38 @@
$(document).ready(function() {
$('form').submit(function(event) {
var data = JSON.stringify($(this).serializeField())
var url = $(this).attr('action')
$.ajax({
type : 'POST',
url : url,
data : data,
dataType : 'json',
encode : true
}).done(function(data) {
console.log(data);
});
event.preventDefault();
});
});
$.fn.serializeField = function() {
var result = {};
this.each(function() {
$(this).find("> *").each(function() {
var $this = $(this);
var name = $this.attr("name");
if ($this.is("fieldset") && name) {
result[name] = $this.serializeField();
}
else {
$.each($this.serializeArray(), function() {
result[this.name] = this.value;
});
}
});
});
return result;
};

View File

@ -1,28 +1,37 @@
<!DOCTYPE html> <!DOCTYPE html>
<html class="no-js" lang="en"> <html class="no-js" lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#fff"> <meta name="theme-color" content="#fff">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/admin/assets/css/normalize.css"> <link rel="stylesheet" href="/admin/static/css/normalize.css">
<link rel="stylesheet" href="/admin/assets/css/main.css"> <link rel="stylesheet" href="/admin/static/css/main.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="/admin/assets/js/app.js"></script> <script src="/admin/static/js/app.js"></script>
</head> </head>
<body> <body>
<header class="site-header"> <header>
Admin <nav>
</header> <ul>
<li><img src="/admin/static/hugo.png"></li>
<li><a href="/admin/browse/content">Content</a></li>
<li><a href="/admin/browse">Browse</a></li>
<li><a href="/admin/settings">Settings</a></li>
</ul>
</nav>
</header>
<main>
{{ template "content" . }}
</main>
<footer>
{{ template "content" . }} </footer>
<footer></footer>
</body> </body>
</html> </html>

View File

@ -1,11 +1,13 @@
{{ define "frontmatter" }} {{ define "frontmatter" }}
{{ range $key, $value := . }} {{ range $key, $value := . }}
{{ if $value.SubContent }} {{ if $value.SubContent }}
<h2>{{ splitCapitalize $value.Name }}</h2> <fieldset name="{{ $value.Name }}">
{{ template "frontmatter" $value.Content }} <legend>{{ splitCapitalize $value.Name }}</legend>
{{ template "frontmatter" $value.Content }}
</fieldset>
{{ else}} {{ else}}
<label for="{{ $value.Tag }}">{{ splitCapitalize $value.Name }}</label> <label for="{{ $value.Name }}">{{ splitCapitalize $value.Name }}</label>
<input name="{{ $value.Tag }}" id="{{ $value.Tag }}" value="{{ $value.Content }}"></input><br> <input name="{{ $value.Name }}" id="{{ $value.Name }}" value="{{ $value.Content }}"></input><br>
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}

View File

@ -3,8 +3,9 @@
{{ with .Body }} {{ with .Body }}
<div class="content"> <div class="content">
<h1>Settings</h1> <h1>Settings</h1>
<form> <form method="POST" action="/admin/settings">
{{ template "frontmatter" . }} {{ template "frontmatter" . }}
<input type="submit" value="Save">
</form> </form>
</div> </div>
{{ end }} {{ end }}