Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Initial theme component implementation #31
Browse files Browse the repository at this point in the history
  • Loading branch information
uberswe committed Nov 2, 2020
1 parent e02f638 commit 08aecdb
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 101 deletions.
38 changes: 34 additions & 4 deletions pkg/routes/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package routes
import (
"fmt"
"github.com/markustenghamn/beubo/pkg/structs"
"github.com/markustenghamn/beubo/pkg/structs/page"
"github.com/markustenghamn/beubo/pkg/structs/page/component"
"github.com/markustenghamn/beubo/pkg/utility"
"net/http"
)
Expand All @@ -18,18 +20,46 @@ func (br *BeuboRouter) Admin(w http.ResponseWriter, r *http.Request) {
utility.ErrorHandler(err, false)
}

var rows []component.Row
for _, site := range sites {
sid := fmt.Sprintf("%d", site.ID)
extra["sites"][sid] = make(map[string]string)
extra["sites"][sid]["id"] = sid
extra["sites"][sid]["title"] = site.Title
extra["sites"][sid]["domain"] = site.Domain
rows = append(rows, component.Row{
Columns: []component.Column{
{Name: "ID", Value: sid},
{Name: "Site", Value: site.Title},
{Name: "Domain", Value: site.Domain},
{Name: ""},
{Name: ""},
{Name: ""},
{Name: ""},
},
})
}

table := component.Table{
Section: "main",
Header: []component.Column{
{Name: "ID"},
{Name: "Site"},
{Name: "Domain"},
{Name: ""},
{Name: ""},
{Name: ""},
{Name: ""},
},
Rows: rows,
PageNumber: 1,
PageDisplayCount: 10,
T: br.Renderer.T,
}

pageData := structs.PageData{
Template: "admin.sites",
Title: "Admin",
Extra: extra,
Components: []page.Component{
table,
},
}

br.Renderer.RenderHTMLPage(w, r, pageData)
Expand Down
1 change: 0 additions & 1 deletion pkg/routes/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (br *BeuboRouter) SiteAdmin(w http.ResponseWriter, r *http.Request) {
pagesRes[pid]["id"] = pid
pagesRes[pid]["title"] = page.Title
pagesRes[pid]["slug"] = page.Slug

}
extra["pagesRes"] = pagesRes

Expand Down
30 changes: 30 additions & 0 deletions pkg/structs/page/component.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package page

import (
"bytes"
"fmt"
"html/template"
"log"
)

// Component is anything that can be rendered on a page. A text field is a component but so is the form the text field is a part of.
type Component interface {
GetSection() string
// render returns a html template string with the content of the field
Render() string
GetTemplateName() string
GetTheme() string
GetTemplate() *template.Template
}

func RenderCompnent(c Component) string {
path := fmt.Sprintf("%s.%s", c.GetTheme(), c.GetTemplateName())
var foundTemplate *template.Template
if c.GetTemplate() == nil {
return ""
}
if foundTemplate = c.GetTemplate().Lookup(path); foundTemplate == nil {
log.Printf("Component file not found %s\n", path)
return ""
}
buf := &bytes.Buffer{}
err := foundTemplate.Execute(buf, c)
if err != nil {
log.Printf("Component file error executing template %s\n", path)
return ""
}
return buf.String()
}
34 changes: 34 additions & 0 deletions pkg/structs/page/component/button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package component

import (
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
)

type Button struct {
Theme string
Template string
Class string
Content string
T *template.Template
}

func (b Button) GetSection() string {
return ""
}

func (b Button) GetTemplateName() string {
return returnTIfNotEmpty(b.Template, "component.button")
}

func (b Button) GetTheme() string {
return returnTIfNotEmpty(b.Theme, "default")
}

func (b Button) GetTemplate() *template.Template {
return b.T
}

func (b Button) Render() string {
return page.RenderCompnent(b)
}
38 changes: 38 additions & 0 deletions pkg/structs/page/component/checkboxfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package component

import (
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
)

type CheckBoxField struct {
Theme string
Template string
Content string
Class string
Name string
Identifier string
Value string
Checked bool
T *template.Template
}

func (cb CheckBoxField) GetSection() string {
return ""
}

func (cb CheckBoxField) GetTemplateName() string {
return returnTIfNotEmpty(cb.Template, "component.checkboxfield")
}

func (cb CheckBoxField) GetTheme() string {
return returnTIfNotEmpty(cb.Template, "default")
}

func (cb CheckBoxField) GetTemplate() *template.Template {
return cb.T
}

func (cb CheckBoxField) Render() string {
return page.RenderCompnent(cb)
}
39 changes: 14 additions & 25 deletions pkg/structs/page/component/form.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package component

import (
"bytes"
"fmt"
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
"log"
)

type Form struct {
Section string
Fields []page.Field
Fields []page.Component
Theme string
Template string
T *template.Template
Expand All @@ -22,26 +19,18 @@ func (f Form) GetSection() string {
return f.Section
}

func (f Form) GetTemplateName() string {
return returnTIfNotEmpty(f.Template, "component.form")
}

func (f Form) GetTheme() string {
return returnTIfNotEmpty(f.Template, "default")
}

func (f Form) GetTemplate() *template.Template {
return f.T
}

func (f Form) Render() string {
tmpl := "component.form"
if f.Template != "" {
tmpl = f.Template
}
theme := "default"
if f.Theme != "" {
theme = f.Theme
}
path := fmt.Sprintf("%s.%s", theme, tmpl)
var foundTemplate *template.Template
if foundTemplate = f.T.Lookup(path); foundTemplate == nil {
log.Printf("Component file not found %s\n", path)
return ""
}
buf := &bytes.Buffer{}
err := foundTemplate.Execute(buf, f)
if err != nil {
log.Printf("Component file error executing template %s\n", path)
return ""
}
return buf.String()
return page.RenderCompnent(f)
}
8 changes: 8 additions & 0 deletions pkg/structs/page/component/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package component

func returnTIfNotEmpty(t string, d string) string {
if t != "" {
return t
}
return d
}
35 changes: 35 additions & 0 deletions pkg/structs/page/component/hiddenfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package component

import (
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
)

type HiddenField struct {
Theme string
Template string
Identifier string
Name string
Value string
T *template.Template
}

func (hf HiddenField) GetSection() string {
return ""
}

func (hf HiddenField) GetTemplateName() string {
return returnTIfNotEmpty(hf.Template, "component.hiddenfield")
}

func (hf HiddenField) GetTheme() string {
return returnTIfNotEmpty(hf.Template, "default")
}

func (hf HiddenField) GetTemplate() *template.Template {
return hf.T
}

func (hf HiddenField) Render() string {
return page.RenderCompnent(hf)
}
38 changes: 38 additions & 0 deletions pkg/structs/page/component/radiofield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package component

import (
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
)

type RadioField struct {
Theme string
Template string
Class string
Identifier string
Name string
Value string
Content string
Checked bool
T *template.Template
}

func (rf RadioField) GetSection() string {
return ""
}

func (rf RadioField) GetTemplateName() string {
return returnTIfNotEmpty(rf.Template, "component.radiofield")
}

func (rf RadioField) GetTheme() string {
return returnTIfNotEmpty(rf.Template, "default")
}

func (rf RadioField) GetTemplate() *template.Template {
return rf.T
}

func (rf RadioField) Render() string {
return page.RenderCompnent(rf)
}
41 changes: 41 additions & 0 deletions pkg/structs/page/component/selectfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package component

import (
"github.com/markustenghamn/beubo/pkg/structs/page"
"html/template"
)

type SelectField struct {
Theme string
Template string
Identifier string
Class string
Name string
Options []SelectFieldOption
T *template.Template
}

type SelectFieldOption struct {
Value string
Content string
}

func (sf SelectField) GetSection() string {
return ""
}

func (sf SelectField) GetTemplateName() string {
return returnTIfNotEmpty(sf.Template, "component.selectfield")
}

func (sf SelectField) GetTheme() string {
return returnTIfNotEmpty(sf.Template, "default")
}

func (sf SelectField) GetTemplate() *template.Template {
return sf.T
}

func (sf SelectField) Render() string {
return page.RenderCompnent(sf)
}
Loading

0 comments on commit 08aecdb

Please sign in to comment.