/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","app/core/config","app/core/core"],function(a,b){"use strict";var c,d,e,f,g,h,i;b&&b.id;return{setters:[function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f=[],g={name:"",type:"graphite",url:"",access:"proxy",jsonData:{},secureJsonFields:{}},h=!1,i=function(){function a(a,b,c,d,e,f,g){var h=this;this.$scope=a,this.$q=b,this.backendSrv=c,this.$routeParams=d,this.$location=e,this.datasourceSrv=f,this.navModelSrv=g,this.navModel=g.getDatasourceNav(0),this.isNew=!0,this.datasources=[],this.tabIndex=0,this.loadDatasourceTypes().then(function(){h.$routeParams.id?h.getDatasourceById(h.$routeParams.id):h.initNewDatasourceModel()})}return a.$inject=["$scope","$q","backendSrv","$routeParams","$location","datasourceSrv","navModelSrv"],a.prototype.initNewDatasourceModel=function(){this.current=c.default.cloneDeep(g),this.$location.search().gettingstarted&&(this.gettingStarted=!0,this.current.isDefault=!0),this.typeChanged()},a.prototype.loadDatasourceTypes=function(){var a=this;return f.length>0?(this.types=f,this.$q.when(null)):this.backendSrv.get("/api/plugins",{enabled:1,type:"datasource"}).then(function(b){f=b,a.types=b})},a.prototype.getDatasourceById=function(a){var b=this;this.backendSrv.get("/api/datasources/"+a).then(function(a){return b.isNew=!1,b.current=a,h&&(h=!1,b.testDatasource()),b.typeChanged()})},a.prototype.userChangedType=function(){this.current=c.default.defaults({id:this.current.id,name:this.current.name,isDefault:this.current.isDefault,type:this.current.type},c.default.cloneDeep(g)),this.typeChanged()},a.prototype.typeChanged=function(){var a=this;return this.hasDashboards=!1,this.backendSrv.get("/api/plugins/"+this.current.type+"/settings").then(function(b){a.datasourceMeta=b,a.hasDashboards=c.default.find(b.includes,{type:"dashboard"})})},a.prototype.updateFrontendSettings=function(){var a=this;return this.backendSrv.get("/api/frontend/settings").then(function(b){d.default.datasources=b.datasources,d.default.defaultDatasource=b.defaultDatasource,a.datasourceSrv.init()})},a.prototype.testDatasource=function(){var a=this;this.datasourceSrv.get(this.current.name).then(function(b){b.testDatasource&&(a.testing={done:!1,status:"error"},a.backendSrv.withNoBackendCache(function(){return b.testDatasource().then(function(b){a.testing.message=b.message,a.testing.status=b.status}).catch(function(b){b.statusText?a.testing.message="HTTP Error "+b.statusText:a.testing.message=b.message})}).finally(function(){a.testing.done=!0}))})},a.prototype.saveChanges=function(){var a=this;if(this.editForm.$valid)return this.current.id?this.backendSrv.put("/api/datasources/"+this.current.id,this.current).then(function(){a.updateFrontendSettings().then(function(){a.testDatasource()})}):this.backendSrv.post("/api/datasources",this.current).then(function(b){a.updateFrontendSettings(),h=!0,a.$location.path("datasources/edit/"+b.id)})},a.prototype.confirmDelete=function(){var a=this;this.backendSrv.delete("/api/datasources/"+this.current.id).then(function(){a.$location.path("datasources")})},a.prototype.delete=function(a){var b=this;e.appEvents.emit("confirm-modal",{title:"Delete",text:"Are you sure you want to delete this datasource?",yesText:"Delete",icon:"fa-trash",onConfirm:function(){b.confirmDelete()}})},a}(),a("DataSourceEditCtrl",i),e.coreModule.controller("DataSourceEditCtrl",i),e.coreModule.directive("datasourceHttpSettings",function(){return{scope:{current:"=",suggestUrl:"@"},templateUrl:"public/app/features/plugins/partials/ds_http_settings.html",link:{pre:function(a,b,c){a.getSuggestUrls=function(){return[a.suggestUrl]}}}}})}}});