forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
303 lines (256 loc) · 7.11 KB
/
template.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
// 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 template
import (
"bytes"
"errors"
"fmt"
"html/template"
"path"
"plugin"
"strings"
"sync"
c "github.com/glvd/go-admin/modules/config"
"github.com/glvd/go-admin/modules/language"
"github.com/glvd/go-admin/modules/logger"
"github.com/glvd/go-admin/modules/menu"
"github.com/glvd/go-admin/plugins/admin/models"
"github.com/glvd/go-admin/template/login"
"github.com/glvd/go-admin/template/types"
)
// Template is the interface which contains methods of ui components.
// It will be used in the plugins for custom the ui.
type Template interface {
// Components
// layout
Col() types.ColAttribute
Row() types.RowAttribute
// form and table
Form() types.FormAttribute
Table() types.TableAttribute
DataTable() types.DataTableAttribute
Tree() types.TreeAttribute
Tabs() types.TabsAttribute
Alert() types.AlertAttribute
Paginator() types.PaginatorAttribute
Popup() types.PopupAttribute
Box() types.BoxAttribute
Label() types.LabelAttribute
Image() types.ImgAttribute
Button() types.ButtonAttribute
// Builder methods
GetTmplList() map[string]string
GetAssetList() []string
GetAsset(string) ([]byte, error)
GetTemplate(bool) (*template.Template, string)
}
func HTML(s string) template.HTML {
return template.HTML(s)
}
func CSS(s string) template.CSS {
return template.CSS(s)
}
func JS(s string) template.JS {
return template.JS(s)
}
// The templateMap contains templates registered.
var templateMap = make(map[string]Template)
// Get the template interface by theme name. If the
// name is not found, it panics.
func Get(theme string) Template {
if temp, ok := templateMap[theme]; ok {
return temp
}
panic("wrong theme name")
}
// Get the default template with the theme name set with the global config.
// If the name is not found, it panics.
func Default() Template {
if temp, ok := templateMap[c.Get().Theme]; ok {
return temp
}
panic("wrong theme name")
}
var (
templateMu sync.Mutex
compMu sync.Mutex
)
// Add makes a template available by the provided theme name.
// If Add is called twice with the same name or if template is nil,
// it panics.
func Add(name string, temp Template) {
templateMu.Lock()
defer templateMu.Unlock()
if temp == nil {
panic("template is nil")
}
if _, dup := templateMap[name]; dup {
panic("add template twice " + name)
}
templateMap[name] = temp
}
func AddFromPlugin(name string, mod string) {
plug, err := plugin.Open(mod)
if err != nil {
logger.Error("AddFromPlugin err", err)
panic(err)
}
tempPlugin, err := plug.Lookup(strings.Title(name))
if err != nil {
logger.Error("AddFromPlugin err", err)
panic(err)
}
var temp Template
temp, ok := tempPlugin.(Template)
if !ok {
logger.Error("AddFromPlugin err: unexpected type from module symbol")
panic(errors.New("AddFromPlugin err: unexpected type from module symbol"))
}
Add(name, temp)
}
// Component is the interface which stand for a ui component.
type Component interface {
// GetTemplate return a *template.Template and a given key.
GetTemplate() (*template.Template, string)
// GetAssetList return the assets url suffix used in the component.
// example:
//
// {{.UrlPrefix}}/assets/login/css/bootstrap.min.css => login/css/bootstrap.min.css
//
// See:
// https://github.com/glvd/go-admin/blob/master/template/login/theme1.tmpl#L32
// https://github.com/glvd/go-admin/blob/master/template/login/list.go
GetAssetList() []string
// GetAsset return the asset content according to the corresponding url suffix.
// Asset content is recommended to use the tool go-bindata to generate.
//
// See: http://github.com/jteeuwen/go-bindata
GetAsset(string) ([]byte, error)
GetContent() template.HTML
IsAPage() bool
GetName() string
}
var compMap = map[string]Component{
"login": login.GetLoginComponent(),
}
// GetComp gets the component by registered name. If the
// name is not found, it panics.
func GetComp(name string) Component {
if comp, ok := compMap[name]; ok {
return comp
}
panic("wrong component name")
}
func GetComponentAssetLists() []string {
assets := make([]string, 0)
for _, comp := range compMap {
assets = append(assets, comp.GetAssetList()...)
}
return assets
}
func GetComponentAssetListsWithinPage() []string {
assets := make([]string, 0)
for _, comp := range compMap {
if !comp.IsAPage() {
assets = append(assets, comp.GetAssetList()...)
}
}
return assets
}
func GetComponentAssetListsHTML() (res template.HTML) {
assets := GetComponentAssetListsWithinPage()
for i := 0; i < len(assets); i++ {
res += getHTMLFromAssetUrl(assets[i])
}
return
}
func getHTMLFromAssetUrl(s string) template.HTML {
fileSuffix := path.Ext(s)
fileSuffix = strings.Replace(fileSuffix, ".", "", -1)
if fileSuffix == "css" {
return template.HTML(`<link rel="stylesheet" href="` + c.Get().AssetUrl + c.Get().Url("/assets"+s) + `">`)
}
if fileSuffix == "js" {
return template.HTML(`<script src="` + c.Get().AssetUrl + c.Get().Url("/assets"+s) + `"></script>`)
}
return ""
}
func GetAsset(path string) ([]byte, error) {
for _, comp := range compMap {
res, err := comp.GetAsset(path)
if err == nil {
return res, err
}
}
return nil, errors.New(path + " not found")
}
// AddComp makes a component available by the provided name.
// If Add is called twice with the same name or if component is nil,
// it panics.
func AddComp(comp Component) {
compMu.Lock()
defer compMu.Unlock()
if comp == nil {
panic("component is nil")
}
if _, dup := compMap[comp.GetName()]; dup {
panic("add component twice " + comp.GetName())
}
compMap[comp.GetName()] = comp
}
// AddLoginComp add the specified login component.
func AddLoginComp(comp Component) {
compMu.Lock()
defer compMu.Unlock()
compMap["login"] = comp
}
// SetComp makes a component available by the provided name.
// If the value corresponding to the key is empty or if component is nil,
// it panics.
func SetComp(name string, comp Component) {
compMu.Lock()
defer compMu.Unlock()
if comp == nil {
panic("component is nil")
}
if _, dup := compMap[name]; dup {
compMap[name] = comp
}
}
func Execute(tmpl *template.Template,
tmplName string,
user models.UserModel,
panel types.Panel,
config c.Config,
globalMenu *menu.Menu) *bytes.Buffer {
buf := new(bytes.Buffer)
err := tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(user, *globalMenu, panel, config, GetComponentAssetListsHTML()))
if err != nil {
fmt.Println("Execute err", err)
}
return buf
}
func DefaultFuncMap() template.FuncMap {
return template.FuncMap{
"lang": language.Get,
"langHtml": language.GetFromHtml,
"link": func(cdnUrl, prefixUrl, assetsUrl string) string {
if cdnUrl == "" {
return prefixUrl + assetsUrl
}
return cdnUrl + assetsUrl
},
"isLinkUrl": func(s string) bool {
return (len(s) > 7 && s[:7] == "http://") || (len(s) > 8 && s[:8] == "https://")
},
}
}
type BaseComponent struct {
}
func (b BaseComponent) GetAssetList() []string {
return make([]string, 0)
}
func (b BaseComponent) GetAsset(name string) ([]byte, error) {
return nil, nil
}