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";
|
|
|
|
import {page_params} from "./page_params";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as settings_data from "./settings_data";
|
|
|
|
import * as stream_create_subscribers_data from "./stream_create_subscribers_data";
|
|
|
|
|
|
|
|
let pill_widget;
|
|
|
|
let all_users_list_widget;
|
|
|
|
|
|
|
|
export function get_principals() {
|
|
|
|
return stream_create_subscribers_data.get_principals();
|
|
|
|
}
|
|
|
|
|
|
|
|
function redraw_subscriber_list() {
|
|
|
|
all_users_list_widget.replace_list_data(stream_create_subscribers_data.sorted_user_ids());
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_user_ids(user_ids) {
|
|
|
|
stream_create_subscribers_data.add_user_ids(user_ids);
|
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_all_users() {
|
|
|
|
const user_ids = stream_create_subscribers_data.get_all_user_ids();
|
|
|
|
add_user_ids(user_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_user_ids(user_ids) {
|
|
|
|
stream_create_subscribers_data.remove_user_ids(user_ids);
|
|
|
|
redraw_subscriber_list();
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function build_pill_widget({$parent_container}) {
|
|
|
|
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;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
pill_widget = add_subscribers_pill.create({$pill_container, get_potential_subscribers});
|
2022-02-08 18:56:40 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function create_handlers($container) {
|
|
|
|
$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);
|
|
|
|
const user_id = Number.parseInt($elem.attr("data-user-id"), 10);
|
2022-02-08 18:56:40 +01:00
|
|
|
remove_user_ids([user_id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
function add_users({pill_user_ids}) {
|
|
|
|
add_user_ids(pill_user_ids);
|
|
|
|
pill_widget.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_subscribers_pill.set_up_handlers({
|
|
|
|
get_pill_widget: () => pill_widget,
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $container,
|
2022-02-08 18:56:40 +01:00
|
|
|
pill_selector: ".add_subscribers_container .input",
|
|
|
|
button_selector: ".add_subscribers_container button.add-subscriber-button",
|
|
|
|
action: add_users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function build_widgets() {
|
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();
|
|
|
|
const current_user_id = page_params.user_id;
|
|
|
|
|
|
|
|
all_users_list_widget = ListWidget.create($("#create_stream_subscribers"), [current_user_id], {
|
|
|
|
name: "new_stream_add_users",
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $add_people_container,
|
2022-02-08 18:56:40 +01:00
|
|
|
modifier(user_id) {
|
|
|
|
const user = people.get_by_user_id(user_id);
|
|
|
|
const item = {
|
|
|
|
show_email: settings_data.show_email(),
|
|
|
|
email: people.get_visible_email(user),
|
|
|
|
user_id,
|
|
|
|
full_name: user.full_name,
|
|
|
|
is_current_user: user_id === current_user_id,
|
|
|
|
disabled: stream_create_subscribers_data.must_be_subscribed(user_id),
|
|
|
|
};
|
|
|
|
return render_new_stream_user(item);
|
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $("#people_to_add .add-user-list-filter"),
|
2022-02-08 18:56:40 +01:00
|
|
|
predicate(user_id, search_term) {
|
|
|
|
const user = people.get_by_user_id(user_id);
|
|
|
|
return people.build_person_matcher(search_term)(user);
|
|
|
|
},
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container,
|
2022-11-17 23:33:43 +01:00
|
|
|
html_selector(user_id) {
|
2022-02-08 18:56:40 +01:00
|
|
|
const user = people.get_by_user_id(user_id);
|
|
|
|
return $(`#${CSS.escape("user_checkbox_" + user.user_id)}`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function add_user_id_to_new_stream(user_id) {
|
|
|
|
// This is only used by puppeteer tests.
|
|
|
|
add_user_ids([user_id]);
|
|
|
|
}
|