2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2024-05-28 16:25:40 +02:00
|
|
|
import type * as tippy from "tippy.js";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2022-04-01 19:22:41 +02:00
|
|
|
import render_add_default_streams from "../templates/settings/add_default_streams.hbs";
|
2021-04-22 12:53:53 +02:00
|
|
|
import render_admin_default_streams_list from "../templates/settings/admin_default_streams_list.hbs";
|
2022-04-01 19:22:41 +02:00
|
|
|
import render_default_stream_choice from "../templates/settings/default_stream_choice.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as channel from "./channel";
|
2022-04-01 19:22:41 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
|
|
|
import * as dropdown_widget from "./dropdown_widget";
|
2023-10-01 22:03:44 +02:00
|
|
|
import * as hash_parser from "./hash_parser";
|
2021-04-13 05:18:25 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
2023-04-25 18:01:02 +02:00
|
|
|
import * as scroll_util from "./scroll_util";
|
2022-04-01 19:22:41 +02:00
|
|
|
import * as settings_profile_fields from "./settings_profile_fields";
|
2024-02-13 02:08:16 +01:00
|
|
|
import {current_user} from "./state_data";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-02-28 00:36:14 +01:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function add_choice_row($widget: JQuery): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
if ($widget.closest(".choice-row").next().hasClass("choice-row")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
create_choice_row();
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function get_chosen_default_streams(): Set<number> {
|
2022-04-01 19:22:41 +02:00
|
|
|
// Return the set of stream id's of streams chosen in the default stream modal.
|
|
|
|
return new Set(
|
2024-05-28 08:17:13 +02:00
|
|
|
// Note: We selectively map through the dropdown elements that contain the
|
|
|
|
// `data-stream-id` attribute to avoid adding an element with undefined value
|
|
|
|
// into the returned `Set`.
|
|
|
|
$("#default-stream-choices .choice-row .dropdown_widget_value[data-stream-id]")
|
|
|
|
.map((_i, elem) => Number($(elem).attr("data-stream-id")))
|
2022-04-01 19:22:41 +02:00
|
|
|
.get(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function create_choice_row(): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
const $container = $("#default-stream-choices");
|
2024-05-10 13:43:20 +02:00
|
|
|
const value = settings_profile_fields.get_value_for_new_option($container);
|
2023-07-19 16:09:53 +02:00
|
|
|
const stream_dropdown_widget_name = `select_default_stream_${value}`;
|
2024-04-04 00:18:17 +02:00
|
|
|
const row_html = render_default_stream_choice({value, stream_dropdown_widget_name});
|
|
|
|
$container.append($(row_html));
|
2022-04-01 19:22:41 +02:00
|
|
|
|
|
|
|
// List of non-default streams that are not yet selected.
|
2024-05-28 16:25:40 +02:00
|
|
|
function get_options(): {name: string; unique_id: number}[] {
|
2022-04-01 19:22:41 +02:00
|
|
|
const chosen_default_streams = get_chosen_default_streams();
|
|
|
|
|
|
|
|
return stream_data
|
|
|
|
.get_non_default_stream_names()
|
|
|
|
.filter((e) => !chosen_default_streams.has(e.unique_id));
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function item_click_callback(event: JQuery.ClickEvent, dropdown: tippy.Instance): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
const $selected_stream = $(event.currentTarget);
|
2024-05-28 16:25:40 +02:00
|
|
|
const selected_stream_name = $selected_stream.attr("data-name")!;
|
|
|
|
const selected_stream_id = Number.parseInt($selected_stream.attr("data-unique-id")!, 10);
|
2022-04-01 19:22:41 +02:00
|
|
|
|
2023-07-19 16:09:53 +02:00
|
|
|
const $stream_dropdown_widget = $(`#${CSS.escape(stream_dropdown_widget_name)}_widget`);
|
|
|
|
const $stream_name = $stream_dropdown_widget.find(".dropdown_widget_value");
|
2022-04-01 19:22:41 +02:00
|
|
|
$stream_name.text(selected_stream_name);
|
2024-05-03 02:45:17 +02:00
|
|
|
$stream_name.attr("data-stream-id", selected_stream_id);
|
2022-04-01 19:22:41 +02:00
|
|
|
|
|
|
|
add_choice_row($stream_dropdown_widget);
|
|
|
|
dropdown.hide();
|
|
|
|
$("#add-default-stream-modal .dialog_submit_button").prop("disabled", false);
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:09:53 +02:00
|
|
|
new dropdown_widget.DropdownWidget({
|
|
|
|
widget_name: stream_dropdown_widget_name,
|
2022-04-01 19:22:41 +02:00
|
|
|
get_options,
|
|
|
|
item_click_callback,
|
2023-07-19 16:09:53 +02:00
|
|
|
$events_container: $container,
|
|
|
|
tippy_props: {
|
|
|
|
placement: "bottom-start",
|
2022-04-01 19:22:41 +02:00
|
|
|
},
|
2023-07-19 16:09:53 +02:00
|
|
|
}).setup();
|
2022-04-01 19:22:41 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-04-08 21:02:08 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function reset(): void {
|
2017-04-17 16:51:27 +02:00
|
|
|
meta.loaded = false;
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-17 16:51:27 +02:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function maybe_disable_widgets(): void {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2018-12-08 17:37:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-15 18:22:45 +02:00
|
|
|
$(".organization-box [data-name='default-channels-list']")
|
2020-07-15 00:34:28 +02:00
|
|
|
.find("input:not(.search), button, select")
|
2020-07-22 02:59:06 +02:00
|
|
|
.prop("disabled", true);
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2018-12-08 17:37:57 +01:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function build_default_stream_table(): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $table = $("#admin_default_streams_table").expectOne();
|
2017-04-20 21:49:12 +02:00
|
|
|
|
2020-03-22 13:04:39 +01:00
|
|
|
const stream_ids = stream_data.get_default_stream_ids();
|
2024-05-28 16:25:40 +02:00
|
|
|
const subs = stream_ids.map((stream_id) => sub_store.get(stream_id)!);
|
2020-03-22 13:04:39 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($table, subs, {
|
2017-04-20 21:49:12 +02:00
|
|
|
name: "default_streams_list",
|
2023-05-01 13:44:40 +02:00
|
|
|
get_item: ListWidget.default_get_item,
|
2023-09-16 00:03:52 +02:00
|
|
|
modifier_html(item) {
|
2020-04-14 14:43:27 +02:00
|
|
|
return render_admin_default_streams_list({
|
2019-07-09 21:24:00 +02:00
|
|
|
stream: item,
|
2024-02-13 02:08:16 +01:00
|
|
|
can_modify: current_user.is_admin,
|
2020-04-14 14:43:27 +02:00
|
|
|
});
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
|
|
|
filter: {
|
2024-05-28 16:25:40 +02:00
|
|
|
$element: $table.closest(".settings-section").find<HTMLInputElement>("input.search"),
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, query) {
|
2020-03-22 16:39:23 +01:00
|
|
|
return item.name.toLowerCase().includes(query.toLowerCase());
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
onupdate() {
|
2023-04-25 18:01:02 +02:00
|
|
|
scroll_util.reset_scrollbar($table);
|
2017-09-15 23:01:36 +02:00
|
|
|
},
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
2024-05-15 18:22:45 +02:00
|
|
|
$parent_container: $("#admin-default-channels-list").expectOne(),
|
2023-05-03 07:06:19 +02:00
|
|
|
init_sort: "name_alphabetic",
|
|
|
|
sort_fields: {
|
|
|
|
...ListWidget.generic_sort_functions("alphabetic", ["name"]),
|
|
|
|
},
|
2024-05-15 18:22:45 +02:00
|
|
|
$simplebar_container: $("#admin-default-channels-list .progressive-table-wrapper"),
|
2020-04-11 16:23:29 +02:00
|
|
|
});
|
2019-08-16 06:37:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_default_streams_loading_indicator"));
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function update_default_streams_table(): void {
|
2023-10-01 22:03:44 +02:00
|
|
|
if (["organization", "settings"].includes(hash_parser.get_current_hash_category())) {
|
2017-04-08 21:02:08 +02:00
|
|
|
$("#admin_default_streams_table").expectOne().find("tr.default_stream_row").remove();
|
2021-02-28 01:21:35 +01:00
|
|
|
build_default_stream_table();
|
2017-04-08 21:02:08 +02:00
|
|
|
}
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function delete_default_stream(
|
|
|
|
stream_id: number,
|
|
|
|
$default_stream_row: JQuery,
|
|
|
|
$alert_element: JQuery,
|
|
|
|
): void {
|
|
|
|
void channel.del({
|
2020-10-07 13:17:55 +02:00
|
|
|
url: "/json/default_streams?" + $.param({stream_id}),
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.generic_row_button_error(xhr, $alert_element);
|
2018-03-07 13:31:23 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2022-01-25 11:36:19 +01:00
|
|
|
$default_stream_row.remove();
|
2018-03-07 13:31:23 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2018-03-07 13:31:23 +01:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function delete_choice_row(e: JQuery.ClickEvent): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
const $row = $(e.currentTarget).parent();
|
|
|
|
$row.remove();
|
|
|
|
|
|
|
|
// Disable the submit button if no streams are selected.
|
|
|
|
$("#add-default-stream-modal .dialog_submit_button").prop(
|
|
|
|
"disabled",
|
|
|
|
$(".choice-row").length <= 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function show_add_default_streams_modal(): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
const html_body = render_add_default_streams();
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function add_default_streams(e: JQuery.ClickEvent): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Keep track of the number of successful requests. Close the modal
|
|
|
|
// only if all the requests are successful.
|
|
|
|
let successful_requests = 0;
|
|
|
|
const chosen_streams = get_chosen_default_streams();
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function make_default_stream_request(stream_id: number): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
const data = {stream_id};
|
2024-05-28 16:25:40 +02:00
|
|
|
void channel.post({
|
2022-04-01 19:22:41 +02:00
|
|
|
url: "/json/default_streams",
|
|
|
|
data,
|
|
|
|
success() {
|
|
|
|
successful_requests = successful_requests + 1;
|
|
|
|
|
|
|
|
if (successful_requests === chosen_streams.size) {
|
2023-10-10 14:32:20 +02:00
|
|
|
dialog_widget.close();
|
2022-04-01 19:22:41 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error(xhr) {
|
|
|
|
ui_report.error(
|
2024-04-18 15:20:36 +02:00
|
|
|
$t_html({defaultMessage: "Failed adding one or more channels."}),
|
2022-04-01 19:22:41 +02:00
|
|
|
xhr,
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const chosen_stream of chosen_streams) {
|
|
|
|
make_default_stream_request(chosen_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
function default_stream_post_render(): void {
|
2022-04-01 19:22:41 +02:00
|
|
|
$("#add-default-stream-modal .dialog_submit_button").prop("disabled", true);
|
|
|
|
|
|
|
|
create_choice_row();
|
|
|
|
$("#default-stream-choices").on("click", "button.delete-choice", delete_choice_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_widget.launch({
|
2024-04-18 15:20:36 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Add default channels"}),
|
2022-04-01 19:22:41 +02:00
|
|
|
html_body,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Add"}),
|
2024-05-02 16:27:00 +02:00
|
|
|
help_link: "/help/set-default-channels-for-new-users",
|
2022-04-01 19:22:41 +02:00
|
|
|
id: "add-default-stream-modal",
|
|
|
|
loading_spinner: true,
|
|
|
|
on_click: add_default_streams,
|
|
|
|
post_render: default_stream_post_render,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function set_up(): void {
|
2021-02-28 01:21:35 +01:00
|
|
|
build_page();
|
|
|
|
maybe_disable_widgets();
|
|
|
|
}
|
2018-12-08 18:16:37 +01:00
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
export function build_page(): void {
|
2017-04-08 21:02:08 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
update_default_streams_table();
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2022-04-01 19:22:41 +02:00
|
|
|
$("#show-add-default-streams-modal").on("click", (e) => {
|
2017-03-28 06:41:26 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-04-01 19:22:41 +02:00
|
|
|
|
|
|
|
show_add_default_streams_modal();
|
2017-03-28 06:41:26 +02:00
|
|
|
});
|
|
|
|
|
2024-05-28 16:25:40 +02:00
|
|
|
$("body").on(
|
|
|
|
"click",
|
|
|
|
".default_stream_row .remove-default-stream",
|
|
|
|
function (this: HTMLElement) {
|
|
|
|
const $row = $(this).closest(".default_stream_row");
|
|
|
|
const stream_id = Number.parseInt($row.attr("data-stream-id")!, 10);
|
|
|
|
delete_default_stream(stream_id, $row, $(this));
|
|
|
|
},
|
|
|
|
);
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|