2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-07-17 22:31:46 +02:00
|
|
|
import render_stream_permission_description from "../templates/stream_settings/stream_permission_description.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-12-21 19:14:42 +01:00
|
|
|
import * as hash_util from "./hash_util";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 00:55:38 +01:00
|
|
|
import * as stream_edit from "./stream_edit";
|
2021-12-15 15:07:39 +01:00
|
|
|
import * as stream_settings_containers from "./stream_settings_containers";
|
2021-07-09 15:51:31 +02:00
|
|
|
import * as stream_settings_ui from "./stream_settings_ui";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function initialize_disable_btn_hint_popover(
|
2022-01-25 11:36:19 +01:00
|
|
|
$btn_wrapper,
|
|
|
|
$popover_btn,
|
|
|
|
$disabled_btn,
|
2020-07-15 00:34:28 +02:00
|
|
|
hint_text,
|
|
|
|
) {
|
2019-05-06 14:35:57 +02:00
|
|
|
// Disabled button blocks mouse events(hover) from reaching
|
|
|
|
// to it's parent div element, so popover don't get triggered.
|
|
|
|
// Add css to prevent this.
|
2022-01-25 11:36:19 +01:00
|
|
|
$disabled_btn.css("pointer-events", "none");
|
|
|
|
$popover_btn.popover({
|
2019-05-06 14:35:57 +02:00
|
|
|
placement: "bottom",
|
2020-07-15 00:34:28 +02:00
|
|
|
content: $("<div>", {class: "sub_disable_btn_hint"}).text(hint_text).prop("outerHTML"),
|
2019-05-06 14:35:57 +02:00
|
|
|
trigger: "manual",
|
|
|
|
html: true,
|
|
|
|
animation: false,
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$btn_wrapper.on("mouseover", (e) => {
|
|
|
|
$popover_btn.popover("show");
|
2019-05-06 14:35:57 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$btn_wrapper.on("mouseout", (e) => {
|
|
|
|
$popover_btn.popover("hide");
|
2019-05-06 14:35:57 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-05-06 14:35:57 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function initialize_cant_subscribe_popover(sub) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $button_wrapper = stream_settings_containers
|
2021-12-15 15:07:39 +01:00
|
|
|
.get_edit_container(sub)
|
|
|
|
.find(".sub_unsub_button_wrapper");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $settings_button = stream_settings_ui.settings_button_for_sub(sub);
|
2021-02-10 17:10:22 +01:00
|
|
|
initialize_disable_btn_hint_popover(
|
2022-01-25 11:36:19 +01:00
|
|
|
$button_wrapper,
|
|
|
|
$settings_button,
|
|
|
|
$settings_button,
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({defaultMessage: "Only stream members can add users to a private stream"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-05-08 08:24:06 +02:00
|
|
|
|
2021-06-26 15:28:24 +02:00
|
|
|
export function update_toggler_for_sub(sub) {
|
2021-12-21 19:14:42 +01:00
|
|
|
if (!hash_util.is_editing_stream(sub.stream_id)) {
|
2021-06-26 15:28:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sub.subscribed) {
|
|
|
|
stream_edit.toggler.enable_tab("personal_settings");
|
|
|
|
stream_edit.toggler.goto(stream_edit.select_tab);
|
|
|
|
} else {
|
|
|
|
if (stream_edit.select_tab === "personal_settings") {
|
|
|
|
// Go to the general settings tab, if the user is not
|
|
|
|
// subscribed. Also preserve the previous selected tab,
|
|
|
|
// to render next time a stream row is selected.
|
|
|
|
stream_edit.toggler.goto("general_settings");
|
|
|
|
} else {
|
|
|
|
stream_edit.toggler.goto(stream_edit.select_tab);
|
|
|
|
}
|
|
|
|
stream_edit.toggler.disable_tab("personal_settings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_settings_button_for_sub(sub) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// This is for the Subscribe/Unsubscribe button in the right panel.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $settings_button = stream_settings_ui.settings_button_for_sub(sub);
|
2019-03-31 13:00:32 +02:00
|
|
|
if (sub.subscribed) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$settings_button.text($t({defaultMessage: "Unsubscribe"})).removeClass("unsubscribed");
|
2019-03-31 13:00:32 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$settings_button.text($t({defaultMessage: "Subscribe"})).addClass("unsubscribed");
|
2019-03-31 13:00:32 +02:00
|
|
|
}
|
2021-04-04 17:23:40 +02:00
|
|
|
if (stream_data.can_toggle_subscription(sub)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$settings_button.prop("disabled", false);
|
|
|
|
$settings_button.popover("destroy");
|
|
|
|
$settings_button.css("pointer-events", "");
|
2019-03-31 13:00:32 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$settings_button.attr("title", "");
|
2021-02-10 17:10:22 +01:00
|
|
|
initialize_cant_subscribe_popover(sub);
|
2022-01-25 11:36:19 +01:00
|
|
|
$settings_button.prop("disabled", true);
|
2019-03-31 13:00:32 +02:00
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-03-31 13:00:32 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_regular_sub_settings(sub) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// These are in the right panel.
|
2021-12-21 19:14:42 +01:00
|
|
|
if (!hash_util.is_editing_stream(sub.stream_id)) {
|
2019-04-02 18:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-03 23:23:32 +01:00
|
|
|
const $settings = $(`.subscription_settings[data-stream-id='${CSS.escape(sub.stream_id)}']`);
|
2019-04-02 18:37:24 +02:00
|
|
|
if (sub.subscribed) {
|
|
|
|
if ($settings.find(".email-address").val().length === 0) {
|
|
|
|
// Rerender stream email address, if not.
|
|
|
|
$settings.find(".email-address").text(sub.email_address);
|
|
|
|
$settings.find(".stream-email-box").show();
|
|
|
|
}
|
2021-07-01 11:34:18 +02:00
|
|
|
$settings.find(".personal_settings").addClass("in");
|
2019-04-02 18:37:24 +02:00
|
|
|
} else {
|
2021-07-01 11:34:18 +02:00
|
|
|
$settings.find(".personal_settings").removeClass("in");
|
2019-04-02 18:37:24 +02:00
|
|
|
// Clear email address widget
|
|
|
|
$settings.find(".email-address").html("");
|
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-04-02 18:37:24 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_change_stream_privacy_settings(sub) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// This is in the right panel.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stream_privacy_btn = $(".change-stream-privacy");
|
2019-04-02 18:43:02 +02:00
|
|
|
|
|
|
|
if (sub.can_change_stream_permissions) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$stream_privacy_btn.show();
|
2019-04-02 18:43:02 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$stream_privacy_btn.hide();
|
2019-04-02 18:43:02 +02:00
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-04-02 18:43:02 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_notification_setting_checkbox(notification_name) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// This is in the right panel (Personal settings).
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stream_row = $("#manage_streams_container .stream-row.active");
|
|
|
|
if (!$stream_row.length) {
|
2020-02-04 13:15:09 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const stream_id = $stream_row.data("stream-id");
|
2021-02-03 23:23:32 +01:00
|
|
|
$(`#${CSS.escape(notification_name)}_${CSS.escape(stream_id)}`).prop(
|
2020-07-15 00:34:28 +02:00
|
|
|
"checked",
|
|
|
|
stream_data.receives_notifications(stream_id, notification_name),
|
|
|
|
);
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2020-02-04 13:15:09 +01:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_stream_row_in_settings_tab(sub) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// This is in the left panel.
|
2019-04-01 11:04:41 +02:00
|
|
|
// This function display/hide stream row in stream settings tab,
|
|
|
|
// used to display immediate effect of add/removal subscription event.
|
|
|
|
// If user is subscribed to stream, it will show sub row under
|
|
|
|
// "Subscribed" tab, otherwise if stream is not public hide
|
|
|
|
// stream row under tab.
|
2021-07-09 15:51:31 +02:00
|
|
|
if (stream_settings_ui.is_subscribed_stream_tab_active()) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $sub_row = stream_settings_ui.row_for_stream_id(sub.stream_id);
|
2019-04-01 11:04:41 +02:00
|
|
|
if (sub.subscribed) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$sub_row.removeClass("notdisplayed");
|
2019-05-09 07:34:31 +02:00
|
|
|
} else if (sub.invite_only || page_params.is_guest) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$sub_row.addClass("notdisplayed");
|
2019-04-01 11:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-04-01 11:04:41 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_stream_subscription_type_text(sub) {
|
2021-02-17 18:19:52 +01:00
|
|
|
// This is in the right panel.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stream_settings = stream_settings_containers.get_edit_container(sub);
|
2020-06-16 13:41:56 +02:00
|
|
|
const template_data = {
|
|
|
|
...sub,
|
|
|
|
stream_post_policy_values: stream_data.stream_post_policy_values,
|
2020-06-16 13:59:02 +02:00
|
|
|
message_retention_text: stream_edit.get_retention_policy_text_for_subscription_type(sub),
|
2020-06-16 13:41:56 +02:00
|
|
|
};
|
2021-07-17 22:31:46 +02:00
|
|
|
const html = render_stream_permission_description(template_data);
|
2021-12-21 19:14:42 +01:00
|
|
|
if (hash_util.is_editing_stream(sub.stream_id)) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$stream_settings.find(".subscription-type-text").expectOne().html(html);
|
2019-05-02 19:10:10 +02:00
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|
2019-05-02 19:10:10 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
export function update_add_subscriptions_elements(sub) {
|
2021-12-21 19:14:42 +01:00
|
|
|
if (!hash_util.is_editing_stream(sub.stream_id)) {
|
2019-05-03 07:59:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-08 20:58:39 +01:00
|
|
|
// We are only concerned with the Subscribers tab for editing streams.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $add_subscribers_container = $(".edit_subscribers_for_stream .add_subscribers_container");
|
2022-02-08 20:58:39 +01:00
|
|
|
|
2021-05-04 18:47:17 +02:00
|
|
|
if (page_params.is_guest || page_params.realm_is_zephyr_mirror_realm) {
|
2019-05-03 07:59:31 +02:00
|
|
|
// For guest users, we just hide the add_subscribers feature.
|
2022-01-25 11:36:19 +01:00
|
|
|
$add_subscribers_container.hide();
|
2019-05-03 07:59:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we adjust whether the widgets are disabled based on
|
|
|
|
// whether this user is authorized to add subscribers.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $input_element = $add_subscribers_container.find(".input").expectOne();
|
|
|
|
const $button_element = $add_subscribers_container
|
2020-07-15 00:34:28 +02:00
|
|
|
.find('button[name="add_subscriber"]')
|
|
|
|
.expectOne();
|
2019-11-02 00:06:25 +01:00
|
|
|
const allow_user_to_add_subs = sub.can_add_subscribers;
|
2019-05-03 07:59:31 +02:00
|
|
|
|
|
|
|
if (allow_user_to_add_subs) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_element.prop("disabled", false);
|
|
|
|
$button_element.prop("disabled", false);
|
|
|
|
$button_element.css("pointer-events", "");
|
|
|
|
$input_element.popover("destroy");
|
2019-05-03 07:59:31 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_element.prop("disabled", true);
|
|
|
|
$button_element.prop("disabled", true);
|
2019-05-03 07:59:31 +02:00
|
|
|
|
2021-02-10 17:10:22 +01:00
|
|
|
initialize_disable_btn_hint_popover(
|
2022-01-25 11:36:19 +01:00
|
|
|
$add_subscribers_container,
|
|
|
|
$input_element,
|
|
|
|
$button_element,
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({defaultMessage: "Only stream members can add users to a private stream"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-05-03 07:59:31 +02:00
|
|
|
}
|
2021-02-10 17:10:22 +01:00
|
|
|
}
|