-
Notifications
You must be signed in to change notification settings - Fork 6
/
dialog_save.go
89 lines (76 loc) · 2.12 KB
/
dialog_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
package control
import (
"context"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
)
const (
saveDlgSaveAction = iota + 10200
)
type SaveablePanel interface {
PanelI
Load(ctx context.Context, pk string) error
Save(ctx context.Context)
Data() interface{}
}
// DialogSavePanel is a dialog panel that pre-loads Save, Cancel and Delete buttons, and treats its one
// child control as an EditablePanel.
type DialogSavePanel struct {
DialogPanel
}
// GetDialogSavePanel 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 GetDialogSavePanel(parent page.ControlI, id string) (*DialogSavePanel, bool) {
if parent.Page().HasControl(id) { // dialog has already been created, but is hidden
return parent.Page().GetControl(id).(*DialogSavePanel), false
}
dlg := NewDialogI(parent.ParentForm(), id+"-dlg")
dp := new(DialogSavePanel)
dp.Self = dp
dp.Init(dlg, id)
return dp, true
}
func (p *DialogSavePanel) Init(dlg page.ControlI, id string) {
p.DialogPanel.Init(dlg, id)
p.AddCloseButton(p.GT("Cancel"), CancelButtonnID)
p.AddButton(p.GT("Save"), SaveButtonID, &DialogButtonOptions{
Validates:true,
IsPrimary:true,
OnClick:action.Ajax(p.ID(), saveDlgSaveAction),
})
}
func (p *DialogSavePanel) 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 *DialogSavePanel) Action(ctx context.Context, a page.ActionParams) {
switch a.ID {
case saveDlgSaveAction:
p.SavePanel().Save(ctx)
p.Hide()
}
}
// EditPanel returns the panel that has the edit controls
func (p *DialogSavePanel) 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(&DialogSavePanel{})
}