team_picker.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.get('/api/teams/search?perpage=10&page=1&query=' + query).then(result => {
  30. return _.map(result.teams, ug => {
  31. return { text: ug.name, value: ug };
  32. });
  33. })
  34. );
  35. }
  36. onChange(option) {
  37. this.teamPicked({ $group: option.value });
  38. }
  39. }
  40. export function teamPicker() {
  41. return {
  42. restrict: 'E',
  43. template: template,
  44. controller: TeamPickerCtrl,
  45. bindToController: true,
  46. controllerAs: 'ctrl',
  47. scope: {
  48. teamPicked: '&',
  49. },
  50. link: function(scope, elem, attrs, ctrl) {
  51. scope.$on('team-picker-reset', () => {
  52. ctrl.reset();
  53. });
  54. },
  55. };
  56. }
  57. coreModule.directive('teamPicker', teamPicker);