-
Notifications
You must be signed in to change notification settings - Fork 6
/
modal.go
315 lines (262 loc) · 8.11 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
package control
import (
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/event"
"github.com/goradd/goradd/pkg/page/action"
"github.com/goradd/goradd/pkg/html"
config2 "github.com/goradd/goradd/pkg/bootstrap/config"
"context"
"fmt"
)
type ModalBackdropType int
const (
ModalBackdrop ModalBackdropType = iota
ModalNoBackdrop
ModalStaticBackdrop
)
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 (
ButtonClick = iota + 3000
DialogClosing
DialogClosed
)
func NewModal(parent page.ControlI, id string) *Modal {
d := &Modal{}
d.Init(d, parent, id)
return d
}
func (d *Modal) Init(self page.ControlI, parent page.ControlI, id string) {
d.Panel.Init(self, parent, id)
d.Tag = "div"
d.SetShouldAutoRender(true)
d.SetValidationType(page.ValidateChildrenOnly) // allows sub items to validate and have validation stop here
d.SetBlockParentValidation(true)
d.On(event.Event("hide.bs.modal").Validate(page.ValidateNone), action.Trigger(d.ID(), event.DialogClosingEvent, nil))
d.On(event.Event("hidden.bs.modal").Validate(page.ValidateNone), action.Trigger(d.ID(), event.DialogClosedEvent, nil))
d.On(event.Event("hidden.bs.modal").Validate(page.ValidateNone), action.Ajax(d.ID(), DialogClosed), action.PrivateAction{})
config2.LoadBootstrap(d.ParentForm())
d.AddClass("modal fade").
SetAttribute("tabindex", -1).
SetAttribute("role", "dialog").
SetAttribute("aria-labelledby", d.ID() + "-title").
SetAttribute("aria-hidden", true)
d.titleBar = NewTitleBar(d, d.ID() + "-titlebar")
d.buttonBar = control.NewPanel(d, d.ID() + "-btnbar")
}
func (d *Modal) this() ModalI {
return d.Self.(ModalI)
}
func (d *Modal) SetTitle(t string) {
if d.titleBar.title != t {
d.titleBar.title = t
d.titleBar.Refresh()
}
}
func (d *Modal) SetHasCloseBox(h bool) {
if d.titleBar.hasCloseBox != h {
d.titleBar.hasCloseBox = h
d.titleBar.Refresh()
}
}
func (d *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
}
d.titleBar.RemoveClassesWithPrefix("bg-")
d.titleBar.RemoveClassesWithPrefix("text-")
d.titleBar.AddClass(class)
}
func (d *Modal) SetBackdrop(b ModalBackdropType) {
d.backdrop = b
d.Refresh()
}
func (d *Modal) Title() string {
return d.titleBar.title
}
func (d *Modal) AddTitlebarClass(class string) {
d.titleBar.AddClass(class)
}
func (d *Modal) ΩDrawingAttributes() *html.Attributes {
a := d.Panel.ΩDrawingAttributes()
a.SetDataAttribute("grctl", "bs-modal")
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 (d *Modal) AddButton(
label string,
id string,
options *control.DialogButtonOptions,
) {
if id == "" {
id = label
}
btn := NewButton(d.buttonBar, d.ID() + "-btn-" + id)
btn.SetLabel(label)
if options != nil {
if options.Validates {
btn.SetValidationType(page.ValidateContainer)
}
if !options.PushLeft && !d.foundRight {
btn.AddClass("ml-auto")
d.foundRight = true
}
if options.IsClose {
btn.SetAttribute("data-dismiss", "modal") // make it a close button
} else if options.ConfirmationMessage == "" {
btn.On(event.Click(), action.Trigger(d.ID(), event.DialogButtonEvent, id))
} else {
btn.On(event.Click(),
action.Confirm(options.ConfirmationMessage),
action.Trigger(d.ID(), event.DialogButtonEvent, id),
)
}
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))
}
}
}
d.buttonBar.Refresh()
}
func (d *Modal) RemoveButton(id string) {
d.buttonBar.RemoveChild(d.ID() + "-btn-" + id)
d.buttonBar.Refresh()
}
func (d *Modal) RemoveAllButtons() {
d.buttonBar.RemoveChildren()
d.Refresh()
}
func (d *Modal) SetButtonVisible(id string, visible bool) ModalI {
if ctrl := d.buttonBar.Child(d.ID() + "-btn-" + id); ctrl != nil {
ctrl.SetVisible(visible)
}
return d.this()
}
// SetButtonStyle sets css styles on a button that is already in the dialog
func (d *Modal) SetButtonStyle(id string, a *html.Style) ModalI {
if ctrl := d.buttonBar.Child(d.ID() + "-btn-" + id); ctrl != nil {
ctrl.SetStyles(a)
}
return d.this()
}
// 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 (d *Modal) AddCloseButton(label string) ModalI {
d.AddButton(label,"", &control.DialogButtonOptions{IsClose:true})
return d.this()
}
func (d *Modal) PrivateAction(ctx context.Context, a page.ActionParams) {
switch a.ID {
case DialogClosed:
d.closed()
}
}
func (d *Modal) Open() {
if d.Parent() == nil {
d.SetParent(d.ParentForm()) // This is a saved modal which has previously been created and removed. Insert it back into the form.
}
d.SetVisible(true)
d.isOpen = true
//d.Refresh()
d.AddRenderScript("modal", "show")
}
func (d *Modal) Close() {
d.ParentForm().Response().ExecuteControlCommand(d.ID(), "modal", page.PriorityLow, "hide")
}
func (d *Modal) closed() {
d.isOpen = false
//d.Remove()
d.SetVisible(false)
}
func (d *Modal) ΩPutCustomScript(ctx context.Context, response *page.Response) {
var backdrop interface{}
switch d.backdrop {
case ModalBackdrop:
backdrop = true
case ModalNoBackdrop:
backdrop = false
case ModalStaticBackdrop:
backdrop = "static"
}
script := fmt.Sprintf (
`$j("#%s").modal({backdrop: %#v, keyboard: %t, focus: true, show: %t});`,
d.ID(), backdrop, d.closeOnEscape, d.isOpen)
response.ExecuteJavaScript(script, page.PriorityStandard)
}
/**
Alert creates a message dialog.
If you specify no buttons, a close box in the corner will be created that will just close the dialog. If you
specify just a string in buttons, or just one string as a slice of strings, one button will be shown that will just close the message.
If you specify more than one button, the first button will be the default button (the one pressed if the user presses the return key). In
this case, you will need to detect the button by adding a On(event.DialogButton(), action) to the dialog returned.
You will also be responsible for calling "Close()" on the dialog after detecting a button in this case.
*/
func BootstrapAlert(form page.FormI, message string, buttons interface{}) control.DialogI {
dlg := NewModal(form, "")
dlg.SetText(message)
if buttons != nil {
switch b := buttons.(type) {
case string:
dlg.AddCloseButton(b)
case []string:
if len(b) == 1 {
dlg.AddCloseButton(b[0])
} else {
dlg.AddButton(b[0], "", &control.DialogButtonOptions{Options: map[string]interface{}{"style": ButtonStylePrimary}})
for _, l := range b[1:] {
dlg.AddButton(l, "", nil)
}
}
}
} else {
dlg.SetHasCloseBox(true)
}
dlg.Open()
return dlg
}
type TitleBar struct {
control.Panel
hasCloseBox bool
title string
}
func NewTitleBar(parent page.ControlI, id string) *TitleBar {
d := &TitleBar{}
d.Panel.Init(d, parent, id)
return d
}
func init() {
control.SetAlertFunction(BootstrapAlert)
}