dashboard_srv.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import {DashboardModel} from './model';
  5. export class DashboardSrv {
  6. dash: any;
  7. /** @ngInject */
  8. constructor(private backendSrv, private $rootScope, private $location) {
  9. }
  10. create(dashboard, meta) {
  11. return new DashboardModel(dashboard, meta);
  12. }
  13. setCurrent(dashboard) {
  14. this.dash = dashboard;
  15. }
  16. getCurrent() {
  17. return this.dash;
  18. }
  19. saveDashboard(options) {
  20. if (!this.dash.meta.canSave && options.makeEditable !== true) {
  21. return Promise.resolve();
  22. }
  23. if (this.dash.title === 'New dashboard') {
  24. return this.saveDashboardAs();
  25. }
  26. var clone = this.dash.getSaveModelClone();
  27. return this.backendSrv.saveDashboard(clone, options).then(data => {
  28. this.dash.version = data.version;
  29. this.$rootScope.appEvent('dashboard-saved', this.dash);
  30. var dashboardUrl = '/dashboard/db/' + data.slug;
  31. if (dashboardUrl !== this.$location.path()) {
  32. this.$location.url(dashboardUrl);
  33. }
  34. this.$rootScope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  35. }).catch(this.handleSaveDashboardError.bind(this));
  36. }
  37. handleSaveDashboardError(err) {
  38. if (err.data && err.data.status === "version-mismatch") {
  39. err.isHandled = true;
  40. this.$rootScope.appEvent('confirm-modal', {
  41. title: 'Conflict',
  42. text: 'Someone else has updated this dashboard.',
  43. text2: 'Would you still like to save this dashboard?',
  44. yesText: "Save & Overwrite",
  45. icon: "fa-warning",
  46. onConfirm: () => {
  47. this.saveDashboard({overwrite: true});
  48. }
  49. });
  50. }
  51. if (err.data && err.data.status === "name-exists") {
  52. err.isHandled = true;
  53. this.$rootScope.appEvent('confirm-modal', {
  54. title: 'Conflict',
  55. text: 'Dashboard with the same name exists.',
  56. text2: 'Would you still like to save this dashboard?',
  57. yesText: "Save & Overwrite",
  58. icon: "fa-warning",
  59. onConfirm: () => {
  60. this.saveDashboard({overwrite: true});
  61. }
  62. });
  63. }
  64. if (err.data && err.data.status === "plugin-dashboard") {
  65. err.isHandled = true;
  66. this.$rootScope.appEvent('confirm-modal', {
  67. title: 'Plugin Dashboard',
  68. text: err.data.message,
  69. text2: 'Your changes will be lost when you update the plugin. Use Save As to create custom version.',
  70. yesText: "Overwrite",
  71. icon: "fa-warning",
  72. altActionText: "Save As",
  73. onAltAction: () => {
  74. this.saveDashboardAs();
  75. },
  76. onConfirm: () => {
  77. this.saveDashboard({overwrite: true});
  78. }
  79. });
  80. }
  81. }
  82. saveDashboardAs() {
  83. var newScope = this.$rootScope.$new();
  84. newScope.clone = this.dash.getSaveModelClone();
  85. newScope.clone.editable = true;
  86. newScope.clone.hideControls = false;
  87. this.$rootScope.appEvent('show-modal', {
  88. src: 'public/app/features/dashboard/partials/saveDashboardAs.html',
  89. scope: newScope,
  90. modalClass: 'modal--narrow'
  91. });
  92. }
  93. }
  94. coreModule.service('dashboardSrv', DashboardSrv);