settings.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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({
  33. title: 'General',
  34. id: 'settings',
  35. icon: 'gicon gicon-preferences',
  36. });
  37. this.sections.push({
  38. title: 'Annotations',
  39. id: 'annotations',
  40. icon: 'gicon gicon-annotation',
  41. });
  42. this.sections.push({
  43. title: 'Variables',
  44. id: 'templating',
  45. icon: 'gicon gicon-variable',
  46. });
  47. this.sections.push({
  48. title: 'Links',
  49. id: 'links',
  50. icon: 'gicon gicon-link',
  51. });
  52. }
  53. if (this.dashboard.id && this.dashboard.meta.canSave) {
  54. this.sections.push({
  55. title: 'Versions',
  56. id: 'versions',
  57. icon: 'fa fa-fw fa-history',
  58. });
  59. }
  60. if (this.dashboard.meta.canMakeEditable) {
  61. this.sections.push({
  62. title: 'General',
  63. icon: 'gicon gicon-preferences',
  64. id: 'make_editable',
  65. });
  66. }
  67. this.sections.push({
  68. title: 'View JSON',
  69. id: 'view_json',
  70. icon: 'gicon gicon-json',
  71. });
  72. const params = this.$location.search();
  73. const url = this.$location.path();
  74. for (let section of this.sections) {
  75. const sectionParams = _.defaults({ editview: section.id }, params);
  76. section.url = url + '?' + $.param(sectionParams);
  77. }
  78. }
  79. onRouteUpdated() {
  80. this.viewId = this.$location.search().editview;
  81. if (this.viewId) {
  82. this.json = JSON.stringify(this.dashboard.getSaveModelClone(), null, 2);
  83. }
  84. if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
  85. this.viewId = 'make_editable';
  86. }
  87. const currentSection = _.find(this.sections, { id: this.viewId });
  88. if (!currentSection) {
  89. this.sections.unshift({
  90. title: 'Not found',
  91. id: '404',
  92. icon: 'fa fa-fw fa-warning',
  93. });
  94. this.viewId = '404';
  95. }
  96. }
  97. openSaveAsModal() {
  98. this.dashboardSrv.showSaveAsModal();
  99. }
  100. hideSettings() {
  101. var urlParams = this.$location.search();
  102. delete urlParams.editview;
  103. setTimeout(() => {
  104. this.$rootScope.$apply(() => {
  105. this.$location.search(urlParams);
  106. });
  107. });
  108. }
  109. makeEditable() {
  110. this.dashboard.editable = true;
  111. this.dashboard.meta.canMakeEditable = false;
  112. this.dashboard.meta.canEdit = true;
  113. this.dashboard.meta.canSave = true;
  114. this.canDelete = true;
  115. this.viewId = 'settings';
  116. this.buildSectionList();
  117. const currentSection = _.find(this.sections, { id: this.viewId });
  118. this.$location.url(currentSection.url);
  119. }
  120. deleteDashboard() {
  121. var confirmText = '';
  122. var text2 = this.dashboard.title;
  123. const alerts = _.sumBy(this.dashboard.panels, panel => {
  124. return panel.alert ? 1 : 0;
  125. });
  126. if (alerts > 0) {
  127. confirmText = 'DELETE';
  128. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
  129. }
  130. appEvents.emit('confirm-modal', {
  131. title: 'Delete',
  132. text: 'Do you want to delete this dashboard?',
  133. text2: text2,
  134. icon: 'fa-trash',
  135. confirmText: confirmText,
  136. yesText: 'Delete',
  137. onConfirm: () => {
  138. this.dashboard.meta.canSave = false;
  139. this.deleteDashboardConfirmed();
  140. },
  141. });
  142. }
  143. deleteDashboardConfirmed() {
  144. this.backendSrv.deleteDashboard(this.dashboard.meta.slug).then(() => {
  145. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  146. this.$location.url('/');
  147. });
  148. }
  149. onFolderChange(folder) {
  150. this.dashboard.meta.folderId = folder.id;
  151. this.dashboard.meta.folderTitle = folder.title;
  152. }
  153. }
  154. export function dashboardSettings() {
  155. return {
  156. restrict: 'E',
  157. templateUrl: 'public/app/features/dashboard/settings/settings.html',
  158. controller: SettingsCtrl,
  159. bindToController: true,
  160. controllerAs: 'ctrl',
  161. transclude: true,
  162. scope: { dashboard: '=' },
  163. };
  164. }
  165. coreModule.directive('dashboardSettings', dashboardSettings);