-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
134 lines (120 loc) · 4.55 KB
/
update.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
package guiapi
import (
"github.com/mbertschler/guiapi/api"
)
// Update is the returned body of a GUI API call.
type Update struct {
Name string `json:",omitempty"` // Name of the action that was called
URL string `json:",omitempty"` // URL that was loaded
Error *api.Error `json:",omitempty"` // Error that occurred while handling the action
HTML []api.HTMLUpdate `json:",omitempty"` // DOM updates to apply
JS []api.JSCall `json:",omitempty"` // JS calls to execute
State any `json:",omitempty"` // State to pass back to the browser
Stream []api.Stream `json:",omitempty"` // Stream to subscribe to via websocket
}
// JSCall returns a new Update that will call the registered JavaScript
// function that is identified by the name, and will pass the arguments.
func JSCall(name string, args any) *Update {
u := &Update{}
u.AddJSCall(name, args)
return u
}
// AddJSCall adds a new JSCall Update that will call the registered JavaScript
// function that is identified by the name, and will pass the arguments.
func (u *Update) AddJSCall(name string, args any) {
u.JS = append(u.JS, api.JSCall{
Name: name,
Args: args,
})
}
// Stream returns a new Update that will connect to a stream
// with the passed name and arguments.
func Stream(name string, args any) *Update {
u := &Update{}
u.AddStream(name, args)
return u
}
// AddStream adds new stream Update that will connect to
// a stream with the passed name and arguments.
func (u *Update) AddStream(name string, args any) {
u.Stream = append(u.Stream, api.Stream{
Name: name,
Args: args,
})
}
// ReplaceContent returns a new Update that replaces the content of the element
// that gets selected by the passed selector with the HTML content.
// The selector gets passed to document.querySelector,
// so it can be any valid CSS selector.
func ReplaceContent(selector, content string) *Update {
u := &Update{}
u.AddReplaceContent(selector, content)
return u
}
// AddReplaceContent adds a new HTML Update that replaces the content of the element
// that gets selected by the passed selector with the HTML content.
// The selector gets passed to document.querySelector,
// so it can be any valid CSS selector.
func (u *Update) AddReplaceContent(selector, content string) {
u.HTML = append(u.HTML, api.HTMLUpdate{
Operation: api.HTMLReplaceContent,
Selector: selector,
Content: content,
})
}
// ReplaceElement returns a new Update that replaces the whole element
// that gets selected by the passed selector with the HTML content.
// The selector gets passed to document.querySelector,
// so it can be any valid CSS selector.
func ReplaceElement(selector, content string) *Update {
u := &Update{}
u.AddReplaceElement(selector, content)
return u
}
// AddReplaceElement adds a HTML Update that replaces the whole element
// that gets selected by the passed selector with the HTML content.
// The selector gets passed to document.querySelector,
// so it can be any valid CSS selector.
func (u *Update) AddReplaceElement(selector, content string) {
u.HTML = append(u.HTML, api.HTMLUpdate{
Operation: api.HTMLReplaceElement,
Selector: selector,
Content: content,
})
}
// InsertBefore returns a new Update that inserts HTML content on the
// same level before the passed selector. The selector gets passed
// to document.querySelector, so it can be any valid CSS selector.
func InsertBefore(selector, content string) *Update {
u := &Update{}
u.AddInsertBefore(selector, content)
return u
}
// AddInsertBefore adds a HTML update that inserts HTML content on the
// same level before the passed selector. The selector gets passed
// to document.querySelector, so it can be any valid CSS selector.
func (u *Update) AddInsertBefore(selector, content string) {
u.HTML = append(u.HTML, api.HTMLUpdate{
Operation: api.HTMLInsertBefore,
Selector: selector,
Content: content,
})
}
// InsertAfter returns a new Update that inserts HTML content on the
// same level after the passed selector. The selector gets passed
// to document.querySelector, so it can be any valid CSS selector.
func InsertAfter(selector, content string) *Update {
u := &Update{}
u.AddInsertAfter(selector, content)
return u
}
// AddInsertAfter adds a HTML update that inserts HTML content on the
// same level after the passed selector. The selector gets passed
// to document.querySelector, so it can be any valid CSS selector.
func (u *Update) AddInsertAfter(selector, content string) {
u.HTML = append(u.HTML, api.HTMLUpdate{
Operation: api.HTMLInsertAfter,
Selector: selector,
Content: content,
})
}