2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2024-02-05 19:03:29 +01:00
|
|
|
import assert from "minimalistic-assert";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2023-06-20 12:18:41 +02:00
|
|
|
import render_inline_decorated_stream_name from "../templates/inline_decorated_stream_name.hbs";
|
2021-02-28 01:02:37 +01:00
|
|
|
import render_move_topic_to_stream from "../templates/move_topic_to_stream.hbs";
|
2024-05-18 23:19:32 +02:00
|
|
|
import render_left_sidebar_stream_actions_popover from "../templates/popovers/left_sidebar/left_sidebar_stream_actions_popover.hbs";
|
2021-02-28 01:02:37 +01:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-03-22 16:09:12 +01:00
|
|
|
import * as browser_history from "./browser_history";
|
2021-01-19 15:27:49 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
2022-02-26 12:28:09 +01:00
|
|
|
import * as composebox_typeahead from "./composebox_typeahead";
|
2021-07-18 11:18:03 +02:00
|
|
|
import * as dialog_widget from "./dialog_widget";
|
2023-06-20 12:18:41 +02:00
|
|
|
import * as dropdown_widget from "./dropdown_widget";
|
2021-02-28 01:07:13 +01:00
|
|
|
import * as hash_util from "./hash_util";
|
2021-05-07 18:41:12 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:02:37 +01:00
|
|
|
import * as message_edit from "./message_edit";
|
2023-10-22 23:36:26 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2024-03-13 14:08:14 +01:00
|
|
|
import * as message_view from "./message_view";
|
2023-08-01 15:53:24 +02:00
|
|
|
import * as popover_menus from "./popover_menus";
|
|
|
|
import {left_sidebar_tippy_options} from "./popover_menus";
|
2024-03-13 14:08:14 +01:00
|
|
|
import {web_channel_default_view_values} from "./settings_config";
|
2021-05-13 18:17:57 +02:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 01:02:37 +01:00
|
|
|
import * as stream_color from "./stream_color";
|
2023-06-20 12:18:41 +02:00
|
|
|
import * as stream_data from "./stream_data";
|
2023-10-03 21:27:54 +02:00
|
|
|
import * as stream_settings_api from "./stream_settings_api";
|
2023-10-10 19:40:26 +02:00
|
|
|
import * as stream_settings_components from "./stream_settings_components";
|
2021-07-09 15:51:31 +02:00
|
|
|
import * as stream_settings_ui from "./stream_settings_ui";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-11-24 16:03:21 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2023-04-18 17:39:16 +02:00
|
|
|
import * as ui_util from "./ui_util";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as unread_ops from "./unread_ops";
|
2024-03-13 14:08:14 +01:00
|
|
|
import {user_settings} from "./user_settings";
|
2023-10-22 23:36:26 +02:00
|
|
|
import * as util from "./util";
|
2023-07-28 11:24:42 +02:00
|
|
|
// In this module, we manage stream popovers
|
|
|
|
// that pop up from the left sidebar.
|
2023-08-01 15:53:24 +02:00
|
|
|
let stream_popover_instance = null;
|
2023-06-20 12:18:41 +02:00
|
|
|
let stream_widget_value;
|
2024-05-07 09:48:07 +02:00
|
|
|
let move_topic_to_stream_topic_typeahead;
|
2017-03-05 17:28:40 +01:00
|
|
|
|
2020-07-08 22:17:16 +02:00
|
|
|
function get_popover_menu_items(sidebar_elem) {
|
|
|
|
if (!sidebar_elem) {
|
|
|
|
blueslip.error("Trying to get menu items when action popover is closed.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-08 22:17:16 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 15:53:24 +02:00
|
|
|
const $popover = $(sidebar_elem.popper);
|
|
|
|
if (!$popover) {
|
2020-07-08 22:17:16 +02:00
|
|
|
blueslip.error("Cannot find popover data for stream sidebar menu.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-08 22:17:16 +02:00
|
|
|
}
|
2023-08-01 15:53:24 +02:00
|
|
|
|
|
|
|
return $("li:not(.divider):visible > a", $popover);
|
2020-07-08 22:17:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:02:37 +01:00
|
|
|
export function stream_sidebar_menu_handle_keyboard(key) {
|
2023-08-01 15:53:24 +02:00
|
|
|
const items = get_popover_menu_items(stream_popover_instance);
|
2023-09-25 06:45:37 +02:00
|
|
|
popover_menus.popover_items_handle_keyboard(key, items);
|
2021-02-28 01:02:37 +01:00
|
|
|
}
|
2020-07-09 03:18:27 +02:00
|
|
|
|
2023-03-30 16:12:27 +02:00
|
|
|
export function elem_to_stream_id($elem) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const stream_id = Number.parseInt($elem.attr("data-stream-id"), 10);
|
2020-01-12 13:39:37 +01:00
|
|
|
|
|
|
|
if (stream_id === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("could not find stream id");
|
2020-01-12 13:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return stream_id;
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:17:00 +02:00
|
|
|
export function is_open() {
|
2023-08-01 15:53:24 +02:00
|
|
|
return Boolean(stream_popover_instance);
|
2021-02-28 01:02:37 +01:00
|
|
|
}
|
2017-03-05 17:28:40 +01:00
|
|
|
|
2021-02-28 01:02:37 +01:00
|
|
|
export function hide_stream_popover() {
|
2023-08-29 18:17:00 +02:00
|
|
|
if (is_open()) {
|
2023-11-06 13:57:29 +01:00
|
|
|
ui_util.hide_left_sidebar_menu_icon();
|
2023-08-01 15:53:24 +02:00
|
|
|
stream_popover_instance.destroy();
|
|
|
|
stream_popover_instance = null;
|
2017-03-05 17:28:40 +01:00
|
|
|
}
|
2021-02-28 01:02:37 +01:00
|
|
|
}
|
2017-03-05 17:28:40 +01:00
|
|
|
|
|
|
|
function stream_popover_sub(e) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(e.currentTarget).parents("ul");
|
|
|
|
const stream_id = elem_to_stream_id($elem);
|
2021-04-15 17:02:54 +02:00
|
|
|
const sub = sub_store.get(stream_id);
|
2017-03-05 17:28:40 +01:00
|
|
|
if (!sub) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Unknown stream", {stream_id});
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-05 17:28:40 +01:00
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:07:49 +01:00
|
|
|
function build_stream_popover(opts) {
|
2023-08-01 15:53:24 +02:00
|
|
|
const {elt, stream_id} = opts;
|
2019-02-22 17:07:49 +01:00
|
|
|
|
2023-08-01 15:53:24 +02:00
|
|
|
// This will allow the user to close the popover by clicking
|
|
|
|
// on the reference element if the popover is already open.
|
|
|
|
if (stream_popover_instance?.reference === elt) {
|
2017-03-05 17:28:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-13 14:08:14 +01:00
|
|
|
const show_go_to_channel_feed =
|
|
|
|
user_settings.web_channel_default_view !==
|
|
|
|
web_channel_default_view_values.channel_feed.code;
|
2024-05-18 21:57:39 +02:00
|
|
|
const content = render_left_sidebar_stream_actions_popover({
|
2021-04-15 17:02:54 +02:00
|
|
|
stream: sub_store.get(stream_id),
|
2024-03-13 14:08:14 +01:00
|
|
|
show_go_to_channel_feed,
|
2019-07-09 21:24:00 +02:00
|
|
|
});
|
2017-03-05 17:28:40 +01:00
|
|
|
|
2023-08-01 15:53:24 +02:00
|
|
|
popover_menus.toggle_popover_menu(elt, {
|
2023-09-19 20:34:40 +02:00
|
|
|
// Add a delay to separate `hideOnClick` and `onShow` so that
|
|
|
|
// `onShow` is called after `hideOnClick`.
|
|
|
|
// See https://github.com/atomiks/tippyjs/issues/230 for more details.
|
|
|
|
delay: [100, 0],
|
2023-08-01 15:53:24 +02:00
|
|
|
...left_sidebar_tippy_options,
|
|
|
|
onCreate(instance) {
|
|
|
|
stream_popover_instance = instance;
|
|
|
|
const $popover = $(instance.popper);
|
|
|
|
$popover.addClass("stream-popover-root");
|
|
|
|
instance.setContent(ui_util.parse_html(content));
|
|
|
|
},
|
2023-09-13 19:21:07 +02:00
|
|
|
onMount(instance) {
|
|
|
|
const $popper = $(instance.popper);
|
2023-11-06 13:57:29 +01:00
|
|
|
ui_util.show_left_sidebar_menu_icon(elt);
|
2023-09-13 19:21:07 +02:00
|
|
|
|
2024-03-13 14:08:14 +01:00
|
|
|
// Go to channel feed instead of first topic.
|
|
|
|
$popper.on("click", ".stream-popover-go-to-channel-feed", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
|
|
|
message_view.show(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
operator: "stream",
|
|
|
|
operand: sub.name,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{trigger: "stream-popover"},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-09-13 19:21:07 +02:00
|
|
|
// Stream settings
|
|
|
|
$popper.on("click", ".open_stream_settings", (e) => {
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
|
|
|
|
2024-04-30 14:46:11 +02:00
|
|
|
const stream_edit_hash = hash_util.channels_settings_edit_url(sub, "general");
|
2023-09-13 19:21:07 +02:00
|
|
|
browser_history.go_to_location(stream_edit_hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pin/unpin
|
|
|
|
$popper.on("click", ".pin_to_top", (e) => {
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
|
|
|
stream_settings_ui.toggle_pin_to_top_stream(sub);
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark all messages in stream as read
|
|
|
|
$popper.on("click", ".mark_stream_as_read", (e) => {
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
|
|
|
unread_ops.mark_stream_as_read(sub.stream_id);
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mute/unmute
|
|
|
|
$popper.on("click", ".toggle_stream_muted", (e) => {
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
2023-11-02 06:18:01 +01:00
|
|
|
stream_settings_api.set_stream_property(sub, {
|
|
|
|
property: "is_muted",
|
|
|
|
value: !sub.is_muted,
|
|
|
|
});
|
2023-09-13 19:21:07 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// New topic in stream menu
|
|
|
|
$popper.on("click", ".popover_new_topic_button", (e) => {
|
|
|
|
const sub = stream_popover_sub(e);
|
|
|
|
hide_stream_popover();
|
|
|
|
|
2024-04-04 02:38:16 +02:00
|
|
|
compose_actions.start({
|
|
|
|
message_type: "stream",
|
2023-09-13 19:21:07 +02:00
|
|
|
trigger: "popover new topic button",
|
2023-11-14 09:20:12 +01:00
|
|
|
stream_id: sub.stream_id,
|
2023-09-13 19:21:07 +02:00
|
|
|
topic: "",
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Unsubscribe
|
2024-02-10 01:39:12 +01:00
|
|
|
$popper.on("click", ".popover_sub_unsub_button", (e) => {
|
2023-09-13 19:21:07 +02:00
|
|
|
const sub = stream_popover_sub(e);
|
2024-02-08 05:44:54 +01:00
|
|
|
hide_stream_popover();
|
2023-10-10 19:40:26 +02:00
|
|
|
stream_settings_components.sub_or_unsub(sub);
|
2023-09-13 19:21:07 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Choose a different color.
|
|
|
|
$popper.on("click", ".choose_stream_color", (e) => {
|
2024-04-18 21:44:12 +02:00
|
|
|
const $popover = $(instance.popper);
|
2023-09-13 19:21:07 +02:00
|
|
|
const $colorpicker = $popover.find(".colorpicker-container").find(".colorpicker");
|
|
|
|
$(".colorpicker-container").show();
|
|
|
|
$colorpicker.spectrum("destroy");
|
|
|
|
$colorpicker.spectrum(stream_color.sidebar_popover_colorpicker_options_full);
|
|
|
|
// In theory this should clean up the old color picker,
|
|
|
|
// but this seems a bit flaky -- the new colorpicker
|
|
|
|
// doesn't fire until you click a button, but the buttons
|
|
|
|
// have been hidden. We work around this by just manually
|
|
|
|
// fixing it up here.
|
|
|
|
$colorpicker.parent().find(".sp-container").removeClass("sp-buttons-disabled");
|
2024-04-18 21:44:12 +02:00
|
|
|
$(e.currentTarget).hide();
|
2023-09-13 19:21:07 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2023-08-01 15:53:24 +02:00
|
|
|
},
|
|
|
|
onHidden() {
|
|
|
|
hide_stream_popover();
|
|
|
|
},
|
2017-03-05 17:28:40 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-22 23:36:26 +02:00
|
|
|
async function get_message_placement_from_server(
|
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
current_message_id,
|
|
|
|
) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
message_edit.is_message_oldest_or_newest(
|
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
current_message_id,
|
|
|
|
(is_oldest, is_newest) => {
|
|
|
|
if (is_oldest) {
|
|
|
|
resolve("first");
|
|
|
|
} else if (is_newest) {
|
|
|
|
resolve("last");
|
|
|
|
} else {
|
|
|
|
resolve("intermediate");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function get_message_placement_in_conversation(
|
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
current_message_id,
|
|
|
|
) {
|
2024-02-05 19:03:29 +01:00
|
|
|
assert(message_lists.current !== undefined);
|
2023-10-22 23:36:26 +02:00
|
|
|
// First we check if the placement of the message can be determined
|
|
|
|
// in the current message list. This allows us to avoid a server call
|
|
|
|
// in most cases.
|
|
|
|
|
|
|
|
if (message_lists.current.data.filter.supports_collapsing_recipients()) {
|
|
|
|
// Next we check if we are in a conversation view. If we are
|
|
|
|
// in a conversation view, we check if the message is the
|
|
|
|
// first or the last message in the current view. If not, we
|
|
|
|
// can conclude that the message is an intermediate message.
|
|
|
|
//
|
|
|
|
// It's safe to assume message_lists.current.data is non-empty, because
|
|
|
|
// current_message_id must be present in it.
|
|
|
|
if (message_lists.current.data.filter.is_conversation_view()) {
|
|
|
|
if (
|
|
|
|
message_lists.current.data.fetch_status.has_found_oldest() &&
|
|
|
|
message_lists.current.data.first().id === current_message_id
|
|
|
|
) {
|
|
|
|
return "first";
|
|
|
|
} else if (
|
|
|
|
message_lists.current.data.fetch_status.has_found_newest() &&
|
|
|
|
message_lists.current.data.last().id === current_message_id
|
|
|
|
) {
|
|
|
|
return "last";
|
|
|
|
}
|
|
|
|
return "intermediate";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are not in a conversation view, but still know
|
|
|
|
// the view contains the entire conversation, we check if
|
|
|
|
// we can find the adjacent messages in the current view
|
|
|
|
// through which we can determine if the message is an
|
|
|
|
// intermediate message or not.
|
|
|
|
const msg_list = message_lists.current.data.all_messages();
|
|
|
|
let found_newer_matching_message = false;
|
|
|
|
let found_older_matching_message = false;
|
|
|
|
const current_dict = {
|
|
|
|
stream_id: current_stream_id,
|
|
|
|
topic: topic_name,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = msg_list.length - 1; i >= 0; i -= 1) {
|
|
|
|
const message = msg_list[i];
|
|
|
|
const message_dict = {
|
|
|
|
stream_id: message.stream_id,
|
|
|
|
topic: message.topic,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (util.same_stream_and_topic(current_dict, message_dict)) {
|
|
|
|
if (message.id > current_message_id) {
|
|
|
|
found_newer_matching_message = true;
|
|
|
|
} else if (message.id < current_message_id) {
|
|
|
|
found_older_matching_message = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_newer_matching_message && found_older_matching_message) {
|
|
|
|
return "intermediate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
message_lists.current.data.fetch_status.has_found_newest() &&
|
|
|
|
!found_newer_matching_message
|
|
|
|
) {
|
|
|
|
return "last";
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
message_lists.current.data.fetch_status.has_found_oldest() &&
|
|
|
|
!found_older_matching_message
|
|
|
|
) {
|
|
|
|
return "first";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case we are unable to determine the placement of the message
|
|
|
|
// in the current message list, we make a server call to determine
|
|
|
|
// the placement.
|
|
|
|
return await get_message_placement_from_server(
|
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
current_message_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function build_move_topic_to_stream_popover(
|
2023-04-14 18:58:10 +02:00
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
only_topic_edit,
|
|
|
|
message,
|
|
|
|
) {
|
2023-05-18 15:53:21 +02:00
|
|
|
const current_stream_name = sub_store.maybe_get_stream_name(current_stream_id);
|
2020-06-03 16:44:57 +02:00
|
|
|
const args = {
|
2020-07-15 00:34:28 +02:00
|
|
|
topic_name,
|
|
|
|
current_stream_id,
|
2020-06-03 16:44:57 +02:00
|
|
|
notify_new_thread: message_edit.notify_new_thread_default,
|
|
|
|
notify_old_thread: message_edit.notify_old_thread_default,
|
2022-10-03 09:41:13 +02:00
|
|
|
from_message_actions_popover: message !== undefined,
|
2023-04-14 18:58:10 +02:00
|
|
|
only_topic_edit,
|
2020-06-03 16:44:57 +02:00
|
|
|
};
|
2020-02-19 01:38:34 +01:00
|
|
|
|
2022-12-22 11:12:31 +01:00
|
|
|
// When the modal is opened for moving the whole topic from left sidebar,
|
|
|
|
// we do not have any message object and so we disable the stream input
|
2023-03-24 15:28:02 +01:00
|
|
|
// based on the move_messages_between_streams_policy setting and topic
|
|
|
|
// input based on edit_topic_policy. In other cases, message object is
|
|
|
|
// available and thus we check the time-based permissions as well in the
|
|
|
|
// below if block to enable or disable the stream and topic input.
|
2022-12-22 11:12:31 +01:00
|
|
|
let disable_stream_input = !settings_data.user_can_move_messages_between_streams();
|
2023-03-24 15:28:02 +01:00
|
|
|
args.disable_topic_input = !settings_data.user_can_move_messages_to_another_topic();
|
2022-12-22 11:12:31 +01:00
|
|
|
|
2023-04-14 18:58:10 +02:00
|
|
|
let modal_heading;
|
|
|
|
if (only_topic_edit) {
|
|
|
|
modal_heading = $t_html({defaultMessage: "Rename topic"});
|
|
|
|
} else {
|
|
|
|
modal_heading = $t_html({defaultMessage: "Move topic"});
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:41:13 +02:00
|
|
|
if (message !== undefined) {
|
|
|
|
modal_heading = $t_html({defaultMessage: "Move messages"});
|
2022-10-05 20:03:20 +02:00
|
|
|
// We disable topic input only for modal is opened from the message actions
|
|
|
|
// popover and not when moving the whole topic from left sidebar. This is
|
|
|
|
// because topic editing permission depend on message and we do not have
|
|
|
|
// any message object when opening the modal and the first message of
|
|
|
|
// topic is fetched from the server after clicking submit.
|
|
|
|
// Though, this will be changed soon as we are going to make topic
|
|
|
|
// edit permission independent of message.
|
2023-04-17 08:50:00 +02:00
|
|
|
|
|
|
|
// We potentially got to this function by clicking a button that implied the
|
|
|
|
// user would be able to move their message. Give a little bit of buffer in
|
|
|
|
// case the button has been around for a bit, e.g. we show the
|
|
|
|
// move_message_button (hovering plus icon) as long as the user would have
|
|
|
|
// been able to click it at the time the mouse entered the message_row. Also
|
|
|
|
// a buffer in case their computer is slow, or stalled for a second, etc
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
|
|
|
// zerver.actions.message_edit.check_update_message
|
|
|
|
const move_limit_buffer = 5;
|
|
|
|
args.disable_topic_input = !message_edit.is_topic_editable(message, move_limit_buffer);
|
|
|
|
disable_stream_input = !message_edit.is_stream_editable(message, move_limit_buffer);
|
2023-10-22 23:36:26 +02:00
|
|
|
args.message_placement = await get_message_placement_in_conversation(
|
|
|
|
current_stream_id,
|
|
|
|
topic_name,
|
|
|
|
message.id,
|
|
|
|
);
|
2022-10-03 09:41:13 +02:00
|
|
|
}
|
2020-02-19 01:38:34 +01:00
|
|
|
|
2022-04-14 06:43:38 +02:00
|
|
|
function get_params_from_form() {
|
|
|
|
return Object.fromEntries(
|
2021-07-18 11:18:03 +02:00
|
|
|
$("#move_topic_form")
|
|
|
|
.serializeArray()
|
|
|
|
.map(({name, value}) => [name, value]),
|
|
|
|
);
|
2022-04-14 06:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_submit_button_disabled_state(select_stream_id) {
|
|
|
|
const {current_stream_id, new_topic_name, old_topic_name} = get_params_from_form();
|
|
|
|
|
2022-04-19 01:21:56 +02:00
|
|
|
// Unlike most topic comparisons in Zulip, we intentionally do
|
|
|
|
// a case-sensitive comparison, since adjusting the
|
|
|
|
// capitalization of a topic is a valid operation.
|
2022-10-05 20:03:20 +02:00
|
|
|
// new_topic_name can be undefined when the new topic input is
|
|
|
|
// disabled in case when user does not have permission to edit
|
|
|
|
// topic and thus submit button is disabled if stream is also
|
|
|
|
// not changed.
|
2022-04-14 06:43:38 +02:00
|
|
|
$("#move_topic_modal .dialog_submit_button")[0].disabled =
|
|
|
|
Number.parseInt(current_stream_id, 10) === Number.parseInt(select_stream_id, 10) &&
|
2022-10-05 20:03:20 +02:00
|
|
|
(new_topic_name === undefined || new_topic_name.trim() === old_topic_name.trim());
|
2022-04-14 06:43:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function move_topic() {
|
|
|
|
const params = get_params_from_form();
|
2021-07-18 11:18:03 +02:00
|
|
|
|
2024-04-19 03:57:47 +02:00
|
|
|
const old_topic_name = params.old_topic_name.trim();
|
2023-04-14 18:58:10 +02:00
|
|
|
let select_stream_id;
|
|
|
|
if (only_topic_edit) {
|
|
|
|
select_stream_id = undefined;
|
|
|
|
} else {
|
2023-06-20 12:18:41 +02:00
|
|
|
select_stream_id = stream_widget_value;
|
2023-04-14 18:58:10 +02:00
|
|
|
}
|
2021-07-18 11:18:03 +02:00
|
|
|
|
|
|
|
let {
|
|
|
|
current_stream_id,
|
|
|
|
new_topic_name,
|
|
|
|
send_notification_to_new_thread,
|
|
|
|
send_notification_to_old_thread,
|
|
|
|
} = params;
|
|
|
|
send_notification_to_new_thread = send_notification_to_new_thread === "on";
|
|
|
|
send_notification_to_old_thread = send_notification_to_old_thread === "on";
|
|
|
|
current_stream_id = Number.parseInt(current_stream_id, 10);
|
2024-04-18 08:06:58 +02:00
|
|
|
select_stream_id = Number.parseInt(select_stream_id, 10);
|
2021-07-18 11:18:03 +02:00
|
|
|
|
2022-10-05 20:03:20 +02:00
|
|
|
if (new_topic_name !== undefined) {
|
|
|
|
// new_topic_name can be undefined when the new topic input is disabled when
|
|
|
|
// user does not have permission to edit topic.
|
|
|
|
new_topic_name = new_topic_name.trim();
|
|
|
|
}
|
2024-04-19 03:57:47 +02:00
|
|
|
if (old_topic_name === new_topic_name) {
|
2022-10-03 09:41:13 +02:00
|
|
|
// We use `undefined` to tell the server that
|
|
|
|
// there has been no change in the topic name.
|
|
|
|
new_topic_name = undefined;
|
|
|
|
}
|
2022-12-16 05:38:20 +01:00
|
|
|
if (select_stream_id === current_stream_id) {
|
|
|
|
// We use `undefined` to tell the server that
|
|
|
|
// there has been no change in stream. This is
|
|
|
|
// important for cases when changing stream is
|
|
|
|
// not allowed or when changes other than
|
|
|
|
// stream-change has been made.
|
|
|
|
select_stream_id = undefined;
|
|
|
|
}
|
2022-10-03 09:41:13 +02:00
|
|
|
|
|
|
|
let propagate_mode = "change_all";
|
|
|
|
if (message !== undefined) {
|
|
|
|
// We already have the message_id here which means that modal is opened using
|
|
|
|
// message popover.
|
|
|
|
propagate_mode = $("#move_topic_modal select.message_edit_topic_propagate").val();
|
2024-04-19 03:57:47 +02:00
|
|
|
const toast_params =
|
|
|
|
propagate_mode === "change_one"
|
|
|
|
? {
|
|
|
|
new_stream_id: select_stream_id || current_stream_id,
|
|
|
|
new_topic_name: new_topic_name ?? old_topic_name,
|
|
|
|
}
|
|
|
|
: undefined;
|
2022-10-03 09:41:13 +02:00
|
|
|
message_edit.move_topic_containing_message_to_stream(
|
|
|
|
message.id,
|
|
|
|
select_stream_id,
|
|
|
|
new_topic_name,
|
|
|
|
send_notification_to_new_thread,
|
|
|
|
send_notification_to_old_thread,
|
|
|
|
propagate_mode,
|
2024-04-19 03:57:47 +02:00
|
|
|
toast_params,
|
2022-10-03 09:41:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:18:03 +02:00
|
|
|
dialog_widget.show_dialog_spinner();
|
2021-07-27 21:14:18 +02:00
|
|
|
message_edit.with_first_message_id(
|
2021-07-18 11:18:03 +02:00
|
|
|
current_stream_id,
|
|
|
|
old_topic_name,
|
|
|
|
(message_id) => {
|
2023-02-04 01:52:39 +01:00
|
|
|
message_edit.move_topic_containing_message_to_stream(
|
|
|
|
message_id,
|
|
|
|
select_stream_id,
|
|
|
|
new_topic_name,
|
|
|
|
send_notification_to_new_thread,
|
|
|
|
send_notification_to_old_thread,
|
|
|
|
propagate_mode,
|
|
|
|
);
|
2021-07-18 11:18:03 +02:00
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
dialog_widget.hide_dialog_spinner();
|
2021-11-24 16:03:21 +01:00
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Error moving topic"}),
|
|
|
|
xhr,
|
|
|
|
$("#move_topic_modal #dialog_error"),
|
|
|
|
);
|
2021-07-18 11:18:03 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-26 12:28:09 +01:00
|
|
|
function set_stream_topic_typeahead() {
|
2023-04-18 17:24:55 +02:00
|
|
|
const $topic_input = $("#move_topic_form .move_messages_edit_topic");
|
2023-06-20 12:18:41 +02:00
|
|
|
const new_stream_id = Number(stream_widget_value, 10);
|
2022-02-26 12:28:09 +01:00
|
|
|
const new_stream_name = sub_store.get(new_stream_id).name;
|
2024-05-07 09:48:07 +02:00
|
|
|
move_topic_to_stream_topic_typeahead?.unlisten();
|
|
|
|
move_topic_to_stream_topic_typeahead = composebox_typeahead.initialize_topic_edit_typeahead(
|
|
|
|
$topic_input,
|
|
|
|
new_stream_name,
|
|
|
|
false,
|
|
|
|
);
|
2022-02-26 12:28:09 +01:00
|
|
|
}
|
|
|
|
|
2023-06-20 12:18:41 +02:00
|
|
|
function render_selected_stream() {
|
2023-06-27 01:40:25 +02:00
|
|
|
const stream_id = Number.parseInt(stream_widget_value, 10);
|
|
|
|
const stream = stream_data.get_sub_by_id(stream_id);
|
2023-06-20 12:18:41 +02:00
|
|
|
if (stream === undefined) {
|
2023-07-19 16:09:53 +02:00
|
|
|
$("#move_topic_to_stream_widget .dropdown_widget_value").text(
|
2024-04-18 18:36:57 +02:00
|
|
|
$t({defaultMessage: "Select a channel"}),
|
2023-07-19 16:09:53 +02:00
|
|
|
);
|
2023-06-20 12:18:41 +02:00
|
|
|
} else {
|
2023-07-19 16:09:53 +02:00
|
|
|
$("#move_topic_to_stream_widget .dropdown_widget_value").html(
|
2023-06-20 12:18:41 +02:00
|
|
|
render_inline_decorated_stream_name({stream, show_colored_icon: true}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function move_topic_on_update(event, dropdown) {
|
|
|
|
stream_widget_value = $(event.currentTarget).attr("data-unique-id");
|
|
|
|
|
|
|
|
update_submit_button_disabled_state(stream_widget_value);
|
2022-02-26 12:28:09 +01:00
|
|
|
set_stream_topic_typeahead();
|
2023-06-20 12:18:41 +02:00
|
|
|
render_selected_stream();
|
|
|
|
|
|
|
|
dropdown.hide();
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2022-02-26 12:28:09 +01:00
|
|
|
}
|
|
|
|
|
2021-07-18 11:18:03 +02:00
|
|
|
function move_topic_post_render() {
|
2023-04-14 18:58:10 +02:00
|
|
|
$("#move_topic_modal .dialog_submit_button").prop("disabled", true);
|
|
|
|
|
2023-04-18 17:24:55 +02:00
|
|
|
const $topic_input = $("#move_topic_form .move_messages_edit_topic");
|
2024-05-07 09:48:07 +02:00
|
|
|
move_topic_to_stream_topic_typeahead = composebox_typeahead.initialize_topic_edit_typeahead(
|
2023-04-14 18:58:10 +02:00
|
|
|
$topic_input,
|
|
|
|
current_stream_name,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (only_topic_edit) {
|
|
|
|
// Set select_stream_id to current_stream_id since we user is not allowed
|
|
|
|
// to edit stream in topic-edit only UI.
|
|
|
|
const select_stream_id = current_stream_id;
|
|
|
|
$topic_input.on("input", () => {
|
|
|
|
update_submit_button_disabled_state(select_stream_id);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-20 12:18:41 +02:00
|
|
|
stream_widget_value = current_stream_id;
|
|
|
|
const streams_list_options = () =>
|
|
|
|
stream_data.get_options_for_dropdown_widget().filter((stream) => {
|
|
|
|
if (stream.stream_id === current_stream_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return stream_data.can_post_messages_in_stream(stream);
|
|
|
|
});
|
2023-07-19 16:09:53 +02:00
|
|
|
|
|
|
|
new dropdown_widget.DropdownWidget({
|
|
|
|
widget_name: "move_topic_to_stream",
|
|
|
|
get_options: streams_list_options,
|
|
|
|
item_click_callback: move_topic_on_update,
|
|
|
|
$events_container: $("#move_topic_modal"),
|
|
|
|
tippy_props: {
|
2023-06-20 12:18:41 +02:00
|
|
|
// Overlap dropdown search input with stream selection button.
|
|
|
|
placement: "bottom-start",
|
|
|
|
offset: [0, -30],
|
|
|
|
},
|
2023-07-19 16:09:53 +02:00
|
|
|
}).setup();
|
2022-04-14 06:43:38 +02:00
|
|
|
|
2023-06-20 12:18:41 +02:00
|
|
|
render_selected_stream();
|
2024-02-02 11:55:13 +01:00
|
|
|
$("#move_topic_to_stream_widget").prop("disabled", disable_stream_input);
|
2023-04-18 17:24:55 +02:00
|
|
|
$("#move_topic_modal .move_messages_edit_topic").on("input", () => {
|
2023-06-20 12:18:41 +02:00
|
|
|
update_submit_button_disabled_state(stream_widget_value);
|
|
|
|
});
|
2021-07-18 11:18:03 +02:00
|
|
|
}
|
2020-05-12 13:41:45 +02:00
|
|
|
|
2023-04-18 17:39:16 +02:00
|
|
|
function focus_on_move_modal_render() {
|
2024-02-02 11:55:13 +01:00
|
|
|
if (!args.disable_topic_input) {
|
2023-04-18 17:39:16 +02:00
|
|
|
ui_util.place_caret_at_end($(".move_messages_edit_topic")[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 11:18:03 +02:00
|
|
|
dialog_widget.launch({
|
2022-10-03 09:41:13 +02:00
|
|
|
html_heading: modal_heading,
|
2021-07-18 11:18:03 +02:00
|
|
|
html_body: render_move_topic_to_stream(args),
|
|
|
|
html_submit_button: $t_html({defaultMessage: "Confirm"}),
|
|
|
|
id: "move_topic_modal",
|
2024-07-08 21:17:24 +02:00
|
|
|
form_id: "move_topic_form",
|
2021-07-18 11:18:03 +02:00
|
|
|
on_click: move_topic,
|
|
|
|
loading_spinner: true,
|
2023-04-18 17:39:16 +02:00
|
|
|
on_shown: focus_on_move_modal_render,
|
2024-05-07 09:48:07 +02:00
|
|
|
on_hidden() {
|
|
|
|
move_topic_to_stream_topic_typeahead = undefined;
|
|
|
|
},
|
2021-07-18 11:18:03 +02:00
|
|
|
post_render: move_topic_post_render,
|
|
|
|
});
|
2020-02-19 01:38:34 +01:00
|
|
|
}
|
|
|
|
|
2023-09-29 10:23:33 +02:00
|
|
|
export function initialize() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#stream_filters").on("click", ".stream-sidebar-menu-icon", (e) => {
|
2023-08-29 19:28:31 +02:00
|
|
|
const elt = e.currentTarget;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stream_li = $(elt).parents("li");
|
|
|
|
const stream_id = elem_to_stream_id($stream_li);
|
2019-02-22 17:07:49 +01:00
|
|
|
|
|
|
|
build_stream_popover({
|
2020-07-20 22:18:43 +02:00
|
|
|
elt,
|
|
|
|
stream_id,
|
2019-02-22 17:07:49 +01:00
|
|
|
});
|
2019-02-05 14:48:39 +01:00
|
|
|
|
2023-08-01 15:53:24 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2023-09-19 20:34:40 +02:00
|
|
|
|
|
|
|
$("body").on("click", ".inbox-stream-menu", (e) => {
|
|
|
|
const elt = e.currentTarget;
|
|
|
|
const stream_id = Number.parseInt($(elt).attr("data-stream-id"), 10);
|
|
|
|
|
|
|
|
build_stream_popover({
|
|
|
|
elt,
|
|
|
|
stream_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-02-28 01:02:37 +01:00
|
|
|
}
|