diff --git a/pkg/routes/admin.go b/pkg/routes/admin.go index 9eb3183..94c1e71 100644 --- a/pkg/routes/admin.go +++ b/pkg/routes/admin.go @@ -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" ) @@ -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) diff --git a/pkg/routes/site.go b/pkg/routes/site.go index ddf560c..67ec06d 100644 --- a/pkg/routes/site.go +++ b/pkg/routes/site.go @@ -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 diff --git a/pkg/structs/page/component.go b/pkg/structs/page/component.go index a4bd886..302fff2 100644 --- a/pkg/structs/page/component.go +++ b/pkg/structs/page/component.go @@ -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() } diff --git a/pkg/structs/page/component/button.go b/pkg/structs/page/component/button.go new file mode 100644 index 0000000..57e0181 --- /dev/null +++ b/pkg/structs/page/component/button.go @@ -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) +} diff --git a/pkg/structs/page/component/checkboxfield.go b/pkg/structs/page/component/checkboxfield.go new file mode 100644 index 0000000..6e73d06 --- /dev/null +++ b/pkg/structs/page/component/checkboxfield.go @@ -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) +} diff --git a/pkg/structs/page/component/form.go b/pkg/structs/page/component/form.go index 8f7792a..deb83db 100644 --- a/pkg/structs/page/component/form.go +++ b/pkg/structs/page/component/form.go @@ -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 @@ -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) } diff --git a/pkg/structs/page/component/functions.go b/pkg/structs/page/component/functions.go new file mode 100644 index 0000000..57899e1 --- /dev/null +++ b/pkg/structs/page/component/functions.go @@ -0,0 +1,8 @@ +package component + +func returnTIfNotEmpty(t string, d string) string { + if t != "" { + return t + } + return d +} diff --git a/pkg/structs/page/component/hiddenfield.go b/pkg/structs/page/component/hiddenfield.go new file mode 100644 index 0000000..388eafc --- /dev/null +++ b/pkg/structs/page/component/hiddenfield.go @@ -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) +} diff --git a/pkg/structs/page/component/radiofield.go b/pkg/structs/page/component/radiofield.go new file mode 100644 index 0000000..95aab96 --- /dev/null +++ b/pkg/structs/page/component/radiofield.go @@ -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) +} diff --git a/pkg/structs/page/component/selectfield.go b/pkg/structs/page/component/selectfield.go new file mode 100644 index 0000000..f9ff5a6 --- /dev/null +++ b/pkg/structs/page/component/selectfield.go @@ -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) +} diff --git a/pkg/structs/page/component/table.go b/pkg/structs/page/component/table.go index aa0eee9..2e15344 100644 --- a/pkg/structs/page/component/table.go +++ b/pkg/structs/page/component/table.go @@ -1,11 +1,8 @@ package component import ( - "bytes" - "fmt" "github.com/markustenghamn/beubo/pkg/structs/page" "html/template" - "log" ) type Table struct { @@ -29,34 +26,26 @@ type Row struct { type Column struct { Name string - Field page.Field + Field page.Component Value string } +func (t Table) GetTemplateName() string { + return returnTIfNotEmpty(t.Template, "component.template") +} + +func (t Table) GetTheme() string { + return returnTIfNotEmpty(t.Template, "default") +} + +func (t Table) GetTemplate() *template.Template { + return t.T +} + func (t Table) Render() string { - tmpl := "component.table" - if t.Template != "" { - tmpl = t.Template - } - theme := "default" - if t.Theme != "" { - theme = t.Theme - } - path := fmt.Sprintf("%s.%s", theme, tmpl) - var foundTemplate *template.Template - if foundTemplate = t.T.Lookup(path); foundTemplate == nil { - log.Printf("Component file not found %s\n", path) - return "" - } - buf := &bytes.Buffer{} - err := foundTemplate.Execute(buf, t) - if err != nil { - log.Printf("Component file error executing template %s\n", path) - return "" - } - return buf.String() -} - -func (c Column) RenderField(value string, field page.Field) { + return page.RenderCompnent(t) +} +func (t Table) RenderColumn(c Column) string { + return page.RenderCompnent(c.Field) } diff --git a/pkg/structs/page/component/text.go b/pkg/structs/page/component/text.go index 61e0e72..112a1e8 100644 --- a/pkg/structs/page/component/text.go +++ b/pkg/structs/page/component/text.go @@ -1,10 +1,8 @@ package component import ( - "bytes" - "fmt" + "github.com/markustenghamn/beubo/pkg/structs/page" "html/template" - "log" ) type Text struct { @@ -20,26 +18,18 @@ func (t Text) GetSection() string { return t.Section } +func (t Text) GetTemplateName() string { + return returnTIfNotEmpty(t.Template, "component.text") +} + +func (t Text) GetTheme() string { + return returnTIfNotEmpty(t.Template, "default") +} + +func (t Text) GetTemplate() *template.Template { + return t.T +} + func (t Text) Render() string { - tmpl := "component.text" - if t.Template != "" { - tmpl = t.Template - } - theme := "default" - if t.Theme != "" { - theme = t.Theme - } - path := fmt.Sprintf("%s.%s", theme, tmpl) - var foundTemplate *template.Template - if foundTemplate = t.T.Lookup(path); foundTemplate == nil { - log.Printf("Component file not found %s\n", path) - return "" - } - buf := &bytes.Buffer{} - err := foundTemplate.Execute(buf, t) - if err != nil { - log.Printf("Component file error executing template %s\n", path) - return "" - } - return buf.String() + return page.RenderCompnent(t) } diff --git a/pkg/structs/page/component/textareafield.go b/pkg/structs/page/component/textareafield.go new file mode 100644 index 0000000..ec7d787 --- /dev/null +++ b/pkg/structs/page/component/textareafield.go @@ -0,0 +1,39 @@ +package component + +import ( + "github.com/markustenghamn/beubo/pkg/structs/page" + "html/template" +) + +type TextAreaField struct { + Content string + Theme string + Template string + Class string + Identifier string + Label string + Name string + Rows int + Cols int + T *template.Template +} + +func (t TextAreaField) GetSection() string { + return "" +} + +func (t TextAreaField) GetTemplateName() string { + return returnTIfNotEmpty(t.Template, "component.textareafield") +} + +func (t TextAreaField) GetTheme() string { + return returnTIfNotEmpty(t.Template, "default") +} + +func (t TextAreaField) GetTemplate() *template.Template { + return t.T +} + +func (t TextAreaField) Render() string { + return page.RenderCompnent(t) +} diff --git a/pkg/structs/page/component/textfield.go b/pkg/structs/page/component/textfield.go new file mode 100644 index 0000000..370434d --- /dev/null +++ b/pkg/structs/page/component/textfield.go @@ -0,0 +1,38 @@ +package component + +import ( + "github.com/markustenghamn/beubo/pkg/structs/page" + "html/template" +) + +type TextField struct { + Theme string + Template string + Class string + Identifier string + Label string + Name string + Value string + Placeholder string + T *template.Template +} + +func (t TextField) GetSection() string { + return "" +} + +func (t TextField) GetTemplateName() string { + return returnTIfNotEmpty(t.Template, "component.textfield") +} + +func (t TextField) GetTheme() string { + return returnTIfNotEmpty(t.Template, "default") +} + +func (t TextField) GetTemplate() *template.Template { + return t.T +} + +func (t TextField) Render() string { + return page.RenderCompnent(t) +} diff --git a/pkg/structs/page/field.go b/pkg/structs/page/field.go deleted file mode 100644 index e28a580..0000000 --- a/pkg/structs/page/field.go +++ /dev/null @@ -1,6 +0,0 @@ -package page - -type Field interface { - // render returns a html template string with the content of the field - Render() string -} diff --git a/pkg/structs/page/field/button.go b/pkg/structs/page/field/button.go deleted file mode 100644 index 3a74b72..0000000 --- a/pkg/structs/page/field/button.go +++ /dev/null @@ -1,9 +0,0 @@ -package field - -type Button struct { - Template string -} - -func (b Button) render() string { - -} diff --git a/pkg/structs/page/field/select.go b/pkg/structs/page/field/select.go deleted file mode 100644 index 5229f47..0000000 --- a/pkg/structs/page/field/select.go +++ /dev/null @@ -1 +0,0 @@ -package field diff --git a/pkg/structs/page/field/textarea.go b/pkg/structs/page/field/textarea.go deleted file mode 100644 index 5229f47..0000000 --- a/pkg/structs/page/field/textarea.go +++ /dev/null @@ -1 +0,0 @@ -package field diff --git a/pkg/structs/page/field/textfield.go b/pkg/structs/page/field/textfield.go deleted file mode 100644 index 5229f47..0000000 --- a/pkg/structs/page/field/textfield.go +++ /dev/null @@ -1 +0,0 @@ -package field diff --git a/themes/default b/themes/default index 324d97e..21780f2 160000 --- a/themes/default +++ b/themes/default @@ -1 +1 @@ -Subproject commit 324d97e12f18b9066b240f805f88fc13969c61d5 +Subproject commit 21780f25c9187508d1199b2d98756af040a4227a