2016-11-11 12:33:51 +01:00
|
|
|
var pm_list = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var private_messages_open = false;
|
|
|
|
|
2016-12-17 01:14:07 +01:00
|
|
|
// You can click on "more conversations" to zoom in. There's no
|
|
|
|
// way to zoom back out other than re-narrowing out and in of the
|
|
|
|
// PM list.
|
|
|
|
var zoomed_in = false;
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
// This module manages the "Private Messages" section in the upper
|
|
|
|
// left corner of the app. This was split out from stream_list.js.
|
|
|
|
|
|
|
|
function get_filter_li() {
|
2016-11-15 16:48:09 +01:00
|
|
|
return $("#global_filters > li[data-name='private']");
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:14:59 +01:00
|
|
|
function update_count_in_dom(count_span, value_span, count) {
|
|
|
|
if (count === 0) {
|
|
|
|
count_span.hide();
|
|
|
|
value_span.text('');
|
|
|
|
} else {
|
|
|
|
count_span.show();
|
|
|
|
value_span.text(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_count(type, name, count) {
|
|
|
|
var count_span = get_filter_li(type, name).find('.count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
update_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:48:09 +01:00
|
|
|
exports.get_conversation_li = function (conversation) {
|
|
|
|
// conversation is something like "foo@example.com,bar@example.com"
|
2016-11-18 15:48:53 +01:00
|
|
|
var user_ids_string = people.emails_strings_to_user_ids_string(conversation);
|
|
|
|
if (!user_ids_string) {
|
|
|
|
blueslip.warn('Unknown conversation: ' + conversation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return exports.get_li_for_user_ids_string(user_ids_string);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_li_for_user_ids_string = function (user_ids_string) {
|
2016-11-11 12:33:51 +01:00
|
|
|
var pm_li = get_filter_li();
|
2016-11-18 15:48:53 +01:00
|
|
|
var convo_li = pm_li.find("li[data-user-ids-string='" + user_ids_string + "']");
|
2016-11-15 16:48:09 +01:00
|
|
|
return convo_li;
|
2016-11-11 12:33:51 +01:00
|
|
|
};
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function set_pm_conversation_count(conversation, count) {
|
2016-11-15 16:48:09 +01:00
|
|
|
var pm_li = pm_list.get_conversation_li(conversation);
|
2016-11-14 17:14:59 +01:00
|
|
|
var count_span = pm_li.find('.private_message_count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
|
|
|
|
if (count_span.length === 0 || value_span.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count_span.removeClass("zero_count");
|
|
|
|
update_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
function remove_expanded_private_messages() {
|
2017-03-05 17:28:40 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
2016-11-11 12:33:51 +01:00
|
|
|
$("ul.expanded_private_messages").remove();
|
|
|
|
resize.resize_stream_filters_container();
|
|
|
|
}
|
|
|
|
|
2016-12-17 01:14:07 +01:00
|
|
|
function zoom_in() {
|
|
|
|
zoomed_in = true;
|
|
|
|
var list_widget = $("ul.expanded_private_messages").expectOne();
|
|
|
|
list_widget.removeClass("zoomed-out").addClass("zoomed-in");
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.close = function () {
|
2016-11-11 12:33:51 +01:00
|
|
|
private_messages_open = false;
|
2016-12-17 01:14:07 +01:00
|
|
|
zoomed_in = false;
|
2016-11-11 12:33:51 +01:00
|
|
|
remove_expanded_private_messages();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports._build_private_messages_list = function (active_conversation, max_private_messages) {
|
|
|
|
|
|
|
|
var private_messages = message_store.recent_private_messages || [];
|
|
|
|
var display_messages = [];
|
|
|
|
var hiding_messages = false;
|
|
|
|
|
2016-11-18 15:48:53 +01:00
|
|
|
// SHIM
|
|
|
|
if (active_conversation) {
|
|
|
|
active_conversation = people.emails_strings_to_user_ids_string(active_conversation);
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
_.each(private_messages, function (private_message_obj, idx) {
|
2016-11-18 15:48:53 +01:00
|
|
|
var user_ids_string = private_message_obj.user_ids_string;
|
|
|
|
var reply_to = people.user_ids_string_to_emails_string(user_ids_string);
|
2017-01-25 16:23:22 +01:00
|
|
|
var recipients_string = people.get_recipients(user_ids_string);
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2016-11-18 17:02:06 +01:00
|
|
|
var num_unread = unread.num_unread_for_person(user_ids_string);
|
2016-11-11 12:33:51 +01:00
|
|
|
|
|
|
|
var always_visible = (idx < max_private_messages) || (num_unread > 0)
|
2016-11-18 15:48:53 +01:00
|
|
|
|| (user_ids_string === active_conversation);
|
2016-11-11 12:33:51 +01:00
|
|
|
|
|
|
|
if (!always_visible) {
|
2016-12-17 01:14:07 +01:00
|
|
|
if (!zoomed_in) {
|
|
|
|
hiding_messages = true;
|
|
|
|
}
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var display_message = {
|
|
|
|
recipients: recipients_string,
|
2016-11-18 15:48:53 +01:00
|
|
|
user_ids_string: user_ids_string,
|
2016-11-11 12:33:51 +01:00
|
|
|
unread: num_unread,
|
|
|
|
is_zero: num_unread === 0,
|
|
|
|
zoom_out_hide: !always_visible,
|
2017-01-12 00:17:43 +01:00
|
|
|
url: narrow.pm_with_uri(reply_to),
|
2016-11-11 12:33:51 +01:00
|
|
|
};
|
|
|
|
display_messages.push(display_message);
|
|
|
|
});
|
|
|
|
|
2016-12-17 01:14:07 +01:00
|
|
|
var zoom_class;
|
|
|
|
|
|
|
|
if (zoomed_in) {
|
|
|
|
zoom_class = "zoomed-in";
|
|
|
|
} else {
|
|
|
|
zoom_class = "zoomed-out";
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
var recipients_dom = templates.render('sidebar_private_message_list',
|
|
|
|
{messages: display_messages,
|
2016-12-17 01:14:07 +01:00
|
|
|
zoom_class: zoom_class,
|
2016-11-11 12:33:51 +01:00
|
|
|
want_show_more_messages_links: hiding_messages});
|
|
|
|
return recipients_dom;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.rebuild_recent = function (active_conversation) {
|
|
|
|
remove_expanded_private_messages();
|
2016-12-01 13:30:30 +01:00
|
|
|
if (private_messages_open) {
|
2016-11-11 12:33:51 +01:00
|
|
|
var max_private_messages = 5;
|
|
|
|
var private_li = get_filter_li();
|
|
|
|
var private_messages_dom = exports._build_private_messages_list(active_conversation,
|
|
|
|
max_private_messages);
|
2016-12-17 01:14:07 +01:00
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
private_li.append(private_messages_dom);
|
|
|
|
}
|
|
|
|
if (active_conversation) {
|
2016-11-15 16:48:09 +01:00
|
|
|
exports.get_conversation_li(active_conversation).addClass('active-sub-filter');
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resize.resize_stream_filters_container();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_private_messages = function () {
|
|
|
|
exports._build_private_messages_list();
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (! narrow_state.active()) {
|
2016-11-11 12:33:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
var is_pm_filter = _.contains(narrow_state.filter().operands('is'), "private");
|
|
|
|
var conversation = narrow_state.filter().operands('pm-with');
|
2016-11-11 12:33:51 +01:00
|
|
|
if (conversation.length === 1) {
|
|
|
|
exports.rebuild_recent(conversation[0]);
|
|
|
|
} else if (conversation.length !== 0) {
|
|
|
|
// TODO: This should be the reply-to of the thread.
|
|
|
|
exports.rebuild_recent("");
|
|
|
|
} else if (is_pm_filter) {
|
|
|
|
exports.rebuild_recent("");
|
2017-02-06 22:50:38 +01:00
|
|
|
$("#global_filters li[data-name='private']").addClass('active-filter');
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_click_handlers = function () {
|
|
|
|
$('#global_filters').on('click', '.show-more-private-messages', function (e) {
|
|
|
|
popovers.hide_all();
|
2016-12-17 01:14:07 +01:00
|
|
|
zoom_in();
|
2016-11-11 12:33:51 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.expand = function (op_pm) {
|
|
|
|
private_messages_open = true;
|
|
|
|
if (op_pm.length === 1) {
|
|
|
|
exports.rebuild_recent(op_pm[0]);
|
|
|
|
} else if (op_pm.length !== 0) {
|
|
|
|
// TODO: Should pass the reply-to of the thread
|
|
|
|
exports.rebuild_recent("");
|
|
|
|
} else {
|
|
|
|
exports.rebuild_recent("");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-14 17:14:59 +01:00
|
|
|
exports.update_dom_with_unread_counts = function (counts) {
|
|
|
|
set_count("global", "private", counts.private_message_count);
|
2016-11-16 00:09:09 +01:00
|
|
|
counts.pm_count.each(function (count, user_ids_string) {
|
|
|
|
// TODO: just use user_ids_string in our markup
|
|
|
|
var emails_string = people.user_ids_string_to_emails_string(user_ids_string);
|
|
|
|
set_pm_conversation_count(emails_string, count);
|
2016-11-14 17:14:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
unread_ui.set_count_toggle_button($("#userlist-toggle-unreadcount"),
|
|
|
|
counts.private_message_count);
|
|
|
|
|
|
|
|
unread_ui.animate_private_message_changes(get_filter_li(),
|
|
|
|
counts.private_message_count);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-14 19:58:51 +02:00
|
|
|
exports.initialize = function () {
|
|
|
|
pm_list.set_click_handlers();
|
|
|
|
};
|
2016-11-11 12:33:51 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = pm_list;
|
|
|
|
}
|
|
|
|
|