settings.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.id && this.dashboard.meta.canAdmin) {
  64. this.sections.push({
  65. title: 'Permissions',
  66. id: 'permissions',
  67. icon: 'fa fa-fw fa-lock',
  68. });
  69. }
  70. if (this.dashboard.meta.canMakeEditable) {
  71. this.sections.push({
  72. title: 'General',
  73. icon: 'gicon gicon-preferences',
  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 = config.appSubUrl + 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. saveDashboard() {
  111. this.dashboardSrv.saveDashboard();
  112. }
  113. hideSettings() {
  114. var urlParams = this.$location.search();
  115. delete urlParams.editview;
  116. setTimeout(() => {
  117. this.$rootScope.$apply(() => {
  118. this.$location.search(urlParams);
  119. });
  120. });
  121. }
  122. makeEditable() {
  123. this.dashboard.editable = true;
  124. this.dashboard.meta.canMakeEditable = false;
  125. this.dashboard.meta.canEdit = true;
  126. this.dashboard.meta.canSave = true;
  127. this.canDelete = true;
  128. this.viewId = 'settings';
  129. this.buildSectionList();
  130. const currentSection = _.find(this.sections, { id: this.viewId });
  131. this.$location.url(currentSection.url);
  132. }
  133. deleteDashboard() {
  134. var confirmText = '';
  135. var text2 = this.dashboard.title;
  136. const alerts = _.sumBy(this.dashboard.panels, panel => {
  137. return panel.alert ? 1 : 0;
  138. });
  139. if (alerts > 0) {
  140. confirmText = 'DELETE';
  141. text2 = `This dashboard contains ${alerts} alerts. Deleting this dashboard will also delete those alerts`;
  142. }
  143. appEvents.emit('confirm-modal', {
  144. title: 'Delete',
  145. text: 'Do you want to delete this dashboard?',
  146. text2: text2,
  147. icon: 'fa-trash',
  148. confirmText: confirmText,
  149. yesText: 'Delete',
  150. onConfirm: () => {
  151. this.dashboard.meta.canSave = false;
  152. this.deleteDashboardConfirmed();
  153. },
  154. });
  155. }
  156. deleteDashboardConfirmed() {
  157. this.backendSrv.deleteDashboard(this.dashboard.meta.slug).then(() => {
  158. appEvents.emit('alert-success', ['Dashboard Deleted', this.dashboard.title + ' has been deleted']);
  159. this.$location.url('/');
  160. });
  161. }
  162. onFolderChange(folder) {
  163. this.dashboard.meta.folderId = folder.id;
  164. this.dashboard.meta.folderTitle = folder.title;
  165. }
  166. }
  167. export function dashboardSettings() {
  168. return {
  169. restrict: 'E',
  170. templateUrl: 'public/app/features/dashboard/settings/settings.html',
  171. controller: SettingsCtrl,
  172. bindToController: true,
  173. controllerAs: 'ctrl',
  174. transclude: true,
  175. scope: { dashboard: '=' },
  176. };
  177. }
  178. coreModule.directive('dashboardSettings', dashboardSettings);