-
Notifications
You must be signed in to change notification settings - Fork 6
/
modal.go
419 lines (352 loc) · 10.3 KB
/
modal.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
package control
import (
"context"
"fmt"
config2 "github.com/goradd/goradd/pkg/bootstrap/config"
"github.com/goradd/html5tag"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/page/event"
)
type ModalBackdropType int
const (
ModalBackdrop ModalBackdropType = iota // Standard bootstrap backdrop. Clicking the backdrop closes the modal.
ModalStaticBackdrop // Clicking the backdrop will not close the modal.
)
type ModalI interface {
control.DialogI
}
// Modal is a bootstrap modal dialog.
// To use a custom template in a bootstrap modal, add a Panel child element or subclass of a panel
// child element. To use the grid system, add the container-fluid class to that embedded panel.
type Modal struct {
control.Panel
isOpen bool
closeOnEscape bool
sizeClass string
titleBar *TitleBar
buttonBar *control.Panel
backdrop ModalBackdropType
foundRight bool // utility for adding buttons. No need to serialize this.
}
// event codes
const (
DialogClosed = iota + 10000
)
func NewModal(parent page.ControlI, id string) *Modal {
d := &Modal{}
d.Self = d
d.Init(parent, id)
return d
}
func (m *Modal) Init(parent page.ControlI, id string) {
if id == "" {
panic("Modals must have an id")
}
m.Panel.Init(parent, id)
m.Tag = "div"
m.SetShouldAutoRender(true)
m.SetValidationType(page.ValidateChildrenOnly) // allows sub items to validate and have validation stop here
m.SetBlockParentValidation(true)
config2.LoadBootstrap(m.ParentForm())
m.AddClass("modal fade").
SetAttribute("tabindex", -1).
SetAttribute("role", "dialog").
SetAttribute("aria-labelledby", m.ID()+"-title").
SetAttribute("aria-hidden", true)
m.titleBar = NewTitleBar(m, m.ID()+"-titlebar")
m.buttonBar = control.NewPanel(m, m.ID()+"-btnbar")
m.On(event.DialogClosed().Validate(page.ValidateNone).Private(), action.Ajax(m.ID(), DialogClosed))
}
func (m *Modal) this() ModalI {
return m.Self.(ModalI)
}
func (m *Modal) SetTitle(t string) {
if m.titleBar.Title != t {
m.titleBar.Title = t
m.titleBar.Refresh()
}
}
func (m *Modal) SetHasCloseBox(h bool) {
if m.titleBar.HasCloseBox != h {
m.titleBar.HasCloseBox = h
m.titleBar.Refresh()
}
}
func (m *Modal) SetDialogStyle(style control.DialogStyle) {
var class string
switch style {
case control.DialogStyleDefault:
class = BackgroundColorNone + " " + TextColorBody
case control.DialogStyleWarning:
class = BackgroundColorWarning + " " + TextColorBody
case control.DialogStyleError:
class = BackgroundColorDanger + " " + TextColorLight
case control.DialogStyleSuccess:
class = BackgroundColorSuccess + " " + TextColorLight
case control.DialogStyleInfo:
class = BackgroundColorInfo + " " + TextColorLight
}
m.titleBar.RemoveClassesWithPrefix("bg-")
m.titleBar.RemoveClassesWithPrefix("text-")
m.titleBar.AddClass(class)
}
func (m *Modal) SetBackdrop(b ModalBackdropType) {
m.backdrop = b
m.Refresh()
}
func (m *Modal) Title() string {
return m.titleBar.Title
}
func (m *Modal) AddTitlebarClass(class string) {
m.titleBar.AddClass(class)
}
func (m *Modal) DrawingAttributes(ctx context.Context) html5tag.Attributes {
a := m.Panel.DrawingAttributes(ctx)
a.SetData("grctl", "bs-modal")
if m.backdrop == ModalStaticBackdrop {
a.SetData("bsBackdrop", "static")
}
return a
}
// AddButton adds a button to the modal. Buttons should be added in the order to appear.
// Styling options you can include in options.Options:
// style - ButtonStyle value
// size - ButtonSize value
func (m *Modal) AddButton(
label string,
id string,
options *control.DialogButtonOptions,
) {
if id == "" {
id = label
}
btn := NewButton(m.buttonBar, m.ID()+"-btn-"+id)
btn.SetLabel(label)
if options != nil {
if options.IsClose {
btn.SetDataAttribute("bsDismiss", "modal") // make it a close button
btn.SetDataAttribute("bsTarget", m.ID()) // make it a close button
} else if options.ConfirmationMessage == "" {
if options.OnClick != nil {
btn.On(event.Click(), options.OnClick)
} else {
btn.On(event.Click(), action.Trigger(m.ID(), event.DialogButtonEvent, id))
}
} else {
if options.OnClick != nil {
btn.On(event.Click(),
action.Group(
action.Confirm(options.ConfirmationMessage),
options.OnClick,
),
)
} else {
btn.On(event.Click(),
action.Group(
action.Confirm(options.ConfirmationMessage),
action.Trigger(m.ID(), event.DialogButtonEvent, id),
),
)
}
}
} else {
btn.On(event.Click(), action.Trigger(m.ID(), event.DialogButtonEvent, id))
}
if (options == nil || !options.PushLeft) && !m.foundRight {
btn.AddClass("ml-auto")
m.foundRight = true
}
if options != nil {
if options.Validates {
btn.SetValidationType(page.ValidateContainer)
}
if options.Options != nil && len(options.Options) > 0 {
if _, ok := options.Options["style"]; ok {
btn.SetButtonStyle(options.Options["style"].(ButtonStyle))
}
if _, ok := options.Options["size"]; ok {
btn.SetButtonSize(options.Options["size"].(ButtonSize))
}
}
if options.IsPrimary {
btn.SetIsPrimary(true)
}
}
m.buttonBar.Refresh()
}
func (m *Modal) RemoveButton(id string) {
m.buttonBar.RemoveChild(m.ID() + "-btn-" + id)
m.buttonBar.Refresh()
}
func (m *Modal) RemoveAllButtons() {
m.buttonBar.RemoveChildren()
m.Refresh()
}
func (m *Modal) SetButtonVisible(id string, visible bool) {
if ctrl := m.buttonBar.Child(m.ID() + "-btn-" + id); ctrl != nil {
ctrl.SetVisible(visible)
}
}
// SetButtonStyle sets css styles on a button that is already in the dialog
func (m *Modal) SetButtonStyle(id string, a html5tag.Style) {
if ctrl := m.buttonBar.Child(m.ID() + "-btn-" + id); ctrl != nil {
ctrl.SetStyles(a)
}
}
// SetButtonText sets the text of a button that is already on the dialog
func (m *Modal) SetButtonText(id string, text string) {
if ctrl := m.buttonBar.Child(m.ID() + "-btn-" + id); ctrl != nil {
ctrl.SetText(text)
}
}
// AddCloseButton adds a button to the list of buttons with the given label, but this button will trigger the DialogCloseEvent
// instead of the DialogButtonEvent. The button will also close the dialog (by hiding it).
func (m *Modal) AddCloseButton(label string, id string) {
m.AddButton(label, id, &control.DialogButtonOptions{IsClose: true})
}
func (m *Modal) PrivateAction(_ context.Context, a page.ActionParams) {
switch a.ID {
case DialogClosed:
m.closed()
}
}
func (m *Modal) Show() {
if m.Parent() == nil {
m.SetParent(m.ParentForm()) // This is a saved modal which has previously been created and removed. Insert it back into the form.
}
m.SetVisible(true)
m.isOpen = true
//d.Refresh()
m.ParentForm().Response().ExecuteJavaScript(fmt.Sprintf("bootstrap.Modal.getInstance(document.getElementById('%s')).show();", m.ID()), page.PriorityLow)
}
func (m *Modal) Hide() {
m.ParentForm().Response().ExecuteJavaScript(fmt.Sprintf("bootstrap.Modal.getInstance(document.getElementById('%s')).hide()", m.ID()), page.PriorityLow)
}
func (m *Modal) closed() {
m.isOpen = false
m.ResetValidation()
m.SetVisible(false)
}
func (m *Modal) PutCustomScript(_ context.Context, response *page.Response) {
script := fmt.Sprintf(`
var m = new bootstrap.Modal(document.getElementById('%s') , {keyboard: %t});
`, m.ID(), m.closeOnEscape)
script += fmt.Sprintf(
`g$("%s").on("hidden.bs.modal", function(){g$("%[1]s").trigger("grdlgclosed")});`, m.ID())
response.ExecuteJavaScript(script, page.PriorityStandard)
}
func (m *Modal) Serialize(e page.Encoder) {
m.Panel.Serialize(e)
if err := e.Encode(m.isOpen); err != nil {
panic(err)
}
if err := e.Encode(m.closeOnEscape); err != nil {
panic(err)
}
if err := e.Encode(m.backdrop); err != nil {
panic(err)
}
if err := e.Encode(m.foundRight); err != nil {
panic(err)
}
}
func (m *Modal) Deserialize(d page.Decoder) {
m.Panel.Deserialize(d)
if err := d.Decode(&m.isOpen); err != nil {
panic(err)
}
if err := d.Decode(&m.closeOnEscape); err != nil {
panic(err)
}
m.titleBar = m.Page().GetControl(m.ID()+"-titlebar").(*TitleBar)
m.buttonBar = m.Page().GetControl(m.ID()+"-btnbar").(*control.Panel)
if err := d.Decode(&m.backdrop); err != nil {
panic(err)
}
if err := d.Decode(&m.foundRight); err != nil {
panic(err)
}
}
type TitleBar struct {
control.Panel
HasCloseBox bool
Title string
}
func NewTitleBar(parent page.ControlI, id string) *TitleBar {
d := &TitleBar{}
d.Self = d
d.Panel.Init(parent, id)
return d
}
func init() {
page.RegisterControl(&Modal{})
page.RegisterControl(&TitleBar{})
}
type ModalButtonCreator struct {
Label string
ID string
Validates bool
ConfirmationMessage string
PushLeft bool
IsClose bool
Options map[string]interface{}
}
func ModalButtons (buttons ...ModalButtonCreator) []ModalButtonCreator {
return buttons
}
type ModalCreator struct {
ID string
Title string
TitlebarClass string
HasCloseBox bool
Style control.DialogStyle
Backdrop ModalBackdropType
Buttons []ModalButtonCreator
OnButton action.ActionI
page.ControlOptions
Children []page.Creator
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c ModalCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewModal(parent.ParentForm(), c.ID) // modals always use the form as the parent
if c.Title != "" {
ctrl.SetTitle(c.Title)
}
if c.TitlebarClass != "" {
ctrl.AddTitlebarClass(c.TitlebarClass)
}
if c.HasCloseBox {
ctrl.SetHasCloseBox(true)
}
if c.Style != control.DialogStyleDefault {
ctrl.SetDialogStyle(c.Style)
}
if c.Backdrop != ModalBackdrop {
ctrl.SetBackdrop(c.Backdrop)
}
if c.Buttons != nil {
for _, button := range c.Buttons {
ctrl.AddButton(button.Label, button.ID, &control.DialogButtonOptions{
Validates: button.Validates,
ConfirmationMessage: button.ConfirmationMessage,
PushLeft: button.PushLeft,
IsClose: button.IsClose,
Options: button.Options,
})
}
}
if c.OnButton != nil {
ctrl.On(event.DialogButton(), c.OnButton)
}
if c.Children != nil {
ctrl.AddControls(ctx, c.Children...)
}
return ctrl
}
// GetListGroup is a convenience method to return the control with the given id from the page.
func GetModal(c page.ControlI, id string) *Modal {
return c.Page().GetControl(id).(*Modal)
}