2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2024-07-30 00:22:57 +02:00
|
|
|
import type {InputPillConfig, InputPillItem} from "./input_pill";
|
2021-02-28 21:29:50 +01:00
|
|
|
import * as input_pill from "./input_pill";
|
2023-11-02 05:15:19 +01:00
|
|
|
import type {User} from "./people";
|
2021-02-28 00:38:58 +01:00
|
|
|
import * as people from "./people";
|
2024-07-30 00:22:57 +02:00
|
|
|
import type {UserPill, UserPillWidget} from "./user_pill";
|
2021-02-28 00:38:58 +01:00
|
|
|
import * as user_pill from "./user_pill";
|
|
|
|
import * as util from "./util";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export let widget: UserPillWidget;
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-01-03 08:00:43 +01:00
|
|
|
const pill_config: InputPillConfig = {
|
2024-01-02 15:13:39 +01:00
|
|
|
exclude_inaccessible_users: true,
|
2022-02-05 19:15:30 +01:00
|
|
|
};
|
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function initialize_pill(): UserPillWidget {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = $("#private_message_recipient").parent();
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2019-10-26 00:11:05 +02:00
|
|
|
const pill = input_pill.create({
|
2022-01-25 11:36:19 +01:00
|
|
|
$container,
|
2022-02-05 19:15:30 +01:00
|
|
|
pill_config,
|
2018-03-06 15:07:55 +01:00
|
|
|
create_item_from_text: user_pill.create_item_from_email,
|
|
|
|
get_text_from_item: user_pill.get_email_from_item,
|
2024-07-30 00:22:57 +02:00
|
|
|
generate_pill_html: (item: InputPillItem<UserPill>) =>
|
|
|
|
user_pill.generate_pill_html(item, true),
|
2018-03-06 15:07:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return pill;
|
2021-02-28 00:38:58 +01:00
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-12-27 01:34:59 +01:00
|
|
|
export function initialize({
|
|
|
|
on_pill_create_or_remove,
|
|
|
|
}: {
|
|
|
|
on_pill_create_or_remove: () => void;
|
|
|
|
}): void {
|
2021-02-28 00:38:58 +01:00
|
|
|
widget = initialize_pill();
|
2020-07-29 17:32:39 +02:00
|
|
|
|
2021-02-28 00:38:58 +01:00
|
|
|
widget.onPillCreate(() => {
|
2023-06-18 08:58:48 +02:00
|
|
|
on_pill_create_or_remove();
|
2023-07-25 20:49:30 +02:00
|
|
|
$("#private_message_recipient").trigger("focus");
|
2020-07-29 17:32:39 +02:00
|
|
|
});
|
|
|
|
|
2021-02-28 00:38:58 +01:00
|
|
|
widget.onPillRemove(() => {
|
2023-06-18 08:58:48 +02:00
|
|
|
on_pill_create_or_remove();
|
2020-07-29 17:32:39 +02:00
|
|
|
});
|
2021-02-28 00:38:58 +01:00
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function clear(): void {
|
2021-02-28 00:38:58 +01:00
|
|
|
widget.clear();
|
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function set_from_typeahead(person: User): void {
|
2018-03-06 15:07:55 +01:00
|
|
|
user_pill.append_person({
|
2021-02-28 00:38:58 +01:00
|
|
|
pill_widget: widget,
|
2020-07-20 22:18:43 +02:00
|
|
|
person,
|
2018-03-06 15:07:55 +01:00
|
|
|
});
|
2021-02-28 00:38:58 +01:00
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function set_from_emails(value: string): void {
|
2018-03-06 15:07:55 +01:00
|
|
|
// value is something like "alice@example.com,bob@example.com"
|
2021-02-28 00:38:58 +01:00
|
|
|
clear();
|
|
|
|
widget.appendValue(value);
|
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function get_user_ids(): number[] {
|
2021-02-28 00:38:58 +01:00
|
|
|
return user_pill.get_user_ids(widget);
|
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function has_unconverted_data(): boolean {
|
2021-02-28 00:38:58 +01:00
|
|
|
return user_pill.has_unconverted_data(widget);
|
|
|
|
}
|
2018-09-18 01:40:27 +02:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function get_user_ids_string(): string {
|
2021-02-28 00:38:58 +01:00
|
|
|
const user_ids = get_user_ids();
|
2019-11-02 00:06:25 +01:00
|
|
|
const sorted_user_ids = util.sorted_ids(user_ids);
|
2020-07-15 01:29:15 +02:00
|
|
|
const user_ids_string = sorted_user_ids.join(",");
|
2018-08-25 17:06:59 +02:00
|
|
|
return user_ids_string;
|
2021-02-28 00:38:58 +01:00
|
|
|
}
|
2018-08-25 17:06:59 +02:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function get_emails(): string {
|
2018-03-06 15:07:55 +01:00
|
|
|
// return something like "alice@example.com,bob@example.com"
|
2021-02-28 00:38:58 +01:00
|
|
|
const user_ids = get_user_ids();
|
2020-07-02 01:45:54 +02:00
|
|
|
const emails = user_ids.map((id) => people.get_by_user_id(id).email).join(",");
|
2018-03-06 15:07:55 +01:00
|
|
|
return emails;
|
2021-02-28 00:38:58 +01:00
|
|
|
}
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2023-11-02 05:15:19 +01:00
|
|
|
export function filter_taken_users(persons: User[]): User[] {
|
2021-02-28 00:38:58 +01:00
|
|
|
return user_pill.filter_taken_users(persons, widget);
|
|
|
|
}
|