folder_settings_ctrl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { FolderPageLoader } from './folder_page_loader';
  2. import appEvents from 'app/core/app_events';
  3. export class FolderSettingsCtrl {
  4. folderPageLoader: FolderPageLoader;
  5. navModel: any;
  6. folderId: number;
  7. uid: string;
  8. canSave = false;
  9. dashboard: any;
  10. meta: any;
  11. title: string;
  12. hasChanged: boolean;
  13. /** @ngInject */
  14. constructor(private backendSrv, navModelSrv, private $routeParams, private $location) {
  15. if (this.$routeParams.uid) {
  16. this.uid = $routeParams.uid;
  17. this.folderPageLoader = new FolderPageLoader(this.backendSrv);
  18. this.folderPageLoader.load(this, this.uid, 'manage-folder-settings').then(folder => {
  19. if ($location.path() !== folder.meta.url) {
  20. $location.path(`${folder.meta.url}/settings`).replace();
  21. }
  22. this.dashboard = folder.dashboard;
  23. this.meta = folder.meta;
  24. this.canSave = folder.meta.canSave;
  25. this.title = this.dashboard.title;
  26. });
  27. }
  28. }
  29. save() {
  30. this.titleChanged();
  31. if (!this.hasChanged) {
  32. return;
  33. }
  34. this.dashboard.title = this.title.trim();
  35. return this.backendSrv
  36. .updateDashboardFolder(this.dashboard, { overwrite: false })
  37. .then(result => {
  38. if (result.url !== this.$location.path()) {
  39. this.$location.url(result.url + '/settings');
  40. }
  41. appEvents.emit('dashboard-saved');
  42. appEvents.emit('alert-success', ['Folder saved']);
  43. })
  44. .catch(this.handleSaveFolderError);
  45. }
  46. titleChanged() {
  47. this.hasChanged = this.dashboard.title.toLowerCase() !== this.title.trim().toLowerCase();
  48. }
  49. delete(evt) {
  50. if (evt) {
  51. evt.stopPropagation();
  52. evt.preventDefault();
  53. }
  54. appEvents.emit('confirm-modal', {
  55. title: 'Delete',
  56. text: `Do you want to delete this folder and all its dashboards?`,
  57. icon: 'fa-trash',
  58. yesText: 'Delete',
  59. onConfirm: () => {
  60. return this.backendSrv.deleteDashboard(this.dashboard.uid).then(() => {
  61. appEvents.emit('alert-success', ['Folder Deleted', `${this.dashboard.title} has been deleted`]);
  62. this.$location.url('dashboards');
  63. });
  64. },
  65. });
  66. }
  67. handleSaveFolderError(err) {
  68. if (err.data && err.data.status === 'version-mismatch') {
  69. err.isHandled = true;
  70. appEvents.emit('confirm-modal', {
  71. title: 'Conflict',
  72. text: 'Someone else has updated this folder.',
  73. text2: 'Would you still like to save this folder?',
  74. yesText: 'Save & Overwrite',
  75. icon: 'fa-warning',
  76. onConfirm: () => {
  77. this.backendSrv.updateDashboardFolder(this.dashboard, { overwrite: true });
  78. },
  79. });
  80. }
  81. if (err.data && err.data.status === 'name-exists') {
  82. err.isHandled = true;
  83. appEvents.emit('alert-error', ['A folder or dashboard with this name exists already.']);
  84. }
  85. }
  86. }