2017-12-20 12:33:33 +01:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
|
import _ from 'lodash';
|
2017-06-08 22:19:11 +02:00
|
|
|
|
|
|
|
|
const template = `
|
|
|
|
|
<div class="dropdown">
|
2017-06-15 16:38:00 -04:00
|
|
|
<gf-form-dropdown model="ctrl.user"
|
|
|
|
|
get-options="ctrl.debouncedSearchUsers($query)"
|
|
|
|
|
css-class="gf-size-auto"
|
2017-06-21 17:14:06 -04:00
|
|
|
on-change="ctrl.onChange($option)"
|
2017-06-15 16:38:00 -04:00
|
|
|
</gf-form-dropdown>
|
|
|
|
|
</div>
|
2017-06-08 22:19:11 +02:00
|
|
|
`;
|
|
|
|
|
export class UserPickerCtrl {
|
2017-06-15 16:38:00 -04:00
|
|
|
user: any;
|
2017-06-08 22:19:11 +02:00
|
|
|
debouncedSearchUsers: any;
|
2017-06-21 17:14:06 -04:00
|
|
|
userPicked: any;
|
2017-06-08 22:19:11 +02:00
|
|
|
|
|
|
|
|
/** @ngInject */
|
2017-09-22 08:42:07 +02:00
|
|
|
constructor(private backendSrv) {
|
2017-06-21 17:14:06 -04:00
|
|
|
this.reset();
|
2017-12-19 16:06:54 +01:00
|
|
|
this.debouncedSearchUsers = _.debounce(this.searchUsers, 500, {
|
|
|
|
|
leading: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
trailing: false,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchUsers(query: string) {
|
2017-12-19 16:06:54 +01:00
|
|
|
return Promise.resolve(
|
|
|
|
|
this.backendSrv
|
2017-12-20 12:33:33 +01:00
|
|
|
.get('/api/users/search?perpage=10&page=1&query=' + query)
|
2017-12-19 16:06:54 +01:00
|
|
|
.then(result => {
|
|
|
|
|
return _.map(result.users, user => {
|
2017-12-20 12:33:33 +01:00
|
|
|
return { text: user.login + ' - ' + user.email, value: user };
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:14:06 -04:00
|
|
|
onChange(option) {
|
2017-12-19 16:06:54 +01:00
|
|
|
this.userPicked({ $user: option.value });
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:14:06 -04:00
|
|
|
reset() {
|
2017-12-20 12:33:33 +01:00
|
|
|
this.user = { text: 'Choose', value: null };
|
2017-06-14 12:21:05 -04:00
|
|
|
}
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
login: string;
|
|
|
|
|
email: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function userPicker() {
|
|
|
|
|
return {
|
2017-12-20 12:33:33 +01:00
|
|
|
restrict: 'E',
|
2017-06-08 22:19:11 +02:00
|
|
|
template: template,
|
|
|
|
|
controller: UserPickerCtrl,
|
|
|
|
|
bindToController: true,
|
2017-12-20 12:33:33 +01:00
|
|
|
controllerAs: 'ctrl',
|
2017-06-08 22:19:11 +02:00
|
|
|
scope: {
|
2017-12-20 12:33:33 +01:00
|
|
|
userPicked: '&',
|
2017-06-14 12:21:05 -04:00
|
|
|
},
|
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
2017-12-20 12:33:33 +01:00
|
|
|
scope.$on('user-picker-reset', () => {
|
2017-06-21 17:14:06 -04:00
|
|
|
ctrl.reset();
|
2017-06-14 12:21:05 -04:00
|
|
|
});
|
2017-12-20 12:33:33 +01:00
|
|
|
},
|
2017-06-08 22:19:11 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
coreModule.directive('userPicker', userPicker);
|