dashboard_srv.ts 3.6 KB

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