-
Notifications
You must be signed in to change notification settings - Fork 6
/
modal.go
480 lines (418 loc) · 13.6 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
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
package control
import (
"context"
"fmt"
config2 "github.com/goradd/goradd/pkg/bootstrap/config"
"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/control/dialog"
"github.com/goradd/goradd/pkg/page/event"
"github.com/goradd/html5tag"
)
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 {
dialog.DialogI
}
// Modal is a Bootstrap modal dialog control.
//
// To create Modal, you have a few options:
// - Call NewModal()
// - Pass a ModalCreator object to the form's AddControls() function.
// - Since Modal implements dialog.DialogI, you can also call the dialog.Alert function. If you have previously
// called setupBootstrap() in your project's config/goradd.go file, then that function
// will call NewModal to create a Bootstrap style 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.
//
// A modal dialog starts out hidden. Call Show() on the modal dialog to display it.
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.
}
const (
DialogClosed = iota + 10000 // The event code for a dialog that is closing
)
// NewModal creates a new Modal dialog control.
func NewModal(parent page.ControlI, id string) *Modal {
d := new(Modal)
d.Init(d, parent, id)
return d
}
// Init is called by the framework to initialize a modal dialog.
//
// Subclasses should call Init after creating themselves.
func (m *Modal) Init(self any, parent page.ControlI, id string) {
if id == "" {
panic("Modals must have an id")
}
m.Panel.Init(self, parent, id)
m.Tag = "div"
m.SetShouldAutoRender(true)
m.SetValidationType(event.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(event.ValidateNone).Private().Action(action.Do().ID(DialogClosed)))
}
func (m *Modal) this() ModalI {
return m.Self().(ModalI)
}
// SetTitle sets the content of the title of the modal dialog.
func (m *Modal) SetTitle(t string) {
if m.titleBar.Title != t {
m.titleBar.Title = t
m.titleBar.Refresh()
}
}
// SetHasCloseBox determines if the modal has a close box in the upper corner which will close the dialog.
func (m *Modal) SetHasCloseBox(h bool) {
if m.titleBar.HasCloseBox != h {
m.titleBar.HasCloseBox = h
m.titleBar.Refresh()
}
}
// SetDialogStyle sets the style of the dialog.
//
// These styles are mapped to Bootstrap TextColor* styles.
func (m *Modal) SetDialogStyle(style dialog.Style) {
var class string
switch style {
case dialog.DefaultStyle:
class = BackgroundColorNone + " " + TextColorBody
case dialog.WarningStyle:
class = BackgroundColorWarning + " " + TextColorBody
case dialog.ErrorStyle:
class = BackgroundColorDanger + " " + TextColorLight
case dialog.SuccessStyle:
class = BackgroundColorSuccess + " " + TextColorLight
case dialog.InfoStyle:
class = BackgroundColorInfo + " " + TextColorLight
}
m.titleBar.RemoveClassesWithPrefix("bg-")
m.titleBar.RemoveClassesWithPrefix("text-")
m.titleBar.AddClass(class)
}
// SetBackdrop determines whether the modal dialog will close when clicking on the backdrop.
func (m *Modal) SetBackdrop(b ModalBackdropType) {
m.backdrop = b
m.Refresh()
}
// Title returns the title of the dialog.
func (m *Modal) Title() string {
return m.titleBar.Title
}
// AddTitlebarClass adds a css class to the class of the title bar.
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 *dialog.ButtonOptions,
) {
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(event.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()
}
// RemoveButton removes the button from the dialog with the given id.
func (m *Modal) RemoveButton(id string) {
m.buttonBar.RemoveChild(m.ID() + "-btn-" + id)
m.buttonBar.Refresh()
}
// RemoveAllButtons removes all the buttons from the modal.
func (m *Modal) RemoveAllButtons() {
m.buttonBar.RemoveChildren()
m.Refresh()
}
// SetButtonVisible sets the visible state of the button with the given id.
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)
}
}
// MergeButtonAttributes merges the give attributes into the button's current attributes.
func (m *Modal) MergeButtonAttributes(id string, a html5tag.Attributes) {
if ctrl := m.buttonBar.Child(m.ID() + "-btn-" + id); ctrl != nil {
ctrl.MergeAttributes(a)
}
}
// 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, &dialog.ButtonOptions{IsClose: true})
}
// DoPrivateAction is called by the framework to record that a dialog was closed.
func (m *Modal) DoPrivateAction(_ context.Context, a action.Params) {
switch a.ID {
case DialogClosed:
m.closed()
}
}
// Show will cause the modal to appear.
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)
}
// Hide will visibly hide the modal, but will keep its html and javascript code in the client.
func (m *Modal) Hide() {
m.ParentForm().Response().ExecuteJavaScript(fmt.Sprintf("bootstrap.Modal.getInstance(document.getElementById('%s')).hide()", m.ID()), page.PriorityLow)
}
// closed is used by the framework to record that a dialog was closed.
func (m *Modal) closed() {
m.isOpen = false
m.ResetValidation()
m.SetVisible(false)
}
// PutCustomScript is called by the framework to insert the javascript required to manage the Bootstrap modal.
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("%s")});`, m.ID(), event.DialogClosedEvent)
response.ExecuteJavaScript(script, page.PriorityStandard)
}
// Serialize is called by the framework to record the state of the modal dialog object.
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)
}
}
// Deserialize is called by the framework to restore the state of the dialog.
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)
}
}
// TitleBar is a control that displays the title bar portion of a modal dialog.
type TitleBar struct {
control.Panel
HasCloseBox bool
Title string
}
// NewTitleBar creates a new TitleBar control.
func NewTitleBar(parent page.ControlI, id string) *TitleBar {
d := new(TitleBar)
d.Panel.Init(d, parent, id)
return d
}
func init() {
page.RegisterControl(&Modal{})
page.RegisterControl(&TitleBar{})
}
// ModalButtonCreator declares a dialog button to put on a modal. Pass the structure to
// the ModalButtons function.
type ModalButtonCreator struct {
Label string
ID string
Validates bool
ConfirmationMessage string
PushLeft bool
IsClose bool
Options map[string]interface{}
}
// ModalButtons is a helper for declaring buttons on a Modal dialog control.
// Pass it to the Buttons parameter of a ModalCreator.
func ModalButtons(buttons ...ModalButtonCreator) []ModalButtonCreator {
return buttons
}
// ModalCreator declares a Bootstrap modal dialog. Pass this structure to the AddControls function of a form.
//
// For example, the following will create a dialog with a title, text, and two buttons.
//
// form.AddControls(
// ModalCreator {
// ID: "my-modal",
// Title: "Look Out!",
// Style: dialog.WarningStyle,
// Buttons: ModalButtons(
// ModalButtonCreator {
// Label: "OK",
// ID: "ok",
// IsClose: true,
// },
// ModalButtonCreator {
// Label: "Cancel",
// ID: "cancel",
// IsClose: true,
// }
// ),
// }
// )
type ModalCreator struct {
ID string
Title string
TitlebarClass string
HasCloseBox bool
Style dialog.Style
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 != dialog.DefaultStyle {
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, &dialog.ButtonOptions{
Validates: button.Validates,
ConfirmationMessage: button.ConfirmationMessage,
PushLeft: button.PushLeft,
IsClose: button.IsClose,
Options: button.Options,
})
}
}
if c.OnButton != nil {
ctrl.On(event.DialogButton().Action(c.OnButton))
}
if c.Children != nil {
ctrl.AddControls(ctx, c.Children...)
}
return ctrl
}
// GetModal 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)
}