dashboard_srv.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. var clone = this.dash.getSaveModelClone();
  24. return this.backendSrv.saveDashboard(clone, options).then(data => {
  25. this.dash.version = data.version;
  26. this.$rootScope.appEvent('dashboard-saved', this.dash);
  27. var dashboardUrl = '/dashboard/db/' + data.slug;
  28. if (dashboardUrl !== this.$location.path()) {
  29. this.$location.url(dashboardUrl);
  30. }
  31. this.$rootScope.appEvent('alert-success', ['Dashboard saved', 'Saved as ' + clone.title]);
  32. }).catch(this.handleSaveDashboardError.bind(this));
  33. }
  34. handleSaveDashboardError(err) {
  35. if (err.data && err.data.status === "version-mismatch") {
  36. err.isHandled = true;
  37. this.$rootScope.appEvent('confirm-modal', {
  38. title: 'Conflict',
  39. text: 'Someone else has updated this dashboard.',
  40. text2: 'Would you still like to save this dashboard?',
  41. yesText: "Save & Overwrite",
  42. icon: "fa-warning",
  43. onConfirm: () => {
  44. this.saveDashboard({overwrite: true});
  45. }
  46. });
  47. }
  48. if (err.data && err.data.status === "name-exists") {
  49. err.isHandled = true;
  50. this.$rootScope.appEvent('confirm-modal', {
  51. title: 'Conflict',
  52. text: 'Dashboard with the same name exists.',
  53. text2: 'Would you still like to save this dashboard?',
  54. yesText: "Save & Overwrite",
  55. icon: "fa-warning",
  56. onConfirm: () => {
  57. this.saveDashboard({overwrite: true});
  58. }
  59. });
  60. }
  61. if (err.data && err.data.status === "plugin-dashboard") {
  62. err.isHandled = true;
  63. this.$rootScope.appEvent('confirm-modal', {
  64. title: 'Plugin Dashboard',
  65. text: err.data.message,
  66. text2: 'Your changes will be lost when you update the plugin. Use Save As to create custom version.',
  67. yesText: "Overwrite",
  68. icon: "fa-warning",
  69. altActionText: "Save As",
  70. onAltAction: () => {
  71. this.saveDashboardAs();
  72. },
  73. onConfirm: function() {
  74. this.saveDashboard({overwrite: true});
  75. }
  76. });
  77. }
  78. }
  79. saveDashboardAs() {
  80. var newScope = this.$rootScope.$new();
  81. newScope.clone = this.dash.getSaveModelClone();
  82. newScope.clone.editable = true;
  83. newScope.clone.hideControls = false;
  84. this.$rootScope.appEvent('show-modal', {
  85. src: 'public/app/features/dashboard/partials/saveDashboardAs.html',
  86. scope: newScope,
  87. modalClass: 'modal--narrow'
  88. });
  89. }
  90. }
  91. coreModule.service('dashboardSrv', DashboardSrv);