2017-06-08 22:19:11 +02:00
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
|
|
const template = `
|
|
|
|
|
<div class="dropdown">
|
|
|
|
|
<metric-segment segment="ctrl.userSegment"
|
|
|
|
|
get-options="ctrl.debouncedSearchUsers($query)"
|
|
|
|
|
on-change="ctrl.onChange()"></metric-segment>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
export class UserPickerCtrl {
|
|
|
|
|
userSegment: any;
|
|
|
|
|
userLogin: string;
|
|
|
|
|
userId: number;
|
|
|
|
|
debouncedSearchUsers: any;
|
|
|
|
|
|
|
|
|
|
/** @ngInject */
|
|
|
|
|
constructor(private backendSrv, private $scope, $sce, private uiSegmentSrv) {
|
|
|
|
|
this.debouncedSearchUsers = _.debounce(this.searchUsers, 500, {'leading': true, 'trailing': false});
|
2017-06-14 12:21:05 -04:00
|
|
|
this.resetUserSegment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetUserSegment() {
|
2017-06-14 12:32:43 -04:00
|
|
|
this.userId = null;
|
|
|
|
|
const userSegment = this.uiSegmentSrv.newSegment({
|
|
|
|
|
value: 'Choose User',
|
|
|
|
|
selectMode: true,
|
|
|
|
|
fake: true,
|
|
|
|
|
cssClass: 'gf-size-auto'
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-14 12:21:05 -04:00
|
|
|
if (!this.userSegment) {
|
|
|
|
|
this.userSegment = userSegment;
|
|
|
|
|
} else {
|
|
|
|
|
this.userSegment.value = userSegment.value;
|
|
|
|
|
this.userSegment.html = userSegment.html;
|
|
|
|
|
this.userSegment.value = userSegment.value;
|
|
|
|
|
}
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchUsers(query: string) {
|
|
|
|
|
return Promise.resolve(this.backendSrv.get('/api/users/search?perpage=10&page=1&query=' + query).then(result => {
|
|
|
|
|
return _.map(result.users, this.userKey.bind(this));
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onChange() {
|
|
|
|
|
this.userLogin = this.userSegment.value.split(' - ')[0];
|
|
|
|
|
|
|
|
|
|
this.backendSrv.get('/api/users/search?perpage=10&page=1&query=' + this.userLogin)
|
|
|
|
|
.then(result => {
|
|
|
|
|
if (!result) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.users.forEach(u => {
|
|
|
|
|
if (u.login === this.userLogin) {
|
|
|
|
|
this.userId = u.id;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 12:21:05 -04:00
|
|
|
userIdChanged(newVal) {
|
|
|
|
|
if (!newVal) {
|
|
|
|
|
this.resetUserSegment();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 22:19:11 +02:00
|
|
|
private userKey(user: User) {
|
|
|
|
|
return this.uiSegmentSrv.newSegment(user.login + ' - ' + user.email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
login: string;
|
|
|
|
|
email: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function userPicker() {
|
|
|
|
|
return {
|
|
|
|
|
restrict: 'E',
|
|
|
|
|
template: template,
|
|
|
|
|
controller: UserPickerCtrl,
|
|
|
|
|
bindToController: true,
|
|
|
|
|
controllerAs: 'ctrl',
|
|
|
|
|
scope: {
|
|
|
|
|
userId: '=',
|
2017-06-14 12:21:05 -04:00
|
|
|
},
|
|
|
|
|
link: function(scope, elem, attrs, ctrl) {
|
|
|
|
|
scope.$watch("ctrl.userId", (newVal, oldVal) => {
|
|
|
|
|
ctrl.userIdChanged(newVal);
|
|
|
|
|
});
|
2017-06-08 22:19:11 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coreModule.directive('userPicker', userPicker);
|