user_group_picker.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. <metric-segment segment="ctrl.userGroupSegment"
  7. get-options="ctrl.debouncedSearchUserGroups($query)"
  8. on-change="ctrl.onChange()"></metric-segment>
  9. </div>
  10. `;
  11. export class UserGroupPickerCtrl {
  12. userGroupSegment: any;
  13. userGroupId: number;
  14. debouncedSearchUserGroups: any;
  15. /** @ngInject */
  16. constructor(private backendSrv, private $scope, $sce, private uiSegmentSrv) {
  17. this.userGroupSegment = this.uiSegmentSrv.newSegment({value: 'Choose User Group', selectMode: true});
  18. this.debouncedSearchUserGroups = _.debounce(this.searchUserGroups, 500, {'leading': true, 'trailing': false});
  19. }
  20. searchUserGroups(query: string) {
  21. return Promise.resolve(this.backendSrv.get('/api/user-groups/search?perpage=10&page=1&query=' + query).then(result => {
  22. return _.map(result.userGroups, ug => { return this.uiSegmentSrv.newSegment(ug.name); });
  23. }));
  24. }
  25. onChange() {
  26. this.backendSrv.get('/api/user-groups/search?perpage=10&page=1&query=' + this.userGroupSegment.value)
  27. .then(result => {
  28. if (!result) {
  29. return;
  30. }
  31. result.userGroups.forEach(ug => {
  32. if (ug.name === this.userGroupSegment.value) {
  33. this.userGroupId = ug.id;
  34. }
  35. });
  36. });
  37. }
  38. }
  39. export function userGroupPicker() {
  40. return {
  41. restrict: 'E',
  42. template: template,
  43. controller: UserGroupPickerCtrl,
  44. bindToController: true,
  45. controllerAs: 'ctrl',
  46. scope: {
  47. userGroupSegment: '=',
  48. userGroupId: '=',
  49. }
  50. };
  51. }
  52. coreModule.directive('userGroupPicker', userGroupPicker);