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