-
Notifications
You must be signed in to change notification settings - Fork 11
/
popup.go
105 lines (82 loc) · 2.16 KB
/
popup.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
package popup
import (
"fmt"
"reflect"
"strings"
"qlova.org/seed"
"qlova.org/seed/client"
"qlova.org/seed/use/css"
"qlova.org/seed/use/html"
"qlova.org/seed/use/js"
"qlova.org/seed/new/html/div"
"qlova.org/seed/set"
"qlova.org/seed/set/transition"
"qlova.org/seed/use/css/units/percentage/of"
)
func ID(p Popup) string {
return strings.Replace(reflect.TypeOf(p).String(), ".", "_", -1)
}
type Popup interface {
Popup() seed.Seed
}
//New returns a new popup seed.
func New(options ...seed.Option) seed.Seed {
var Popup = div.New(html.SetTag("div"),
css.SetDisplay(css.Flex),
css.SetFlexDirection(css.Column),
set.Size(100%of.Parent, 100%of.Parent),
set.Layer(1),
css.SetPosition(css.Absolute),
transition.SetOnEnter(OnShow),
transition.SetOnExit(OnHide),
)
for _, option := range options {
option.AddTo(Popup)
}
return Popup
}
type data struct {
popups map[reflect.Type]Popup
}
//Show shows the provided popup.
func Show(p Popup) js.Script {
return func(q js.Ctx) {
//Sort out script arguments of the page.
popup, args := parseArgs(p)
q(seed.Mutate(func(d *data) {
if d.popups == nil {
d.popups = make(map[reflect.Type]Popup)
}
d.popups[reflect.TypeOf(p)] = popup
}))
fmt.Fprintf(q, `seed.show("%v", %v);`, ID(p), args.GetObject().String())
}
}
//Wrap shows the provided popup while the provided script is running.
func Wrap(p Popup, s ...client.Script) js.Script {
return func(q js.Ctx) {
//Sort out script arguments of the page.
popup, args := parseArgs(p)
q(seed.Mutate(func(d *data) {
if d.popups == nil {
d.popups = make(map[reflect.Type]Popup)
}
d.popups[reflect.TypeOf(p)] = popup
}))
fmt.Fprintf(q, `seed.show("%v", %v); try {`, ID(p), args.GetObject().String())
client.NewScript(s...).GetScript()(q)
fmt.Fprintf(q, `seed.hide("%[1]v"); } catch(e) { seed.hide("%[1]v"); throw e; }`, ID(p))
}
}
//Hide hides the provided popup.
func Hide(p Popup) js.Script {
return func(q js.Ctx) {
fmt.Fprintf(q, `seed.hide("%v");`, ID(p))
}
}
func OnShow(f ...client.Script) seed.Option {
return client.On("show", f...)
}
func OnHide(f ...client.Script) seed.Option {
return client.On("hide", f...)
}