grafana/public/app/routes/dashboard_loaders.ts
Dominik Prokop d2a13c4715
FieldOverride: Support data links via field overrides (#23590)
* Move xss and sanitize packages to grafana-data

* Move text, url and location utils to grafana-data

* Move grafana config types to grafana-data

* Move field display value proxy to grafana-data

* Fix

* Move data links built in vars to grafana-data

* Attach links supplier to when applying field overrides

* Prep tests

* Use links suppliers attached via field overrides

* locationUtil dependencies type

* Move sanitize-url declaration to grafana-data

* Revert "Move sanitize-url declaration to grafana-data"

This reverts commit 11db9f5e55.

* Fix typo

* fix ts vol1

* Remove import from runtime in data.... Make TS happy at the same time ;)

* Lovely TS, please shut up

* Lovely TS, please shut up vol2

* fix tests

* Fixes

* minor refactor

* Attach get links to FieldDisplayValue for seamless usage

* Update packages/grafana-data/src/field/fieldOverrides.ts

* Make storybook build
2020-04-20 07:37:38 +02:00

92 lines
2.8 KiB
TypeScript

import coreModule from 'app/core/core_module';
import { locationUtil, UrlQueryMap } from '@grafana/data';
import { DashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
import { ILocationService } from 'angular';
import { AppEventEmitter, CoreEvents, Scope } from 'app/types';
import { backendSrv } from 'app/core/services/backend_srv';
export class LoadDashboardCtrl {
/** @ngInject */
constructor(
$scope: Scope & AppEventEmitter,
$routeParams: UrlQueryMap,
dashboardLoaderSrv: DashboardLoaderSrv,
$location: ILocationService,
$browser: any
) {
$scope.appEvent(CoreEvents.dashboardFetchStart);
if (!$routeParams.uid && !$routeParams.slug) {
backendSrv.get('/api/dashboards/home').then((homeDash: { redirectUri: string; meta: any }) => {
if (homeDash.redirectUri) {
const newUrl = locationUtil.stripBaseFromUrl(homeDash.redirectUri);
$location.path(newUrl);
} else {
const meta = homeDash.meta;
meta.canSave = meta.canShare = meta.canStar = false;
$scope.initDashboard(homeDash, $scope);
}
});
return;
}
// if no uid, redirect to new route based on slug
if (!($routeParams.type === 'script' || $routeParams.type === 'snapshot') && !$routeParams.uid) {
// @ts-ignore
backendSrv.getDashboardBySlug($routeParams.slug).then(res => {
if (res) {
$location.path(locationUtil.stripBaseFromUrl(res.meta.url)).replace();
}
});
return;
}
dashboardLoaderSrv.loadDashboard($routeParams.type, $routeParams.slug, $routeParams.uid).then((result: any) => {
if (result.meta.url) {
const url = locationUtil.stripBaseFromUrl(result.meta.url);
if (url !== $location.path()) {
// replace url to not create additional history items and then return so that initDashboard below isn't executed multiple times.
$location.path(url).replace();
return;
}
}
result.meta.autofitpanels = $routeParams.autofitpanels;
result.meta.kiosk = $routeParams.kiosk;
$scope.initDashboard(result, $scope);
});
}
}
export class NewDashboardCtrl {
/** @ngInject */
constructor($scope: any, $routeParams: UrlQueryMap) {
$scope.initDashboard(
{
meta: {
canStar: false,
canShare: false,
isNew: true,
folderId: Number($routeParams.folderId),
},
dashboard: {
title: 'New dashboard',
panels: [
{
type: 'add-panel',
gridPos: { x: 0, y: 0, w: 12, h: 9 },
title: 'Panel Title',
},
],
},
},
$scope
);
}
}
coreModule.controller('LoadDashboardCtrl', LoadDashboardCtrl);
coreModule.controller('NewDashboardCtrl', NewDashboardCtrl);