forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpm_list.js
235 lines (191 loc) · 7.15 KB
/
pm_list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var pm_list = (function () {
var exports = {};
var private_messages_open = false;
// 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;
// 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() {
return $(".top_left_private_messages");
}
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(count) {
var count_span = get_filter_li().find('.count');
var value_span = count_span.find('.value');
update_count_in_dom(count_span, value_span, count);
}
exports.get_conversation_li = function (conversation) {
// conversation is something like "foo@example.com,bar@example.com"
var user_ids_string = people.reply_to_to_user_ids_string(conversation);
if (!user_ids_string) {
return;
}
return exports.get_li_for_user_ids_string(user_ids_string);
};
exports.get_li_for_user_ids_string = function (user_ids_string) {
var pm_li = get_filter_li();
var convo_li = pm_li.find("li[data-user-ids-string='" + user_ids_string + "']");
return convo_li;
};
function set_pm_conversation_count(user_ids_string, count) {
var pm_li = pm_list.get_li_for_user_ids_string(user_ids_string);
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);
}
function remove_expanded_private_messages() {
stream_popover.hide_topic_popover();
$("#private-container").remove();
resize.resize_stream_filters_container();
}
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 () {
private_messages_open = false;
zoomed_in = false;
remove_expanded_private_messages();
};
exports._build_private_messages_list = function (active_conversation, max_private_messages) {
var private_messages = pm_conversations.recent.get();
var display_messages = [];
var hiding_messages = false;
// SHIM
if (active_conversation) {
active_conversation = people.emails_strings_to_user_ids_string(active_conversation);
}
_.each(private_messages, function (private_message_obj, idx) {
var user_ids_string = private_message_obj.user_ids_string;
var reply_to = people.user_ids_string_to_emails_string(user_ids_string);
var recipients_string = people.get_recipients(user_ids_string);
var num_unread = unread.num_unread_for_person(user_ids_string);
var always_visible = idx < max_private_messages || num_unread > 0
|| user_ids_string === active_conversation;
if (!always_visible) {
if (!zoomed_in) {
hiding_messages = true;
}
}
var is_group = user_ids_string.indexOf(',') >= 0;
var user_circle_class = buddy_data.get_user_circle_class(user_ids_string);
var fraction_present;
if (is_group) {
user_circle_class = 'user_circle_fraction';
fraction_present = buddy_data.huddle_fraction_present(user_ids_string);
}
var display_message = {
recipients: recipients_string,
user_ids_string: user_ids_string,
unread: num_unread,
is_zero: num_unread === 0,
zoom_out_hide: !always_visible,
url: hash_util.pm_with_uri(reply_to),
user_circle_class: user_circle_class,
fraction_present: fraction_present,
is_group: is_group,
};
display_messages.push(display_message);
});
var zoom_class;
if (zoomed_in) {
zoom_class = "zoomed-in";
} else {
zoom_class = "zoomed-out";
}
var recipients_dom = templates.render('sidebar_private_message_list',
{messages: display_messages,
zoom_class: zoom_class,
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) {
var active_li = exports.get_conversation_li(active_conversation);
if (active_li) {
active_li.addClass('active-sub-filter');
}
}
resize.resize_stream_filters_container();
};
exports.update_private_messages = function () {
if (!narrow_state.active()) {
return;
}
var is_pm_filter = _.contains(narrow_state.filter().operands('is'), "private");
var conversation = narrow_state.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("");
$(".top_left_private_messages").addClass('active-filter');
}
if ($("#private-container").length !== 0) {
ui.set_up_scrollbar($("#private-container"));
}
};
exports.set_click_handlers = function () {
$('.top_left_private_messages').on('click', '.show-more-private-messages', function (e) {
popovers.hide_all();
zoom_in();
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("");
}
};
exports.update_dom_with_unread_counts = function (counts) {
set_count(counts.private_message_count);
counts.pm_count.each(function (count, user_ids_string) {
// TODO: just use user_ids_string in our markup
set_pm_conversation_count(user_ids_string, count);
});
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);
};
exports.initialize = function () {
pm_list.set_click_handlers();
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = pm_list;
}
window.pm_list = pm_list;