Skip to content

Commit

Permalink
Extract static/js/pm_list.js.
Browse files Browse the repository at this point in the history
This handles most of the details of building the Private Messages
section in the upper left corner of the app.
  • Loading branch information
showell authored and timabbott committed Nov 11, 2016
1 parent af794be commit 7b0c645
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 115 deletions.
4 changes: 3 additions & 1 deletion frontend_tests/node_tests/stream_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ set_global('message_store', {
recent_private_messages: new global.Array()
});

// TODO: move pm_list-related tests to their own module
var pm_list = require('js/pm_list.js');
var stream_list = require('js/stream_list.js');

var jsdom = require("jsdom");
Expand Down Expand Up @@ -46,7 +48,7 @@ global.compile_template('stream_privacy');
return 1;
};

var convos_html = stream_list._build_private_messages_list(active_conversation, max_conversations);
var convos_html = pm_list._build_private_messages_list(active_conversation, max_conversations);
global.write_test_output("test_build_private_messages_list", convos_html);

var conversation = $(convos_html).find('a').text().trim();
Expand Down
6 changes: 3 additions & 3 deletions static/js/message_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ exports.update_messages = function update_messages(events) {
}
unread.update_unread_counts();
stream_list.update_streams_sidebar();
stream_list.update_private_messages();
pm_list.update_private_messages();
};


Expand Down Expand Up @@ -394,7 +394,7 @@ exports.insert_new_messages = function insert_new_messages(messages) {
unread.process_visible();
notifications.received_messages(messages);
stream_list.update_streams_sidebar();
stream_list.update_private_messages();
pm_list.update_private_messages();
};

function process_result(messages, opts) {
Expand Down Expand Up @@ -423,7 +423,7 @@ function process_result(messages, opts) {

activity.process_loaded_messages(messages);
stream_list.update_streams_sidebar();
stream_list.update_private_messages();
pm_list.update_private_messages();

if (opts.cont !== undefined) {
opts.cont(messages);
Expand Down
149 changes: 149 additions & 0 deletions static/js/pm_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
var pm_list = (function () {

var exports = {};

var private_messages_open = false;

// This module manages the "Private Messages" section in the upper
// left corner of the app. This was split out from stream_list.js.
//
// TODO: We want to manage our own unread counts, but that logic is
// still in stream_list.js.

// This is a copy of a similar method in stream_list.js, but
// we don't want to de-dup this, we want to simplify it.
function iterate_to_find(selector, name_to_find, context) {
var lowercase_name = name_to_find.toLowerCase();
var found = _.find($(selector, context), function (elem) {
return $(elem).attr('data-name').toLowerCase() === lowercase_name;
});
return found ? $(found) : $();
}

function get_filter_li() {
return iterate_to_find("#global_filters > li", "private");
}

exports.get_private_message_filter_li = function (conversation) {
var pm_li = get_filter_li();
return iterate_to_find(".expanded_private_messages li.expanded_private_message",
conversation, pm_li);
};

function remove_expanded_private_messages() {
popovers.hide_topic_sidebar_popover();
$("ul.expanded_private_messages").remove();
resize.resize_stream_filters_container();
}

exports.reset_to_unnarrowed = function () {
private_messages_open = false;
$("ul.filters li").removeClass('active-filter active-sub-filter');
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;

_.each(private_messages, function (private_message_obj, idx) {
var recipients_string = private_message_obj.display_reply_to;
var replies_to = private_message_obj.reply_to;
var num_unread = unread.num_unread_for_person(private_message_obj.reply_to);

var always_visible = (idx < max_private_messages) || (num_unread > 0)
|| (replies_to === active_conversation);

if (!always_visible) {
hiding_messages = true;
}

var display_message = {
recipients: recipients_string,
reply_to: replies_to,
unread: num_unread,
is_zero: num_unread === 0,
zoom_out_hide: !always_visible,
url: narrow.pm_with_uri(private_message_obj.reply_to)
};
display_messages.push(display_message);
});

var recipients_dom = templates.render('sidebar_private_message_list',
{messages: display_messages,
want_show_more_messages_links: hiding_messages});
return recipients_dom;
};

exports.rebuild_recent = function (active_conversation) {
remove_expanded_private_messages();
if (private_messages_open)
{
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);
private_li.append(private_messages_dom);
}
if (active_conversation) {
exports.get_private_message_filter_li(active_conversation).addClass('active-sub-filter');
}

resize.resize_stream_filters_container();
};

exports.update_private_messages = function () {
exports._build_private_messages_list();

if (! narrow.active()) {
return;
}

var is_pm_filter = _.contains(narrow.filter().operands('is'), "private");
var conversation = narrow.filter().operands('pm-with');
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("");
}
};

exports.set_click_handlers = function () {
$('#global_filters').on('click', '.show-more-private-messages', function (e) {
popovers.hide_all();
$(".expanded_private_messages").expectOne().removeClass("zoom-out").addClass("zoom-in");
$(".expanded_private_messages li.expanded_private_message").each(function () {
$(this).show();
});

e.preventDefault();
e.stopPropagation();
});
};

exports.expand = function (op_pm) {
private_messages_open = true;
if (op_pm.length === 1) {
$("#user_presences li[data-email='" + op_pm[0] + "']").addClass('active-filter');
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 {
$("#global_filters li[data-name='private']").addClass('active-filter zoom-out');
exports.rebuild_recent("");
}
};


return exports;
}());
if (typeof module !== 'undefined') {
module.exports = pm_list;
}

115 changes: 5 additions & 110 deletions static/js/stream_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var stream_list = (function () {
var exports = {};

var zoomed_stream = '';
var private_messages_open = false;
var last_private_message_count = 0;
var last_mention_count = 0;
var previous_sort_order;
Expand Down Expand Up @@ -178,28 +177,14 @@ function zoom_out(options) {
$("#stream_filters li.narrow-filter").show();
}

function remove_expanded_private_messages() {
popovers.hide_topic_sidebar_popover();
$("ul.expanded_private_messages").remove();
resize.resize_stream_filters_container();
}

function reset_to_unnarrowed(narrowed_within_same_stream) {
if (topic_list.is_zoomed() && narrowed_within_same_stream !== true) {
zoom_out({clear_topics: true});
} else {
topic_list.remove_expanded_topics();
}

private_messages_open = false;
$("ul.filters li").removeClass('active-filter active-sub-filter');
remove_expanded_private_messages();
}

function get_private_message_filter_li(conversation) {
var pm_li = get_filter_li('global', 'private');
return iterate_to_find(".expanded_private_messages li.expanded_private_message",
conversation, pm_li);
pm_list.reset_to_unnarrowed();
}

exports.set_in_home_view = function (stream, in_home) {
Expand Down Expand Up @@ -334,7 +319,7 @@ function set_count_toggle_button(elem, count) {
}

exports.set_pm_conversation_count = function (conversation, count) {
var pm_li = get_private_message_filter_li(conversation);
var pm_li = pm_list.get_private_message_filter_li(conversation);
var count_span = pm_li.find('.private_message_count');
var value_span = count_span.find('.value');

Expand All @@ -350,65 +335,13 @@ exports.remove_narrow_filter = function (name, type) {
get_filter_li(type, name).remove();
};

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;

_.each(private_messages, function (private_message_obj, idx) {
var recipients_string = private_message_obj.display_reply_to;
var replies_to = private_message_obj.reply_to;
var num_unread = unread.num_unread_for_person(private_message_obj.reply_to);

var always_visible = (idx < max_private_messages) || (num_unread > 0)
|| (replies_to === active_conversation);

if (!always_visible) {
hiding_messages = true;
}

var display_message = {
recipients: recipients_string,
reply_to: replies_to,
unread: num_unread,
is_zero: num_unread === 0,
zoom_out_hide: !always_visible,
url: narrow.pm_with_uri(private_message_obj.reply_to)
};
display_messages.push(display_message);
});

var recipients_dom = templates.render('sidebar_private_message_list',
{messages: display_messages,
want_show_more_messages_links: hiding_messages});
return recipients_dom;
};

function rebuild_recent_topics(stream) {
// TODO: Call rebuild_recent_topics less, not on every new
// message.
var stream_li = get_filter_li('stream', stream);
topic_list.rebuild(stream_li, stream);
}

function rebuild_recent_private_messages(active_conversation) {
remove_expanded_private_messages();
if (private_messages_open)
{
var max_private_messages = 5;
var private_li = get_filter_li('global', 'private');
var private_messages_dom = exports._build_private_messages_list(active_conversation,
max_private_messages);
private_li.append(private_messages_dom);
}
if (active_conversation) {
get_private_message_filter_li(active_conversation).addClass('active-sub-filter');
}

resize.resize_stream_filters_container();
}

exports.update_streams_sidebar = function () {
exports.build_stream_list();

Expand All @@ -424,25 +357,6 @@ exports.update_streams_sidebar = function () {
}
};

exports.update_private_messages = function () {
exports._build_private_messages_list();

if (! narrow.active()) {
return;
}

var is_pm_filter = _.contains(narrow.filter().operands('is'), "private");
var conversation = narrow.filter().operands('pm-with');
if (conversation.length === 1) {
rebuild_recent_private_messages(conversation[0]);
} else if (conversation.length !== 0) {
// TODO: This should be the reply-to of the thread.
rebuild_recent_private_messages("");
} else if (is_pm_filter) {
rebuild_recent_private_messages("");
}
};

function do_new_messages_animation(message_type) {
var li = get_filter_li("global", message_type);
li.addClass("new_messages");
Expand Down Expand Up @@ -532,6 +446,8 @@ $(function () {
zoom_out: zoom_out
});

pm_list.set_click_handlers();

$(document).on('narrow_activated.zulip', function (event) {
reset_to_unnarrowed(narrow.stream() === zoomed_stream);

Expand All @@ -551,17 +467,7 @@ $(function () {

var op_pm = event.filter.operands('pm-with');
if ((op_is.length !== 0 && _.contains(op_is, "private")) || op_pm.length !== 0) {
private_messages_open = true;
if (op_pm.length === 1) {
$("#user_presences li[data-email='" + op_pm[0] + "']").addClass('active-filter');
rebuild_recent_private_messages(op_pm[0]);
} else if (op_pm.length !== 0) {
// TODO: Should pass the reply-to of the thread
rebuild_recent_private_messages("");
} else {
$("#global_filters li[data-name='private']").addClass('active-filter zoom-out');
rebuild_recent_private_messages("");
}
pm_list.expand(op_pm);
}

var op_stream = event.filter.operands('stream');
Expand Down Expand Up @@ -594,17 +500,6 @@ $(function () {
previous_unpinned_order = undefined;
});

$('#global_filters').on('click', '.show-more-private-messages', function (e) {
popovers.hide_all();
$(".expanded_private_messages").expectOne().removeClass("zoom-out").addClass("zoom-in");
$(".expanded_private_messages li.expanded_private_message").each(function () {
$(this).show();
});

e.preventDefault();
e.stopPropagation();
});

$('#stream_filters').on('click', 'li .subscription_block', function (e) {
if (e.metaKey || e.ctrlKey) {
return;
Expand Down
2 changes: 1 addition & 1 deletion tools/jslint/check-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var globals =
+ ' avatar feature_flags search_suggestion referral stream_color Dict'
+ ' Filter summary admin stream_data muting WinChan muting_ui Socket channel gear_menu'
+ ' message_flags bot_data loading favicon resize scroll_bar condense floating_recipient_bar'
+ ' copy_and_paste click_handlers topic_list'
+ ' copy_and_paste click_handlers topic_list pm_list'

// colorspace.js
+ ' colorspace'
Expand Down
2 changes: 2 additions & 0 deletions tools/lib/find_add_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
'success',
'text-error',
'warning',
'zoom-in', # TODO: clean these up, they are confusing
'zoom-out',
]

def raise_error(fn, i, line):
Expand Down
Loading

0 comments on commit 7b0c645

Please sign in to comment.