dashboard_srv.ts 4.1 KB

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