-
Notifications
You must be signed in to change notification settings - Fork 6
/
tabs.go
62 lines (50 loc) · 1.72 KB
/
tabs.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
package control
import (
"context"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/javascript"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/page/event"
)
type TabsI interface {
control.PanelI
}
// A Tabs pane draws its child controls as a set of tabs. The labels of the children serve as the tab labels.
// This currently draws everything at once, with the current panel visible, but everything else has hidden html.
type Tabs struct {
control.Panel
selectedID string // selected child id
}
// TODO: Modify this so that you can optionally show each panel through ajax
func NewTabs(parent page.ControlI, id string) *Tabs {
t := &Tabs{}
t.Init(t, parent, id)
return t
}
func (l *Tabs) Init(self page.ControlI, parent page.ControlI, id string) {
l.Panel.Init(self, parent, id)
l.On(event.Event("show.bs.tab"), action.SetControlValue(l.ID(), "selectedId", javascript.JsCode("event.target.id")))
}
func (c *Tabs) ΩDrawingAttributes(ctx context.Context) html.Attributes {
a := c.Panel.ΩDrawingAttributes(ctx)
a.SetDataAttribute("grctl", "bs-tabs")
return a
}
type TabsCreator struct {
// ID is the control id of the html widget and must be unique to the page
ID string
page.ControlOptions
Children []page.Creator
}
func (c TabsCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewTabs(parent, c.ID)
ctrl.ApplyOptions(c.ControlOptions)
ctrl.AddControls(ctx, c.Children...)
return ctrl
}
// GetTabs is a convenience method to return the control with the given id from the page.
func GetTabs(c page.ControlI, id string) *Tabs {
return c.Page().GetControl(id).(*Tabs)
}