settings.ts 5.0 KB

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