team_picker.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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, {'leading': true, 'trailing': false});
  19. this.reset();
  20. }
  21. reset() {
  22. this.group = {text: 'Choose', value: null};
  23. }
  24. searchGroups(query: string) {
  25. return Promise.resolve(this.backendSrv.get('/api/teams/search?perpage=10&page=1&query=' + query).then(result => {
  26. return _.map(result.teams, ug => {
  27. return {text: ug.name, value: ug};
  28. });
  29. }));
  30. }
  31. onChange(option) {
  32. this.teamPicked({$group: option.value});
  33. }
  34. }
  35. export function teamPicker() {
  36. return {
  37. restrict: 'E',
  38. template: template,
  39. controller: TeamPickerCtrl,
  40. bindToController: true,
  41. controllerAs: 'ctrl',
  42. scope: {
  43. teamPicked: '&',
  44. },
  45. link: function(scope, elem, attrs, ctrl) {
  46. scope.$on("team-picker-reset", () => {
  47. ctrl.reset();
  48. });
  49. }
  50. };
  51. }
  52. coreModule.directive('teamPicker', teamPicker);