dashboard_srv.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  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, err) {
  18. if (err.data && err.data.status === "version-mismatch") {
  19. err.isHandled = true;
  20. this.$rootScope.appEvent('confirm-modal', {
  21. title: 'Conflict',
  22. text: 'Someone else has updated this dashboard.',
  23. text2: 'Would you still like to save this dashboard?',
  24. yesText: "Save & Overwrite",
  25. icon: "fa-warning",
  26. onConfirm: () => {
  27. this.save(clone, {overwrite: true});
  28. }
  29. });
  30. }
  31. if (err.data && err.data.status === "name-exists") {
  32. err.isHandled = true;
  33. this.$rootScope.appEvent('confirm-modal', {
  34. title: 'Conflict',
  35. text: 'Dashboard with the same name exists.',
  36. text2: 'Would you still like to save this dashboard?',
  37. yesText: "Save & Overwrite",
  38. icon: "fa-warning",
  39. onConfirm: () => {
  40. this.save(clone, {overwrite: true});
  41. }
  42. });
  43. }
  44. if (err.data && err.data.status === "plugin-dashboard") {
  45. err.isHandled = true;
  46. this.$rootScope.appEvent('confirm-modal', {
  47. title: 'Plugin Dashboard',
  48. text: err.data.message,
  49. text2: '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. return this.backendSrv.saveDashboard(clone, options)
  73. .then(this.postSave.bind(this, clone))
  74. .catch(this.handleSaveDashboardError.bind(this, clone));
  75. }
  76. saveDashboard(options, clone) {
  77. if (clone) {
  78. this.setCurrent(this.create(clone, this.dash.meta));
  79. }
  80. if (!this.dash.meta.canSave && options.makeEditable !== true) {
  81. return Promise.resolve();
  82. }
  83. if (this.dash.title === 'New dashboard') {
  84. return this.showSaveAsModal();
  85. }
  86. if (this.dash.version > 0) {
  87. return this.showSaveModal();
  88. }
  89. return this.save(this.dash.getSaveModelClone(), options);
  90. }
  91. showSaveAsModal() {
  92. this.$rootScope.appEvent('show-modal', {
  93. templateHtml: '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
  94. modalClass: 'modal--narrow'
  95. });
  96. }
  97. showSaveModal() {
  98. this.$rootScope.appEvent('show-modal', {
  99. templateHtml: '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  100. modalClass: 'modal--narrow'
  101. });
  102. }
  103. starDashboard(dashboardId, isStarred) {
  104. let promise;
  105. if (isStarred) {
  106. promise = this.backendSrv.delete('/api/user/stars/dashboard/' + dashboardId).then(() => {
  107. return false;
  108. });
  109. } else {
  110. promise = this.backendSrv.post('/api/user/stars/dashboard/' + dashboardId).then(() => {
  111. return true;
  112. });
  113. }
  114. return promise.then(res => {
  115. if (this.dash && this.dash.id === dashboardId) {
  116. this.dash.meta.isStarred = res;
  117. }
  118. return res;
  119. });
  120. }
  121. }
  122. coreModule.service('dashboardSrv', DashboardSrv);