grafana/public/app/features/dashboard/services/DashboardSrv.ts
Torkel Ödegaard 49b5fc4b9a
Chore: More typescript strict null fixes, going for sub 200 (#26134)
* Chore: Fix typescript strict null errors

* Added new limit

* Fixed ts issue

* fixed tests

* trying to fix type inference

* Fixing more ts errors

* Revert tsconfig option

* Fix

* Fixed code

* More fixes

* fix tests

* Updated snapshot

* Chore: More ts strict null fixes

* More fixes in some really messed up azure config components

* More fixes, current count: 441

* 419

* More fixes

* Fixed invalid initial state in explore

* Fixing tests

* Fixed tests

* Explore fix

* More fixes

* Progress

* Sub 300

* Now at 218

* Progress

* Update

* Progress

* Updated tests

* at 159

* fixed tests

* Fixed test
2020-07-09 15:16:35 +02:00

85 lines
2.3 KiB
TypeScript

import coreModule from 'app/core/core_module';
import { appEvents } from 'app/core/app_events';
import { DashboardModel } from '../state/DashboardModel';
import { removePanel } from '../utils/panel';
import { CoreEvents, DashboardMeta } from 'app/types';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
import { backendSrv } from 'app/core/services/backend_srv';
import { promiseToDigest } from '../../../core/utils/promiseToDigest';
import { saveDashboard } from 'app/features/manage-dashboards/state/actions';
export class DashboardSrv {
dashboard: DashboardModel;
/** @ngInject */
constructor(private $rootScope: GrafanaRootScope) {
appEvents.on(CoreEvents.removePanel, this.onRemovePanel);
}
create(dashboard: any, meta: DashboardMeta) {
return new DashboardModel(dashboard, meta);
}
setCurrent(dashboard: DashboardModel) {
this.dashboard = dashboard;
}
getCurrent(): DashboardModel {
return this.dashboard;
}
onRemovePanel = (panelId: number) => {
const dashboard = this.getCurrent();
removePanel(dashboard, dashboard.getPanelById(panelId)!, true);
};
saveJSONDashboard(json: string) {
const parsedJson = JSON.parse(json);
return saveDashboard({
dashboard: parsedJson,
folderId: this.dashboard.meta.folderId || parsedJson.folderId,
});
}
starDashboard(dashboardId: string, isStarred: any) {
let promise;
if (isStarred) {
promise = promiseToDigest(this.$rootScope)(
backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
return false;
})
);
} else {
promise = promiseToDigest(this.$rootScope)(
backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
return true;
})
);
}
return promise.then((res: boolean) => {
if (this.dashboard && this.dashboard.id === dashboardId) {
this.dashboard.meta.isStarred = res;
}
return res;
});
}
}
coreModule.service('dashboardSrv', DashboardSrv);
//
// Code below is to export the service to react components
//
let singletonInstance: DashboardSrv;
export function setDashboardSrv(instance: DashboardSrv) {
singletonInstance = instance;
}
export function getDashboardSrv(): DashboardSrv {
return singletonInstance;
}