generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
htmx.go
226 lines (187 loc) · 6.05 KB
/
htmx.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Package htmx provides HTMX attributes and helpers for gomponents.
// See https://htmx.org/
package htmx
import (
"io"
g "maragu.dev/gomponents"
)
// Boost to add or remove progressive enhancement for links and forms.
// See https://htmx.org/attributes/hx-boost
func Boost(v string) g.Node {
return attr("boost", v)
}
// Get from the specified URL.
// See https://htmx.org/attributes/hx-get
func Get(url string) g.Node {
return attr("get", url)
}
// On handles any event with a script inline.
// See https://htmx.org/attributes/hx-on
func On(name string, v string) g.Node {
return &rawAttr{name: "on:" + name, value: v}
}
// Post to the specified URL.
// See https://htmx.org/attributes/hx-post
func Post(url string) g.Node {
return attr("post", url)
}
// PushURL into the browser location bar, creating a new history entry.
// See https://htmx.org/attributes/hx-push-url
func PushURL(v string) g.Node {
return attr("push-url", v)
}
// Select content to swap in from a response.
// See https://htmx.org/attributes/hx-select
func Select(v string) g.Node {
return attr("select", v)
}
// SelectOOB content to swap in from a response, out of band (somewhere other than the target).
// See https://htmx.org/attributes/hx-select-oob
func SelectOOB(v string) g.Node {
return attr("select-oob", v)
}
// Swap controls how content is swapped in.
// See https://htmx.org/attributes/hx-swap
func Swap(v string) g.Node {
return attr("swap", v)
}
// SwapOOB marks content in a response to be out of band (should swap in somewhere other than the target).
// See https://htmx.org/attributes/hx-swap-oob
func SwapOOB(v string) g.Node {
return attr("swap-oob", v)
}
// Target specifies the target element to be swapped.
// See https://htmx.org/attributes/hx-target
func Target(v string) g.Node {
return attr("target", v)
}
// Trigger specifies the event that triggers the request.
// See https://htmx.org/attributes/hx-trigger
func Trigger(v string) g.Node {
return attr("trigger", v)
}
// Vals adds values to the parameters to submit with the request (JSON-formatted).
// See https://htmx.org/attributes/hx-vals
func Vals(v string) g.Node {
return attr("vals", v)
}
// Confirm shows a confirm() dialog before issuing a request.
// See https://htmx.org/attributes/hx-confirm
func Confirm(v string) g.Node {
return attr("confirm", v)
}
// Delete will issue a DELETE to the specified URL and swap the HTML into the DOM using a swap strategy.
// See https://htmx.org/attributes/hx-delete
func Delete(v string) g.Node {
return attr("delete", v)
}
// Disable htmx processing for the given node and any children nodes.
// See https://htmx.org/attributes/hx-disable
func Disable(v string) g.Node {
return attr("disable", v)
}
// Disable element until htmx request completes.
// See https://htmx.org/attributes/hx-disabled-elt/
func DisabledElt(v string) g.Node {
return attr("disabled-elt", v)
}
// Disinherit controls and disables automatic attribute inheritance for child nodes.
// See https://htmx.org/attributes/hx-disinherit
func Disinherit(v string) g.Node {
return attr("disinherit", v)
}
// Encoding changes the request encoding type.
// See https://htmx.org/attributes/hx-encoding
func Encoding(v string) g.Node {
return attr("encoding", v)
}
// Ext sets extensions to use for this element.
// See https://htmx.org/attributes/hx-ext
func Ext(v string) g.Node {
return attr("ext", v)
}
// Headers adds to the headers that will be submitted with the request.
// See https://htmx.org/attributes/hx-headers
func Headers(v string) g.Node {
return attr("headers", v)
}
// History prevents sensitive data being saved to the history cache.
// See https://htmx.org/attributes/hx-history
func History(v string) g.Node {
return attr("history", v)
}
// HistoryElt sets the element to snapshot and restore during history navigation.
// See https://htmx.org/attributes/hx-history-elt
func HistoryElt(v string) g.Node {
return attr("history-elt", v)
}
// Include additional data in requests.
// See https://htmx.org/attributes/hx-include
func Include(v string) g.Node {
return attr("include", v)
}
// Indicator sets the element to put the htmx-request class on during the request.
// See https://htmx.org/attributes/hx-indicator
func Indicator(v string) g.Node {
return attr("indicator", v)
}
// Params filters the parameters that will be submitted with a request.
// See https://htmx.org/attributes/hx-params
func Params(v string) g.Node {
return attr("params", v)
}
// Patch issues a PATCH to the specified URL.
// See https://htmx.org/attributes/hx-patch
func Patch(v string) g.Node {
return attr("patch", v)
}
// Preserve specifies elements to keep unchanged between requests.
// See https://htmx.org/attributes/hx-preserve
func Preserve(v string) g.Node {
return attr("preserve", v)
}
// Prompt shows a prompt() before submitting a request.
// See https://htmx.org/attributes/hx-prompt
func Prompt(v string) g.Node {
return attr("prompt", v)
}
// Put issues a PUT to the specified URL.
// See https://htmx.org/attributes/hx-put
func Put(v string) g.Node {
return attr("put", v)
}
// ReplaceURL replaces the URL in the browser location bar.
// See https://htmx.org/attributes/hx-replace-url
func ReplaceURL(v string) g.Node {
return attr("replace-url", v)
}
// Request configures various aspects of the request.
// See https://htmx.org/attributes/hx-request
func Request(v string) g.Node {
return attr("request", v)
}
// Sync controls how requests made by different elements are synchronized.
// See https://htmx.org/attributes/hx-sync
func Sync(v string) g.Node {
return attr("sync", v)
}
// Validate forces elements to validate themselves before a request.
// See https://htmx.org/attributes/hx-validate
func Validate(v string) g.Node {
return attr("validate", v)
}
func attr(name, value string) g.Node {
return g.Attr("hx-"+name, value)
}
// rawAttr is an attribute that doesn't escape its value.
type rawAttr struct {
name string
value string
}
func (r *rawAttr) Render(w io.Writer) error {
_, err := w.Write([]byte(" hx-" + r.name + `="` + r.value + `"`))
return err
}
func (r *rawAttr) Type() g.NodeType {
return g.AttributeType
}