ds_list_ctrl.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import coreModule from '../../core/core_module';
  2. import _ from 'lodash';
  3. export class DataSourcesCtrl {
  4. datasources: any;
  5. unfiltered: any;
  6. navModel: any;
  7. searchQuery: string;
  8. /** @ngInject */
  9. constructor(private $scope, private backendSrv, private datasourceSrv, private navModelSrv) {
  10. this.navModel = this.navModelSrv.getNav('cfg', 'datasources', 0);
  11. backendSrv.get('/api/datasources').then(result => {
  12. this.datasources = result;
  13. this.unfiltered = result;
  14. });
  15. }
  16. onQueryUpdated() {
  17. const regex = new RegExp(this.searchQuery, 'ig');
  18. this.datasources = _.filter(this.unfiltered, item => {
  19. regex.lastIndex = 0;
  20. return regex.test(item.name) || regex.test(item.type);
  21. });
  22. }
  23. removeDataSourceConfirmed(ds) {
  24. this.backendSrv
  25. .delete('/api/datasources/' + ds.id)
  26. .then(
  27. () => {
  28. this.$scope.appEvent('alert-success', ['Datasource deleted', '']);
  29. },
  30. () => {
  31. this.$scope.appEvent('alert-error', ['Unable to delete datasource', '']);
  32. }
  33. )
  34. .then(() => {
  35. this.backendSrv.get('/api/datasources').then(result => {
  36. this.datasources = result;
  37. });
  38. this.backendSrv.get('/api/frontend/settings').then(settings => {
  39. this.datasourceSrv.init(settings.datasources);
  40. });
  41. });
  42. }
  43. removeDataSource(ds) {
  44. this.$scope.appEvent('confirm-modal', {
  45. title: 'Delete',
  46. text: 'Are you sure you want to delete datasource ' + ds.name + '?',
  47. yesText: 'Delete',
  48. icon: 'fa-trash',
  49. onConfirm: () => {
  50. this.removeDataSourceConfirmed(ds);
  51. },
  52. });
  53. }
  54. }
  55. coreModule.controller('DataSourcesCtrl', DataSourcesCtrl);