2017-12-20 12:33:33 +01:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
|
import _ from 'lodash';
|
2017-06-09 21:57:24 +02:00
|
|
|
|
|
|
|
|
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-06-09 21:57:24 +02:00
|
|
|
`;
|
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;
|
2017-06-09 21:57:24 +02:00
|
|
|
|
|
|
|
|
/** @ngInject */
|
2017-09-22 08:42:07 +02:00
|
|
|
constructor(private backendSrv) {
|
2017-12-19 16:06:54 +01:00
|
|
|
this.debouncedSearchGroups = _.debounce(this.searchGroups, 500, {
|
|
|
|
|
leading: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
trailing: false,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2017-06-21 17:57:04 -04:00
|
|
|
this.reset();
|
2017-06-14 12:21:05 -04:00
|
|
|
}
|
|
|
|
|
|
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-09 21:57:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:57:04 -04:00
|
|
|
searchGroups(query: string) {
|
2017-12-19 16:06:54 +01:00
|
|
|
return Promise.resolve(
|
2017-12-21 08:39:31 +01:00
|
|
|
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-12-19 16:06:54 +01:00
|
|
|
);
|
2017-06-09 21:57:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:57:04 -04:00
|
|
|
onChange(option) {
|
2017-12-19 16:06:54 +01:00
|
|
|
this.teamPicked({ $group: option.value });
|
2017-06-09 21:57:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-08 18:25:45 +03:00
|
|
|
export function teamPicker() {
|
2017-06-09 21:57:24 +02:00
|
|
|
return {
|
2017-12-20 12:33:33 +01:00
|
|
|
restrict: 'E',
|
2017-06-09 21:57:24 +02:00
|
|
|
template: template,
|
2017-12-08 18:25:45 +03:00
|
|
|
controller: TeamPickerCtrl,
|
2017-06-09 21:57:24 +02:00
|
|
|
bindToController: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
controllerAs: 'ctrl',
|
2017-06-09 21:57:24 +02:00
|
|
|
scope: {
|
2017-12-20 12:33:33 +01:00
|
|
|
teamPicked: '&',
|
2017-06-14 12:21:05 -04:00
|
|
|
},
|
|
|
|
|
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-06-14 12:21:05 -04:00
|
|
|
});
|
2017-12-20 12:33:33 +01:00
|
|
|
},
|
2017-06-09 21:57:24 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.directive('teamPicker', teamPicker);
|