-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
page.go
177 lines (145 loc) · 4.27 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
// 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 (
"fmt"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/config"
"github.com/GoAdminGroup/go-admin/modules/menu"
"github.com/GoAdminGroup/go-admin/modules/system"
"github.com/GoAdminGroup/go-admin/modules/utils"
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
"html/template"
"strconv"
)
// Attribute is the component interface of template. Every component of
// template should implement it.
type Attribute struct {
TemplateList map[string]string
}
// 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
// Components assets
AssetsList template.HTML
}
func NewPage(user models.UserModel, menu menu.Menu, panel Panel, cfg config.Config, assetsList template.HTML) Page {
return Page{
User: user,
Menu: menu,
Panel: panel,
System: SystemInfo{
Version: system.Version(),
},
UrlPrefix: cfg.AssertPrefix(),
Title: cfg.Title,
Logo: cfg.Logo,
MiniLogo: cfg.MiniLogo,
ColorScheme: cfg.ColorScheme,
IndexUrl: cfg.GetIndexURL(),
CdnUrl: cfg.AssetUrl,
CustomHeadHtml: cfg.CustomHeadHtml,
CustomFootHtml: cfg.CustomFootHtml,
AssetsList: assetsList,
}
}
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
}
// Panel contains the main content of the template which used as pjax.
type Panel struct {
Title string
Description string
Content template.HTML
Url string
// Whether to toggle the sidebar
MiniSidebar bool
// Auto refresh page switch.
AutoRefresh bool
// Refresh page intervals, the unit is second.
RefreshInterval []int
}
func (p Panel) GetContent(params ...bool) Panel {
prod := false
if len(params) > 0 {
prod = params[0]
}
animation := template.HTML("")
style := template.HTML("")
remove := template.HTML("")
ani := config.Get().Animation
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 prod {
utils.CompressedContent(&p.Content)
}
return p
}
type GetPanelFn func(ctx interface{}) (Panel, error)
type GetPanelInfoFn func(ctx *context.Context) (Panel, error)