| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import coreModule from "app/core/core_module";
- import { DashboardModel } from "./dashboard_model";
- export class DashboardSrv {
- dash: any;
- /** @ngInject */
- constructor(private backendSrv, private $rootScope, private $location) {}
- create(dashboard, meta) {
- return new DashboardModel(dashboard, meta);
- }
- setCurrent(dashboard) {
- this.dash = dashboard;
- }
- getCurrent() {
- return this.dash;
- }
- handleSaveDashboardError(clone, err) {
- if (err.data && err.data.status === "version-mismatch") {
- err.isHandled = true;
- this.$rootScope.appEvent("confirm-modal", {
- title: "Conflict",
- text: "Someone else has updated this dashboard.",
- text2: "Would you still like to save this dashboard?",
- yesText: "Save & Overwrite",
- icon: "fa-warning",
- onConfirm: () => {
- this.save(clone, { overwrite: true });
- }
- });
- }
- if (err.data && err.data.status === "name-exists") {
- err.isHandled = true;
- this.$rootScope.appEvent("confirm-modal", {
- title: "Conflict",
- text: "Dashboard with the same name exists.",
- text2: "Would you still like to save this dashboard?",
- yesText: "Save & Overwrite",
- icon: "fa-warning",
- onConfirm: () => {
- this.save(clone, { overwrite: true });
- }
- });
- }
- if (err.data && err.data.status === "plugin-dashboard") {
- err.isHandled = true;
- this.$rootScope.appEvent("confirm-modal", {
- title: "Plugin Dashboard",
- text: err.data.message,
- text2:
- "Your changes will be lost when you update the plugin. Use Save As to create custom version.",
- yesText: "Overwrite",
- icon: "fa-warning",
- altActionText: "Save As",
- onAltAction: () => {
- this.showSaveAsModal();
- },
- onConfirm: () => {
- this.save(clone, { overwrite: true });
- }
- });
- }
- }
- postSave(clone, data) {
- this.dash.version = data.version;
- var dashboardUrl = "/dashboard/db/" + data.slug;
- if (dashboardUrl !== this.$location.path()) {
- this.$location.url(dashboardUrl);
- }
- this.$rootScope.appEvent("dashboard-saved", this.dash);
- this.$rootScope.appEvent("alert-success", ["Dashboard saved"]);
- }
- save(clone, options) {
- return this.backendSrv
- .saveDashboard(clone, options)
- .then(this.postSave.bind(this, clone))
- .catch(this.handleSaveDashboardError.bind(this, clone));
- }
- saveDashboard(options, clone) {
- if (clone) {
- this.setCurrent(this.create(clone, this.dash.meta));
- }
- if (!this.dash.meta.canSave && options.makeEditable !== true) {
- return Promise.resolve();
- }
- if (this.dash.title === "New dashboard") {
- return this.showSaveAsModal();
- }
- if (this.dash.version > 0) {
- return this.showSaveModal();
- }
- return this.save(this.dash.getSaveModelClone(), options);
- }
- showSaveAsModal() {
- this.$rootScope.appEvent("show-modal", {
- templateHtml:
- '<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',
- modalClass: "modal--narrow"
- });
- }
- showSaveModal() {
- this.$rootScope.appEvent("show-modal", {
- templateHtml:
- '<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',
- modalClass: "modal--narrow"
- });
- }
- starDashboard(dashboardId, isStarred) {
- let promise;
- if (isStarred) {
- promise = this.backendSrv
- .delete("/api/user/stars/dashboard/" + dashboardId)
- .then(() => {
- return false;
- });
- } else {
- promise = this.backendSrv
- .post("/api/user/stars/dashboard/" + dashboardId)
- .then(() => {
- return true;
- });
- }
- return promise.then(res => {
- if (this.dash && this.dash.id === dashboardId) {
- this.dash.meta.isStarred = res;
- }
- return res;
- });
- }
- }
- coreModule.service("dashboardSrv", DashboardSrv);
|