dashboard_loaders.ts 2.3 KB

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