forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
221 lines (182 loc) · 4.77 KB
/
button.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
package types
import (
"github.com/GoAdminGroup/go-admin/modules/utils"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"html/template"
"net/url"
)
type Button interface {
Content() (template.HTML, template.JS)
GetAction() Action
URL() string
METHOD() string
ID() string
}
type BaseButton struct {
Id, Url, Method string
Title template.HTML
Action Action
}
func (b *BaseButton) Content() (template.HTML, template.JS) {
return "", ""
}
func (b *BaseButton) GetAction() Action {
return b.Action
}
func (b *BaseButton) ID() string {
return b.Id
}
func (b *BaseButton) URL() string {
return b.Url
}
func (b *BaseButton) METHOD() string {
return b.Method
}
type DefaultButton struct {
*BaseButton
Color template.HTML
TextColor template.HTML
Icon string
Direction template.HTML
}
func GetDefaultButton(title template.HTML, icon string, action Action, colors ...template.HTML) *DefaultButton {
return defaultButton(title, "right", icon, action, colors...)
}
func defaultButton(title, direction template.HTML, icon string, action Action, colors ...template.HTML) *DefaultButton {
id := btnUUID()
action.SetBtnId(id)
var color, textColor template.HTML
if len(colors) > 0 {
color = colors[0]
}
if len(colors) > 1 {
textColor = colors[1]
}
node := action.GetCallbacks()
return &DefaultButton{
BaseButton: &BaseButton{
Id: id,
Title: title,
Action: action,
Url: node.Path,
Method: node.Method,
},
Color: color,
TextColor: textColor,
Icon: icon,
Direction: direction,
}
}
func GetColumnButton(title template.HTML, icon string, action Action, colors ...template.HTML) *DefaultButton {
return defaultButton(title, "", icon, action, colors...)
}
func (b *DefaultButton) Content() (template.HTML, template.JS) {
color := template.HTML("")
if b.Color != template.HTML("") {
color = template.HTML(`background-color:`) + b.Color + template.HTML(`;`)
}
textColor := template.HTML("")
if b.TextColor != template.HTML("") {
textColor = template.HTML(`color:`) + b.TextColor + template.HTML(`;`)
}
style := template.HTML("")
addColor := color + textColor
if addColor != template.HTML("") {
style = template.HTML(`style="`) + addColor + template.HTML(`"`)
}
h := `<div class="btn-group pull-` + b.Direction + `" style="margin-right: 10px">
<a ` + style + ` class="` + template.HTML(b.Id) + ` btn btn-sm btn-default ` + b.Action.BtnClass() + `" ` + b.Action.BtnAttribute() + `>
<i class="fa ` + template.HTML(b.Icon) + `"></i> ` + b.Title + `
</a>
</div>` + b.Action.ExtContent()
return h, b.Action.Js()
}
type ActionButton struct {
*BaseButton
}
func GetActionButton(title template.HTML, action Action, ids ...string) *ActionButton {
id := ""
if len(ids) > 0 {
id = ids[0]
} else {
id = "action-info-btn-" + utils.Uuid(10)
}
action.SetBtnId(id)
node := action.GetCallbacks()
return &ActionButton{
BaseButton: &BaseButton{
Id: id,
Title: title,
Action: action,
Url: node.Path,
Method: node.Method,
},
}
}
func (b *ActionButton) Content() (template.HTML, template.JS) {
h := template.HTML(`<li style="cursor: pointer;"><a data-id="{{.Id}}" class="`+template.HTML(b.Id)+`" `+b.Action.BtnAttribute()+`>`+b.Title+`</a></li>`) + b.Action.ExtContent()
return h, b.Action.Js()
}
type Buttons []Button
func (b Buttons) Content() (template.HTML, template.JS) {
h := template.HTML("")
j := template.JS("")
for _, btn := range b {
hh, jj := btn.Content()
h += hh
j += jj
}
return h, j
}
func (b Buttons) FooterContent() template.HTML {
footer := template.HTML("")
for _, btn := range b {
footer += btn.GetAction().FooterContent()
}
return footer
}
func (b Buttons) CheckPermission(user models.UserModel) Buttons {
btns := make(Buttons, 0)
for _, btn := range b {
if user.CheckPermissionByUrlMethod(btn.URL(), btn.METHOD(), url.Values{}) {
btns = append(btns, btn)
}
}
return btns
}
type NavButton struct {
*BaseButton
Icon string
}
func GetNavButton(title template.HTML, icon string, action Action) *NavButton {
id := btnUUID()
action.SetBtnId(id)
node := action.GetCallbacks()
return &NavButton{
BaseButton: &BaseButton{
Id: id,
Title: title,
Action: action,
Url: node.Path,
Method: node.Method,
},
Icon: icon,
}
}
func (n *NavButton) Content() (template.HTML, template.JS) {
icon := template.HTML("")
title := template.HTML("")
if n.Icon != "" {
icon = template.HTML(`<i class="fa ` + n.Icon + `"></i>`)
}
if n.Title != "" {
title = `<span>` + n.Title + `</span>`
}
h := template.HTML(`<li>
<a class="`+template.HTML(n.Id)+`" `+n.Action.BtnAttribute()+`>
`+icon+`
`+title+`
</a>
</li>`) + n.Action.ExtContent()
return h, n.Action.Js()
}