settings.ts 6.1 KB

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