-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.go
109 lines (88 loc) · 2.13 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tutorial
import (
"context"
"github.com/goradd/goradd/pkg/page/action"
"strings"
"github.com/goradd/goradd/pkg/page"
. "github.com/goradd/goradd/pkg/page/control"
)
const IndexFormPath = "/goradd/tutorial.g"
const IndexFormId = "IndexForm"
const (
ViewSourceAction = iota + 1
)
type IndexForm struct {
FormBase
currentPageRecord pageRecord
}
type createFunction func(ctx context.Context, parent page.ControlI) page.ControlI
type pageRecord struct {
order int
id string
title string
f createFunction
files []string
}
type pageRecordList []pageRecord
var pages = make(map[string]pageRecordList)
func (p pageRecordList) Less(i, j int) bool {
return p[i].order < p[j].order
}
func NewIndexForm(ctx context.Context) page.FormI {
f := &IndexForm{}
f.Init(ctx, f, IndexFormPath, IndexFormId)
f.AddRelatedFiles()
f.AddControls(ctx,
PanelCreator{
ID:"detailPanel",
},
ButtonCreator{
ID: "viewSourceButton",
Text: "View Source",
OnClick: action.Ajax(f.ID(), ViewSourceAction),
},
)
NewSourcePanel(f, "sourcePanel")
return f
}
func (f *IndexForm) LoadControls(ctx context.Context) {
if pageID, ok := page.GetContext(ctx).FormValue("pageID"); ok {
// pageID is a category and integer id combined
parts := strings.Split(pageID, "-")
if len(parts) != 2 {
return
}
pl, ok := pages[parts[0]]
if !ok {
return
}
id := parts[1]
for _,pr := range pl {
if pr.id == id {
pr.f(ctx, GetPanel(f, "detailPanel"))
f.currentPageRecord = pr
break
}
}
} else {
NewDefaultPanel(ctx, GetPanel(f, "detailPanel"))
}
}
func (f *IndexForm) Action(ctx context.Context, a page.ActionParams) {
switch a.ID {
case ViewSourceAction:
GetSourcePanel(f).show(f.currentPageRecord.files)
}
}
func init() {
page.RegisterPage(IndexFormPath, NewIndexForm, IndexFormId)
}
func RegisterTutorialPage(category string, order int, id string, title string, f createFunction, files []string) {
v, ok := pages[category]
if !ok {
pages[category] = pageRecordList{pageRecord{order, id, title, f, files}}
} else {
v = append(v, pageRecord{order, id, title, f, files})
pages[category] = v
}
}