dashboard_loaders.ts 2.4 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(homeDash => {
  9. if (homeDash.redirectUri) {
  10. const newUrl = locationUtil.stripBaseFromUrl(homeDash.redirectUri);
  11. $location.path(newUrl);
  12. } else {
  13. const meta = homeDash.meta;
  14. meta.canSave = meta.canShare = meta.canStar = false;
  15. $scope.initDashboard(homeDash, $scope);
  16. }
  17. });
  18. return;
  19. }
  20. // if no uid, redirect to new route based on slug
  21. if (!($routeParams.type === 'script' || $routeParams.type === 'snapshot') && !$routeParams.uid) {
  22. backendSrv.getDashboardBySlug($routeParams.slug).then(res => {
  23. if (res) {
  24. $location.path(locationUtil.stripBaseFromUrl(res.meta.url)).replace();
  25. }
  26. });
  27. return;
  28. }
  29. dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug, $routeParams.uid).then(result => {
  30. if (result.meta.url) {
  31. const url = locationUtil.stripBaseFromUrl(result.meta.url);
  32. if (url !== $location.path()) {
  33. // replace url to not create additional history items and then return so that initDashboard below isn't executed multiple times.
  34. $location.path(url).replace();
  35. return;
  36. }
  37. }
  38. result.meta.autofitpanels = $routeParams.autofitpanels;
  39. result.meta.kiosk = $routeParams.kiosk;
  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);