D3vugu-components/utils/attribute-map.go

41 lines
982 B
Go
Raw Permalink Normal View History

package utils
import "github.com/vugu/vugu"
// AttributesAppend can be used to dynamically add entries to AttrMap attributes.
// This way you can add a class to a component that will not be overwritten by attributes set by the parent.
type AttributesAppend struct {
AttrMap vugu.AttrMap
Classes string
}
// AttributeList returns an attribute list corresponding to this map using the rules from VGNode.AddAttrInterface.
func (a AttributesAppend) AttributeList() (ret []vugu.VGAttribute) {
combinedClasses := a.Classes
if a.AttrMap != nil {
if attrMapClassInt, ok := a.AttrMap["class"]; ok {
if attrMapClass, ok := attrMapClassInt.(string); ok {
if combinedClasses != "" {
combinedClasses += " "
}
combinedClasses += attrMapClass
}
}
}
var n vugu.VGNode
if combinedClasses != "" {
n.AddAttrInterface("class", combinedClasses)
}
for k := range a.AttrMap {
if k != "class" {
n.AddAttrInterface(k, a.AttrMap[k])
}
}
return n.Attr
}