working settings
This commit is contained in:
		
							parent
							
								
									4d07761bd5
								
							
						
					
					
						commit
						a388b6a025
					
				| 
						 | 
				
			
			@ -50,7 +50,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
 | 
			
		|||
		page := new(page.Page)
 | 
			
		||||
		page.Title = "Edit"
 | 
			
		||||
		page.Body = inf
 | 
			
		||||
		return page.Render(w, "edit")
 | 
			
		||||
		return page.Render(w, r, "edit")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 200, nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,12 +24,12 @@ func Pretty(content []byte, language string) (interface{}, error) {
 | 
			
		|||
		return []string{}, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//log.Print(c)
 | 
			
		||||
	return rawToPretty(c, ""), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type frontmatter struct {
 | 
			
		||||
	Name       string
 | 
			
		||||
	Tag        string
 | 
			
		||||
	Content    interface{}
 | 
			
		||||
	SubContent bool
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -55,7 +55,6 @@ func rawToPretty(config interface{}, master string) interface{} {
 | 
			
		|||
	for index := range names {
 | 
			
		||||
		c := new(frontmatter)
 | 
			
		||||
		c.Name = names[index]
 | 
			
		||||
		c.Tag = master + "_" + names[index]
 | 
			
		||||
		c.SubContent = false
 | 
			
		||||
 | 
			
		||||
		i := config.(map[string]interface{})[names[index]]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										6
									
								
								hugo.go
								
								
								
								
							
							
						
						
									
										6
									
								
								hugo.go
								
								
								
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -31,8 +31,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
 | 
			
		|||
	if middleware.Path(r.URL.Path).Matches("/admin") {
 | 
			
		||||
		page := parseComponents(r)[1]
 | 
			
		||||
 | 
			
		||||
		if page == "assets" {
 | 
			
		||||
			filename := strings.Replace(r.URL.Path, "/admin/assets", "static", 1)
 | 
			
		||||
		if page == "static" {
 | 
			
		||||
			filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
 | 
			
		||||
			file, err := assets.Asset(filename)
 | 
			
		||||
 | 
			
		||||
			if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,8 +24,13 @@ type Page struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Render the page
 | 
			
		||||
func (p *Page) Render(w http.ResponseWriter, templates ...string) (int, error) {
 | 
			
		||||
	templates = append(templates, "base_full")
 | 
			
		||||
func (p *Page) Render(w http.ResponseWriter, r *http.Request, templates ...string) (int, error) {
 | 
			
		||||
	if r.URL.Query().Get("minimal") == "true" {
 | 
			
		||||
		templates = append(templates, "base_minimal")
 | 
			
		||||
	} else {
 | 
			
		||||
		templates = append(templates, "base_full")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tpl *template.Template
 | 
			
		||||
 | 
			
		||||
	for i, t := range templates {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
package settings
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
| 
						 | 
				
			
			@ -8,20 +10,46 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/frontmatter"
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/page"
 | 
			
		||||
	"github.com/spf13/hugo/commands"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type test struct {
 | 
			
		||||
	Test string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Execute the page
 | 
			
		||||
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" {
 | 
			
		||||
		err := os.Remove("config." + language)
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
		language := getConfigFrontMatter()
 | 
			
		||||
 | 
			
		||||
		if language == "" {
 | 
			
		||||
			log.Print("Configuration frontmatter can't be defined")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Print(err)
 | 
			
		||||
			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)
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -39,7 +67,7 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
 | 
			
		|||
		page := new(page.Page)
 | 
			
		||||
		page.Title = "Settings"
 | 
			
		||||
		page.Body = f
 | 
			
		||||
		return page.Render(w, "settings", "frontmatter")
 | 
			
		||||
		return page.Render(w, r, "settings", "frontmatter")
 | 
			
		||||
	}
 | 
			
		||||
	return 200, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,36 +1,76 @@
 | 
			
		|||
body {
 | 
			
		||||
  font-family: 'Roboto', sans-serif;
 | 
			
		||||
  padding-top: 3em;
 | 
			
		||||
    font-family: 'Roboto', sans-serif;
 | 
			
		||||
    color: #212121;
 | 
			
		||||
    height: 100%;
 | 
			
		||||
    width: 100%;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* SITE HEADER */
 | 
			
		||||
 | 
			
		||||
.site-header {
 | 
			
		||||
  height: 3em;
 | 
			
		||||
  width: 100%;
 | 
			
		||||
  background-color: #263238;
 | 
			
		||||
  color: #fff;
 | 
			
		||||
  padding: 0 2em;
 | 
			
		||||
  box-sizing: border-box;
 | 
			
		||||
  position: fixed;
 | 
			
		||||
  top: 0;
 | 
			
		||||
  left: 0;
 | 
			
		||||
header {
 | 
			
		||||
    position: fixed;
 | 
			
		||||
    top: 0;
 | 
			
		||||
    left: 0;
 | 
			
		||||
    height: 3em;
 | 
			
		||||
    width: 100%;
 | 
			
		||||
    background-color: #212121;
 | 
			
		||||
    padding: 0 2em;
 | 
			
		||||
    box-sizing: border-box;
 | 
			
		||||
    z-index: 999;
 | 
			
		||||
    color: #fff;
 | 
			
		||||
}
 | 
			
		||||
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 {
 | 
			
		||||
  margin: 1.5em auto;
 | 
			
		||||
  width: 80%;
 | 
			
		||||
  max-width: 960px;
 | 
			
		||||
    margin: 1.5em auto;
 | 
			
		||||
    width: 80%;
 | 
			
		||||
    max-width: 800px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
textarea {
 | 
			
		||||
  width: 100%;
 | 
			
		||||
  min-height: 50em;
 | 
			
		||||
  resize: vertical;
 | 
			
		||||
  border: 0;
 | 
			
		||||
  font-family: inherit;
 | 
			
		||||
    width: 100%;
 | 
			
		||||
    min-height: 50em;
 | 
			
		||||
    resize: vertical;
 | 
			
		||||
    border: 0;
 | 
			
		||||
    font-family: inherit;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.block {
 | 
			
		||||
  margin: 1em 0;
 | 
			
		||||
/* FORMS */
 | 
			
		||||
 | 
			
		||||
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;}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 48 KiB  | 
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -1,28 +1,37 @@
 | 
			
		|||
<!DOCTYPE html>
 | 
			
		||||
<html class="no-js" lang="en">
 | 
			
		||||
  <head>
 | 
			
		||||
    <meta charset="utf-8">
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1">
 | 
			
		||||
    <meta name="theme-color" content="#fff">
 | 
			
		||||
    <title>{{ .Title }}</title>
 | 
			
		||||
<head>
 | 
			
		||||
<meta charset="utf-8">
 | 
			
		||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
<meta name="viewport" content="width=device-width, initial-scale=1">
 | 
			
		||||
<meta name="theme-color" content="#fff">
 | 
			
		||||
<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/assets/css/main.css">
 | 
			
		||||
<link rel="stylesheet" href="/admin/static/css/normalize.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="/admin/assets/js/app.js"></script>
 | 
			
		||||
  </head>
 | 
			
		||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
 | 
			
		||||
<script src="/admin/static/js/app.js"></script>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
  <body>
 | 
			
		||||
    <header class="site-header">
 | 
			
		||||
      Admin
 | 
			
		||||
    </header>
 | 
			
		||||
<body>
 | 
			
		||||
  <header>
 | 
			
		||||
    <nav>
 | 
			
		||||
      <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>
 | 
			
		||||
</html>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,13 @@
 | 
			
		|||
{{ define "frontmatter" }}
 | 
			
		||||
  {{ range $key, $value := . }}
 | 
			
		||||
    {{ if $value.SubContent }}
 | 
			
		||||
      <h2>{{ splitCapitalize $value.Name }}</h2>
 | 
			
		||||
      {{ template "frontmatter" $value.Content }}
 | 
			
		||||
      <fieldset name="{{ $value.Name }}">
 | 
			
		||||
        <legend>{{ splitCapitalize $value.Name }}</legend>
 | 
			
		||||
        {{ template "frontmatter" $value.Content }}
 | 
			
		||||
      </fieldset>
 | 
			
		||||
    {{ else}}
 | 
			
		||||
    <label for="{{ $value.Tag }}">{{ splitCapitalize $value.Name }}</label>
 | 
			
		||||
    <input name="{{ $value.Tag }}" id="{{ $value.Tag }}" value="{{ $value.Content }}"></input><br>
 | 
			
		||||
    <label for="{{ $value.Name }}">{{ splitCapitalize $value.Name }}</label>
 | 
			
		||||
    <input name="{{ $value.Name }}" id="{{ $value.Name }}" value="{{ $value.Content }}"></input><br>
 | 
			
		||||
    {{ end }}
 | 
			
		||||
  {{ end }}
 | 
			
		||||
{{ end }}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,8 +3,9 @@
 | 
			
		|||
{{ with .Body }}
 | 
			
		||||
<div class="content">
 | 
			
		||||
  <h1>Settings</h1>
 | 
			
		||||
  <form>
 | 
			
		||||
  <form method="POST" action="/admin/settings">
 | 
			
		||||
  {{ template "frontmatter" . }}
 | 
			
		||||
  <input type="submit" value="Save">
 | 
			
		||||
  </form>
 | 
			
		||||
</div>
 | 
			
		||||
{{ end }}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue