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

74 lines
1.6 KiB
TypeScript
Raw Normal View History

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();
this.debouncedSearchUsers = _.debounce(this.searchUsers, 500, {
leading: true,
trailing: false
});
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, user => {
return { text: user.login + " - " + user.email, value: user };
});
})
);
2017-06-08 22:19:11 +02:00
}
2017-06-21 17:14:06 -04:00
onChange(option) {
this.userPicked({ $user: option.value });
2017-06-08 22:19:11 +02:00
}
2017-06-21 17:14:06 -04:00
reset() {
this.user = { text: "Choose", value: null };
}
2017-06-08 22:19:11 +02:00
}
export interface User {
id: number;
name: string;
login: string;
email: string;
}
export function userPicker() {
return {
restrict: "E",
2017-06-08 22:19:11 +02:00
template: template,
controller: UserPickerCtrl,
bindToController: true,
controllerAs: "ctrl",
2017-06-08 22:19:11 +02:00
scope: {
userPicked: "&"
},
link: function(scope, elem, attrs, ctrl) {
2017-06-21 17:14:06 -04:00
scope.$on("user-picker-reset", () => {
ctrl.reset();
});
2017-06-08 22:19:11 +02:00
}
};
}
coreModule.directive("userPicker", userPicker);