ds_list_ctrl.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. let regex = new RegExp(this.searchQuery, 'ig');
  18. this.datasources = _.filter(this.unfiltered, item => {
  19. return regex.test(item.name) || regex.test(item.type);
  20. });
  21. }
  22. removeDataSourceConfirmed(ds) {
  23. this.backendSrv
  24. .delete('/api/datasources/' + ds.id)
  25. .then(
  26. () => {
  27. this.$scope.appEvent('alert-success', ['Datasource deleted', '']);
  28. },
  29. () => {
  30. this.$scope.appEvent('alert-error', ['Unable to delete datasource', '']);
  31. }
  32. )
  33. .then(() => {
  34. this.backendSrv.get('/api/datasources').then(result => {
  35. this.datasources = result;
  36. });
  37. this.backendSrv.get('/api/frontend/settings').then(settings => {
  38. this.datasourceSrv.init(settings.datasources);
  39. });
  40. });
  41. }
  42. removeDataSource(ds) {
  43. this.$scope.appEvent('confirm-modal', {
  44. title: 'Delete',
  45. text: 'Are you sure you want to delete datasource ' + ds.name + '?',
  46. yesText: 'Delete',
  47. icon: 'fa-trash',
  48. onConfirm: () => {
  49. this.removeDataSourceConfirmed(ds);
  50. },
  51. });
  52. }
  53. }
  54. coreModule.controller('DataSourcesCtrl', DataSourcesCtrl);