-
Notifications
You must be signed in to change notification settings - Fork 6
/
proxy.go
143 lines (124 loc) · 3.99 KB
/
proxy.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package control
import (
"github.com/goradd/goradd/pkg/javascript"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/event"
"bytes"
"github.com/goradd/goradd/pkg/html"
"fmt"
"context"
html2 "html"
)
type ProxyI interface {
page.ControlI
LinkHtml(label string,
actionValue string,
attributes *html.Attributes,
) string
TagHtml(label string,
actionValue string,
attributes *html.Attributes,
tag string,
rawHtml bool,
) string
ButtonHtml(label string,
eventActionValue string,
attributes *html.Attributes,
rawHtml bool,
) string
OnSubmit(actions ...action.ActionI) page.EventI
}
type Proxy struct {
page.Control
}
func NewProxy(parent page.ControlI) *Proxy {
p := Proxy{}
p.Init(parent)
return &p
}
func (p *Proxy) Init(parent page.ControlI) {
p.Control.Init(p, parent, "")
p.SetShouldAutoRender(true)
p.SetActionValue(javascript.JsCode(`$j(this).data("grAv")`))
}
func (p *Proxy) this() ProxyI {
return p.Self.(ProxyI)
}
// OnSubmit is a shortcut for adding a click event handler that is particular to buttons. It debounces the click, to
// prevent potential accidental multiple form submissions. All events fired after this event fires will be lost. It is
// intended to be used when the action will result in navigating to a new page.
func (p *Proxy) OnSubmit(actions ...action.ActionI) page.EventI {
return p.On(event.Click().Terminating().Delay(250), actions...)
}
// Draw is used by the form engine to draw the control. As a proxy, there is no html to draw, but this is where the scripts attached to the
// proxy get sent to the response. This should get drawn by the auto-drawing routine, since proxies are not rendered in templates.
func (p *Proxy) Draw(ctx context.Context, buf *bytes.Buffer) (err error) {
response := p.ParentForm().Response()
// p.this().PutCustomScript(ctx, response) // Proxies should not have custom scripts?
p.GetActionScripts(response)
p.PostRender(ctx, buf)
return
}
// LinkHtml renders the proxy as a link. Generally, only do this if you are actually linking to a page. If not, use
// a button.
func (p *Proxy) LinkHtml(label string,
actionValue string,
attributes *html.Attributes,
) string {
if attributes == nil {
attributes = html.NewAttributes()
}
if !attributes.Has("href") {
attributes.Set("href", "javascript:;")
}
return p.TagHtml(label, actionValue, attributes, "a", false)
}
// TagHtml lets you customize the tag that will be used to embed the proxy.
func (p *Proxy) TagHtml(label string,
actionValue string,
attributes *html.Attributes,
tag string,
rawHtml bool,
) string {
a := html.NewAttributes()
a.SetDataAttribute("grProxy", p.ID())
if actionValue != "" {
a.SetDataAttribute("grAv", actionValue)
}
if attributes != nil {
a.Merge(attributes) // will only apply defaults that are not in attributes
}
if !rawHtml {
label = html2.EscapeString(label)
}
return html.RenderTagNoSpace(tag, a, label)
}
// ButtonHtml outputs the proxy as a button tag.
// eventActionValue becomes the event's value parameter
func (p *Proxy) ButtonHtml(label string,
eventActionValue string,
attributes *html.Attributes,
rawHtml bool,
) string {
a := html.NewAttributes()
a.Set("onclick", "return false")
a.Set("type", "button")
if attributes != nil {
a.Merge(attributes)
}
return p.TagHtml(label, eventActionValue, a, "button", rawHtml)
}
// Attributes returns attributes that can be included in any tag to attach a proxy to the tag.
func (p *Proxy) ActionAttributes(actionValue string) *html.Attributes {
a := html.NewAttributes()
a.SetDataAttribute("grProxy", p.ID())
if actionValue != "" {
a.SetDataAttribute("grAv", actionValue)
}
return a
}
// WrapEvent is an internal function to allow the control to customize its treatment of event processing.
func (p *Proxy) WrapEvent(eventName string, selector string, eventJs string) string {
return fmt.Sprintf(`$j('#%s').on('%s', '[data-gr-proxy="%s"]', function(event, ui){%s});`, p.ParentForm().ID(), eventName, p.ID(), eventJs)
}