settings.ts 5.1 KB

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