2022-02-08 18:56:40 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
|
|
|
import render_new_stream_user from "../templates/stream_settings/new_stream_user.hbs";
|
|
|
|
import render_new_stream_users from "../templates/stream_settings/new_stream_users.hbs";
|
|
|
|
|
|
|
|
import * as add_subscribers_pill from "./add_subscribers_pill";
|
|
|
|
import * as ListWidget from "./list_widget";
|
2024-06-10 21:11:41 +02:00
|
|
|
import type {ListWidget as ListWidgetType} from "./list_widget";
|
2022-02-08 18:56:40 +01:00
|
|
|
import * as people from "./people";
|
2024-02-13 02:08:16 +01:00
|
|
|
import {current_user} from "./state_data";
|
2022-02-08 18:56:40 +01:00
|
|
|
import * as stream_create_subscribers_data from "./stream_create_subscribers_data";
|
2023-10-03 10:16:10 +02:00
|
|
|
import * as user_sort from "./user_sort";
|
2022-02-08 18:56:40 +01:00
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
let all_users_list_widget: ListWidgetType<number, people.User>;
|
2022-02-08 18:56:40 +01:00
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
export function get_principals(): number[] {
|
2022-02-08 18:56:40 +01:00
|
|
|
return stream_create_subscribers_data.get_principals();
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
function redraw_subscriber_list(): void {
|
2022-02-08 18:56:40 +01:00
|
|
|
all_users_list_widget.replace_list_data(stream_create_subscribers_data.sorted_user_ids());
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
function add_user_ids(user_ids: number[]): void {
|
2022-02-08 18:56:40 +01:00
|
|
|
stream_create_subscribers_data.add_user_ids(user_ids);
|
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
function add_all_users(): void {
|
2022-02-08 18:56:40 +01:00
|
|
|
const user_ids = stream_create_subscribers_data.get_all_user_ids();
|
|
|
|
add_user_ids(user_ids);
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:07:49 +02:00
|
|
|
function soft_remove_user_id(user_id: number): void {
|
|
|
|
stream_create_subscribers_data.soft_remove_user_id(user_id);
|
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
function undo_soft_remove_user_id(user_id: number): void {
|
|
|
|
stream_create_subscribers_data.undo_soft_remove_user_id(user_id);
|
2022-02-08 18:56:40 +01:00
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
2024-06-26 13:54:45 +02:00
|
|
|
function sync_user_ids(user_ids: number[]): void {
|
|
|
|
stream_create_subscribers_data.sync_user_ids(user_ids);
|
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
function build_pill_widget({$parent_container}: {$parent_container: JQuery}): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_container = $parent_container.find(".pill-container");
|
2022-02-08 18:56:40 +01:00
|
|
|
const get_potential_subscribers = stream_create_subscribers_data.get_potential_subscribers;
|
2024-06-26 13:54:45 +02:00
|
|
|
add_subscribers_pill.create_without_add_button({
|
|
|
|
$pill_container,
|
|
|
|
get_potential_subscribers,
|
|
|
|
onPillCreateAction: add_user_ids,
|
|
|
|
// It is better to sync the current set of user ids in the input
|
|
|
|
// instead of removing user_ids from the user_ids_set, otherwise
|
|
|
|
// we'll have to have more complex logic of when to remove
|
|
|
|
// a user and when not to depending upon their group, channel
|
|
|
|
// and individual pills.
|
|
|
|
onPillRemoveAction: sync_user_ids,
|
|
|
|
});
|
2022-02-08 18:56:40 +01:00
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
export function create_handlers($container: JQuery): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.on("click", ".add_all_users_to_stream", (e) => {
|
2022-02-08 18:56:40 +01:00
|
|
|
e.preventDefault();
|
|
|
|
add_all_users();
|
2022-03-16 22:19:20 +01:00
|
|
|
$(".add-user-list-filter").trigger("focus");
|
2022-02-08 18:56:40 +01:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.on("click", ".remove_potential_subscriber", (e) => {
|
2022-02-08 18:56:40 +01:00
|
|
|
e.preventDefault();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(e.target);
|
2024-06-10 21:11:41 +02:00
|
|
|
const user_id = Number.parseInt($elem.attr("data-user-id")!, 10);
|
2024-06-27 11:07:49 +02:00
|
|
|
soft_remove_user_id(user_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
$container.on("click", ".undo_soft_removed_potential_subscriber", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const $elem = $(e.target);
|
|
|
|
const user_id = Number.parseInt($elem.attr("data-user-id")!, 10);
|
|
|
|
undo_soft_remove_user_id(user_id);
|
2022-02-08 18:56:40 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
export function build_widgets(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $add_people_container = $("#people_to_add");
|
|
|
|
$add_people_container.html(render_new_stream_users({}));
|
2022-02-08 18:56:40 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $simplebar_container = $add_people_container.find(".subscriber_list_container");
|
2022-02-08 18:56:40 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
build_pill_widget({$parent_container: $add_people_container});
|
2022-02-08 18:56:40 +01:00
|
|
|
|
|
|
|
stream_create_subscribers_data.initialize_with_current_user();
|
2024-02-13 02:08:16 +01:00
|
|
|
const current_user_id = current_user.user_id;
|
2022-02-08 18:56:40 +01:00
|
|
|
|
|
|
|
all_users_list_widget = ListWidget.create($("#create_stream_subscribers"), [current_user_id], {
|
|
|
|
name: "new_stream_add_users",
|
2023-02-27 08:46:43 +01:00
|
|
|
get_item: people.get_by_user_id,
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $add_people_container,
|
2023-09-16 00:03:52 +02:00
|
|
|
modifier_html(user) {
|
2022-02-08 18:56:40 +01:00
|
|
|
const item = {
|
2021-10-26 15:43:39 +02:00
|
|
|
email: user.delivery_email,
|
2023-02-27 08:46:43 +01:00
|
|
|
user_id: user.user_id,
|
2022-02-08 18:56:40 +01:00
|
|
|
full_name: user.full_name,
|
2023-02-27 08:46:43 +01:00
|
|
|
is_current_user: user.user_id === current_user_id,
|
|
|
|
disabled: stream_create_subscribers_data.must_be_subscribed(user.user_id),
|
2024-01-08 15:39:20 +01:00
|
|
|
img_src: people.small_avatar_url_for_person(user),
|
2024-06-27 11:07:49 +02:00
|
|
|
soft_removed: stream_create_subscribers_data.user_id_in_soft_remove_list(
|
|
|
|
user.user_id,
|
|
|
|
),
|
2022-02-08 18:56:40 +01:00
|
|
|
};
|
|
|
|
return render_new_stream_user(item);
|
|
|
|
},
|
2023-02-27 08:46:43 +01:00
|
|
|
sort_fields: {
|
2023-10-03 10:16:10 +02:00
|
|
|
email: user_sort.sort_email,
|
|
|
|
id: user_sort.sort_user_id,
|
2023-05-03 07:06:19 +02:00
|
|
|
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
|
2023-02-27 08:46:43 +01:00
|
|
|
},
|
2022-02-08 18:56:40 +01:00
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $("#people_to_add .add-user-list-filter"),
|
2023-02-27 08:46:43 +01:00
|
|
|
predicate(user, search_term) {
|
2022-02-08 18:56:40 +01:00
|
|
|
return people.build_person_matcher(search_term)(user);
|
|
|
|
},
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container,
|
2023-02-27 08:46:43 +01:00
|
|
|
html_selector(user) {
|
2022-02-08 18:56:40 +01:00
|
|
|
return $(`#${CSS.escape("user_checkbox_" + user.user_id)}`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-10 21:11:41 +02:00
|
|
|
export function add_user_id_to_new_stream(user_id: number): void {
|
2022-02-08 18:56:40 +01:00
|
|
|
// This is only used by puppeteer tests.
|
|
|
|
add_user_ids([user_id]);
|
|
|
|
}
|