grafana/public/app/features/dashboard/settings/settings.ts

210 lines
5.3 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import { coreModule, appEvents, contextSrv } from 'app/core/core';
import { DashboardModel } from '../dashboard_model';
import $ from 'jquery';
import _ from 'lodash';
import config from 'app/core/config';
2017-12-01 14:04:48 -06:00
export class SettingsCtrl {
dashboard: DashboardModel;
isOpen: boolean;
viewId: string;
2017-12-11 08:04:48 -06:00
json: string;
2017-12-11 09:28:57 -06:00
alertCount: number;
2017-12-12 04:49:01 -06:00
canSaveAs: boolean;
2018-01-16 07:41:08 -06:00
canSave: boolean;
2017-12-12 04:49:01 -06:00
canDelete: boolean;
2017-12-11 10:19:17 -06:00
sections: any[];
2017-12-01 14:04:48 -06:00
/** @ngInject */
constructor(private $scope, private $location, private $rootScope, private backendSrv, private dashboardSrv) {
2017-12-11 06:47:04 -06:00
// temp hack for annotations and variables editors
// that rely on inherited scope
$scope.dashboard = this.dashboard;
2017-12-08 11:15:24 -06:00
2017-12-20 05:33:33 -06:00
this.$scope.$on('$destroy', () => {
2017-12-11 06:04:06 -06:00
this.dashboard.updateSubmenuVisibility();
2017-12-20 05:33:33 -06:00
this.$rootScope.$broadcast('refresh');
2017-12-11 06:04:06 -06:00
});
2017-12-11 08:04:48 -06:00
2017-12-12 04:49:01 -06:00
this.canSaveAs = contextSrv.isEditor;
2018-01-16 07:41:08 -06:00
this.canSave = this.dashboard.meta.canSave;
2017-12-12 04:49:01 -06:00
this.canDelete = this.dashboard.meta.canSave;
2017-12-11 09:28:57 -06:00
2017-12-11 10:19:17 -06:00
this.buildSectionList();
2017-12-12 04:49:01 -06:00
this.onRouteUpdated();
2017-12-11 10:19:17 -06:00
$rootScope.onAppEvent('$routeUpdate', this.onRouteUpdated.bind(this), $scope);
2017-12-11 10:19:17 -06:00
}
buildSectionList() {
this.sections = [];
2017-12-15 07:51:20 -06:00
2017-12-11 10:19:17 -06:00
if (this.dashboard.meta.canEdit) {
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'General',
id: 'settings',
icon: 'gicon gicon-preferences',
});
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'Annotations',
id: 'annotations',
icon: 'gicon gicon-annotation',
});
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'Variables',
id: 'templating',
icon: 'gicon gicon-variable',
});
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'Links',
id: 'links',
icon: 'gicon gicon-link',
});
}
2017-12-11 10:19:17 -06:00
if (this.dashboard.id && this.dashboard.meta.canSave) {
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'Versions',
id: 'versions',
icon: 'fa fa-fw fa-history',
});
2017-12-11 10:19:17 -06: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 07:51:20 -06:00
if (this.dashboard.meta.canMakeEditable) {
this.sections.push({
title: 'General',
icon: 'gicon gicon-preferences',
2017-12-20 05:33:33 -06:00
id: 'make_editable',
});
2017-12-11 10:19:17 -06:00
}
this.sections.push({
2017-12-20 05:33:33 -06:00
title: 'View JSON',
id: 'view_json',
icon: 'gicon gicon-json',
});
2017-12-11 10:19:17 -06:00
const params = this.$location.search();
const url = this.$location.path();
for (let section of this.sections) {
const sectionParams = _.defaults({ editview: section.id }, params);
section.url = config.appSubUrl + url + '?' + $.param(sectionParams);
2017-12-11 10:19:17 -06:00
}
2017-12-12 04:49:01 -06:00
}
onRouteUpdated() {
this.viewId = this.$location.search().editview;
if (this.viewId) {
this.json = JSON.stringify(this.dashboard.getSaveModelClone(), null, 2);
}
2017-12-11 10:19:17 -06:00
2017-12-20 05:33:33 -06:00
if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
this.viewId = 'make_editable';
2017-12-15 07:51:20 -06:00
}
2017-12-11 10:19:17 -06:00
const currentSection = _.find(this.sections, { id: this.viewId });
if (!currentSection) {
this.sections.unshift({
2017-12-20 05:33:33 -06:00
title: 'Not found',
id: '404',
icon: 'fa fa-fw fa-warning',
});
2017-12-20 05:33:33 -06:00
this.viewId = '404';
2017-12-11 10:19:17 -06:00
}
2017-12-08 08:53:26 -06:00
}
2017-12-12 04:49:01 -06:00
openSaveAsModal() {
this.dashboardSrv.showSaveAsModal();
2017-12-01 14:04:48 -06:00
}
2018-01-16 07:41:08 -06:00
saveDashboard() {
this.dashboardSrv.saveDashboard();
}
2017-12-01 14:04:48 -06:00
hideSettings() {
var urlParams = this.$location.search();
delete urlParams.editview;
setTimeout(() => {
this.$rootScope.$apply(() => {
this.$location.search(urlParams);
});
});
}
2017-12-11 06:47:04 -06:00
2017-12-11 09:28:57 -06: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();
const currentSection = _.find(this.sections, { id: this.viewId });
this.$location.url(currentSection.url);
2017-12-11 09:28:57 -06:00
}
2017-12-12 04:49:01 -06:00
deleteDashboard() {
2017-12-20 05:33:33 -06:00
var confirmText = '';
2017-12-12 04:49:01 -06:00
var text2 = this.dashboard.title;
const alerts = _.sumBy(this.dashboard.panels, panel => {
return panel.alert ? 1 : 0;
});
if (alerts > 0) {
2017-12-20 05:33:33 -06:00
confirmText = 'DELETE';
2017-12-12 04:49:01 -06:00
text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
}
2017-12-20 05:33:33 -06:00
appEvents.emit('confirm-modal', {
title: 'Delete',
text: 'Do you want to delete this dashboard?',
2017-12-12 04:49:01 -06:00
text2: text2,
2017-12-20 05:33:33 -06:00
icon: 'fa-trash',
2017-12-12 04:49:01 -06:00
confirmText: confirmText,
2017-12-20 05:33:33 -06:00
yesText: 'Delete',
2017-12-12 04:49:01 -06:00
onConfirm: () => {
this.dashboard.meta.canSave = false;
this.deleteDashboardConfirmed();
2017-12-20 05:33:33 -06:00
},
2017-12-12 04:49:01 -06:00
});
2017-12-11 09:28:57 -06:00
}
2017-12-12 04:49:01 -06:00
deleteDashboardConfirmed() {
this.backendSrv.deleteDashboard(this.dashboard.meta.slug).then(() => {
appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
2017-12-20 05:33:33 -06:00
this.$location.url('/');
2017-12-11 09:28:57 -06:00
});
}
2017-12-11 06:47:04 -06:00
onFolderChange(folder) {
this.dashboard.meta.folderId = folder.id;
2017-12-11 10:19:17 -06:00
this.dashboard.meta.folderTitle = folder.title;
2017-12-11 06:47:04 -06:00
}
2017-12-01 14:04:48 -06:00
}
export function dashboardSettings() {
return {
2017-12-20 05:33:33 -06:00
restrict: 'E',
templateUrl: 'public/app/features/dashboard/settings/settings.html',
2017-12-01 14:04:48 -06:00
controller: SettingsCtrl,
bindToController: true,
2017-12-20 05:33:33 -06:00
controllerAs: 'ctrl',
2017-12-01 14:04:48 -06:00
transclude: true,
2017-12-20 05:33:33 -06:00
scope: { dashboard: '=' },
2017-12-01 14:04:48 -06:00
};
}
2017-12-20 05:33:33 -06:00
coreModule.directive('dashboardSettings', dashboardSettings);