settings.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { coreModule, appEvents, contextSrv } from 'app/core/core';
  2. import { DashboardModel } from '../dashboard_model';
  3. import $ from 'jquery';
  4. import _ from 'lodash';
  5. export class SettingsCtrl {
  6. dashboard: DashboardModel;
  7. isOpen: boolean;
  8. viewId: string;
  9. json: string;
  10. alertCount: number;
  11. canSaveAs: boolean;
  12. canDelete: boolean;
  13. sections: any[];
  14. /** @ngInject */
  15. constructor(private $scope, private $location, private $rootScope, private backendSrv, private dashboardSrv) {
  16. // temp hack for annotations and variables editors
  17. // that rely on inherited scope
  18. $scope.dashboard = this.dashboard;
  19. this.$scope.$on('$destroy', () => {
  20. this.dashboard.updateSubmenuVisibility();
  21. this.$rootScope.$broadcast('refresh');
  22. });
  23. this.canSaveAs = contextSrv.isEditor;
  24. this.canDelete = this.dashboard.meta.canSave;
  25. this.buildSectionList();
  26. this.onRouteUpdated();
  27. $rootScope.onAppEvent('$routeUpdate', this.onRouteUpdated.bind(this), $scope);
  28. }
  29. buildSectionList() {
  30. this.sections = [];
  31. if (this.dashboard.meta.canEdit) {
  32. this.sections.push({ title: 'General', id: 'settings', icon: 'gicon gicon-preferences' });
  33. this.sections.push({ title: 'Annotations', id: 'annotations', icon: 'gicon gicon-annotation' });
  34. this.sections.push({ title: 'Variables', id: 'templating', icon: 'gicon gicon-variable' });
  35. this.sections.push({ title: 'Links', id: 'links', icon: 'gicon gicon-link' });
  36. }
  37. if (this.dashboard.id && this.dashboard.meta.canSave) {
  38. this.sections.push({ title: 'Versions', id: 'versions', icon: 'fa fa-fw fa-history' });
  39. }
  40. if (this.dashboard.meta.canMakeEditable) {
  41. this.sections.push({ title: 'Make Editable', icon: 'fa fa-fw fa-edit', id: 'make_editable' });
  42. }
  43. this.sections.push({ title: 'View JSON', id: 'view_json', icon: 'gicon gicon-json' });
  44. const params = this.$location.search();
  45. const url = this.$location.path();
  46. for (let section of this.sections) {
  47. const sectionParams = _.defaults({ editview: section.id }, params);
  48. section.url = url + '?' + $.param(sectionParams);
  49. }
  50. }
  51. onRouteUpdated() {
  52. this.viewId = this.$location.search().editview;
  53. if (this.viewId) {
  54. this.json = JSON.stringify(this.dashboard.getSaveModelClone(), null, 2);
  55. }
  56. if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
  57. this.viewId = 'make_editable';
  58. }
  59. const currentSection = _.find(this.sections, { id: this.viewId });
  60. if (!currentSection) {
  61. this.sections.unshift({ title: 'Not found', id: '404', icon: 'fa fa-fw fa-warning' });
  62. this.viewId = '404';
  63. }
  64. }
  65. openSaveAsModal() {
  66. this.dashboardSrv.showSaveAsModal();
  67. }
  68. hideSettings() {
  69. var urlParams = this.$location.search();
  70. delete urlParams.editview;
  71. setTimeout(() => {
  72. this.$rootScope.$apply(() => {
  73. this.$location.search(urlParams);
  74. });
  75. });
  76. }
  77. makeEditable() {
  78. this.dashboard.editable = true;
  79. return this.dashboardSrv.saveDashboard({ makeEditable: true, overwrite: false }).then(() => {
  80. // force refresh whole page
  81. window.location.href = window.location.href;
  82. });
  83. }
  84. deleteDashboard() {
  85. var confirmText = '';
  86. var text2 = this.dashboard.title;
  87. const alerts = _.sumBy(this.dashboard.panels, panel => {
  88. return panel.alert ? 1 : 0;
  89. });
  90. if (alerts > 0) {
  91. confirmText = 'DELETE';
  92. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
  93. }
  94. appEvents.emit('confirm-modal', {
  95. title: 'Delete',
  96. text: 'Do you want to delete this dashboard?',
  97. text2: text2,
  98. icon: 'fa-trash',
  99. confirmText: confirmText,
  100. yesText: 'Delete',
  101. onConfirm: () => {
  102. this.dashboard.meta.canSave = false;
  103. this.deleteDashboardConfirmed();
  104. }
  105. });
  106. }
  107. deleteDashboardConfirmed() {
  108. this.backendSrv.deleteDashboard(this.dashboard.meta.slug).then(() => {
  109. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  110. this.$location.url('/');
  111. });
  112. }
  113. onFolderChange(folder) {
  114. this.dashboard.folderId = folder.id;
  115. this.dashboard.meta.folderId = folder.id;
  116. this.dashboard.meta.folderTitle = folder.title;
  117. }
  118. }
  119. export function dashboardSettings() {
  120. return {
  121. restrict: 'E',
  122. templateUrl: 'public/app/features/dashboard/settings/settings.html',
  123. controller: SettingsCtrl,
  124. bindToController: true,
  125. controllerAs: 'ctrl',
  126. transclude: true,
  127. scope: { dashboard: '=' },
  128. };
  129. }
  130. coreModule.directive('dashboardSettings', dashboardSettings);