dashboard_srv.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import coreModule from 'app/core/core_module';
  2. import { DashboardModel } from './dashboard_model';
  3. export class DashboardSrv {
  4. dash: any;
  5. /** @ngInject */
  6. constructor(private backendSrv, private $rootScope, private $location) {}
  7. create(dashboard, meta) {
  8. return new DashboardModel(dashboard, meta);
  9. }
  10. setCurrent(dashboard) {
  11. this.dash = dashboard;
  12. }
  13. getCurrent() {
  14. return this.dash;
  15. }
  16. handleSaveDashboardError(clone, err) {
  17. if (err.data && err.data.status === 'version-mismatch') {
  18. err.isHandled = true;
  19. this.$rootScope.appEvent('confirm-modal', {
  20. title: 'Conflict',
  21. text: 'Someone else has updated this dashboard.',
  22. text2: 'Would you still like to save this dashboard?',
  23. yesText: 'Save & Overwrite',
  24. icon: 'fa-warning',
  25. onConfirm: () => {
  26. this.save(clone, { overwrite: true });
  27. },
  28. });
  29. }
  30. if (err.data && err.data.status === 'name-exists') {
  31. err.isHandled = true;
  32. this.$rootScope.appEvent('confirm-modal', {
  33. title: 'Conflict',
  34. text: 'Dashboard with the same name exists.',
  35. text2: 'Would you still like to save this dashboard?',
  36. yesText: 'Save & Overwrite',
  37. icon: 'fa-warning',
  38. onConfirm: () => {
  39. this.save(clone, { overwrite: true });
  40. },
  41. });
  42. }
  43. if (err.data && err.data.status === 'plugin-dashboard') {
  44. err.isHandled = true;
  45. this.$rootScope.appEvent('confirm-modal', {
  46. title: 'Plugin Dashboard',
  47. text: err.data.message,
  48. text2: 'Your changes will be lost when you update the plugin. Use Save As to create custom version.',
  49. yesText: 'Overwrite',
  50. icon: 'fa-warning',
  51. altActionText: 'Save As',
  52. onAltAction: () => {
  53. this.showSaveAsModal();
  54. },
  55. onConfirm: () => {
  56. this.save(clone, { overwrite: true });
  57. },
  58. });
  59. }
  60. }
  61. postSave(clone, data) {
  62. this.dash.version = data.version;
  63. var dashboardUrl = '/dashboard/db/' + data.slug;
  64. if (dashboardUrl !== this.$location.path()) {
  65. this.$location.url(dashboardUrl);
  66. }
  67. this.$rootScope.appEvent('dashboard-saved', this.dash);
  68. this.$rootScope.appEvent('alert-success', ['Dashboard saved']);
  69. }
  70. save(clone, options) {
  71. options = options || {};
  72. options.folderId = this.dash.meta.folderId;
  73. return this.backendSrv
  74. .saveDashboard(clone, options)
  75. .then(this.postSave.bind(this, clone))
  76. .catch(this.handleSaveDashboardError.bind(this, clone));
  77. }
  78. saveDashboard(options, clone) {
  79. if (clone) {
  80. this.setCurrent(this.create(clone, this.dash.meta));
  81. }
  82. if (!this.dash.meta.canSave && options.makeEditable !== true) {
  83. return Promise.resolve();
  84. }
  85. if (this.dash.title === 'New dashboard') {
  86. return this.showSaveAsModal();
  87. }
  88. if (this.dash.version > 0) {
  89. return this.showSaveModal();
  90. }
  91. return this.save(this.dash.getSaveModelClone(), options);
  92. }
  93. showSaveAsModal() {
  94. this.$rootScope.appEvent('show-modal', {
  95. templateHtml: '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
  96. modalClass: 'modal--narrow',
  97. });
  98. }
  99. showSaveModal() {
  100. this.$rootScope.appEvent('show-modal', {
  101. templateHtml: '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  102. modalClass: 'modal--narrow',
  103. });
  104. }
  105. starDashboard(dashboardId, isStarred) {
  106. let promise;
  107. if (isStarred) {
  108. promise = this.backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
  109. return false;
  110. });
  111. } else {
  112. promise = this.backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
  113. return true;
  114. });
  115. }
  116. return promise.then(res => {
  117. if (this.dash && this.dash.id === dashboardId) {
  118. this.dash.meta.isStarred = res;
  119. }
  120. return res;
  121. });
  122. }
  123. }
  124. coreModule.service('dashboardSrv', DashboardSrv);