| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import { coreModule, appEvents, contextSrv } from 'app/core/core';
- import { DashboardModel } from '../../state/DashboardModel';
- import $ from 'jquery';
- import _ from 'lodash';
- import angular from 'angular';
- import config from 'app/core/config';
- export class SettingsCtrl {
- dashboard: DashboardModel;
- isOpen: boolean;
- viewId: string;
- json: string;
- alertCount: number;
- canSaveAs: boolean;
- canSave: boolean;
- canDelete: boolean;
- sections: any[];
- hasUnsavedFolderChange: boolean;
- /** @ngInject */
- constructor(
- private $scope,
- private $route,
- private $location,
- private $rootScope,
- private backendSrv,
- private dashboardSrv
- ) {
- // temp hack for annotations and variables editors
- // that rely on inherited scope
- $scope.dashboard = this.dashboard;
- this.$scope.$on('$destroy', () => {
- this.dashboard.updateSubmenuVisibility();
- setTimeout(() => {
- this.$rootScope.appEvent('dash-scroll', { restore: true });
- this.dashboard.startRefresh();
- });
- });
- this.canSaveAs = contextSrv.hasEditPermissionInFolders;
- this.canSave = this.dashboard.meta.canSave;
- this.canDelete = this.dashboard.meta.canSave;
- this.buildSectionList();
- this.onRouteUpdated();
- this.$rootScope.onAppEvent('$routeUpdate', this.onRouteUpdated.bind(this), $scope);
- this.$rootScope.appEvent('dash-scroll', { animate: false, pos: 0 });
- this.$rootScope.onAppEvent('dashboard-saved', this.onPostSave.bind(this), $scope);
- }
- buildSectionList() {
- this.sections = [];
- if (this.dashboard.meta.canEdit) {
- this.sections.push({
- title: 'General',
- id: 'settings',
- icon: 'gicon gicon-preferences',
- });
- this.sections.push({
- title: 'Annotations',
- id: 'annotations',
- icon: 'gicon gicon-annotation',
- });
- this.sections.push({
- title: 'Variables',
- id: 'templating',
- icon: 'gicon gicon-variable',
- });
- this.sections.push({
- title: 'Links',
- id: 'links',
- icon: 'gicon gicon-link',
- });
- }
- if (this.dashboard.id && this.dashboard.meta.canSave) {
- this.sections.push({
- title: 'Versions',
- id: 'versions',
- icon: 'fa fa-fw fa-history',
- });
- }
- if (this.dashboard.id && this.dashboard.meta.canAdmin) {
- this.sections.push({
- title: 'Permissions',
- id: 'permissions',
- icon: 'fa fa-fw fa-lock',
- });
- }
- if (this.dashboard.meta.canMakeEditable) {
- this.sections.push({
- title: 'General',
- icon: 'gicon gicon-preferences',
- id: 'make_editable',
- });
- }
- this.sections.push({
- title: 'JSON Model',
- id: 'dashboard_json',
- icon: 'gicon gicon-json',
- });
- const params = this.$location.search();
- const url = this.$location.path();
- for (const section of this.sections) {
- const sectionParams = _.defaults({ editview: section.id }, params);
- section.url = config.appSubUrl + url + '?' + $.param(sectionParams);
- }
- }
- onRouteUpdated() {
- this.viewId = this.$location.search().editview;
- if (this.viewId) {
- this.json = angular.toJson(this.dashboard.getSaveModelClone(), true);
- }
- if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
- this.viewId = 'make_editable';
- }
- const currentSection: any = _.find(this.sections, { id: this.viewId } as any);
- if (!currentSection) {
- this.sections.unshift({
- title: 'Not found',
- id: '404',
- icon: 'fa fa-fw fa-warning',
- });
- this.viewId = '404';
- }
- }
- openSaveAsModal() {
- this.dashboardSrv.showSaveAsModal();
- }
- saveDashboard() {
- this.dashboardSrv.saveDashboard();
- }
- saveDashboardJson() {
- this.dashboardSrv.saveJSONDashboard(this.json).then(() => {
- this.$route.reload();
- });
- }
- onPostSave() {
- this.hasUnsavedFolderChange = false;
- }
- hideSettings() {
- const urlParams = this.$location.search();
- delete urlParams.editview;
- setTimeout(() => {
- this.$rootScope.$apply(() => {
- this.$location.search(urlParams);
- });
- });
- }
- 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: any = _.find(this.sections, { id: this.viewId } as any);
- this.$location.url(currentSection.url);
- }
- deleteDashboard() {
- let confirmText = '';
- let text2 = this.dashboard.title;
- if (this.dashboard.meta.provisioned) {
- appEvents.emit('confirm-modal', {
- 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;
- }
- 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();
- },
- });
- }
- deleteDashboardConfirmed() {
- this.backendSrv.deleteDashboard(this.dashboard.uid).then(() => {
- appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
- this.$location.url('/');
- });
- }
- onFolderChange(folder) {
- this.dashboard.meta.folderId = folder.id;
- this.dashboard.meta.folderTitle = folder.title;
- this.hasUnsavedFolderChange = true;
- }
- getFolder() {
- return {
- id: this.dashboard.meta.folderId,
- title: this.dashboard.meta.folderTitle,
- url: this.dashboard.meta.folderUrl,
- };
- }
- }
- export function dashboardSettings() {
- return {
- restrict: 'E',
- templateUrl: 'public/app/features/dashboard/components/DashboardSettings/template.html',
- controller: SettingsCtrl,
- bindToController: true,
- controllerAs: 'ctrl',
- transclude: true,
- scope: { dashboard: '=' },
- };
- }
- coreModule.directive('dashboardSettings', dashboardSettings);
|