dashboard_srv.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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:
  49. 'Your changes will be lost when you update the plugin. Use Save As to create custom version.',
  50. yesText: 'Overwrite',
  51. icon: 'fa-warning',
  52. altActionText: 'Save As',
  53. onAltAction: () => {
  54. this.showSaveAsModal();
  55. },
  56. onConfirm: () => {
  57. this.save(clone, { overwrite: true });
  58. },
  59. });
  60. }
  61. }
  62. postSave(clone, data) {
  63. this.dash.version = data.version;
  64. var dashboardUrl = '/dashboard/db/' + data.slug;
  65. if (dashboardUrl !== this.$location.path()) {
  66. this.$location.url(dashboardUrl);
  67. }
  68. this.$rootScope.appEvent('dashboard-saved', this.dash);
  69. this.$rootScope.appEvent('alert-success', ['Dashboard saved']);
  70. }
  71. save(clone, options) {
  72. options = options || {};
  73. options.folderId = this.dash.meta.folderId;
  74. return this.backendSrv
  75. .saveDashboard(clone, options)
  76. .then(this.postSave.bind(this, clone))
  77. .catch(this.handleSaveDashboardError.bind(this, clone));
  78. }
  79. saveDashboard(options, clone) {
  80. if (clone) {
  81. this.setCurrent(this.create(clone, this.dash.meta));
  82. }
  83. if (!this.dash.meta.canSave && options.makeEditable !== true) {
  84. return Promise.resolve();
  85. }
  86. if (this.dash.title === 'New dashboard') {
  87. return this.showSaveAsModal();
  88. }
  89. if (this.dash.version > 0) {
  90. return this.showSaveModal();
  91. }
  92. return this.save(this.dash.getSaveModelClone(), options);
  93. }
  94. showSaveAsModal() {
  95. this.$rootScope.appEvent('show-modal', {
  96. templateHtml:
  97. '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
  98. modalClass: 'modal--narrow',
  99. });
  100. }
  101. showSaveModal() {
  102. this.$rootScope.appEvent('show-modal', {
  103. templateHtml:
  104. '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  105. modalClass: 'modal--narrow',
  106. });
  107. }
  108. starDashboard(dashboardId, isStarred) {
  109. let promise;
  110. if (isStarred) {
  111. promise = this.backendSrv
  112. .delete('/api/user/stars/dashboard/' + dashboardId)
  113. .then(() => {
  114. return false;
  115. });
  116. } else {
  117. promise = this.backendSrv
  118. .post('/api/user/stars/dashboard/' + dashboardId)
  119. .then(() => {
  120. return true;
  121. });
  122. }
  123. return promise.then(res => {
  124. if (this.dash && this.dash.id === dashboardId) {
  125. this.dash.meta.isStarred = res;
  126. }
  127. return res;
  128. });
  129. }
  130. }
  131. coreModule.service('dashboardSrv', DashboardSrv);