/*! grafana - v4.5.1 - 2017-09-19
 * 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.handleSaveDashboardError=function(a,b){var c=this;b.data&&"version-mismatch"===b.data.status&&(b.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(){c.save(a,{overwrite:!0})}})),b.data&&"name-exists"===b.data.status&&(b.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(){c.save(a,{overwrite:!0})}})),b.data&&"plugin-dashboard"===b.data.status&&(b.isHandled=!0,this.$rootScope.appEvent("confirm-modal",{title:"Plugin Dashboard",text:b.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(){c.showSaveAsModal()},onConfirm:function(){c.save(a,{overwrite:!0})}}))},a.prototype.postSave=function(a,b){this.dash.version=b.version;var c="/dashboard/db/"+b.slug;c!==this.$location.path()&&this.$location.url(c),this.$rootScope.appEvent("dashboard-saved",this.dash),this.$rootScope.appEvent("alert-success",["Dashboard saved"])},a.prototype.save=function(a,b){return this.backendSrv.saveDashboard(a,b).then(this.postSave.bind(this,a)).catch(this.handleSaveDashboardError.bind(this,a))},a.prototype.saveDashboard=function(a,b){return b&&this.setCurrent(this.create(b,this.dash.meta)),this.dash.meta.canSave||a.makeEditable===!0?"New dashboard"===this.dash.title?this.showSaveAsModal():this.dash.version>0?this.showSaveModal():this.save(this.dash.getSaveModelClone(),a):Promise.resolve()},a.prototype.showSaveAsModal=function(){var a=this.$rootScope.$new();a.clone=this.dash.getSaveModelClone(),a.clone.editable=!0,a.clone.hideControls=!1,this.$rootScope.appEvent("show-modal",{templateHtml:'<save-dashboard-as-modal dismiss="dismiss()"></save-dashboard-as-modal>',scope:a,modalClass:"modal--narrow"})},a.prototype.showSaveModal=function(){this.$rootScope.appEvent("show-modal",{templateHtml:'<save-dashboard-modal dismiss="dismiss()"></save-dashboard-modal>',scope:this.$rootScope.$new(),modalClass:"modal--narrow"})},a}(),a("DashboardSrv",e),c.default.service("dashboardSrv",e)}}});