2016-01-09 23:34:20 +01:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/url"
|
|
|
|
|
"path"
|
2016-02-09 22:10:36 +01:00
|
|
|
"strings"
|
|
|
|
|
|
2016-03-14 17:16:10 +01:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-02-09 22:10:36 +01:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2016-01-09 23:34:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FrontendPluginBase struct {
|
|
|
|
|
PluginBase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (fp *FrontendPluginBase) initFrontendPlugin() {
|
2016-04-08 16:42:33 -04:00
|
|
|
if isExternalPlugin(fp.PluginDir) {
|
2016-01-09 23:34:20 +01:00
|
|
|
StaticRoutes = append(StaticRoutes, &PluginStaticRoute{
|
2016-03-14 17:16:10 +01:00
|
|
|
Directory: fp.PluginDir,
|
2016-01-09 23:34:20 +01:00
|
|
|
PluginId: fp.Id,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 22:10:36 +01:00
|
|
|
fp.handleModuleDefaults()
|
|
|
|
|
|
2016-03-17 10:15:16 +01:00
|
|
|
fp.Info.Logos.Small = getPluginLogoUrl(fp.Type, fp.Info.Logos.Small, fp.BaseUrl)
|
|
|
|
|
fp.Info.Logos.Large = getPluginLogoUrl(fp.Type, fp.Info.Logos.Large, fp.BaseUrl)
|
2016-02-09 22:10:36 +01:00
|
|
|
|
|
|
|
|
for i := 0; i < len(fp.Info.Screenshots); i++ {
|
2016-03-07 21:45:49 +01:00
|
|
|
fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.BaseUrl)
|
2016-02-09 22:36:42 +08:00
|
|
|
}
|
2016-02-09 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-17 10:15:16 +01:00
|
|
|
func getPluginLogoUrl(pluginType, path, baseUrl string) string {
|
2016-03-15 09:15:24 +01:00
|
|
|
if path == "" {
|
2016-03-17 10:15:16 +01:00
|
|
|
return "public/img/icn-" + pluginType + ".svg"
|
2016-03-15 09:15:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return evalRelativePluginUrlPath(path, baseUrl)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 22:10:36 +01:00
|
|
|
func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
|
2018-01-03 22:07:38 +01:00
|
|
|
appSubPath := strings.Replace(strings.Replace(fp.PluginDir, app.PluginDir, "", 1), "\\", "/", -1)
|
2016-02-09 22:10:36 +01:00
|
|
|
fp.IncludedInAppId = app.Id
|
|
|
|
|
fp.BaseUrl = app.BaseUrl
|
2016-09-27 14:39:51 +02:00
|
|
|
|
|
|
|
|
if isExternalPlugin(app.PluginDir) {
|
|
|
|
|
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module"
|
|
|
|
|
} else {
|
|
|
|
|
fp.Module = util.JoinUrlFragments("app/plugins/app/"+app.Id, appSubPath) + "/module"
|
|
|
|
|
}
|
2016-01-09 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (fp *FrontendPluginBase) handleModuleDefaults() {
|
|
|
|
|
|
2016-04-08 16:42:33 -04:00
|
|
|
if isExternalPlugin(fp.PluginDir) {
|
2016-01-09 23:56:39 +01:00
|
|
|
fp.Module = path.Join("plugins", fp.Id, "module")
|
2016-02-09 18:17:32 +01:00
|
|
|
fp.BaseUrl = path.Join("public/plugins", fp.Id)
|
2016-01-09 23:34:20 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 16:42:33 -04:00
|
|
|
fp.IsCorePlugin = true
|
2016-01-09 23:34:20 +01:00
|
|
|
fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module")
|
2016-02-09 18:17:32 +01:00
|
|
|
fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id)
|
2016-01-09 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-08 16:42:33 -04:00
|
|
|
func isExternalPlugin(pluginDir string) bool {
|
2016-03-14 17:16:10 +01:00
|
|
|
return !strings.Contains(pluginDir, setting.StaticRootPath)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:31:02 +01:00
|
|
|
func evalRelativePluginUrlPath(pathStr string, baseUrl string) string {
|
2016-02-26 18:25:39 +01:00
|
|
|
if pathStr == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 23:34:20 +01:00
|
|
|
u, _ := url.Parse(pathStr)
|
|
|
|
|
if u.IsAbs() {
|
|
|
|
|
return pathStr
|
|
|
|
|
}
|
2016-03-07 14:31:02 +01:00
|
|
|
return path.Join(baseUrl, pathStr)
|
2016-01-09 23:34:20 +01:00
|
|
|
}
|