-
Notifications
You must be signed in to change notification settings - Fork 1
/
attrib.go
518 lines (414 loc) · 13.6 KB
/
attrib.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Package htmx provides a set of functions for generating HTML attributes as nodes.
// These attributes can be used in conjunction with the Fiber framework and the htmx library.
package htmx
import (
"encoding/json"
"fmt"
"github.com/zeiss/pkg/conv"
"github.com/zeiss/pkg/errorx"
)
// HxEventType represents the type of htmx event.
type HxEventType string
// String returns the string representation of the HxEventType.
func (e HxEventType) String() string {
return string(e)
}
// List of predefined htmx event types.
const (
HxEventTypeAbort HxEventType = "htmx:abort"
HxEventTypeAfterLoad HxEventType = "htmx:afterLoad"
HxEventTypeAfterProcessNode HxEventType = "htmx:afterProcessNode"
HxEventTypeAfterRequest HxEventType = "htmx:afterRequest"
)
// HxBoost sets the hx-boost attribute to enable or disable boosting.
func HxBoost(v bool) Node {
return Attribute("hx-boost", conv.String(v))
}
// HxGet sets the hx-get attribute to specify the URL for GET requests.
func HxGet(url string) Node {
return Attribute("hx-get", url)
}
// HxPost sets the hx-post attribute to specify the URL for POST requests.
func HxPost(url string) Node {
return Attribute("hx-post", url)
}
// HxPushUrl sets the hx-push-url attribute to enable or disable URL pushing.
func HxPushUrl(v bool) Node {
return Attribute("hx-boost", conv.String(v))
}
// HxTarget sets the hx-target attribute to specify the target element for the response.
func HxTarget(target string) Node {
return Attribute("hx-target", target)
}
// HxSelect sets the hx-select attribute to specify the target element for selection.
func HxSelect(target string) Node {
return Attribute("hx-select", target)
}
// HxSelectOob sets the hx-select-oob attribute to specify the target element for out-of-band selection.
func HxSelectOob(target string) Node {
return Attribute("hx-select-oob", target)
}
// HxSwap sets the hx-swap attribute to specify the target element for swapping.
func HxSwap(target string) Node {
return Attribute("hx-swap", target)
}
// HxSwapOob sets the hx-swap-oob attribute to specify the target element for out-of-band swapping.
func HxSwapOob(target string) Node {
return Attribute("hx-swap-oob", target)
}
// HxTrigger sets the hx-trigger attribute to specify the target element for triggering an event.
func HxTrigger(target string) Node {
return Attribute("hx-trigger", target)
}
// HxConfirm sets the hx-confirm attribute to display a confirmation message.
func HxConfirm(msg string) Node {
return Attribute("hx-confirm", msg)
}
// HxPrompt sets the hx-prompt attribute to display a prompt message.
func HxPrompt(msg string) Node {
return Attribute("hx-prompt", msg)
}
// HxDelete sets the hx-delete attribute to specify the URL for DELETE requests.
func HxDelete(url string) Node {
return Attribute("hx-delete", url)
}
// HxOn sets the hx-put-{target} attribute to specify the JavaScript code to execute on a PUT request.
func HxOn(target string, js string) Node {
return Attribute(fmt.Sprintf("hx-on:%s", target), js)
}
// HxPut sets the hx-put attribute to specify the URL for PUT requests.
func HxPut(url string) Node {
return Attribute("hx-put", url)
}
// HxPatch sets the hx-patch attribute to specify the URL for PATCH requests.
func HxPatch(url string) Node {
return Attribute("hx-patch", url)
}
// HxIndicator sets the hx-indicator attribute to specify the target element for showing an indicator.
func HxIndicator(target string) Node {
return Attribute("hx-indicator", target)
}
// HxEncoding sets the hx-encoding attribute to specify the encoding type for form submission.
func HxEncoding(enc string) Node {
return Attribute("hx-encoding", enc)
}
// HxExt sets the hx-ext attribute to specify the file extension for file uploads.
func HxExt(ext string) Node {
return Attribute("hx-ext", ext)
}
// HxTarget404 sets the hx-target-404 attribute to specify the target element for 404 responses.
func HxTarget404(target string) Node {
return Attribute("hx-target-404", target)
}
// HxTarget403 sets the hx-target-403 attribute to specify the target element for 403 responses.
func HxTarget403(target string) Node {
return Attribute("hx-target-403", target)
}
// HxTarget401 sets the hx-target-401 attribute to specify the target element for 401 responses.
func HxTarget401(target string) Node {
return Attribute("hx-target-401", target)
}
// HxTarget500 sets the hx-target-500 attribute to specify the target element for 500 responses.
func HxTarget500(target string) Node {
return Attribute("hx-target-500", target)
}
// HxTarget5xx sets the hx-target-5xx attribute to specify the target element for 5xx responses.
func HxTarget5xx(target string) Node {
return Attribute("hx-target-5*", target)
}
// HxTarget4xx sets the hx-target-4xx attribute to specify the target element for 4xx responses.
func HxTarget4xx(target string) Node {
return Attribute("hx-target-4*", target)
}
// HxTargetError sets the hx-target-error attribute to specify the target element for error responses.
func HxTargetError(target string) Node {
return Attribute("hx-target-error", target)
}
// HxTarget50x sets the hx-target-50x attribute to specify the target element for 50x responses.
func HxTarget50x(target string) Node {
return Attribute("hx-target-50*", target)
}
// HxDisable sets the hx-disable attribute to disable htmx functionality.
func HxDisable() Node {
return Attribute("hx-disable")
}
// HxDisabledElt sets the hx-disable-elt attribute to disable the target element.
func HxDisabledElt(target string) Node {
return Attribute("hx-disabled-elt", target)
}
// HxValidate sets the hx-validate attribute to enable or disable form validation.
func HxValidate(v bool) Node {
return Attribute("hx-validate", conv.String(v))
}
// HxInclude sets the hx-include attribute to specify the target element for inclusion.
func HxInclude(target string) Node {
return Attribute("hx-include", target)
}
// HxHeaders sets the hx-headers attribute to specify the headers for the request.
func HxHeaders(headers map[string]string) Node {
return Attribute("hx-headers", string(errorx.Ignore(json.Marshal(headers))))
}
// Async sets the async attribute for script elements.
func Async() Node {
return Attribute("async")
}
// AutoFocus sets the autofocus attribute for form elements.
func AutoFocus() Node {
return Attribute("autofocus")
}
// AutoPlay sets the autoplay attribute for media elements.
func AutoPlay() Node {
return Attribute("autoplay")
}
// Checked sets the checked attribute for input elements.
func Checked() Node {
return Attribute("checked")
}
// Controls sets the controls attribute for media elements.
func Controls() Node {
return Attribute("controls")
}
// Defer sets the defer attribute for script elements.
func Defer() Node {
return Attribute("defer")
}
// Disabled sets the disabled attribute for form elements.
func Disabled() Node {
return Attribute("disabled")
}
// Loop sets the loop attribute for media elements.
func Loop() Node {
return Attribute("loop")
}
// Multiple sets the multiple attribute for input elements.
func Multiple() Node {
return Attribute("multiple")
}
// Muted sets the muted attribute for media elements.
func Muted() Node {
return Attribute("muted")
}
// PlaysInline sets the playsinline attribute for media elements.
func PlaysInline() Node {
return Attribute("playsinline")
}
// ReadOnly sets the readonly attribute for form elements.
func ReadOnly() Node {
return Attribute("readonly")
}
// Required sets the required attribute for form elements.
func Required() Node {
return Attribute("required")
}
// Selected sets the selected attribute for option elements.
func Selected() Node {
return Attribute("selected")
}
// Accept sets the accept attribute for file input elements.
func Accept(v string) Node {
return Attribute("accept", v)
}
// Action sets the action attribute for form elements.
func Action(v string) Node {
return Attribute("action", v)
}
// Alt sets the alt attribute for image elements.
func Alt(v string) Node {
return Attribute("alt", v)
}
// Aria sets the aria-{name} attribute for elements.
func Aria(name, v string) Node {
return Attribute("aria-"+name, v)
}
// As sets the as attribute for link elements.
func As(v string) Node {
return Attribute("as", v)
}
// AutoComplete sets the autocomplete attribute for form elements.
func AutoComplete(v string) Node {
return Attribute("autocomplete", v)
}
// Charset sets the charset attribute for meta elements.
func Charset(v string) Node {
return Attribute("charset", v)
}
// Class sets the class attribute for elements.
func Class(v string) Node {
return Attribute("class", v)
}
// Cols sets the cols attribute for textarea elements.
func Cols(v string) Node {
return Attribute("cols", v)
}
// ColSpan sets the colspan attribute for table cells.
func ColSpan(v string) Node {
return Attribute("colspan", v)
}
// Content sets the content attribute for meta elements.
func Content(v string) Node {
return Attribute("content", v)
}
// DataAttribute sets the data-{name} attribute for elements.
func DataAttribute(name, v string) Node {
return Attribute("data-"+name, v)
}
// For sets the for attribute for label elements.
func For(v string) Node {
return Attribute("for", v)
}
// FormAttribute sets the form attribute for elements.
func FormAttribute(v string) Node {
return Attribute("form", v)
}
// Height sets the height attribute for elements.
func Height(v string) Node {
return Attribute("height", v)
}
// Href sets the href attribute for anchor elements.
func Href(v string) Node {
return Attribute("href", v)
}
// ID sets the id attribute for elements.
func ID(v string) Node {
return Attribute("id", v)
}
// Lang sets the lang attribute for elements.
func Lang(v string) Node {
return Attribute("lang", v)
}
// Loading sets the loading attribute for elements.
func Loading(v string) Node {
return Attribute("loading", v)
}
// Max sets the max attribute for input elements.
func Max(v string) Node {
return Attribute("max", v)
}
// MaxLength sets the maxlength attribute for input elements.
func MaxLength(v string) Node {
return Attribute("maxlength", v)
}
// Method sets the method attribute for form elements.
func Method(v string) Node {
return Attribute("method", v)
}
// Min sets the min attribute for input elements.
func Min(v string) Node {
return Attribute("min", v)
}
// MinLength sets the minlength attribute for input elements.
func MinLength(v string) Node {
return Attribute("minlength", v)
}
// Name sets the name attribute for elements.
func Name(v string) Node {
return Attribute("name", v)
}
// Pattern sets the pattern attribute for input elements.
func Pattern(v string) Node {
return Attribute("pattern", v)
}
// Placeholder sets the placeholder attribute for input elements.
func Placeholder(v string) Node {
return Attribute("placeholder", v)
}
// Poster sets the poster attribute for video elements.
func Poster(v string) Node {
return Attribute("poster", v)
}
// Preload sets the preload attribute for media elements.
func Preload(v string) Node {
return Attribute("preload", v)
}
// Rel sets the rel attribute for link elements.
func Rel(v string) Node {
return Attribute("rel", v)
}
// Role sets the role attribute for elements.
func Role(v string) Node {
return Attribute("role", v)
}
// Rows sets the rows attribute for textarea elements.
func Rows(v string) Node {
return Attribute("rows", v)
}
// RowSpan sets the rowspan attribute for table cells.
func RowSpan(v string) Node {
return Attribute("rowspan", v)
}
// Src sets the src attribute for elements.
func Src(v string) Node {
return Attribute("src", v)
}
// SrcSet sets the srcset attribute for elements.
func SrcSet(v string) Node {
return Attribute("srcset", v)
}
// Step sets the step attribute for input elements.
func Step(v string) Node {
return Attribute("step", v)
}
// StyleAttribute sets the style attribute for elements.
func StyleAttribute(v string) Node {
return Attribute("style", v)
}
// TabIndex sets the tabindex attribute for elements.
func TabIndex(v string) Node {
return Attribute("tabindex", v)
}
// Target sets the target attribute for elements.
func Target(v string) Node {
return Attribute("target", v)
}
// TitleAttribute sets the title attribute for elements.
func TitleAttribute(v string) Node {
return Attribute("title", v)
}
// Type sets the type attribute for elements.
func Type(v string) Node {
return Attribute("type", v)
}
// Value sets the value attribute for elements.
func Value(v string) Node {
return Attribute("value", v)
}
// Width sets the width attribute for elements.
func Width(v string) Node {
return Attribute("width", v)
}
// EncType sets the enctype attribute for form elements.
func EncType(v string) Node {
return Attribute("enctype", v)
}
// OnClick sets the onclick attribute for elements.
func OnClick(v string) Node {
return Attribute("onclick", v)
}
// Is sets the is attribute for custom elements.
func Is(v string) Node {
return Attribute("is", v)
}
// Integrity sets the integrity attribute for elements.
func Integrity(v string) Node {
return Attribute("integrity", v)
}
// NoValidate sets the novalidate attribute for form elements.
func NoValidate() Node {
return Attribute("novalidate")
}
// CrossOrigin sets the crossorigin attribute for elements.
func CrossOrigin(v string) Node {
return Attribute("crossorigin", v)
}
// List sets the list attribute for input elements.
func List(v string) Node {
return Attribute("list", v)
}
// OnePasswordIgnore sets the data-1p-ignore attribute for elements.
func OnePasswordIgnore() Node {
return Attribute("data-1p-ignore")
}
// ContentEditable sets the contenteditable attribute for elements.
func ContentEditable(v string) Node {
return Attribute("contenteditable", v)
}