dashboard_loaders.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import coreModule from 'app/core/core_module';
  2. import locationUtil from 'app/core/utils/location_util';
  3. import { UrlQueryMap } from '@grafana/runtime';
  4. import { DashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
  5. import { BackendSrv } from 'app/core/services/backend_srv';
  6. import { ILocationService } from 'angular';
  7. export class LoadDashboardCtrl {
  8. /** @ngInject */
  9. constructor(
  10. $scope: any,
  11. $routeParams: UrlQueryMap,
  12. dashboardLoaderSrv: DashboardLoaderSrv,
  13. backendSrv: BackendSrv,
  14. $location: ILocationService,
  15. $browser: any
  16. ) {
  17. $scope.appEvent('dashboard-fetch-start');
  18. if (!$routeParams.uid && !$routeParams.slug) {
  19. backendSrv.get('/api/dashboards/home').then((homeDash: { redirectUri: string; meta: any }) => {
  20. if (homeDash.redirectUri) {
  21. const newUrl = locationUtil.stripBaseFromUrl(homeDash.redirectUri);
  22. $location.path(newUrl);
  23. } else {
  24. const meta = homeDash.meta;
  25. meta.canSave = meta.canShare = meta.canStar = false;
  26. $scope.initDashboard(homeDash, $scope);
  27. }
  28. });
  29. return;
  30. }
  31. // if no uid, redirect to new route based on slug
  32. if (!($routeParams.type === 'script' || $routeParams.type === 'snapshot') && !$routeParams.uid) {
  33. // @ts-ignore
  34. backendSrv.getDashboardBySlug($routeParams.slug).then(res => {
  35. if (res) {
  36. $location.path(locationUtil.stripBaseFromUrl(res.meta.url)).replace();
  37. }
  38. });
  39. return;
  40. }
  41. dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug, $routeParams.uid).then((result: any) => {
  42. if (result.meta.url) {
  43. const url = locationUtil.stripBaseFromUrl(result.meta.url);
  44. if (url !== $location.path()) {
  45. // replace url to not create additional history items and then return so that initDashboard below isn't executed multiple times.
  46. $location.path(url).replace();
  47. return;
  48. }
  49. }
  50. result.meta.autofitpanels = $routeParams.autofitpanels;
  51. result.meta.kiosk = $routeParams.kiosk;
  52. $scope.initDashboard(result, $scope);
  53. });
  54. }
  55. }
  56. export class NewDashboardCtrl {
  57. /** @ngInject */
  58. constructor($scope: any, $routeParams: UrlQueryMap) {
  59. $scope.initDashboard(
  60. {
  61. meta: {
  62. canStar: false,
  63. canShare: false,
  64. isNew: true,
  65. folderId: Number($routeParams.folderId),
  66. },
  67. dashboard: {
  68. title: 'New dashboard',
  69. panels: [
  70. {
  71. type: 'add-panel',
  72. gridPos: { x: 0, y: 0, w: 12, h: 9 },
  73. title: 'Panel Title',
  74. },
  75. ],
  76. },
  77. },
  78. $scope
  79. );
  80. }
  81. }
  82. coreModule.controller('LoadDashboardCtrl', LoadDashboardCtrl);
  83. coreModule.controller('NewDashboardCtrl', NewDashboardCtrl);