team_picker.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. const template = `
  4. <div class="dropdown">
  5. <gf-form-dropdown model="ctrl.group"
  6. get-options="ctrl.debouncedSearchGroups($query)"
  7. css-class="gf-size-auto"
  8. on-change="ctrl.onChange($option)"
  9. </gf-form-dropdown>
  10. </div>
  11. `;
  12. export class TeamPickerCtrl {
  13. group: any;
  14. teamPicked: any;
  15. debouncedSearchGroups: any;
  16. /** @ngInject */
  17. constructor(private backendSrv) {
  18. this.debouncedSearchGroups = _.debounce(this.searchGroups, 500, {
  19. leading: true,
  20. trailing: false,
  21. });
  22. this.reset();
  23. }
  24. reset() {
  25. this.group = { text: 'Choose', value: null };
  26. }
  27. searchGroups(query: string) {
  28. return Promise.resolve(
  29. this.backendSrv
  30. .get('/api/teams/search?perpage=10&page=1&query=' + query)
  31. .then(result => {
  32. return _.map(result.teams, ug => {
  33. return { text: ug.name, value: ug };
  34. });
  35. })
  36. );
  37. }
  38. onChange(option) {
  39. this.teamPicked({ $group: option.value });
  40. }
  41. }
  42. export function teamPicker() {
  43. return {
  44. restrict: 'E',
  45. template: template,
  46. controller: TeamPickerCtrl,
  47. bindToController: true,
  48. controllerAs: 'ctrl',
  49. scope: {
  50. teamPicked: '&',
  51. },
  52. link: function(scope, elem, attrs, ctrl) {
  53. scope.$on('team-picker-reset', () => {
  54. ctrl.reset();
  55. });
  56. },
  57. };
  58. }
  59. coreModule.directive('teamPicker', teamPicker);