forked from src-d/flamingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.go
234 lines (201 loc) · 6.07 KB
/
form.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
package flamingo
// Form is the way to post complex structures with the bot.
// Forms can have title, text, color, footer, several Fields
// (buttons, text fields, images, ...), etc.
type Form struct {
// Title is the title of the form.
Title string
// AuthorIconURL sets the icon of the form author to the given one.
AuthorIconURL string
// AuthorName sets the name of the form author to the given one.
AuthorName string
// Text is the text of the form.
Text string
// Combine will force all the form to be in a single message.
// The behavior of this is actually client-dependant.
Combine bool
// Color is the color of the form. This is client-specific.
Color string
// Footer is a small text on the footer of the form.
Footer string
// Fields is a collection of field groups on the form.
Fields []FieldGroup
}
// Field is an element of a FieldGroup.
type Field interface {
isField()
}
// FieldGroup is a collection of Fields of a concrete type.
type FieldGroup interface {
// ID returns the ID of the group (only valid for ButtonGroup)
ID() string
// Items returns all the fields in the group.
Items() []Field
// Type returns the type of the group.
Type() FieldGroupType
}
// FieldGroupType is the type of elements contained in the group.
type FieldGroupType byte
const (
// ButtonGroup is a group of buttons.
ButtonGroup FieldGroupType = 1 << iota
// TextFieldGroup is a group of text fields.
TextFieldGroup
// ImageGroup is a single image.
ImageGroup
// TextGroup is a single text message.
TextGroup
)
type fieldGroup struct {
kind FieldGroupType
id string
items []Field
}
func (g *fieldGroup) Items() []Field {
return g.items
}
func (g *fieldGroup) ID() string {
return g.id
}
func (g *fieldGroup) Type() FieldGroupType {
return g.kind
}
// NewButtonGroup creates a FieldGroup with the given buttons.
func NewButtonGroup(id string, buttons ...Button) FieldGroup {
var items []Field
for _, b := range buttons {
items = append(items, b)
}
return &fieldGroup{
kind: ButtonGroup,
id: id,
items: items,
}
}
// NewTextFieldGroup creates a FieldGroup with the given text fields.
func NewTextFieldGroup(fields ...TextField) FieldGroup {
var items []Field
for _, f := range fields {
items = append(items, f)
}
return &fieldGroup{
kind: TextFieldGroup,
items: items,
}
}
// ButtonType is the kind of button. Even though there are several
// types of buttons this is really very platform-specific.
type ButtonType byte
const (
// DefaultButton is the default button.
DefaultButton ButtonType = iota
// PrimaryButton is a button with an accent color to stand out.
PrimaryButton
// DangerButton is a typically red button to indicate a dangerous action.
DangerButton
)
// Button is a single button element.
type Button struct {
// Text is the visible text of the button.
Text string
// Name is the name of the button.
Name string
// Value is the value of the button.
Value string
// Type is the type of the button.
Type ButtonType
// Confirmation defines a confirmation popup to be shown after the button
// is clicked. This is platform-specific behaviour.
Confirmation *Confirmation
}
// NewButton creates a new default button. Value and name will be the same, if
// that does not work for you, you will need to create a Button by hand.
func NewButton(text, value string) Button {
return Button{
Text: text,
Value: value,
Name: value,
}
}
// NewPrimaryButton creates a new primary button. Value and name will be the same, if
// that does not work for you, you will need to create a Button by hand.
func NewPrimaryButton(text, value string) Button {
b := NewButton(text, value)
b.Type = PrimaryButton
return b
}
// NewDangerButton creates a new danger button. Value and name will be the same, if
// that does not work for you, you will need to create a Button by hand.
func NewDangerButton(text, value string) Button {
b := NewButton(text, value)
b.Type = DangerButton
return b
}
func (b Button) isField() {}
// Confirmation is a confirmation popup to be shown after a button is clicked.
// Slack-specific at the moment.
type Confirmation struct {
// Title of the confirm window.
Title string
// Text of the confirm window.
Text string
// Ok button text of the confirm window.
Ok string
// Dismiss button text of the confirm window.
Dismiss string
}
// TextField is a field which displays a label with its text value.
type TextField struct {
// Title is the label of the field.
Title string
// Value is the text value of the field.
Value string
// Short is a platform-specific feature. If available, will be rendered as
// a short field instead of a wide one.
Short bool
}
// NewTextField creates a new textfield.
func NewTextField(title, value string) TextField {
return TextField{
Title: title,
Value: value,
}
}
// NewShortTextField creates a new short text field.
func NewShortTextField(title, value string) TextField {
return TextField{
Title: title,
Value: value,
Short: true,
}
}
func (f TextField) isField() {}
// Image is an image to be posted.
type Image struct {
// URL is the URL of the image.
URL string
// Text of the image.
Text string
// ThumbnailURL is the URL of the thumbnail to be displayed.
ThumbnailURL string
}
func (f Image) isField() {}
// ID always returns an empty string since Images have no ID. This method is just
// implemented to fulfill the FieldGroup interface.
func (f Image) ID() string { return "" }
// Items returns a slice with only the image itself, which is both a fieldgroup and
// a field.
func (f Image) Items() []Field { return []Field{f} }
// Type returns the ImageGroup type.
func (f Image) Type() FieldGroupType { return ImageGroup }
// Text is a single free text message to put on a form as a field.
type Text string
func (f Text) isField() {}
// ID always returns an empty string, because Text don't have IDs. This
// is only for buttons.
func (f Text) ID() string { return "" }
// Items returns a slice with only the text itself, which is a
// field.
func (f Text) Items() []Field { return []Field{f} }
// Type returns always the TextGroup type.
func (f Text) Type() FieldGroupType { return TextGroup }