From 5d2d29a61d8a472171c832f65cf7132b2b2e8d16 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sat, 21 Mar 2020 22:16:30 +0500 Subject: [PATCH] Default classes for implemented elements. --- elements/button.go | 14 ++++++++++++++ elements/nav.go | 13 +++++++++++++ elements/section.go | 17 +++++++++++++++++ examples/helloworld/main.go | 4 ++-- metas/generic.go | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/elements/button.go b/elements/button.go index 4f3faef..3e0b403 100644 --- a/elements/button.go +++ b/elements/button.go @@ -1,12 +1,18 @@ package elements import ( + "strings" + "github.com/gopherjs/gopherjs/js" "go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/metas" ) +const ( + buttonDefaultClass = "button" +) + // ButtonOptions is a "button" HTML element configuration structure. type ButtonOptions struct { Class string @@ -35,9 +41,17 @@ func (b *Button) Build() *js.Object { b.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementButton) if b.options == nil { + b.AddClassesFromString(buttonDefaultClass) + b.BuildChilds(b.Object) + return b.Object } + // Button should always have "button" class. + if !strings.Contains(b.options.Class, "button") { + b.options.Class += " button" + } + if b.options.Class != "" { b.AddClassesFromString(b.options.Class) } diff --git a/elements/nav.go b/elements/nav.go index d81ea48..864c6d3 100644 --- a/elements/nav.go +++ b/elements/nav.go @@ -1,12 +1,18 @@ package elements import ( + "strings" + "github.com/gopherjs/gopherjs/js" "go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/metas" ) +const ( + navDefaultClass = "navbar" +) + // NavOptions is a "nav" HTML element configuration structure. type NavOptions struct { Class string @@ -33,9 +39,16 @@ func (n *Nav) Build() *js.Object { n.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementNav) if n.options == nil { + n.AddClassesFromString(navDefaultClass) + n.BuildChilds(n.Object) + return n.Object } + if !strings.Contains(n.options.Class, "navbar") { + n.options.Class += " navbar" + } + if n.options.Class != "" { n.AddClassesFromString(n.options.Class) } diff --git a/elements/section.go b/elements/section.go index cf8a1de..ed53550 100644 --- a/elements/section.go +++ b/elements/section.go @@ -1,12 +1,18 @@ package elements import ( + "strings" + "github.com/gopherjs/gopherjs/js" "go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/metas" ) +const ( + sectionDefaultClass = "section" +) + // SectionOptions is a "section" HTML element configuration structure. type SectionOptions struct { Class string @@ -31,6 +37,17 @@ func NewSection(sectionOptions *SectionOptions) *Section { func (s *Section) Build() *js.Object { s.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementSection) + if s.options == nil { + s.AddClassesFromString(sectionDefaultClass) + s.BuildChilds(s.Object) + + return s.Object + } + + if !strings.Contains(s.options.Class, "section") { + s.options.Class += " section" + } + if s.options.Class != "" { s.AddClassesFromString(s.options.Class) } diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 58d8c7d..8437b0f 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -18,7 +18,7 @@ func main() { } func constructBody() { - mainSection := elements.NewSection(&elements.SectionOptions{Class: "section"}) + mainSection := elements.NewSection(nil) mainDiv := elements.NewDiv(&elements.DivOptions{ Class: "container", @@ -40,7 +40,7 @@ func constructBody() { columnsDiv.AddChild(centerDiv) startTestButton := elements.NewButton(&elements.ButtonOptions{ - Class: "button is-large is-rounded is-primary", + Class: "is-large is-rounded is-primary", ID: "main_button", Text: "Click Me!", OnClickHandler: func(e *js.Object) { diff --git a/metas/generic.go b/metas/generic.go index 328c873..141e7e9 100644 --- a/metas/generic.go +++ b/metas/generic.go @@ -20,7 +20,7 @@ type Generic struct { // AddClassesFromString adds classes to object from string. Classes // should be specified like "class1 class2 ... classN". func (g *Generic) AddClassesFromString(classes string) { - classesSplitted := strings.Split(classes, " ") + classesSplitted := strings.Split(strings.Trim(classes, " "), " ") for _, class := range classesSplitted { g.Object.Get(common.HTMLElementParameterClassList).Call(common.JSCallAdd, class)