navModel.ts 3.2 KB

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