start categories listing page, in the process of reworking the template code a bit
This commit is contained in:
parent
d8c7ec2295
commit
4f023f6093
|
@ -59,6 +59,7 @@ func (category *Category) Depth() int {
|
||||||
return depth
|
return depth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function for templates (add form select list)
|
||||||
func (category *Category) ToString() string {
|
func (category *Category) ToString() string {
|
||||||
return strings.Repeat("- ", category.Depth()) + category.Name
|
return strings.Repeat("- ", category.Depth()) + category.Name
|
||||||
}
|
}
|
20
main.go
20
main.go
|
@ -6,11 +6,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"flag"
|
"flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -39,7 +36,6 @@ var (
|
||||||
config Config
|
config Config
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
|
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
|
||||||
templates = map[string]*template.Template{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfig(env string) {
|
func loadConfig(env string) {
|
||||||
|
@ -71,22 +67,6 @@ func dbConnect() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTemplates() {
|
|
||||||
files, _ := filepath.Glob("templates/pages/*.html")
|
|
||||||
re := regexp.MustCompile("templates/pages/(.*).html")
|
|
||||||
fmt.Println("Loading Templates:")
|
|
||||||
for _, t := range files {
|
|
||||||
name := re.FindStringSubmatch(t)
|
|
||||||
fmt.Println(" ", name[1])
|
|
||||||
var err error
|
|
||||||
templates[name[1]], err = template.ParseFiles("templates/layout.html", t)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Template load error: ", err)
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("transmet ", VERSION)
|
fmt.Println("transmet ", VERSION)
|
||||||
|
|
||||||
|
|
|
@ -62,14 +62,6 @@ func getPostHandler(getFn, postFn func(http.ResponseWriter, *http.Request)) func
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) {
|
|
||||||
err := templates[template].Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Exec err: ", err)
|
|
||||||
}
|
|
||||||
// TODO: show error 500 page
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log in page handler
|
// Log in page handler
|
||||||
func LoginFormHandler(w http.ResponseWriter, r *http.Request) {
|
func LoginFormHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
session, _ := store.Get(r, "c_user")
|
session, _ := store.Get(r, "c_user")
|
||||||
|
@ -252,12 +244,24 @@ func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func ServeFileHandler(res http.ResponseWriter, req *http.Request) {
|
func ServeFileHandler(res http.ResponseWriter, req *http.Request) {
|
||||||
fname := path.Base(req.URL.Path)
|
fname := path.Base(req.URL.Path)
|
||||||
http.ServeFile(res, req, "./"+fname)
|
http.ServeFile(res, req, "./"+fname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
|
session, _ := store.Get(r, "c_user")
|
||||||
|
flashes := GetFlashes(session)
|
||||||
|
session.Save(r, w)
|
||||||
|
categories.LoadCategories(db)
|
||||||
|
|
||||||
|
ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree})
|
||||||
|
}
|
||||||
|
|
||||||
|
func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
|
http.Redirect(w, r, "/categories", http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
func init_route_handlers() {
|
func init_route_handlers() {
|
||||||
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/"))))
|
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/"))))
|
||||||
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css/"))))
|
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css/"))))
|
||||||
|
@ -274,6 +278,7 @@ func init_route_handlers() {
|
||||||
r.HandleFunc("/export", userHandler(exportHandler))
|
r.HandleFunc("/export", userHandler(exportHandler))
|
||||||
r.HandleFunc("/added", userHandler(addedHandler))
|
r.HandleFunc("/added", userHandler(addedHandler))
|
||||||
|
|
||||||
|
r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler)))
|
||||||
|
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
templates = map[string]*template.Template{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tempalte helper functions
|
||||||
|
var funcMap = template.FuncMap {
|
||||||
|
"add": func (x, y int) int { return x + y },
|
||||||
|
"minus": func (x, y int) int { return x - y },
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func initTemplates() {
|
||||||
|
files, _ := filepath.Glob("templates/pages/*.html")
|
||||||
|
re := regexp.MustCompile("templates/pages/(.*).html")
|
||||||
|
fmt.Println("Loading Templates:")
|
||||||
|
for _, t := range files {
|
||||||
|
name := re.FindStringSubmatch(t)
|
||||||
|
fmt.Println(" ", name[1])
|
||||||
|
templates[name[1]] = template.Must(template.New(name[1]).Funcs(funcMap).ParseFiles("templates/layout.html", t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) {
|
||||||
|
err := templates[template].ExecuteTemplate(w, "layout.html", data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Exec err: ", err)
|
||||||
|
}
|
||||||
|
// TODO: show error 500 page
|
||||||
|
}
|
|
@ -29,6 +29,7 @@
|
||||||
<span class="icon-bar"></span>
|
<span class="icon-bar"></span>
|
||||||
</button>
|
</button>
|
||||||
<a class="navbar-brand" href="/">transmet</a>
|
<a class="navbar-brand" href="/">transmet</a>
|
||||||
|
<a class="navbar-brand" href="/categories">categories</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="collapse navbar-collapse">
|
<div class="collapse navbar-collapse">
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
{{define "body"}}
|
||||||
|
<h2 class="form-categories-heading">Categories</h2>
|
||||||
|
{{template "flashes" .}}
|
||||||
|
<div class="row">
|
||||||
|
|
||||||
|
{{range $category := .categories}}
|
||||||
|
{{template "row-category" $category}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{define "row-category"}}
|
||||||
|
{{if .Parent.Valid }}
|
||||||
|
{{with .Depth}}
|
||||||
|
<div class="col-xs-{{.}} child-depth"></div>
|
||||||
|
<div class="col-xs-{{minus 12 .}} category-row">
|
||||||
|
{{end}}
|
||||||
|
{{else}}
|
||||||
|
<div class="col-xs-12 category-row">
|
||||||
|
{{end}}
|
||||||
|
{{.Name}}</div>
|
||||||
|
|
||||||
|
{{range $child := .Children}}
|
||||||
|
{{template "row-category" $child}}
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
|
@ -1,5 +1,5 @@
|
||||||
{{define "body"}}
|
{{define "body"}}
|
||||||
<h2 class="form-add-heading">List</h2>
|
<h2 class="list-heading">List</h2>
|
||||||
{{template "flashes" .}}
|
{{template "flashes" .}}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
Loading…
Reference in New Issue