settings.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.id && this.dashboard.meta.canAdmin) {
  62. this.sections.push({
  63. title: 'Permissions',
  64. id: 'permissions',
  65. icon: 'fa fa-fw fa-lock',
  66. });
  67. }
  68. if (this.dashboard.meta.canMakeEditable) {
  69. this.sections.push({
  70. title: 'General',
  71. icon: 'gicon gicon-preferences',
  72. id: 'make_editable',
  73. });
  74. }
  75. this.sections.push({
  76. title: 'View JSON',
  77. id: 'view_json',
  78. icon: 'gicon gicon-json',
  79. });
  80. const params = this.$location.search();
  81. const url = this.$location.path();
  82. for (let section of this.sections) {
  83. const sectionParams = _.defaults({ editview: section.id }, params);
  84. section.url = config.appSubUrl + url + '?' + $.param(sectionParams);
  85. }
  86. }
  87. onRouteUpdated() {
  88. this.viewId = this.$location.search().editview;
  89. if (this.viewId) {
  90. this.json = JSON.stringify(this.dashboard.getSaveModelClone(), null, 2);
  91. }
  92. if (this.viewId === 'settings' && this.dashboard.meta.canMakeEditable) {
  93. this.viewId = 'make_editable';
  94. }
  95. const currentSection = _.find(this.sections, { id: this.viewId });
  96. if (!currentSection) {
  97. this.sections.unshift({
  98. title: 'Not found',
  99. id: '404',
  100. icon: 'fa fa-fw fa-warning',
  101. });
  102. this.viewId = '404';
  103. }
  104. }
  105. openSaveAsModal() {
  106. this.dashboardSrv.showSaveAsModal();
  107. }
  108. hideSettings() {
  109. var urlParams = this.$location.search();
  110. delete urlParams.editview;
  111. setTimeout(() => {
  112. this.$rootScope.$apply(() => {
  113. this.$location.search(urlParams);
  114. });
  115. });
  116. }
  117. makeEditable() {
  118. this.dashboard.editable = true;
  119. this.dashboard.meta.canMakeEditable = false;
  120. this.dashboard.meta.canEdit = true;
  121. this.dashboard.meta.canSave = true;
  122. this.canDelete = true;
  123. this.viewId = 'settings';
  124. this.buildSectionList();
  125. const currentSection = _.find(this.sections, { id: this.viewId });
  126. this.$location.url(currentSection.url);
  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', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  154. this.$location.url('/');
  155. });
  156. }
  157. onFolderChange(folder) {
  158. this.dashboard.meta.folderId = folder.id;
  159. this.dashboard.meta.folderTitle = folder.title;
  160. }
  161. }
  162. export function dashboardSettings() {
  163. return {
  164. restrict: 'E',
  165. templateUrl: 'public/app/features/dashboard/settings/settings.html',
  166. controller: SettingsCtrl,
  167. bindToController: true,
  168. controllerAs: 'ctrl',
  169. transclude: true,
  170. scope: { dashboard: '=' },
  171. };
  172. }
  173. coreModule.directive('dashboardSettings', dashboardSettings);