-
Notifications
You must be signed in to change notification settings - Fork 11
/
view.go
121 lines (95 loc) · 2.42 KB
/
view.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
110
111
112
113
114
115
116
117
118
119
120
121
//Package view provides local view that can be swapped in and out.
package view
import (
"reflect"
"strings"
"qlova.org/seed"
"qlova.org/seed/client"
"qlova.org/seed/client/clientside"
"qlova.org/seed/use/css"
"qlova.org/seed/use/html"
"qlova.org/seed/use/js"
"qlova.org/seed/set"
"qlova.org/seed/set/transition"
"qlova.org/seed/use/css/units/percentage/of"
)
func Name(view View) string {
return strings.Replace(reflect.TypeOf(view).String(), ".", "_", -1)
}
//Controller is responsible for showing the current view.
type Controller struct {
of seed.Seed
}
//ControllerOf returns the Controller for the given seed.
func ControllerOf(c seed.Seed) Controller {
return Controller{c}
}
func (c Controller) Seed() seed.Seed {
return c.of
}
//Is returns true if the given view is the current view of the given controller..
func (c Controller) Is(v View) *clientside.Bool {
return &clientside.Bool{
Name: client.ID(c.of) + ".view." + Name(v),
}
}
//Goto returns a script that goes to the given view.
func (c Controller) Goto(view View) js.Script {
//Sort out script arguments of the page.
view, args := parseArgs(view, c.of)
var data data
c.of.Load(&data)
var key = reflect.TypeOf(view)
if _, ok := data.views[key]; !ok {
if data.views == nil {
data.views = make(map[reflect.Type]bool)
c.of.Save(data)
}
data.views[reflect.TypeOf(view)] = true
var template = seed.New(
html.SetTag("template"),
)
template.Use()
template.AddTo(c.of)
var element = view.View(c)
element.With(
html.AddClass(Name(view)),
)
element.Use()
element.AddTo(template)
}
c.of.Save(data)
var seed_view = js.Function{js.NewValue(`seed.view`)}
return func(q js.Ctx) {
q.Run(seed_view, html.Element(c.of), js.NewString(Name(view)), args)
}
}
//View is a local view.
type View interface {
View(Controller) seed.Seed
}
func New(options ...seed.Option) seed.Seed {
return seed.New(
html.SetTag("div"),
set.Size(100%of.Parent, 100%of.Parent),
css.SetDisplay(css.Flex),
css.SetFlexDirection(css.Column),
seed.NewOption(func(c seed.Seed) {
c.With(
OnEnter(clientside.Render(c)),
)
}),
transition.SetOnEnter(OnEnter),
transition.SetOnExit(OnExit),
seed.Options(options),
)
}
type data struct {
views map[reflect.Type]bool
}
func OnEnter(f ...client.Script) seed.Option {
return client.On("viewenter", f...)
}
func OnExit(f ...client.Script) seed.Option {
return client.On("viewexit", f...)
}