2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
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
|
|
|
|
2022-04-01 19:22:41 +02:00
|
|
|
function add_choice_row($widget) {
|
|
|
|
if ($widget.closest(".choice-row").next().hasClass("choice-row")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
create_choice_row();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_chosen_default_streams() {
|
|
|
|
// Return the set of stream id's of streams chosen in the default stream modal.
|
|
|
|
return new Set(
|
2023-07-19 16:09:53 +02:00
|
|
|
$("#default-stream-choices .choice-row .dropdown_widget_value")
|
2022-04-01 19:22:41 +02:00
|
|
|
.map((_i, elem) => $(elem).data("stream-id")?.toString())
|
|
|
|
.get(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_choice_row() {
|
|
|
|
const $container = $("#default-stream-choices");
|
|
|
|
const value = settings_profile_fields.get_value_for_new_option("#default-stream-choices");
|
2023-07-19 16:09:53 +02:00
|
|
|
const stream_dropdown_widget_name = `select_default_stream_${value}`;
|
2022-04-01 19:22:41 +02:00
|
|
|
const row = render_default_stream_choice({value, stream_dropdown_widget_name});
|
|
|
|
$container.append(row);
|
|
|
|
|
|
|
|
// List of non-default streams that are not yet selected.
|
|
|
|
function get_options() {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
function item_click_callback(event, dropdown) {
|
|
|
|
const $selected_stream = $(event.currentTarget);
|
|
|
|
const selected_stream_name = $selected_stream.attr("data-name");
|
|
|
|
const selected_stream_id = Number.parseInt($selected_stream.data("unique-id"), 10);
|
|
|
|
|
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);
|
|
|
|
$stream_name.data("stream-id", selected_stream_id);
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function reset() {
|
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
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function maybe_disable_widgets() {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (current_user.is_admin) {
|
2018-12-08 17:37:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".organization-box [data-name='default-streams-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
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function build_default_stream_table() {
|
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();
|
2021-04-15 17:02:54 +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: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $table.closest(".settings-section").find(".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
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $("#admin-default-streams-list").expectOne(),
|
2023-05-03 07:06:19 +02:00
|
|
|
init_sort: "name_alphabetic",
|
|
|
|
sort_fields: {
|
|
|
|
...ListWidget.generic_sort_functions("alphabetic", ["name"]),
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $("#admin-default-streams-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
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function update_default_streams_table() {
|
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
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function delete_default_stream(stream_id, $default_stream_row, $alert_element) {
|
2018-03-07 13:31:23 +01:00
|
|
|
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
|
|
|
|
2022-04-01 19:22:41 +02:00
|
|
|
function delete_choice_row(e) {
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_add_default_streams_modal() {
|
|
|
|
const html_body = render_add_default_streams();
|
|
|
|
|
|
|
|
function add_default_streams(e) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
function make_default_stream_request(stream_id) {
|
|
|
|
const data = {stream_id};
|
|
|
|
channel.post({
|
|
|
|
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(
|
|
|
|
$t_html({defaultMessage: "Failed adding one or more streams."}),
|
|
|
|
xhr,
|
|
|
|
$("#dialog_error"),
|
|
|
|
);
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const chosen_stream of chosen_streams) {
|
|
|
|
make_default_stream_request(chosen_stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function default_stream_post_render() {
|
|
|
|
$("#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({
|
|
|
|
html_heading: $t_html({defaultMessage: "Add default streams"}),
|
|
|
|
html_body,
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Add"}),
|
|
|
|
help_link: "/help/set-default-streams-for-new-users",
|
|
|
|
id: "add-default-stream-modal",
|
|
|
|
loading_spinner: true,
|
|
|
|
on_click: add_default_streams,
|
|
|
|
post_render: default_stream_post_render,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function set_up() {
|
|
|
|
build_page();
|
|
|
|
maybe_disable_widgets();
|
|
|
|
}
|
2018-12-08 18:16:37 +01:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function build_page() {
|
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
|
|
|
});
|
|
|
|
|
2018-03-25 19:57:38 +02:00
|
|
|
$("body").on("click", ".default_stream_row .remove-default-stream", function (e) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(this).closest(".default_stream_row");
|
|
|
|
const stream_id = Number.parseInt($row.attr("data-stream-id"), 10);
|
|
|
|
delete_default_stream(stream_id, $row, $(e.target));
|
2018-03-25 19:57:38 +02:00
|
|
|
});
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|