ds_list_ctrl.ts 1.7 KB

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