2019-11-02 00:06:25 +01:00
|
|
|
const render_tab_bar = require('../templates/tab_bar.hbs');
|
2020-05-21 00:53:14 +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-06-02 23:07:40 +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-06-02 23:07:40 +02:00
|
|
|
title: i18n.t('All messages'),
|
2020-02-03 17:01:11 +01:00
|
|
|
icon: 'home',
|
|
|
|
};
|
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-06-16 19:20:22 +02:00
|
|
|
if (filter.has_operator('stream') && !filter._sub) {
|
2020-06-15 21:23:52 +02:00
|
|
|
tab_data.sub_count = '0';
|
|
|
|
tab_data.formatted_sub_count = '0';
|
|
|
|
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)
|
|
|
|
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-06-16 00:27:26 +02:00
|
|
|
if (filter === undefined || !filter._sub) {return;}
|
|
|
|
$("#tab_list .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-06-11 14:57:42 +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-07-02 01:45:54 +02:00
|
|
|
$("#tab_list 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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const color = $(".search_closed").css("color");
|
|
|
|
const night_mode_color = $(".nightmode .closed_icon").css("color");
|
|
|
|
|
|
|
|
// make sure that hover plays nicely with whether search is being
|
|
|
|
// opened or not.
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".narrow_description > a").hover(() => {
|
2020-05-16 19:32:30 +02:00
|
|
|
if (night_mode_color) {
|
|
|
|
$(".search_closed").css("color", night_mode_color);
|
|
|
|
} else {
|
|
|
|
$(".search_closed").css("color", color);
|
|
|
|
}
|
2020-07-02 01:45:54 +02:00
|
|
|
}, () => {
|
2020-05-16 19:32:30 +02:00
|
|
|
$(".search_closed").css("color", "");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-06-08 19:23:43 +02:00
|
|
|
if (page_params.search_pills_enabled && $('#search_query').is(':focus')) {
|
|
|
|
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-02 01:45:54 +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-08 23:41:08 +02:00
|
|
|
// TODO: Implement rerendering for subscriber count changes.
|
|
|
|
// We simply need to call this function in the appropriate places.
|
2020-05-16 19:40:13 +02:00
|
|
|
const filter = narrow_state.filter();
|
|
|
|
build_tab_bar(filter);
|
|
|
|
};
|
|
|
|
|
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");
|
|
|
|
$("#tab_list").addClass("hidden");
|
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
|
|
|
$("#tab_list").removeClass("hidden");
|
|
|
|
}
|
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;
|