2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
const render_message_view_header = require("../templates/message_view_header.hbs");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const rendered_markdown = require("./rendered_markdown");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
function get_sub_count(current_stream) {
|
|
|
|
const sub_count = current_stream.subscriber_count;
|
|
|
|
return sub_count;
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
function get_formatted_sub_count(current_stream) {
|
|
|
|
let sub_count = get_sub_count(current_stream);
|
|
|
|
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-02-03 17:01:11 +01:00
|
|
|
if (filter === undefined) {
|
|
|
|
return {
|
2020-07-15 01:29:15 +02:00
|
|
|
title: i18n.t("All messages"),
|
|
|
|
icon: "home",
|
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";
|
|
|
|
message_view_header.rendered_narrow_description = i18n.t(
|
|
|
|
"This stream does not exist or is private.",
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
message_view_header.sub_count = get_sub_count(current_stream);
|
|
|
|
message_view_header.formatted_sub_count = get_formatted_sub_count(current_stream);
|
2020-06-15 21:23:52 +02:00
|
|
|
// the "title" is passed as a variable and doesn't get translated (nor should it)
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.sub_count_tooltip_text = i18n.t(
|
|
|
|
"__count__ users are subscribed to #__title__",
|
|
|
|
{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
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
exports.colorize_message_view_header = function () {
|
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);
|
2020-02-03 17:01:11 +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) {
|
|
|
|
exports.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()) {
|
|
|
|
exports.open_search_bar_and_close_narrow_description();
|
|
|
|
} 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")) {
|
2020-06-08 19:23:43 +02:00
|
|
|
exports.open_search_bar_and_close_narrow_description();
|
|
|
|
} else {
|
|
|
|
exports.close_search_bar_and_open_narrow_description();
|
|
|
|
}
|
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.
|
|
|
|
exports.reset_searchbox_text = function () {
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
exports.exit_search = function () {
|
|
|
|
const filter = narrow_state.filter();
|
|
|
|
if (!filter || filter.is_common_narrow()) {
|
|
|
|
// for common narrows, we change the UI (and don't redirect)
|
|
|
|
exports.close_search_bar_and_open_narrow_description();
|
|
|
|
} else {
|
|
|
|
// for "searching narrows", we redirect
|
|
|
|
window.location.replace(filter.generate_redirect_url());
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
2013-07-08 23:33:47 +02:00
|
|
|
};
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2020-05-16 19:40:13 +02:00
|
|
|
exports.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) => {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.exit_search();
|
2020-02-03 17:01:11 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-05-16 19:40:13 +02:00
|
|
|
exports.render_title_area = function () {
|
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);
|
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
|
|
|
|
exports.maybe_rerender_title_area_for_stream = function (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) {
|
2020-07-08 23:44:01 +02:00
|
|
|
message_view_header.render_title_area();
|
2020-06-22 20:29:06 +02:00
|
|
|
}
|
2020-05-16 19:40:13 +02:00
|
|
|
};
|
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
exports.open_search_bar_and_close_narrow_description = function () {
|
2020-06-10 23:13:33 +02:00
|
|
|
exports.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");
|
2020-02-03 17:01:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.close_search_bar_and_open_narrow_description = function () {
|
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
|
|
|
}
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
window.message_view_header = exports;
|