/
gins_view.go
57 lines (52 loc) · 1.58 KB
/
gins_view.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/os/gview"
"github.com/gogf/gf/util/gutil"
)
const (
frameCoreComponentNameViewer = "gf.core.component.viewer"
configNodeNameViewer = "viewer"
)
// View returns an instance of View with default settings.
// The parameter <name> is the name for the instance.
func View(name ...string) *gview.View {
instanceName := gview.DefaultName
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameViewer, instanceName)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
return getViewInstance(instanceName)
}).(*gview.View)
}
func getViewInstance(name ...string) *gview.View {
instanceName := gview.DefaultName
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
view := gview.Instance(instanceName)
// To avoid file no found error while it's not necessary.
if Config().Available() {
var m map[string]interface{}
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameViewer)
if nodeKey == "" {
nodeKey = configNodeNameViewer
}
m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
if len(m) == 0 {
m = Config().GetMap(nodeKey)
}
if len(m) > 0 {
if err := view.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return view
}