settings.ts 4.9 KB

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