settings.ts 5.7 KB

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