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

157 lines
4.5 KiB
TypeScript
Raw Normal View History

2017-12-11 10:19:17 -06:00
import { coreModule, appEvents, contextSrv } from 'app/core/core';
import { DashboardModel } from '../dashboard_model';
2017-12-08 08:53:26 -06:00
import $ from 'jquery';
import _ from 'lodash';
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;
canDelete: boolean;
2017-12-11 10:19:17 -06:00
sections: any[];
2017-12-01 14:04:48 -06:00
/** @ngInject */
2017-12-11 09:28:57 -06:00
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-11 06:04:06 -06:00
this.$scope.$on('$destroy', () => {
this.dashboard.updateSubmenuVisibility();
2017-12-11 10:19:17 -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;
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);
}
buildSectionList() {
this.sections = [];
if (this.dashboard.meta.canEdit) {
this.sections.push({ title: 'General', id: 'settings', icon: 'fa fa-fw fa-sliders' });
this.sections.push({ title: 'Annotations', id: 'annotations', icon: 'fa fa-fw fa-comment-o' });
this.sections.push({ title: 'Variables', id: 'templating', icon: 'fa fa-fw fa-dollar' });
this.sections.push({ title: 'Links', id: 'links', icon: 'fa fa-fw fa-external-link' });
if (this.dashboard.id) {
this.sections.push({ title: 'Versions', id: 'versions', icon: 'fa fa-fw fa-history' });
}
}
if (contextSrv.isEditor && !this.dashboard.editable) {
this.sections.push({ title: 'Make Editable', icon: 'fa fa-fw fa-edit', id: 'make_editable' });
this.viewId = 'make_editable';
}
this.sections.push({ title: 'View JSON', id: 'view_json', icon: 'fa fa-fw fa-code' });
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 = url + '?' + $.param(sectionParams);
}
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
const currentSection = _.find(this.sections, { id: this.viewId });
if (!currentSection) {
this.sections.unshift({ title: 'Not found', id: '404', icon: 'fa fa-fw fa-warning' });
this.viewId = '404';
return;
}
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
}
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;
2017-12-11 10:19:17 -06:00
return this.dashboardSrv.saveDashboard({ makeEditable: true, overwrite: false }).then(() => {
2017-12-11 09:28:57 -06:00
// force refresh whole page
window.location.href = window.location.href;
});
}
2017-12-12 04:49:01 -06:00
deleteDashboard() {
var confirmText = '';
var text2 = this.dashboard.title;
const alerts = _.sumBy(this.dashboard.panels, panel => {
return panel.alert ? 1 : 0;
});
if (alerts > 0) {
confirmText = 'DELETE';
text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
}
appEvents.emit('confirm-modal', {
title: 'Delete',
text: 'Do you want to delete this dashboard?',
text2: text2,
icon: 'fa-trash',
confirmText: confirmText,
yesText: 'Delete',
onConfirm: () => {
this.dashboard.meta.canSave = false;
this.deleteDashboardConfirmed();
}
});
2017-12-11 09:28:57 -06:00
}
2017-12-12 04:49:01 -06:00
deleteDashboardConfirmed() {
2017-12-11 09:28:57 -06:00
this.backendSrv.delete('/api/dashboards/db/' + this.dashboard.meta.slug).then(() => {
appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
this.$location.url('/');
});
}
2017-12-11 06:47:04 -06:00
onFolderChange(folder) {
this.dashboard.folderId = folder.id;
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 {
restrict: 'E',
templateUrl: 'public/app/features/dashboard/settings/settings.html',
controller: SettingsCtrl,
bindToController: true,
controllerAs: 'ctrl',
transclude: true,
2017-12-11 10:19:17 -06:00
scope: { dashboard: '=' },
2017-12-01 14:04:48 -06:00
};
}
coreModule.directive('dashboardSettings', dashboardSettings);