/*! grafana - v4.2.0 - 2017-03-24
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["app/core/core_module","./model"],function(a,b){"use strict";var c,d,e;b&&b.id;return{setters:[function(a){c=a},function(a){d=a}],execute:function(){e=function(){function a(a,b,c){this.backendSrv=a,this.$rootScope=b,this.$location=c}return a.$inject=["backendSrv","$rootScope","$location"],a.prototype.create=function(a,b){return new d.DashboardModel(a,b)},a.prototype.setCurrent=function(a){this.dash=a},a.prototype.getCurrent=function(){return this.dash},a.prototype.saveDashboard=function(a){var b=this;if(!this.dash.meta.canSave&&a.makeEditable!==!0)return Promise.resolve();if("New dashboard"===this.dash.title)return this.saveDashboardAs();var c=this.dash.getSaveModelClone();return this.backendSrv.saveDashboard(c,a).then(function(a){b.dash.version=a.version,b.$rootScope.appEvent("dashboard-saved",b.dash);var d="/dashboard/db/"+a.slug;d!==b.$location.path()&&b.$location.url(d),b.$rootScope.appEvent("alert-success",["Dashboard saved","Saved as "+c.title])}).catch(this.handleSaveDashboardError.bind(this))},a.prototype.handleSaveDashboardError=function(a){var b=this;a.data&&"version-mismatch"===a.data.status&&(a.isHandled=!0,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:function(){b.saveDashboard({overwrite:!0})}})),a.data&&"name-exists"===a.data.status&&(a.isHandled=!0,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:function(){b.saveDashboard({overwrite:!0})}})),a.data&&"plugin-dashboard"===a.data.status&&(a.isHandled=!0,this.$rootScope.appEvent("confirm-modal",{title:"Plugin Dashboard",text:a.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:function(){b.saveDashboardAs()},onConfirm:function(){b.saveDashboard({overwrite:!0})}}))},a.prototype.saveDashboardAs=function(){var a=this.$rootScope.$new();a.clone=this.dash.getSaveModelClone(),a.clone.editable=!0,a.clone.hideControls=!1,this.$rootScope.appEvent("show-modal",{src:"public/app/features/dashboard/partials/saveDashboardAs.html",scope:a,modalClass:"modal--narrow"})},a}(),a("DashboardSrv",e),c.default.service("dashboardSrv",e)}}});