-
Notifications
You must be signed in to change notification settings - Fork 6
/
panel.go
89 lines (73 loc) · 2.43 KB
/
panel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package control
import (
"context"
"fmt"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
)
// Children is just a helper function for doing declarative control creation for child control creators
func Children(creators ...page.Creator) []page.Creator {
return creators
}
type PanelI interface {
page.ControlI
}
// Panel is a Goradd control that is a basic "div" wrapper. Use it to style and listen to events on a div. It
// can also be used as the basis for more advanced javascript controls.
type Panel struct {
page.Control
}
func NewPanel(parent page.ControlI, id string) *Panel {
p := &Panel{}
p.Init(p, parent, id)
return p
}
func (c *Panel) Init(self PanelI, parent page.ControlI, id string) {
c.Control.Init(self, parent, id)
c.Tag = "div"
}
func (c *Panel) this() PanelI {
return c.Self.(PanelI)
}
func (c *Panel) ΩDrawingAttributes() html.Attributes {
a := c.Control.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "panel")
return a
}
// Value satisfies the Valuer interface and returns the text of the panel.
func (c *Panel) Value() interface{} {
return c.Text()
}
// SetValue satisfies the Valuer interface and sets the text of the panel.
func (c *Panel) SetValue(v interface{}) page.ControlI {
return c.SetText(fmt.Sprintf("%v", v))
}
// PanelCreator creates a div control with child controls.
// Pass it to AddControls or as a child of a parent control.
type PanelCreator struct {
// ID is the id the tag will have on the page and must be unique on the page
ID string
// Text is text that will become the innerhtml part of the tag
Text string
// If you set TextIsHtml, the Text will not be escaped prior to drawing
TextIsHtml bool
// Children is a list of creators to use to create the child controls of the panel.
// You can wrap your child creators with the Children() function as a helper.
Children []page.Creator
page.ControlOptions
}
// Create is called by the framework to create the panel. You do not normally need to call this.
func (c PanelCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewPanel(parent, c.ID)
if c.Text != "" {
ctrl.SetText(c.Text)
}
ctrl.SetTextIsHtml(c.TextIsHtml)
ctrl.ApplyOptions(c.ControlOptions)
ctrl.AddControls(ctx, c.Children...)
return ctrl
}
// GetPanel is a convenience method to return the panel with the given id from the page.
func GetPanel(c page.ControlI, id string) *Panel {
return c.Page().GetControl(id).(*Panel)
}