/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["angular","lodash","app/core/config","app/core/core"],function(a){var b,c,d,e,f,g,h,i;return{setters:[function(a){b=a},function(a){c=a},function(a){d=a},function(a){e=a}],execute:function(){f=[],g={name:"",type:"graphite",url:"",access:"proxy",jsonData:{}},h=!1,i=function(){function a(a,c,d,e,f,h){var i=this;this.$scope=a,this.$q=c,this.backendSrv=d,this.$routeParams=e,this.$location=f,this.datasourceSrv=h,this.isNew=!0,this.datasources=[],this.tabIndex=0,this.loadDatasourceTypes().then(function(){i.$routeParams.id?i.getDatasourceById(i.$routeParams.id):(i.current=b["default"].copy(g),i.typeChanged())})}return a.$inject=["$scope","$q","backendSrv","$routeParams","$location","datasourceSrv"],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.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"].findWhere(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.testing={done:!1},this.datasourceSrv.get(this.current.name).then(function(b){return b.testDatasource?b.testDatasource().then(function(b){a.testing.message=b.message,a.testing.status=b.status,a.testing.title=b.title})["catch"](function(b){b.statusText?(a.testing.message=b.statusText,a.testing.title="HTTP Error"):(a.testing.message=b.message,a.testing.title="Unknown error")}):void delete a.testing})["finally"](function(){a.testing&&(a.testing.done=!0)})},a.prototype.saveChanges=function(a){var b=this;if(this.editForm.$valid)return this.current.id?this.backendSrv.put("/api/datasources/"+this.current.id,this.current).then(function(){b.updateFrontendSettings().then(function(){b.testDatasource()})}):this.backendSrv.post("/api/datasources",this.current).then(function(a){b.updateFrontendSettings(),h=!0,b.$location.path("datasources/edit/"+a.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]}}}}})}}});