navModel.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { DataSourceSettings, PluginType, NavModel, NavModelItem, PluginInclude } from '@grafana/ui';
  2. import config from 'app/core/config';
  3. import { GenericDataSourcePlugin } from '../settings/PluginSettings';
  4. export function buildNavModel(dataSource: DataSourceSettings, plugin: GenericDataSourcePlugin): NavModelItem {
  5. const pluginMeta = plugin.meta;
  6. const navModel = {
  7. img: pluginMeta.info.logos.large,
  8. id: 'datasource-' + dataSource.id,
  9. subTitle: `Type: ${pluginMeta.name}`,
  10. url: '',
  11. text: dataSource.name,
  12. breadcrumbs: [{ title: 'Data Sources', url: 'datasources' }],
  13. children: [
  14. {
  15. active: false,
  16. icon: 'fa fa-fw fa-sliders',
  17. id: `datasource-settings-${dataSource.id}`,
  18. text: 'Settings',
  19. url: `datasources/edit/${dataSource.id}/`,
  20. },
  21. ],
  22. };
  23. if (plugin.configPages) {
  24. for (const page of plugin.configPages) {
  25. navModel.children.push({
  26. active: false,
  27. text: page.title,
  28. icon: page.icon,
  29. url: `datasources/edit/${dataSource.id}/?page=${page.id}`,
  30. id: `datasource-page-${page.id}`,
  31. });
  32. }
  33. }
  34. if (pluginMeta.includes && hasDashboards(pluginMeta.includes)) {
  35. navModel.children.push({
  36. active: false,
  37. icon: 'fa fa-fw fa-th-large',
  38. id: `datasource-dashboards-${dataSource.id}`,
  39. text: 'Dashboards',
  40. url: `datasources/edit/${dataSource.id}/dashboards`,
  41. });
  42. }
  43. if (config.buildInfo.isEnterprise) {
  44. navModel.children.push({
  45. active: false,
  46. icon: 'fa fa-fw fa-lock',
  47. id: `datasource-permissions-${dataSource.id}`,
  48. text: 'Permissions',
  49. url: `datasources/edit/${dataSource.id}/permissions`,
  50. });
  51. }
  52. return navModel;
  53. }
  54. export function getDataSourceLoadingNav(pageName: string): NavModel {
  55. const main = buildNavModel(
  56. {
  57. access: '',
  58. basicAuth: false,
  59. basicAuthUser: '',
  60. basicAuthPassword: '',
  61. withCredentials: false,
  62. database: '',
  63. id: 1,
  64. isDefault: false,
  65. jsonData: { authType: 'credentials', defaultRegion: 'eu-west-2' },
  66. name: 'Loading',
  67. orgId: 1,
  68. password: '',
  69. readOnly: false,
  70. type: 'Loading',
  71. typeLogoUrl: 'public/img/icn-datasource.svg',
  72. url: '',
  73. user: '',
  74. },
  75. {
  76. meta: {
  77. id: '1',
  78. type: PluginType.datasource,
  79. name: '',
  80. info: {
  81. author: {
  82. name: '',
  83. url: '',
  84. },
  85. description: '',
  86. links: [{ name: '', url: '' }],
  87. logos: {
  88. large: '',
  89. small: '',
  90. },
  91. screenshots: [],
  92. updated: '',
  93. version: '',
  94. },
  95. includes: [],
  96. module: '',
  97. baseUrl: '',
  98. },
  99. } as GenericDataSourcePlugin
  100. );
  101. let node: NavModelItem;
  102. // find active page
  103. for (const child of main.children) {
  104. if (child.id.indexOf(pageName) > 0) {
  105. child.active = true;
  106. node = child;
  107. break;
  108. }
  109. }
  110. return {
  111. main: main,
  112. node: node,
  113. };
  114. }
  115. function hasDashboards(includes: PluginInclude[]): boolean {
  116. return (
  117. includes.find(include => {
  118. return include.type === 'dashboard';
  119. }) !== undefined
  120. );
  121. }