2022-09-22 15:04:48 -05:00
package navtree
2022-09-28 01:29:35 -05:00
import (
"encoding/json"
"sort"
)
2022-09-22 15:04:48 -05:00
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.
2022-10-03 09:05:19 -05:00
WeightHome = ( iota - 20 ) * 100
WeightSavedItems
2022-09-22 15:04:48 -05:00
WeightCreate
WeightDashboard
WeightExplore
WeightAlerting
2022-10-05 04:46:27 -05:00
WeightAlertsAndIncidents
WeightMonitoring
2023-04-14 05:46:18 -05:00
WeightDataConnections
2022-10-05 04:46:27 -05:00
WeightApps
2023-04-17 10:01:32 -05:00
WeightPlugin
WeightConfig
2022-09-22 15:04:48 -05:00
WeightAdmin
WeightProfile
WeightHelp
)
2022-09-28 01:29:35 -05:00
const (
2022-11-18 03:05:45 -06:00
NavIDRoot = "root"
2023-04-20 05:10:12 -05:00
NavIDDashboards = "dashboards/browse"
2022-09-28 01:29:35 -05:00
NavIDCfg = "cfg" // NavIDCfg is the id for org configuration navigation node
NavIDAlertsAndIncidents = "alerts-and-incidents"
NavIDAlerting = "alerting"
2023-03-16 12:55:23 -05:00
NavIDAlertingLegacy = "alerting-legacy"
2022-09-28 01:29:35 -05:00
NavIDMonitoring = "monitoring"
NavIDReporting = "reports"
2022-10-05 04:46:27 -05:00
NavIDApps = "apps"
2022-09-28 01:29:35 -05:00
)
2022-09-22 15:04:48 -05:00
type NavLink struct {
2023-04-17 10:01:32 -05:00
Id string ` json:"id,omitempty" `
Text string ` json:"text" `
SubTitle string ` json:"subTitle,omitempty" `
Icon string ` json:"icon,omitempty" ` // Available icons can be browsed in Storybook: https://developers.grafana.com/ui/latest/index.html?path=/story/docs-overview-icon--icons-overview
Img string ` json:"img,omitempty" `
Url string ` json:"url,omitempty" `
Target string ` json:"target,omitempty" `
SortWeight int64 ` json:"sortWeight,omitempty" `
HideFromTabs bool ` json:"hideFromTabs,omitempty" `
RoundIcon bool ` json:"roundIcon,omitempty" `
IsSection bool ` json:"isSection,omitempty" `
Children [ ] * NavLink ` json:"children,omitempty" `
HighlightText string ` json:"highlightText,omitempty" `
HighlightID string ` json:"highlightId,omitempty" `
EmptyMessageId string ` json:"emptyMessageId,omitempty" `
PluginID string ` json:"pluginId,omitempty" ` // (Optional) The ID of the plugin that registered nav link (e.g. as a standalone plugin page)
IsCreateAction bool ` json:"isCreateAction,omitempty" `
2022-09-22 15:04:48 -05:00
}
2022-09-28 01:29:35 -05:00
func ( node * NavLink ) Sort ( ) {
Sort ( node . Children )
}
type NavTreeRoot struct {
Children [ ] * NavLink
}
func ( root * NavTreeRoot ) AddSection ( node * NavLink ) {
root . Children = append ( root . Children , node )
}
func ( root * NavTreeRoot ) RemoveSection ( node * NavLink ) {
var result [ ] * NavLink
for _ , child := range root . Children {
if child != node {
result = append ( result , child )
}
}
root . Children = result
}
func ( root * NavTreeRoot ) FindById ( id string ) * NavLink {
return FindById ( root . Children , id )
}
func ( root * NavTreeRoot ) Sort ( ) {
Sort ( root . Children )
}
func ( root * NavTreeRoot ) MarshalJSON ( ) ( [ ] byte , error ) {
return json . Marshal ( root . Children )
}
func Sort ( nodes [ ] * NavLink ) {
sort . SliceStable ( nodes , func ( i , j int ) bool {
iw := nodes [ i ] . SortWeight
if iw == 0 {
iw = int64 ( i ) + 1
}
jw := nodes [ j ] . SortWeight
if jw == 0 {
jw = int64 ( j ) + 1
}
2022-09-22 15:04:48 -05:00
2022-09-28 01:29:35 -05:00
return iw < jw
} )
for _ , child := range nodes {
child . Sort ( )
2022-09-22 15:04:48 -05:00
}
2022-09-28 01:29:35 -05:00
}
2023-04-20 05:10:12 -05:00
func ( root * NavTreeRoot ) ApplyAdminIA ( ) {
2022-11-18 09:11:59 -06:00
orgAdminNode := root . FindById ( NavIDCfg )
if orgAdminNode != nil {
2023-01-13 00:32:09 -06:00
adminNodeLinks := [ ] * NavLink { }
2022-11-18 09:11:59 -06:00
2023-01-13 09:27:26 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "datasources" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "plugins" ) )
2023-04-20 05:10:12 -05:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "global-users" ) )
2023-01-13 09:27:26 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "teams" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "serviceaccounts" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "apikeys" ) )
2023-04-20 05:10:12 -05:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "org-settings" ) )
2023-04-13 09:07:43 -05:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "authentication" ) )
2023-01-13 00:32:09 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "server-settings" ) )
2023-01-13 09:27:26 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "global-orgs" ) )
2023-08-03 13:17:00 -05:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "feature-toggles" ) )
2022-11-18 09:11:59 -06:00
2023-01-13 09:27:26 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "upgrading" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "licensing" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "recordedQueries" ) ) // enterprise only
2023-01-13 00:32:09 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "correlations" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "plugin-page-grafana-cloud-link-app" ) )
2022-11-18 09:11:59 -06:00
2023-01-13 00:32:09 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "ldap" ) )
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "standalone-plugin-page-/a/grafana-auth-app" ) ) // Cloud Access Policies
2022-12-08 12:57:33 -06:00
adminNodeLinks = AppendIfNotNil ( adminNodeLinks , root . FindById ( "storage" ) )
2022-11-18 09:11:59 -06:00
if len ( adminNodeLinks ) > 0 {
orgAdminNode . Children = adminNodeLinks
} else {
root . RemoveSection ( orgAdminNode )
}
}
}
func AppendIfNotNil ( children [ ] * NavLink , newChild * NavLink ) [ ] * NavLink {
if newChild != nil {
return append ( children , newChild )
}
return children
}
2022-09-28 01:29:35 -05:00
func FindById ( nodes [ ] * NavLink , id string ) * NavLink {
for _ , child := range nodes {
if child . Id == id {
return child
} else if len ( child . Children ) > 0 {
if found := FindById ( child . Children , id ) ; found != nil {
return found
}
}
2022-09-22 15:04:48 -05:00
}
2022-09-28 01:29:35 -05:00
return nil
2022-09-22 15:04:48 -05:00
}