dashboard_srv.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. create(dashboard, meta) {
  8. return new DashboardModel(dashboard, meta);
  9. }
  10. setCurrent(dashboard) {
  11. this.dash = dashboard;
  12. }
  13. getCurrent() {
  14. return this.dash;
  15. }
  16. handleSaveDashboardError(clone, err) {
  17. if (err.data && err.data.status === "version-mismatch") {
  18. err.isHandled = true;
  19. this.$rootScope.appEvent("confirm-modal", {
  20. title: "Conflict",
  21. text: "Someone else has updated this dashboard.",
  22. text2: "Would you still like to save this dashboard?",
  23. yesText: "Save & Overwrite",
  24. icon: "fa-warning",
  25. onConfirm: () => {
  26. this.save(clone, { overwrite: true });
  27. }
  28. });
  29. }
  30. if (err.data && err.data.status === "name-exists") {
  31. err.isHandled = true;
  32. this.$rootScope.appEvent("confirm-modal", {
  33. title: "Conflict",
  34. text: "Dashboard with the same name exists.",
  35. text2: "Would you still like to save this dashboard?",
  36. yesText: "Save & Overwrite",
  37. icon: "fa-warning",
  38. onConfirm: () => {
  39. this.save(clone, { overwrite: true });
  40. }
  41. });
  42. }
  43. if (err.data && err.data.status === "plugin-dashboard") {
  44. err.isHandled = true;
  45. this.$rootScope.appEvent("confirm-modal", {
  46. title: "Plugin Dashboard",
  47. text: err.data.message,
  48. text2:
  49. "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
  73. .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. this.$rootScope.appEvent("show-modal", {
  94. templateHtml:
  95. '<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:
  102. '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
  103. modalClass: "modal--narrow"
  104. });
  105. }
  106. starDashboard(dashboardId, isStarred) {
  107. let promise;
  108. if (isStarred) {
  109. promise = this.backendSrv
  110. .delete("/api/user/stars/dashboard/" + dashboardId)
  111. .then(() => {
  112. return false;
  113. });
  114. } else {
  115. promise = this.backendSrv
  116. .post("/api/user/stars/dashboard/" + dashboardId)
  117. .then(() => {
  118. return true;
  119. });
  120. }
  121. return promise.then(res => {
  122. if (this.dash && this.dash.id === dashboardId) {
  123. this.dash.meta.isStarred = res;
  124. }
  125. return res;
  126. });
  127. }
  128. }
  129. coreModule.service("dashboardSrv", DashboardSrv);