settings.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: 'fa fa-fw fa-sliders' });
  33. this.sections.push({ title: 'Annotations', id: 'annotations', icon: 'fa fa-fw fa-comment-o' });
  34. this.sections.push({ title: 'Variables', id: 'templating', icon: 'fa fa-fw fa-dollar' });
  35. this.sections.push({ title: 'Links', id: 'links', icon: 'fa fa-fw fa-external-link' });
  36. if (this.dashboard.id) {
  37. this.sections.push({ title: 'Versions', id: 'versions', icon: 'fa fa-fw fa-history' });
  38. }
  39. }
  40. if (contextSrv.isEditor && !this.dashboard.editable) {
  41. this.sections.push({ title: 'Make Editable', icon: 'fa fa-fw fa-edit', id: 'make_editable' });
  42. this.viewId = 'make_editable';
  43. }
  44. this.sections.push({ title: 'View JSON', id: 'view_json', icon: 'fa fa-fw fa-code' });
  45. const params = this.$location.search();
  46. const url = this.$location.path();
  47. for (let section of this.sections) {
  48. const sectionParams = _.defaults({ editview: section.id }, params);
  49. section.url = url + '?' + $.param(sectionParams);
  50. }
  51. }
  52. onRouteUpdated() {
  53. this.viewId = this.$location.search().editview;
  54. if (this.viewId) {
  55. this.json = JSON.stringify(this.dashboard.getSaveModelClone(), null, 2);
  56. }
  57. const currentSection = _.find(this.sections, { id: this.viewId });
  58. if (!currentSection) {
  59. this.sections.unshift({ title: 'Not found', id: '404', icon: 'fa fa-fw fa-warning' });
  60. this.viewId = '404';
  61. return;
  62. }
  63. }
  64. openSaveAsModal() {
  65. this.dashboardSrv.showSaveAsModal();
  66. }
  67. hideSettings() {
  68. var urlParams = this.$location.search();
  69. delete urlParams.editview;
  70. setTimeout(() => {
  71. this.$rootScope.$apply(() => {
  72. this.$location.search(urlParams);
  73. });
  74. });
  75. }
  76. makeEditable() {
  77. this.dashboard.editable = true;
  78. return this.dashboardSrv.saveDashboard({ makeEditable: true, overwrite: false }).then(() => {
  79. // force refresh whole page
  80. window.location.href = window.location.href;
  81. });
  82. }
  83. deleteDashboard() {
  84. var confirmText = '';
  85. var text2 = this.dashboard.title;
  86. const alerts = _.sumBy(this.dashboard.panels, panel => {
  87. return panel.alert ? 1 : 0;
  88. });
  89. if (alerts > 0) {
  90. confirmText = 'DELETE';
  91. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
  92. }
  93. appEvents.emit('confirm-modal', {
  94. title: 'Delete',
  95. text: 'Do you want to delete this dashboard?',
  96. text2: text2,
  97. icon: 'fa-trash',
  98. confirmText: confirmText,
  99. yesText: 'Delete',
  100. onConfirm: () => {
  101. this.dashboard.meta.canSave = false;
  102. this.deleteDashboardConfirmed();
  103. }
  104. });
  105. }
  106. deleteDashboardConfirmed() {
  107. this.backendSrv.delete('/api/dashboards/db/' + this.dashboard.meta.slug).then(() => {
  108. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  109. this.$location.url('/');
  110. });
  111. }
  112. onFolderChange(folder) {
  113. this.dashboard.folderId = folder.id;
  114. this.dashboard.meta.folderId = folder.id;
  115. this.dashboard.meta.folderTitle = folder.title;
  116. }
  117. }
  118. export function dashboardSettings() {
  119. return {
  120. restrict: 'E',
  121. templateUrl: 'public/app/features/dashboard/settings/settings.html',
  122. controller: SettingsCtrl,
  123. bindToController: true,
  124. controllerAs: 'ctrl',
  125. transclude: true,
  126. scope: { dashboard: '=' },
  127. };
  128. }
  129. coreModule.directive('dashboardSettings', dashboardSettings);