Files
grafana/public/app/core/components/team_picker.ts

65 lines
1.4 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import coreModule from 'app/core/core_module';
import _ from 'lodash';
const template = `
<div class="dropdown">
2017-06-21 17:57:04 -04:00
<gf-form-dropdown model="ctrl.group"
get-options="ctrl.debouncedSearchGroups($query)"
css-class="gf-size-auto"
on-change="ctrl.onChange($option)"
</gf-form-dropdown>
</div>
`;
2017-12-08 18:25:45 +03:00
export class TeamPickerCtrl {
2017-06-21 17:57:04 -04:00
group: any;
2017-12-08 18:25:45 +03:00
teamPicked: any;
2017-06-21 17:57:04 -04:00
debouncedSearchGroups: any;
/** @ngInject */
2017-09-22 08:42:07 +02:00
constructor(private backendSrv) {
this.debouncedSearchGroups = _.debounce(this.searchGroups, 500, {
leading: true,
2017-12-20 12:33:33 +01:00
trailing: false,
});
2017-06-21 17:57:04 -04:00
this.reset();
}
2017-06-21 17:57:04 -04:00
reset() {
2017-12-20 12:33:33 +01:00
this.group = { text: 'Choose', value: null };
}
2017-06-21 17:57:04 -04:00
searchGroups(query: string) {
return Promise.resolve(
this.backendSrv.get('/api/teams/search?perpage=10&page=1&query=' + query).then(result => {
return _.map(result.teams, ug => {
return { text: ug.name, value: ug };
});
})
);
}
2017-06-21 17:57:04 -04:00
onChange(option) {
this.teamPicked({ $group: option.value });
}
}
2017-12-08 18:25:45 +03:00
export function teamPicker() {
return {
2017-12-20 12:33:33 +01:00
restrict: 'E',
template: template,
2017-12-08 18:25:45 +03:00
controller: TeamPickerCtrl,
bindToController: true,
2017-12-20 12:33:33 +01:00
controllerAs: 'ctrl',
scope: {
2017-12-20 12:33:33 +01:00
teamPicked: '&',
},
link: function(scope, elem, attrs, ctrl) {
2017-12-20 12:33:33 +01:00
scope.$on('team-picker-reset', () => {
2017-06-21 17:57:04 -04:00
ctrl.reset();
});
2017-12-20 12:33:33 +01:00
},
};
}
2017-12-20 12:33:33 +01:00
coreModule.directive('teamPicker', teamPicker);