dashboard_srv.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. handleSaveDashboardError(clone, err) {
  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, {overwrite: true});
  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: 'Dashboard with the same name 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, {overwrite: true});
  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. var dashboardUrl = '/dashboard/db/' + data.slug;
  67. if (dashboardUrl !== this.$location.path()) {
  68. this.$location.url(dashboardUrl);
  69. }
  70. this.$rootScope.appEvent('dashboard-saved', this.dash);
  71. this.$rootScope.appEvent('alert-success', ['Dashboard saved']);
  72. }
  73. save(clone, options) {
  74. return this.backendSrv.saveDashboard(clone, options)
  75. .then(this.postSave.bind(this, clone))
  76. .catch(this.handleSaveDashboardError.bind(this, clone));
  77. }
  78. saveDashboard(options, clone) {
  79. if (clone) {
  80. this.setCurrent(this.create(clone, this.dash.meta));
  81. }
  82. if (!this.dash.meta.canSave && options.makeEditable !== true) {
  83. return Promise.resolve();
  84. }
  85. if (this.dash.title === 'New dashboard') {
  86. return this.showSaveAsModal();
  87. }
  88. if (this.dash.version > 0) {
  89. return this.showSaveModal();
  90. }
  91. return this.save(this.dash.getSaveModelClone(), options);
  92. }
  93. showSaveAsModal() {
  94. this.$rootScope.appEvent('show-modal', {
  95. templateHtml: '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
  96. modalClass: 'modal--narrow'
  97. });
  98. }
  99. showSaveModal() {
  100. this.$rootScope.appEvent('show-modal', {
  101. templateHtml: '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  102. modalClass: 'modal--narrow'
  103. });
  104. }
  105. }
  106. coreModule.service('dashboardSrv', DashboardSrv);