gf/frame/gins/gins_view.go

58 lines
1.6 KiB
Go
Raw Normal View History

2020-12-14 13:26:48 +08:00
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
2020-02-23 20:25:55 +08:00
//
// 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"
2020-02-23 20:25:55 +08:00
)
const (
2020-12-14 13:26:48 +08:00
frameCoreComponentNameViewer = "gf.core.component.viewer"
configNodeNameViewer = "viewer"
2020-02-23 20:25:55 +08:00
)
// 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
2020-02-23 20:25:55 +08:00
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
2020-12-14 13:26:48 +08:00
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{}
2020-12-14 13:26:48 +08:00
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameViewer)
2020-07-10 00:20:46 +08:00
if nodeKey == "" {
2020-12-14 13:26:48 +08:00
nodeKey = configNodeNameViewer
2020-07-10 00:20:46 +08:00
}
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)
2020-02-23 20:25:55 +08:00
}
}
}
return view
2020-02-23 20:25:55 +08:00
}