user_group_picker.ts 1.5 KB

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