settings.ts 5.9 KB

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