forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
331 lines (275 loc) · 7.8 KB
/
page.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
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package types
import (
"bytes"
"fmt"
"html/template"
"strconv"
"github.com/marerm27/go-admin/context"
"github.com/marerm27/go-admin/modules/config"
"github.com/marerm27/go-admin/modules/menu"
"github.com/marerm27/go-admin/modules/system"
"github.com/marerm27/go-admin/modules/utils"
"github.com/marerm27/go-admin/plugins/admin/models"
)
// Attribute is the component interface of template. Every component of
// template should implement it.
type Attribute struct {
TemplateList map[string]string
Separation bool
}
// Page used in the template as a top variable.
type Page struct {
// User is the login user.
User models.UserModel
// Menu is the left side menu of the template.
Menu menu.Menu
// Panel is the main content of template.
Panel Panel
// System contains some system info.
System SystemInfo
// UrlPrefix is the prefix of url.
UrlPrefix string
// Title is the title of the web page.
Title string
// Logo is the logo of the template.
Logo template.HTML
// MiniLogo is the downsizing logo of the template.
MiniLogo template.HTML
// ColorScheme is the color scheme of the template.
ColorScheme string
// IndexUrl is the home page url of the site.
IndexUrl string
// AssetUrl is the cdn link of assets
CdnUrl string
// Custom html in the tag head.
CustomHeadHtml template.HTML
// Custom html after body.
CustomFootHtml template.HTML
TmplHeadHTML template.HTML
TmplFootJS template.HTML
// Components assets
AssetsList template.HTML
// Footer info
FooterInfo template.HTML
// Load as Iframe or not
Iframe bool
// Whether update menu or not
UpdateMenu bool
// Top Nav Buttons
navButtons Buttons
NavButtonsHTML template.HTML
}
type NewPageParam struct {
User models.UserModel
Menu *menu.Menu
UpdateMenu bool
Panel Panel
Logo template.HTML
Assets template.HTML
Buttons Buttons
Iframe bool
TmplHeadHTML template.HTML
TmplFootJS template.HTML
NavButtonsHTML template.HTML
NavButtonsJS template.HTML
}
func (param *NewPageParam) NavButtonsAndJS() (template.HTML, template.HTML) {
navBtnFooter := template.HTML("")
navBtn := template.HTML("")
btnJS := template.JS("")
for _, btn := range param.Buttons {
if btn.IsType(ButtonTypeNavDropDown) {
content, js := btn.Content()
navBtn += content
btnJS += js
for _, item := range btn.(*NavDropDownButton).Items {
navBtnFooter += item.GetAction().FooterContent()
_, js := item.Content()
btnJS += js
}
} else {
navBtnFooter += btn.GetAction().FooterContent()
content, js := btn.Content()
navBtn += content
btnJS += js
}
}
return template.HTML(ParseTableDataTmpl(navBtn)),
navBtnFooter + template.HTML(ParseTableDataTmpl(`<script>`+btnJS+`</script>`))
}
func NewPage(param *NewPageParam) *Page {
if param.NavButtonsHTML == template.HTML("") {
param.NavButtonsHTML, param.NavButtonsJS = param.NavButtonsAndJS()
}
logo := param.Logo
if logo == template.HTML("") {
logo = config.GetLogo()
}
return &Page{
User: param.User,
Menu: *param.Menu,
Panel: param.Panel,
UpdateMenu: param.UpdateMenu,
System: SystemInfo{
Version: system.Version(),
Theme: config.GetTheme(),
},
UrlPrefix: config.AssertPrefix(),
Title: config.GetTitle(),
Logo: logo,
MiniLogo: config.GetMiniLogo(),
ColorScheme: config.GetColorScheme(),
IndexUrl: config.GetIndexURL(),
CdnUrl: config.GetAssetUrl(),
CustomHeadHtml: config.GetCustomHeadHtml(),
CustomFootHtml: config.GetCustomFootHtml() + param.NavButtonsJS,
FooterInfo: config.GetFooterInfo(),
AssetsList: param.Assets,
navButtons: param.Buttons,
Iframe: param.Iframe,
NavButtonsHTML: param.NavButtonsHTML,
TmplHeadHTML: param.TmplHeadHTML,
TmplFootJS: param.TmplFootJS,
}
}
func (page *Page) AddButton(title template.HTML, icon string, action Action) *Page {
page.navButtons = append(page.navButtons, GetNavButton(title, icon, action))
page.CustomFootHtml += action.FooterContent()
return page
}
func NewPagePanel(panel Panel) *Page {
return &Page{
Panel: panel,
System: SystemInfo{
Version: system.Version(),
},
}
}
// SystemInfo contains basic info of system.
type SystemInfo struct {
Version string
Theme string
}
type TableRowData struct {
Id template.HTML
Ids template.HTML
Value map[string]InfoItem
}
func ParseTableDataTmpl(content interface{}) string {
var (
c string
ok bool
)
if c, ok = content.(string); !ok {
if cc, ok := content.(template.HTML); ok {
c = string(cc)
} else {
c = string(content.(template.JS))
}
}
t := template.New("row_data_tmpl")
t, _ = t.Parse(c)
buf := new(bytes.Buffer)
_ = t.Execute(buf, TableRowData{Ids: `typeof(selectedRows)==="function" ? selectedRows().join() : ""`})
return buf.String()
}
func ParseTableDataTmplWithID(id template.HTML, content string, value ...map[string]InfoItem) string {
t := template.New("row_data_tmpl")
t, _ = t.Parse(content)
buf := new(bytes.Buffer)
v := make(map[string]InfoItem)
if len(value) > 0 {
v = value[0]
}
_ = t.Execute(buf, TableRowData{
Id: id,
Ids: `typeof(selectedRows)==="function" ? selectedRows().join() : ""`,
Value: v,
})
return buf.String()
}
// Panel contains the main content of the template which used as pjax.
type Panel struct {
Title template.HTML
Description template.HTML
Content template.HTML
CSS template.CSS
JS template.JS
Url string
// Whether to toggle the sidebar
MiniSidebar bool
// Auto refresh page switch.
AutoRefresh bool
// Refresh page intervals, the unit is second.
RefreshInterval []int
Callbacks Callbacks
}
type Component interface {
GetContent() template.HTML
GetJS() template.JS
GetCSS() template.CSS
GetCallbacks() Callbacks
}
func (p Panel) AddComponent(comp Component) Panel {
p.JS += comp.GetJS()
p.CSS += comp.GetCSS()
p.Content += comp.GetContent()
p.Callbacks = append(p.Callbacks, comp.GetCallbacks()...)
return p
}
func (p Panel) AddJS(js template.JS) Panel {
p.JS += js
return p
}
func (p Panel) GetContent(params ...bool) Panel {
compress := false
if len(params) > 0 {
compress = params[0]
}
var (
animation, style, remove = template.HTML(""), template.HTML(""), template.HTML("")
ani = config.GetAnimation()
)
if ani.Type != "" && (len(params) < 2 || params[1]) {
animation = template.HTML(` class='pjax-container-content animated ` + ani.Type + `'`)
if ani.Delay != 0 {
style = template.HTML(fmt.Sprintf(`animation-delay: %fs;-webkit-animation-delay: %fs;`, ani.Delay, ani.Delay))
}
if ani.Duration != 0 {
style = template.HTML(fmt.Sprintf(`animation-duration: %fs;-webkit-animation-duration: %fs;`, ani.Duration, ani.Duration))
}
if style != "" {
style = ` style="` + style + `"`
}
remove = template.HTML(`<script>
$('.pjax-container-content .modal.fade').on('show.bs.modal', function (event) {
// Fix Animate.css
$('.pjax-container-content').removeClass('` + ani.Type + `');
});
</script>`)
}
p.Content = `<div` + animation + style + ">" + p.Content + "</div>" + remove
if p.MiniSidebar {
p.Content += `<script>$("body").addClass("sidebar-collapse")</script>`
}
if p.AutoRefresh {
refreshTime := 60
if len(p.RefreshInterval) > 0 {
refreshTime = p.RefreshInterval[0]
}
p.Content += `<script>
window.setTimeout(function(){
$.pjax.reload('#pjax-container');
}, ` + template.HTML(strconv.Itoa(refreshTime*1000)) + `);
</script>`
}
if compress {
utils.CompressedContent(&p.Content)
}
return p
}
type GetPanelFn func(ctx interface{}) (Panel, error)
type GetPanelInfoFn func(ctx *context.Context) (Panel, error)