2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
import render_message_view_header from "../templates/message_view_header.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-02-28 01:24:04 +01:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:24:04 +01:00
|
|
|
import * as peer_data from "./peer_data";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as recent_topics from "./recent_topics";
|
2021-02-28 01:24:04 +01:00
|
|
|
import * as rendered_markdown from "./rendered_markdown";
|
|
|
|
import * as search from "./search";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-01-21 15:55:49 +01:00
|
|
|
function get_formatted_sub_count(sub_count) {
|
2020-02-03 17:01:11 +01:00
|
|
|
if (sub_count >= 1000) {
|
|
|
|
// parseInt() is used to floor the value of division to an integer
|
2020-10-07 09:17:30 +02:00
|
|
|
sub_count = Number.parseInt(sub_count / 1000, 10) + "k";
|
2017-05-13 20:54:53 +02:00
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
return sub_count;
|
|
|
|
}
|
2017-05-13 20:54:53 +02:00
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
function make_message_view_header(filter) {
|
|
|
|
const message_view_header = {};
|
2020-07-05 12:19:09 +02:00
|
|
|
if (recent_topics.is_visible()) {
|
|
|
|
return {
|
2021-04-13 06:51:54 +02:00
|
|
|
title: $t({defaultMessage: "Recent topics"}),
|
2020-07-05 12:19:09 +02:00
|
|
|
icon: "clock-o",
|
|
|
|
};
|
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
if (filter === undefined) {
|
|
|
|
return {
|
2021-04-13 06:51:54 +02:00
|
|
|
title: $t({defaultMessage: "All messages"}),
|
2020-11-10 09:17:07 +01:00
|
|
|
icon: "align-left",
|
2020-02-03 17:01:11 +01:00
|
|
|
};
|
2017-08-12 21:30:48 +02:00
|
|
|
}
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.title = filter.get_title();
|
|
|
|
message_view_header.icon = filter.get_icon();
|
2020-07-15 01:29:15 +02:00
|
|
|
if (filter.has_operator("stream") && !filter._sub) {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.sub_count = "0";
|
|
|
|
message_view_header.formatted_sub_count = "0";
|
2021-04-13 06:51:54 +02:00
|
|
|
message_view_header.rendered_narrow_description = $t({
|
|
|
|
defaultMessage: "This stream does not exist or is private.",
|
|
|
|
});
|
2020-07-08 23:44:01 +02:00
|
|
|
return message_view_header;
|
2020-06-15 21:23:52 +02:00
|
|
|
}
|
2020-06-16 19:20:22 +02:00
|
|
|
if (filter._sub) {
|
|
|
|
// We can now be certain that the narrow
|
|
|
|
// involves a stream which exists and
|
|
|
|
// the current user can access.
|
2020-06-16 19:43:26 +02:00
|
|
|
const current_stream = filter._sub;
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.rendered_narrow_description = current_stream.rendered_description;
|
2021-01-21 15:55:49 +01:00
|
|
|
const sub_count = peer_data.get_subscriber_count(current_stream.stream_id);
|
|
|
|
message_view_header.sub_count = sub_count;
|
|
|
|
message_view_header.formatted_sub_count = get_formatted_sub_count(sub_count);
|
2020-06-15 21:23:52 +02:00
|
|
|
// the "title" is passed as a variable and doesn't get translated (nor should it)
|
2021-04-13 06:51:54 +02:00
|
|
|
message_view_header.sub_count_tooltip_text = $t(
|
|
|
|
{defaultMessage: "{count} users are subscribed to #{title}"},
|
2020-07-08 23:44:01 +02:00
|
|
|
{count: message_view_header.sub_count, title: message_view_header.title},
|
|
|
|
);
|
|
|
|
message_view_header.stream_settings_link =
|
2020-07-15 00:34:28 +02:00
|
|
|
"#streams/" + current_stream.stream_id + "/" + current_stream.name;
|
2013-12-13 22:15:00 +01:00
|
|
|
}
|
2020-07-08 23:44:01 +02:00
|
|
|
return message_view_header;
|
2020-02-03 17:01:11 +01:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function colorize_message_view_header() {
|
2020-02-03 17:01:11 +01:00
|
|
|
const filter = narrow_state.filter();
|
2020-07-15 00:34:28 +02:00
|
|
|
if (filter === undefined || !filter._sub) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header .stream > .fa").css("color", filter._sub.color);
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
function append_and_display_title_area(message_view_header_data) {
|
|
|
|
const message_view_header_elem = $("#message_view_header");
|
|
|
|
message_view_header_elem.empty();
|
|
|
|
const rendered = render_message_view_header(message_view_header_data);
|
|
|
|
message_view_header_elem.append(rendered);
|
|
|
|
if (message_view_header_data.stream_settings_link) {
|
2021-02-28 01:24:04 +01:00
|
|
|
colorize_message_view_header();
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header_elem.removeClass("notdisplayed");
|
|
|
|
const content = message_view_header_elem.find("span.rendered_markdown");
|
2020-05-21 00:53:14 +02:00
|
|
|
if (content) {
|
|
|
|
// Update syntax like stream names, emojis, mentions, timestamps.
|
|
|
|
rendered_markdown.update_elements(content);
|
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2020-05-16 19:32:30 +02:00
|
|
|
function bind_title_area_handlers() {
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".search_closed").on("click", (e) => {
|
2020-06-07 04:04:51 +02:00
|
|
|
search.initiate_search();
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header span:nth-last-child(2)").on("click", (e) => {
|
2020-05-16 19:32:30 +02:00
|
|
|
if (document.getSelection().type === "Range") {
|
|
|
|
// Allow copy/paste to work normally without interference.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let links behave normally, ie, do nothing if <a>
|
|
|
|
if ($(e.target).closest("a").length === 0) {
|
|
|
|
search.initiate_search();
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-14 23:14:18 +02:00
|
|
|
// handler that makes sure that hover plays nicely
|
|
|
|
// with whether search is being opened or not.
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header .narrow_description > a")
|
2020-07-20 21:55:26 +02:00
|
|
|
.on("mouseenter", () => {
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header .search_closed").addClass("search_icon_hover_highlight");
|
2020-07-20 21:55:26 +02:00
|
|
|
})
|
|
|
|
.on("mouseleave", () => {
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header .search_closed").removeClass("search_icon_hover_highlight");
|
2020-07-20 21:55:26 +02:00
|
|
|
});
|
2020-05-16 19:32:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
function build_message_view_header(filter) {
|
|
|
|
// This makes sure we don't waste time appending
|
|
|
|
// message_view_header on a template where it's never used
|
2020-02-03 17:01:11 +01:00
|
|
|
if (filter && !filter.is_common_narrow()) {
|
2021-02-28 01:24:04 +01:00
|
|
|
open_search_bar_and_close_narrow_description();
|
2020-02-03 17:01:11 +01:00
|
|
|
} else {
|
2020-07-08 23:44:01 +02:00
|
|
|
const message_view_header_data = make_message_view_header(filter);
|
|
|
|
append_and_display_title_area(message_view_header_data);
|
2020-05-16 19:32:30 +02:00
|
|
|
bind_title_area_handlers();
|
2020-07-15 01:29:15 +02:00
|
|
|
if (page_params.search_pills_enabled && $("#search_query").is(":focus")) {
|
2021-02-28 01:24:04 +01:00
|
|
|
open_search_bar_and_close_narrow_description();
|
2020-06-08 19:23:43 +02:00
|
|
|
} else {
|
2021-02-28 01:24:04 +01:00
|
|
|
close_search_bar_and_open_narrow_description();
|
2020-06-08 19:23:43 +02:00
|
|
|
}
|
2017-08-12 22:13:58 +02:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:13:33 +02:00
|
|
|
// we rely entirely on this function to ensure
|
|
|
|
// the searchbar has the right text.
|
2021-02-28 01:24:04 +01:00
|
|
|
export function reset_searchbox_text() {
|
2020-06-11 00:22:22 +02:00
|
|
|
let search_string = narrow_state.search_string();
|
2020-06-10 23:13:33 +02:00
|
|
|
if (search_string !== "") {
|
2020-06-11 00:22:22 +02:00
|
|
|
if (!page_params.search_pills_enabled && !narrow_state.filter().is_search()) {
|
|
|
|
// saves the user a keystroke for quick searches
|
|
|
|
search_string = search_string + " ";
|
|
|
|
}
|
2020-06-10 23:13:33 +02:00
|
|
|
$("#search_query").val(search_string);
|
|
|
|
}
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2020-06-10 23:13:33 +02:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function exit_search() {
|
2020-02-03 17:01:11 +01:00
|
|
|
const filter = narrow_state.filter();
|
|
|
|
if (!filter || filter.is_common_narrow()) {
|
|
|
|
// for common narrows, we change the UI (and don't redirect)
|
2021-02-28 01:24:04 +01:00
|
|
|
close_search_bar_and_open_narrow_description();
|
2020-02-03 17:01:11 +01:00
|
|
|
} else {
|
|
|
|
// for "searching narrows", we redirect
|
2020-04-22 21:29:21 +02:00
|
|
|
window.location.href = filter.generate_redirect_url();
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
2020-10-21 02:07:48 +02:00
|
|
|
$(".app").trigger("focus");
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function initialize() {
|
|
|
|
render_title_area();
|
2020-02-03 17:01:11 +01:00
|
|
|
|
2020-05-16 19:32:30 +02:00
|
|
|
// register searchbar click handler
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#search_exit").on("click", (e) => {
|
2021-02-28 01:24:04 +01:00
|
|
|
exit_search();
|
2020-02-03 17:01:11 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function render_title_area() {
|
2020-06-22 20:29:06 +02:00
|
|
|
const filter = narrow_state.filter();
|
2020-07-08 23:44:01 +02:00
|
|
|
build_message_view_header(filter);
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2020-06-22 20:29:06 +02:00
|
|
|
|
|
|
|
// This function checks if "modified_sub" which is the stream whose values
|
|
|
|
// have been updated is the same as the stream which is currently
|
|
|
|
// narrowed (filter._sub) and rerenders if necessary
|
2021-02-28 01:24:04 +01:00
|
|
|
export function maybe_rerender_title_area_for_stream(modified_sub) {
|
2020-05-16 19:40:13 +02:00
|
|
|
const filter = narrow_state.filter();
|
2020-06-22 20:29:06 +02:00
|
|
|
if (filter && filter._sub && filter._sub.stream_id === modified_sub.stream_id) {
|
2021-02-28 01:24:04 +01:00
|
|
|
render_title_area();
|
2020-06-22 20:29:06 +02:00
|
|
|
}
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2020-05-16 19:40:13 +02:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function open_search_bar_and_close_narrow_description() {
|
|
|
|
reset_searchbox_text();
|
2020-02-03 17:01:11 +01:00
|
|
|
$(".navbar-search").addClass("expanded");
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header").addClass("hidden");
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
|
2021-02-28 01:24:04 +01:00
|
|
|
export function close_search_bar_and_open_narrow_description() {
|
2020-05-15 14:44:30 +02:00
|
|
|
const filter = narrow_state.filter();
|
|
|
|
if (!(filter && !filter.is_common_narrow())) {
|
|
|
|
$(".navbar-search").removeClass("expanded");
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header").removeClass("hidden");
|
2020-05-15 14:44:30 +02:00
|
|
|
}
|
2021-02-28 01:24:04 +01:00
|
|
|
}
|