(function () {
var DataSourcesCtrl = function ($scope, $location, growl, Events, DataSource) {
Events.record(currentUser, "view", "page", "admin/data_sources");
$scope.$parent.pageTitle = "Data Sources";
$scope.dataSources = DataSource.query();
};
var DataSourceCtrl = function ($scope, $routeParams, $http, $location, growl, Events, DataSource) {
Events.record(currentUser, "view", "page", "admin/data_source");
$scope.$parent.pageTitle = "Data Sources";
$scope.dataSourceId = $routeParams.dataSourceId;
if ($scope.dataSourceId == "new") {
$scope.dataSource = new DataSource({options: {}});
} else {
$scope.dataSource = DataSource.get({id: $routeParams.dataSourceId});
}
$scope.$watch('dataSource.id', function(id) {
if (id != $scope.dataSourceId && id !== undefined) {
$location.path('/data_sources/' + id).replace();
}
});
function deleteDataSource() {
Events.record(currentUser, "delete", "datasource", $scope.dataSource.id);
$scope.dataSource.$delete(function (resource) {
growl.addSuccessMessage("Data source deleted successfully.");
$location.path('/data_sources/');
}.bind(this), function (httpResponse) {
console.log("Failed to delete data source: ", httpResponse.status, httpResponse.statusText, httpResponse.data);
growl.addErrorMessage("Failed to delete data source.");
});
}
function testConnection (callback) {
Events.record(currentUser, "test", "datasource", $scope.dataSource.id);
DataSource.test({id: $scope.dataSource.id}, function (httpResponse) {
if (httpResponse.ok) {
growl.addSuccessMessage(' Success.', {enableHtml: true, ttl: 3000});
} else {
growl.addErrorMessage(' Connection Test Failed:
' + httpResponse.message, {enableHtml: true, ttl: -1});
}
callback();
}, function (httpResponse) {
console.log("Failed to test data source: ", httpResponse.status, httpResponse.statusText, httpResponse);
growl.addErrorMessage(' Unknown error occurred while performing connection test. Please try again later.', {enableHtml: true, ttl: -1});
callback();
});
}
$scope.actions = [
{name: 'Delete', class: 'btn-danger', callback: deleteDataSource},
{name: 'Test Connection', class: 'btn-default', callback: testConnection, disableWhenDirty: true}
]
};
angular.module('redash.controllers')
.controller('DataSourcesCtrl', ['$scope', '$location', 'growl', 'Events', 'DataSource', DataSourcesCtrl])
.controller('DataSourceCtrl', ['$scope', '$routeParams', '$http', '$location', 'growl', 'Events', 'DataSource', DataSourceCtrl])
})();