grafana/public/app/features/dashboard/components/DashboardSettings/SettingsCtrl.ts

271 lines
7.5 KiB
TypeScript
Raw Normal View History

import { appEvents, contextSrv, coreModule } from 'app/core/core';
import { DashboardModel } from '../../state/DashboardModel';
2017-12-20 19:33:33 +08:00
import $ from 'jquery';
import _ from 'lodash';
import angular, { ILocationService } from 'angular';
import config from 'app/core/config';
import { BackendSrv } from 'app/core/services/backend_srv';
import { DashboardSrv } from '../../services/DashboardSrv';
import { CoreEvents } from 'app/types';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
import { AppEvents } from '@grafana/data';
import { e2e } from '@grafana/e2e';
2017-12-02 04:04:48 +08:00
export class SettingsCtrl {
dashboard: DashboardModel;
isOpen: boolean;
viewId: string;
2017-12-11 22:04:48 +08:00
json: string;
2017-12-11 23:28:57 +08:00
alertCount: number;
2017-12-12 18:49:01 +08:00
canSaveAs: boolean;
2018-01-16 21:41:08 +08:00
canSave: boolean;
2017-12-12 18:49:01 +08:00
canDelete: boolean;
2017-12-12 00:19:17 +08:00
sections: any[];
hasUnsavedFolderChange: boolean;
selectors: typeof e2e.pages.DashboardSettings.selectors;
2017-12-02 04:04:48 +08:00
/** @ngInject */
2018-04-12 04:59:29 +08:00
constructor(
private $scope: any,
private $route: any,
private $location: ILocationService,
private $rootScope: GrafanaRootScope,
private backendSrv: BackendSrv,
private dashboardSrv: DashboardSrv
2018-04-12 04:59:29 +08:00
) {
2017-12-11 20:47:04 +08:00
// temp hack for annotations and variables editors
// that rely on inherited scope
$scope.dashboard = this.dashboard;
2017-12-09 01:15:24 +08:00
2017-12-20 19:33:33 +08:00
this.$scope.$on('$destroy', () => {
2017-12-11 20:04:06 +08:00
this.dashboard.updateSubmenuVisibility();
setTimeout(() => {
this.$rootScope.appEvent(CoreEvents.dashScroll, { restore: true });
2018-11-06 00:46:09 +08:00
this.dashboard.startRefresh();
});
2017-12-11 20:04:06 +08:00
});
2017-12-11 22:04:48 +08:00
this.canSaveAs = contextSrv.hasEditPermissionInFolders;
2018-01-16 21:41:08 +08:00
this.canSave = this.dashboard.meta.canSave;
2017-12-12 18:49:01 +08:00
this.canDelete = this.dashboard.meta.canSave;
2017-12-11 23:28:57 +08:00
2017-12-12 00:19:17 +08:00
this.buildSectionList();
2017-12-12 18:49:01 +08:00
this.onRouteUpdated();
2017-12-12 00:19:17 +08:00
this.$rootScope.onAppEvent(CoreEvents.routeUpdated, this.onRouteUpdated.bind(this), $scope);
this.$rootScope.appEvent(CoreEvents.dashScroll, { animate: false, pos: 0 });
this.$rootScope.onAppEvent(CoreEvents.dashboardSaved, this.onPostSave.bind(this), $scope);
this.selectors = e2e.pages.DashboardSettings.selectors;
2017-12-12 00:19:17 +08:00
}
buildSectionList() {
this.sections = [];
2017-12-15 21:51:20 +08:00
2017-12-12 00:19:17 +08:00
if (this.dashboard.meta.canEdit) {
this.sections.push({
2017-12-20 19:33:33 +08:00
title: 'General',
id: 'settings',
icon: 'gicon gicon-preferences',
});
this.sections.push({
2017-12-20 19:33:33 +08:00
title: 'Annotations',
id: 'annotations',
icon: 'gicon gicon-annotation',
});
this.sections.push({
2017-12-20 19:33:33 +08:00
title: 'Variables',
id: 'templating',
icon: 'gicon gicon-variable',
});
this.sections.push({
2017-12-20 19:33:33 +08:00
title: 'Links',
id: 'links',
icon: 'gicon gicon-link',
});
}
2017-12-12 00:19:17 +08:00
if (this.dashboard.id && this.dashboard.meta.canSave) {
this.sections.push({
2017-12-20 19:33:33 +08:00
title: 'Versions',
id: 'versions',
icon: 'fa fa-fw fa-history',
});
2017-12-12 00:19:17 +08:00
}
if (this.dashboard.id && this.dashboard.meta.canAdmin) {
this.sections.push({
title: 'Permissions',
id: 'permissions',
icon: 'fa fa-fw fa-lock',
});
}
2017-12-15 21:51:20 +08:00
if (this.dashboard.meta.canMakeEditable) {
this.sections.push({
title: 'General',
icon: 'gicon gicon-preferences',
2017-12-20 19:33:33 +08:00
id: 'make_editable',
});
2017-12-12 00:19:17 +08:00
}
this.sections.push({
2018-05-08 17:24:20 +08:00
title: 'JSON Model',
2018-04-12 04:59:29 +08:00
id: 'dashboard_json',
2017-12-20 19:33:33 +08:00
icon: 'gicon gicon-json',
});
2017-12-12 00:19:17 +08:00
const params = this.$location.search();
const url = this.$location.path();
for (const section of this.sections) {
2017-12-12 00:19:17 +08:00
const sectionParams = _.defaults({ editview: section.id }, params);
section.url = config.appSubUrl + url + '?' + $.param(sectionParams);
2017-12-12 00:19:17 +08:00
}
2017-12-12 18:49:01 +08:00
}
onRouteUpdated() {
this.viewId = this.$location.search().editview;
if (this.viewId) {
this.json = angular.toJson(this.dashboard.getSaveModelClone(), true);
2017-12-12 18:49:01 +08:00
}
2017-12-12 00:19:17 +08:00
2017-12-20 19:33:33 +08:00
if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
this.viewId = 'make_editable';
2017-12-15 21:51:20 +08:00
}
2019-04-15 18:11:52 +08:00
const currentSection: any = _.find(this.sections, { id: this.viewId } as any);
2017-12-12 00:19:17 +08:00
if (!currentSection) {
this.sections.unshift({
2017-12-20 19:33:33 +08:00
title: 'Not found',
id: '404',
icon: 'fa fa-fw fa-warning',
});
2017-12-20 19:33:33 +08:00
this.viewId = '404';
2017-12-12 00:19:17 +08:00
}
2017-12-08 22:53:26 +08:00
}
2017-12-12 18:49:01 +08:00
openSaveAsModal() {
this.dashboardSrv.showSaveAsModal();
2017-12-02 04:04:48 +08:00
}
2018-01-16 21:41:08 +08:00
saveDashboard() {
this.dashboardSrv.saveDashboard();
}
2018-04-12 04:59:29 +08:00
saveDashboardJson() {
this.dashboardSrv.saveJSONDashboard(this.json).then(() => {
this.$route.reload();
});
2018-01-16 21:41:08 +08:00
}
onPostSave() {
this.hasUnsavedFolderChange = false;
}
2017-12-02 04:04:48 +08:00
hideSettings() {
2018-08-27 02:19:23 +08:00
const urlParams = this.$location.search();
2017-12-02 04:04:48 +08:00
delete urlParams.editview;
setTimeout(() => {
this.$rootScope.$apply(() => {
this.$location.search(urlParams);
});
});
}
2017-12-11 20:47:04 +08:00
2017-12-11 23:28:57 +08:00
makeEditable() {
this.dashboard.editable = true;
this.dashboard.meta.canMakeEditable = false;
this.dashboard.meta.canEdit = true;
this.dashboard.meta.canSave = true;
this.canDelete = true;
this.viewId = 'settings';
this.buildSectionList();
2019-04-15 18:11:52 +08:00
const currentSection: any = _.find(this.sections, { id: this.viewId } as any);
this.$location.url(currentSection.url);
2017-12-11 23:28:57 +08:00
}
2017-12-12 18:49:01 +08:00
deleteDashboard() {
let confirmText = '';
let text2 = this.dashboard.title;
2017-12-12 18:49:01 +08:00
if (this.dashboard.meta.provisioned) {
appEvents.emit(CoreEvents.showConfirmModal, {
title: 'Cannot delete provisioned dashboard',
text: `
This dashboard is managed by Grafanas provisioning and cannot be deleted. Remove the dashboard from the
config file to delete it.
`,
text2: `
<i>See <a class="external-link" href="http://docs.grafana.org/administration/provisioning/#dashboards" target="_blank">
documentation</a> for more information about provisioning.</i>
</br>
File path: ${this.dashboard.meta.provisionedExternalId}
`,
text2htmlBind: true,
icon: 'fa-trash',
noText: 'OK',
});
return;
}
2017-12-12 18:49:01 +08:00
const alerts = _.sumBy(this.dashboard.panels, panel => {
return panel.alert ? 1 : 0;
});
if (alerts > 0) {
2017-12-20 19:33:33 +08:00
confirmText = 'DELETE';
2017-12-12 18:49:01 +08:00
text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
}
appEvents.emit(CoreEvents.showConfirmModal, {
2017-12-20 19:33:33 +08:00
title: 'Delete',
text: 'Do you want to delete this dashboard?',
2017-12-12 18:49:01 +08:00
text2: text2,
2017-12-20 19:33:33 +08:00
icon: 'fa-trash',
2017-12-12 18:49:01 +08:00
confirmText: confirmText,
2017-12-20 19:33:33 +08:00
yesText: 'Delete',
2017-12-12 18:49:01 +08:00
onConfirm: () => {
this.dashboard.meta.canSave = false;
this.deleteDashboardConfirmed();
2017-12-20 19:33:33 +08:00
},
2017-12-12 18:49:01 +08:00
});
2017-12-11 23:28:57 +08:00
}
2017-12-12 18:49:01 +08:00
deleteDashboardConfirmed() {
this.backendSrv.deleteDashboard(this.dashboard.uid, false).then(() => {
appEvents.emit(AppEvents.alertSuccess, ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
2017-12-20 19:33:33 +08:00
this.$location.url('/');
2017-12-11 23:28:57 +08:00
});
}
onFolderChange(folder: { id: number; title: string }) {
2017-12-11 20:47:04 +08:00
this.dashboard.meta.folderId = folder.id;
2017-12-12 00:19:17 +08:00
this.dashboard.meta.folderTitle = folder.title;
this.hasUnsavedFolderChange = true;
2017-12-11 20:47:04 +08:00
}
getFolder() {
return {
id: this.dashboard.meta.folderId,
title: this.dashboard.meta.folderTitle,
url: this.dashboard.meta.folderUrl,
};
}
2017-12-02 04:04:48 +08:00
}
export function dashboardSettings() {
return {
2017-12-20 19:33:33 +08:00
restrict: 'E',
2019-01-23 18:48:27 +08:00
templateUrl: 'public/app/features/dashboard/components/DashboardSettings/template.html',
2017-12-02 04:04:48 +08:00
controller: SettingsCtrl,
bindToController: true,
2017-12-20 19:33:33 +08:00
controllerAs: 'ctrl',
2017-12-02 04:04:48 +08:00
transclude: true,
2017-12-20 19:33:33 +08:00
scope: { dashboard: '=' },
2017-12-02 04:04:48 +08:00
};
}
2017-12-20 19:33:33 +08:00
coreModule.directive('dashboardSettings', dashboardSettings);