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"
|
2020-07-08 20:48:29 +08:00
|
|
|
"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 {
|
2020-12-14 13:02:08 +08:00
|
|
|
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)
|
2020-03-10 20:13:36 +08:00
|
|
|
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
|
|
|
return getViewInstance(instanceName)
|
|
|
|
}).(*gview.View)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getViewInstance(name ...string) *gview.View {
|
2020-12-14 13:02:08 +08:00
|
|
|
instanceName := gview.DefaultName
|
2020-03-10 20:13:36 +08:00
|
|
|
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)
|
2020-03-10 20:13:36 +08:00
|
|
|
}
|
2020-07-08 20:48:29 +08:00
|
|
|
if len(m) > 0 {
|
2020-03-10 20:13:36 +08:00
|
|
|
if err := view.SetConfigWithMap(m); err != nil {
|
|
|
|
panic(err)
|
2020-02-23 20:25:55 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 20:13:36 +08:00
|
|
|
}
|
|
|
|
return view
|
2020-02-23 20:25:55 +08:00
|
|
|
}
|