NavLinks: Make ordering in navigation configurable (#20382)

The ordering of links in the navigation bar is currently based the order of the slice containing the navigation tree. Since Grafana supports adding more links to the navigation bar with `RunIndexDataHooks` which runs at the very end of the function this means that any link registered through a hook will be placed last in the slice and be displayed last in the menu. With this PR the ordering can be specified with a weight which allows for placing links created by extensions in a more intuitive place where applicable.

Stable sorting is used to ensure that the current FIFO ordering is preserved when either no weight is set or two items shares the same weight.
This commit is contained in:
Emil Tullstedt 2019-11-15 09:28:55 +01:00 committed by GitHub
parent d602da20f6
commit d4e013fd44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 32 deletions

View File

@ -22,6 +22,23 @@ type PluginCss struct {
Dark string `json:"dark"` Dark string `json:"dark"`
} }
const (
// These weights may be used by an extension to reliably place
// itself in relation to a particular item in the menu. The weights
// are negative to ensure that the default items are placed above
// any items with default weight.
WeightCreate = (iota - 20) * 100
WeightDashboard
WeightExplore
WeightProfile
WeightAlerting
WeightPlugin
WeightConfig
WeightAdmin
WeightHelp
)
type NavLink struct { type NavLink struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
@ -31,6 +48,7 @@ type NavLink struct {
Img string `json:"img,omitempty"` Img string `json:"img,omitempty"`
Url string `json:"url,omitempty"` Url string `json:"url,omitempty"`
Target string `json:"target,omitempty"` Target string `json:"target,omitempty"`
SortWeight int64 `json:"sortWeight,omitempty"`
Divider bool `json:"divider,omitempty"` Divider bool `json:"divider,omitempty"`
HideFromMenu bool `json:"hideFromMenu,omitempty"` HideFromMenu bool `json:"hideFromMenu,omitempty"`
HideFromTabs bool `json:"hideFromTabs,omitempty"` HideFromTabs bool `json:"hideFromTabs,omitempty"`

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/dtos"
@ -115,11 +116,12 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
children = append(children, &dtos.NavLink{Text: "Import", SubTitle: "Import dashboard from file or Grafana.com", Id: "import", Icon: "gicon gicon-dashboard-import", Url: setting.AppSubUrl + "/dashboard/import"}) children = append(children, &dtos.NavLink{Text: "Import", SubTitle: "Import dashboard from file or Grafana.com", Id: "import", Icon: "gicon gicon-dashboard-import", Url: setting.AppSubUrl + "/dashboard/import"})
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Text: "Create", Text: "Create",
Id: "create", Id: "create",
Icon: "fa fa-fw fa-plus", Icon: "fa fa-fw fa-plus",
Url: setting.AppSubUrl + "/dashboard/new", Url: setting.AppSubUrl + "/dashboard/new",
Children: children, Children: children,
SortWeight: dtos.WeightCreate,
}) })
} }
@ -132,21 +134,23 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
} }
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Text: "Dashboards", Text: "Dashboards",
Id: "dashboards", Id: "dashboards",
SubTitle: "Manage dashboards & folders", SubTitle: "Manage dashboards & folders",
Icon: "gicon gicon-dashboard", Icon: "gicon gicon-dashboard",
Url: setting.AppSubUrl + "/", Url: setting.AppSubUrl + "/",
Children: dashboardChildNavs, SortWeight: dtos.WeightDashboard,
Children: dashboardChildNavs,
}) })
if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR || setting.ViewersCanEdit) { if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR || setting.ViewersCanEdit) {
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Text: "Explore", Text: "Explore",
Id: "explore", Id: "explore",
SubTitle: "Explore your data", SubTitle: "Explore your data",
Icon: "gicon gicon-explore", Icon: "gicon gicon-explore",
Url: setting.AppSubUrl + "/explore", SortWeight: dtos.WeightExplore,
Url: setting.AppSubUrl + "/explore",
}) })
} }
@ -163,6 +167,7 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
Img: data.User.GravatarUrl, Img: data.User.GravatarUrl,
Url: setting.AppSubUrl + "/profile", Url: setting.AppSubUrl + "/profile",
HideFromMenu: true, HideFromMenu: true,
SortWeight: dtos.WeightProfile,
Children: []*dtos.NavLink{ Children: []*dtos.NavLink{
{Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "gicon gicon-preferences"}, {Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "gicon gicon-preferences"},
{Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true}, {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
@ -186,12 +191,13 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
} }
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Text: "Alerting", Text: "Alerting",
SubTitle: "Alert rules & notifications", SubTitle: "Alert rules & notifications",
Id: "alerting", Id: "alerting",
Icon: "gicon gicon-alert", Icon: "gicon gicon-alert",
Url: setting.AppSubUrl + "/alerting/list", Url: setting.AppSubUrl + "/alerting/list",
Children: alertChildNavs, Children: alertChildNavs,
SortWeight: dtos.WeightAlerting,
}) })
} }
@ -203,10 +209,11 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
for _, plugin := range enabledPlugins.Apps { for _, plugin := range enabledPlugins.Apps {
if plugin.Pinned { if plugin.Pinned {
appLink := &dtos.NavLink{ appLink := &dtos.NavLink{
Text: plugin.Name, Text: plugin.Name,
Id: "plugin-page-" + plugin.Id, Id: "plugin-page-" + plugin.Id,
Url: plugin.DefaultNavUrl, Url: plugin.DefaultNavUrl,
Img: plugin.Info.Logos.Small, Img: plugin.Info.Logos.Small,
SortWeight: dtos.WeightPlugin,
} }
for _, include := range plugin.Includes { for _, include := range plugin.Includes {
@ -297,12 +304,13 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
} }
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Id: "cfg", Id: "cfg",
Text: "Configuration", Text: "Configuration",
SubTitle: "Organization: " + c.OrgName, SubTitle: "Organization: " + c.OrgName,
Icon: "gicon gicon-cog", Icon: "gicon gicon-cog",
Url: configNodes[0].Url, Url: configNodes[0].Url,
Children: configNodes, SortWeight: dtos.WeightConfig,
Children: configNodes,
}) })
if c.IsGrafanaAdmin { if c.IsGrafanaAdmin {
@ -326,6 +334,7 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
Id: "admin", Id: "admin",
Icon: "gicon gicon-shield", Icon: "gicon gicon-shield",
Url: setting.AppSubUrl + "/admin/users", Url: setting.AppSubUrl + "/admin/users",
SortWeight: dtos.WeightAdmin,
Children: adminNavLinks, Children: adminNavLinks,
}) })
} }
@ -337,6 +346,7 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
Url: "#", Url: "#",
Icon: "gicon gicon-question", Icon: "gicon gicon-question",
HideFromMenu: true, HideFromMenu: true,
SortWeight: dtos.WeightHelp,
Children: []*dtos.NavLink{ Children: []*dtos.NavLink{
{Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"}, {Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
{Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"}, {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
@ -345,6 +355,10 @@ func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, er
}) })
hs.HooksService.RunIndexDataHooks(&data) hs.HooksService.RunIndexDataHooks(&data)
sort.SliceStable(data.NavTree, func(i, j int) bool {
return data.NavTree[i].SortWeight < data.NavTree[j].SortWeight
})
return &data, nil return &data, nil
} }