forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloating_recipient_bar.js
334 lines (269 loc) · 10.5 KB
/
floating_recipient_bar.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"use strict";
const XDate = require("xdate");
let is_floating_recipient_bar_showing = false;
function top_offset(elem) {
return elem.offset().top - $("#message_view_header").safeOuterHeight();
}
exports.first_visible_message = function (bar) {
// The first truly visible message would be computed using the
// bottom of the floating recipient bar; but we want the date from
// the first visible message were the floating recipient bar not
// displayed, which will always be the first messages whose bottom
// overlaps the floating recipient bar's space (since you ).
const messages = bar.children(".message_row");
const frb_bottom = exports.get_frb_bottom();
const frb_top = frb_bottom - 25;
let result;
for (const message_element of messages) {
// The details of this comparison function are sensitive, since we're
// balancing between three possible bugs:
//
// * If we compare against the bottom of the floating
// recipient bar, we end up with a bug where if the floating
// recipient bar is just above a normal recipient bar while
// overlapping a series of 1-line messages, there might be 2
// messages occluded by the recipient bar, and we want the
// second one, not the first.
//
// * If we compare the message bottom against the top of the
// floating recipient bar, and the floating recipient bar is
// over a "Yesterday/Today" message date row, we might
// confusingly have the floating recipient bar display
// e.g. "Yesterday" even though all messages in view were
// actually sent "Today".
//
// * If the the floating recipient bar is over a
// between-message groups date separator or similar widget,
// there might be no message overlap with the floating
// recipient bar.
//
// Careful testing of these two corner cases with
// message_viewport.scrollTop() to set precise scrolling
// positions determines the value for date_bar_height_offset.
let message = $(message_element);
const message_bottom = top_offset(message) + message.safeOuterHeight();
const date_bar_height_offset = 10;
if (message_bottom > frb_top) {
result = message;
}
// Important: This will break if we ever have things that are
// not message rows inside a recipient_row block.
message = message.next(".message_row");
if (
message.length > 0 &&
result &&
// Before returning a result, we check whether the next
// message's top is actually below the bottom of the
// floating recipient bar; this is different from the
// bottom of our current message because there may be a
// between-messages date separator row in between.
top_offset(message) < frb_bottom - date_bar_height_offset
) {
result = message;
}
if (result) {
return result;
}
}
// If none of the messages are visible, just take the last message.
return $(messages[messages.length - 1]);
};
exports.get_date = function (elem) {
const message_row = exports.first_visible_message(elem);
if (!message_row || !message_row.length) {
return undefined;
}
const msg_id = rows.id(message_row);
if (msg_id === undefined) {
return undefined;
}
const message = message_store.get(msg_id);
if (!message) {
return undefined;
}
const time = new XDate(message.timestamp * 1000);
const today = new XDate();
const rendered_date = timerender.render_date(time, undefined, today)[0].outerHTML;
return rendered_date;
};
exports.get_frb_bottom = function () {
const bar = $("#floating_recipient_bar");
const bar_top = top_offset(bar);
const bar_bottom = bar_top + bar.safeOuterHeight();
return bar_bottom;
};
exports.relevant_recipient_bars = function () {
let elems = [];
// This line of code does a reverse traversal
// from the selected message, which should be
// in the visible part of the feed, but is sometimes
// not exactly where we want. The value we get
// may be be too far up in the feed, but we can
// deal with that later.
let first_elem = exports.candidate_recipient_bar();
if (!first_elem) {
first_elem = $(".focused_table").find(".recipient_row").first();
}
if (first_elem.length === 0) {
return [];
}
elems.push(first_elem);
const max_offset = top_offset($("#compose"));
let header_height = first_elem.find(".message_header").safeOuterHeight();
// It's okay to overestimate header_height a bit, as we don't
// really need an FRB for a section that barely shows.
header_height += 10;
function next(elem) {
elem = elem.next();
while (elem.length !== 0 && !elem.hasClass("recipient_row")) {
elem = elem.next();
}
return elem;
}
// Now start the forward traversal of recipient bars.
// We'll stop when we go below the fold.
let elem = next(first_elem);
while (elem.length) {
if (top_offset(elem) < header_height) {
// If we are close to the top, then the prior
// elements we found are no longer relevant,
// because either the selected item we started
// with in our reverse traversal was too high,
// or there's simply not enough room to draw
// a recipient bar without it being ugly.
elems = [];
}
if (top_offset(elem) > max_offset) {
// Out of sight, out of mind!
// (The element is below the fold, so we stop the
// traversal.)
break;
}
elems.push(elem);
elem = next(elem);
}
if (elems.length === 0) {
blueslip.warn("Unexpected situation--maybe viewport height is very short.");
return [];
}
const items = elems.map((elem, i) => {
let date_html;
let need_frb;
if (i === 0) {
date_html = exports.get_date(elem);
need_frb = top_offset(elem) < 0;
} else {
date_html = elem.find(".recipient_row_date").html();
need_frb = false;
}
const date_text = $(date_html).text();
// Add title here to facilitate troubleshooting.
const title = elem.find(".message_label_clickable").last().attr("title");
const item = {
elem,
title,
date_html,
date_text,
need_frb,
};
return item;
});
items[0].show_date = true;
for (let i = 1; i < items.length; i += 1) {
items[i].show_date = items[i].date_text !== items[i - 1].date_text;
}
for (const item of items) {
if (!item.need_frb) {
delete item.date_html;
}
}
return items;
};
exports.candidate_recipient_bar = function () {
// Find a recipient bar that is close to being onscreen
// but above the "top". This function is guaranteed to
// return **some** recipient bar that is above the fold,
// if there is one, but it may not be the optimal one if
// our pointer is messed up. Starting with the pointer
// is just an optimization here, and our caller will do
// a forward traversal and clean up as necessary.
// In most cases we find the bottom-most of recipient
// bars that is still above the fold.
// Start with the pointer's current location.
const selected_row = current_msg_list.selected_row();
if (selected_row === undefined || selected_row.length === 0) {
return undefined;
}
let candidate = rows.get_message_recipient_row(selected_row);
if (candidate === undefined) {
return undefined;
}
while (candidate.length) {
if (candidate.hasClass("recipient_row") && top_offset(candidate) < 0) {
return candidate;
}
// We cannot use .prev(".recipient_row") here, because that
// returns nothing if the previous element is not a recipient
// row, rather than finding the first recipient_row.
candidate = candidate.prev();
}
return undefined;
};
function show_floating_recipient_bar() {
if (!is_floating_recipient_bar_showing) {
$("#floating_recipient_bar").css("visibility", "visible");
is_floating_recipient_bar_showing = true;
}
}
let old_source;
function replace_floating_recipient_bar(source_info) {
const source_recipient_bar = source_info.elem;
let new_label;
let other_label;
let header;
if (source_recipient_bar !== old_source) {
if (source_recipient_bar.children(".message_header_stream").length !== 0) {
new_label = $("#current_label_stream");
other_label = $("#current_label_private_message");
header = source_recipient_bar.children(".message_header_stream");
} else {
new_label = $("#current_label_private_message");
other_label = $("#current_label_stream");
header = source_recipient_bar.children(".message_header_private_message");
}
new_label.find(".message_header").replaceWith(header.clone());
other_label.css("display", "none");
new_label.css("display", "block");
new_label.attr("zid", rows.id(rows.first_message_in_group(source_recipient_bar)));
new_label.toggleClass("message-fade", source_recipient_bar.hasClass("message-fade"));
old_source = source_recipient_bar;
}
const rendered_date = source_info.date_html || "";
$("#floating_recipient_bar").find(".recipient_row_date").html(rendered_date);
show_floating_recipient_bar();
}
exports.hide = function () {
if (is_floating_recipient_bar_showing) {
$("#floating_recipient_bar").css("visibility", "hidden");
is_floating_recipient_bar_showing = false;
}
};
exports.de_clutter_dates = function (items) {
for (const item of items) {
item.elem.find(".recipient_row_date").toggle(item.show_date);
}
};
exports.update = function () {
const items = exports.relevant_recipient_bars();
if (!items || items.length === 0) {
exports.hide();
return;
}
exports.de_clutter_dates(items);
if (!items[0].need_frb) {
exports.hide();
return;
}
replace_floating_recipient_bar(items[0]);
};
window.floating_recipient_bar = exports;