Default classes for implemented elements.

This commit is contained in:
Stanislav Nikitin 2020-03-21 22:16:30 +05:00
parent cc5c1a3e20
commit 5d2d29a61d
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
5 changed files with 47 additions and 3 deletions

View File

@ -1,12 +1,18 @@
package elements package elements
import ( import (
"strings"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/common"
"go.dev.pztrn.name/bulpherjs/metas" "go.dev.pztrn.name/bulpherjs/metas"
) )
const (
buttonDefaultClass = "button"
)
// ButtonOptions is a "button" HTML element configuration structure. // ButtonOptions is a "button" HTML element configuration structure.
type ButtonOptions struct { type ButtonOptions struct {
Class string Class string
@ -35,9 +41,17 @@ func (b *Button) Build() *js.Object {
b.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementButton) b.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementButton)
if b.options == nil { if b.options == nil {
b.AddClassesFromString(buttonDefaultClass)
b.BuildChilds(b.Object)
return 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 != "" { if b.options.Class != "" {
b.AddClassesFromString(b.options.Class) b.AddClassesFromString(b.options.Class)
} }

View File

@ -1,12 +1,18 @@
package elements package elements
import ( import (
"strings"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/common"
"go.dev.pztrn.name/bulpherjs/metas" "go.dev.pztrn.name/bulpherjs/metas"
) )
const (
navDefaultClass = "navbar"
)
// NavOptions is a "nav" HTML element configuration structure. // NavOptions is a "nav" HTML element configuration structure.
type NavOptions struct { type NavOptions struct {
Class string Class string
@ -33,9 +39,16 @@ func (n *Nav) Build() *js.Object {
n.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementNav) n.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementNav)
if n.options == nil { if n.options == nil {
n.AddClassesFromString(navDefaultClass)
n.BuildChilds(n.Object)
return n.Object return n.Object
} }
if !strings.Contains(n.options.Class, "navbar") {
n.options.Class += " navbar"
}
if n.options.Class != "" { if n.options.Class != "" {
n.AddClassesFromString(n.options.Class) n.AddClassesFromString(n.options.Class)
} }

View File

@ -1,12 +1,18 @@
package elements package elements
import ( import (
"strings"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"go.dev.pztrn.name/bulpherjs/common" "go.dev.pztrn.name/bulpherjs/common"
"go.dev.pztrn.name/bulpherjs/metas" "go.dev.pztrn.name/bulpherjs/metas"
) )
const (
sectionDefaultClass = "section"
)
// SectionOptions is a "section" HTML element configuration structure. // SectionOptions is a "section" HTML element configuration structure.
type SectionOptions struct { type SectionOptions struct {
Class string Class string
@ -31,6 +37,17 @@ func NewSection(sectionOptions *SectionOptions) *Section {
func (s *Section) Build() *js.Object { func (s *Section) Build() *js.Object {
s.Object = js.Global.Get(common.HTMLElementDocument).Call(common.JSCallCreateElement, common.HTMLElementSection) 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 != "" { if s.options.Class != "" {
s.AddClassesFromString(s.options.Class) s.AddClassesFromString(s.options.Class)
} }

View File

@ -18,7 +18,7 @@ func main() {
} }
func constructBody() { func constructBody() {
mainSection := elements.NewSection(&elements.SectionOptions{Class: "section"}) mainSection := elements.NewSection(nil)
mainDiv := elements.NewDiv(&elements.DivOptions{ mainDiv := elements.NewDiv(&elements.DivOptions{
Class: "container", Class: "container",
@ -40,7 +40,7 @@ func constructBody() {
columnsDiv.AddChild(centerDiv) columnsDiv.AddChild(centerDiv)
startTestButton := elements.NewButton(&elements.ButtonOptions{ startTestButton := elements.NewButton(&elements.ButtonOptions{
Class: "button is-large is-rounded is-primary", Class: "is-large is-rounded is-primary",
ID: "main_button", ID: "main_button",
Text: "Click Me!", Text: "Click Me!",
OnClickHandler: func(e *js.Object) { OnClickHandler: func(e *js.Object) {

View File

@ -20,7 +20,7 @@ type Generic struct {
// AddClassesFromString adds classes to object from string. Classes // AddClassesFromString adds classes to object from string. Classes
// should be specified like "class1 class2 ... classN". // should be specified like "class1 class2 ... classN".
func (g *Generic) AddClassesFromString(classes string) { func (g *Generic) AddClassesFromString(classes string) {
classesSplitted := strings.Split(classes, " ") classesSplitted := strings.Split(strings.Trim(classes, " "), " ")
for _, class := range classesSplitted { for _, class := range classesSplitted {
g.Object.Get(common.HTMLElementParameterClassList).Call(common.JSCallAdd, class) g.Object.Get(common.HTMLElementParameterClassList).Call(common.JSCallAdd, class)