-
Notifications
You must be signed in to change notification settings - Fork 6
/
save.go
91 lines (77 loc) · 2.1 KB
/
save.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
90
91
package dialog
import (
"context"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
)
const (
saveDlgSaveAction = iota + 10200
)
type SaveablePanel interface {
control.PanelI
Load(ctx context.Context, pk string) error
Save(ctx context.Context)
Data() interface{}
}
// SavePanel is a dialog panel that pre-loads Save, Cancel and Delete buttons, and treats its one
// child control as an EditablePanel.
type SavePanel struct {
DialogPanel
}
// GetSavePanel creates a panel that is designed to hold an SaveablePanel. It itself will be wrapped with
// the application's default dialog style, and it will automatically get Save, Cancel and Delete buttons.
func GetSavePanel(parent page.ControlI, id string) (*SavePanel, bool) {
if parent.Page().HasControl(id) { // dialog has already been created, but is hidden
return parent.Page().GetControl(id).(*SavePanel), false
}
dlg := NewDialogI(parent.ParentForm(), id+"-dlg")
dp := new(SavePanel)
dp.Self = dp
dp.Init(dlg, id)
return dp, true
}
func (p *SavePanel) Init(dlg page.ControlI, id string) {
p.DialogPanel.Init(dlg, id)
p.AddCloseButton(p.GT("Cancel"), CancelButtonnID)
p.AddButton(p.GT("Save"), SaveButtonID, &ButtonOptions{
Validates: true,
IsPrimary: true,
OnClick: action.Ajax(p.ID(), saveDlgSaveAction),
})
}
func (p *SavePanel) Load(ctx context.Context, pk string) (data interface{}, err error) {
ep := p.SavePanel()
if ep == nil {
panic("the child of a SaveDialog must be an SaveablePanel")
}
err = ep.Load(ctx, pk)
if err != nil {
return
}
data = ep.Data()
return
}
func (p *SavePanel) DoAction(ctx context.Context, a action.Params) {
switch a.ID {
case saveDlgSaveAction:
p.SavePanel().Save(ctx)
p.Hide()
}
}
// SavePanel returns the panel that has the controls
func (p *SavePanel) SavePanel() SaveablePanel {
children := p.Children()
if len(children) == 0 {
return nil
}
for i := len(children) - 1; i >= 0; i-- {
if c, ok := children[i].(SaveablePanel); ok {
return c
}
}
return nil
}
func init() {
page.RegisterControl(&SavePanel{})
}