-
Notifications
You must be signed in to change notification settings - Fork 0
/
location_options.go
110 lines (90 loc) · 2.89 KB
/
location_options.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
package hx
import (
"encoding/json"
"fmt"
)
// Location sets the HX-Location header.
//
// This header is used for client-side redirection without a full page reload.
// Additional properties like Target can be specified for more complex behaviors.
//
// For more details, see: https://htmx.org/headers/hx-location
//
// Simple redirection example:
//
// hx.Response(w, hx.Location("/test"))
// // Sets HX-Location header to "/test"
//
// Redirection with additional target example:
//
// hx.Response(w, hx.Location("/test",
// hx.Target("#testdiv"),
// ))
// // Sets HX-Location header to a JSON object: {"path":"/test","target":"#testdiv"}
func Location(path string, properties ...property) responseOptionFunc {
return func(o *HtmxResponse) {
loc := location{
Path: path,
}
if len(properties) == 0 {
o.headers[HxLocation] = loc.Path
return
}
for _, property := range properties {
property.apply(&loc)
}
value, err := json.Marshal(loc)
if err != nil {
panic(fmt.Errorf("unable to marshal HX-Location header: %w", err))
}
o.headers[HxLocation] = string(value)
}
}
// Source sets the 'source' property of the HX-Location header.
//
// More details: https://htmx.org/headers/hx-location
type Source string
func (l Source) apply(o *location) { o.Source = string(l) }
// EventName sets the 'event' property of the HX-Location header.
//
// More details: https://htmx.org/headers/hx-location
type EventName string
func (l EventName) apply(o *location) { o.Event = string(l) }
// Handler sets the 'handler' property of the HX-Location header.
//
// More details: https://htmx.org/headers/hx-location
type Handler string
func (l Handler) apply(o *location) { o.Handler = string(l) }
// Target sets the 'target' property of the HX-Location header.
//
// More details: https://htmx.org/headers/hx-location
type Target string
func (l Target) apply(o *location) { o.Target = string(l) }
// Swap sets the 'swap' property of the HX-Location header.
//
// Either a string or a Reswap constant can be used.
//
// More details: https://htmx.org/headers/hx-location
func Swap[T string | Reswap](swap T) propertyFunc {
return func(o *location) { o.Swap = string(swap) }
}
// Values sets the 'values' property of the HX-Location header.
//
// Accepts any type, but a map[string]any or a struct with JSON tags is recommended.
//
// More details: https://htmx.org/headers/hx-location
func Values(values any) propertyFunc {
return func(o *location) { o.Values = values }
}
// Headers sets the 'headers' property of the HX-Location header.
//
// Accepts a map[string]string.
//
// More details: https://htmx.org/headers/hx-location
type Headers map[string]string
func (l Headers) apply(o *location) { o.Headers = l }
// Select sets the 'select' property of the HX-Location header.
//
// More details: https://htmx.org/headers/hx-location
type Select string
func (l Select) apply(o *location) { o.Select = string(l) }