2020-07-15 01:29:15 +02:00
|
|
|
const render_tab_bar = require("../templates/tab_bar.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-07-15 01:29:15 +02:00
|
|
|
sub_count = 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-02-03 17:01:11 +01:00
|
|
|
function make_tab_data(filter) {
|
|
|
|
const tab_data = {};
|
|
|
|
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-02-03 17:01:11 +01:00
|
|
|
tab_data.title = filter.get_title();
|
|
|
|
tab_data.icon = filter.get_icon();
|
2020-07-15 01:29:15 +02:00
|
|
|
if (filter.has_operator("stream") && !filter._sub) {
|
|
|
|
tab_data.sub_count = "0";
|
|
|
|
tab_data.formatted_sub_count = "0";
|
2020-06-15 21:23:52 +02:00
|
|
|
tab_data.rendered_narrow_description = i18n.t("This stream does not exist or is private.");
|
|
|
|
return tab_data;
|
|
|
|
}
|
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-06-15 21:23:52 +02:00
|
|
|
tab_data.rendered_narrow_description = current_stream.rendered_description;
|
|
|
|
tab_data.sub_count = get_sub_count(current_stream);
|
|
|
|
tab_data.formatted_sub_count = get_formatted_sub_count(current_stream);
|
|
|
|
// the "title" is passed as a variable and doesn't get translated (nor should it)
|
2020-07-15 00:34:28 +02:00
|
|
|
tab_data.sub_count_tooltip_text = i18n.t("__count__ users are subscribed to #__title__", {
|
|
|
|
count: tab_data.sub_count,
|
|
|
|
title: tab_data.title,
|
|
|
|
});
|
|
|
|
tab_data.stream_settings_link =
|
|
|
|
"#streams/" + current_stream.stream_id + "/" + current_stream.name;
|
2013-12-13 22:15:00 +01:00
|
|
|
}
|
2020-02-03 17:01:11 +01:00
|
|
|
return tab_data;
|
|
|
|
}
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
exports.colorize_tab_bar = function () {
|
|
|
|
const filter = narrow_state.filter();
|
2020-07-15 00:34:28 +02:00
|
|
|
if (filter === undefined || !filter._sub) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#tab_bar .stream > .fa").css("color", filter._sub.color);
|
2020-02-03 17:01:11 +01:00
|
|
|
};
|
2013-05-09 21:12:53 +02:00
|
|
|
|
2020-05-16 19:22:47 +02:00
|
|
|
function append_and_display_title_area(tab_bar_data) {
|
2020-06-11 14:57:42 +02:00
|
|
|
const tab_bar_elem = $("#tab_bar");
|
|
|
|
tab_bar_elem.empty();
|
2020-02-03 17:01:11 +01:00
|
|
|
const rendered = render_tab_bar(tab_bar_data);
|
2020-06-11 14:57:42 +02:00
|
|
|
tab_bar_elem.append(rendered);
|
2020-02-03 17:01:11 +01:00
|
|
|
if (tab_bar_data.stream_settings_link) {
|
|
|
|
exports.colorize_tab_bar();
|
2013-05-09 21:12:53 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
tab_bar_elem.removeClass("notdisplayed");
|
|
|
|
const content = tab_bar_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-06-28 03:06:22 +02:00
|
|
|
$("#tab_bar 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-20 21:55:26 +02:00
|
|
|
$("#tab_bar .narrow_description > a")
|
|
|
|
.on("mouseenter", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#tab_bar .search_closed").addClass("search_icon_hover_highlight");
|
2020-07-20 21:55:26 +02:00
|
|
|
})
|
|
|
|
.on("mouseleave", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#tab_bar .search_closed").removeClass("search_icon_hover_highlight");
|
2020-07-20 21:55:26 +02:00
|
|
|
});
|
2020-05-16 19:32:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 17:01:11 +01:00
|
|
|
function build_tab_bar(filter) {
|
|
|
|
// This makes sure we don't waste time appending tab_bar on a template where it's never used
|
|
|
|
if (filter && !filter.is_common_narrow()) {
|
|
|
|
exports.open_search_bar_and_close_narrow_description();
|
|
|
|
} else {
|
|
|
|
const tab_bar_data = make_tab_data(filter);
|
2020-05-16 19:22:47 +02:00
|
|
|
append_and_display_title_area(tab_bar_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-02-03 17:01:11 +01:00
|
|
|
tab_bar.exit_search();
|
|
|
|
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();
|
|
|
|
build_tab_bar(filter);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
tab_bar.render_title_area();
|
|
|
|
}
|
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-06-28 03:06:22 +02:00
|
|
|
$("#tab_bar").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-06-28 03:06:22 +02:00
|
|
|
$("#tab_bar").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
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.tab_bar = exports;
|