forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.js
143 lines (125 loc) · 5.14 KB
/
archive.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
var archive = (function () {
var exports = {};
function should_separate_into_groups(current_msg_time, next_msg_time) {
var current_time = new XDate(current_msg_time * 1000);
var next_time = new XDate(next_msg_time * 1000);
return current_time.toDateString() !== next_time.toDateString();
}
function all_message_timestamps_to_human_readable() {
$('.message_time').each(function () {
var time = new XDate(parseInt($(this).text(), 10) * 1000);
$(this).text(time.toString('h:mm TT'));
});
}
exports.initialize = function () {
var all_message_groups = [];
var current_message_group = {};
var today = new XDate();
var recipient_and_topic = $('#display_recipient').html();
var stream_name = recipient_and_topic.split('-')[0];
var topic = recipient_and_topic.split('-')[1];
var recipient_color = color_data.pick_color();
current_message_group.message_containers = [];
current_message_group.show_group_date_divider = false;
current_message_group.display_recipient = stream_name;
current_message_group.topic = topic;
current_message_group.background_color = recipient_color;
function separate_into_groups(current_message_row, cur_msg_time, next_msg_time) {
var time = new XDate(next_msg_time * 1000);
var prev_time = new XDate(cur_msg_time * 1000);
current_message_group.message_containers.push(current_message_row[0].outerHTML);
var date_element = timerender.render_date(prev_time, undefined, today)[0];
current_message_group.date = date_element.outerHTML;
all_message_groups.push(current_message_group);
current_message_group = {};
current_message_group.message_containers = [];
current_message_group.group_date_divider_html =
timerender.render_date(time, prev_time, today)[0].outerHTML;
current_message_group.show_group_date_divider = true;
current_message_group.display_recipient = stream_name;
current_message_group.topic = topic;
current_message_group.background_color = recipient_color;
}
$('.message_row').each(function () {
var current_message_row = $(this);
var cur_msg_time = parseInt(current_message_row.find('.message_time').first().html(), 10);
var next_msg_time = parseInt(current_message_row.next().find('.message_time').first().html(), 10);
if (current_message_row.next().length === 0) {
separate_into_groups(current_message_row, cur_msg_time);
return;
}
if (should_separate_into_groups(cur_msg_time, next_msg_time)) {
separate_into_groups(current_message_row, cur_msg_time, next_msg_time);
return;
}
current_message_group.message_containers.push(current_message_row[0].outerHTML);
var time = new XDate(cur_msg_time * 1000);
var date_element = timerender.render_date(time, undefined, today)[0];
current_message_group.date = date_element.outerHTML;
});
var context = {
message_groups: all_message_groups,
};
var message_groups_html = templates.render('archive_message_group', context);
$('.message_row').each(function () {
$(this).detach();
});
$('.message_table').prepend(message_groups_html);
$('.messagebox').css('box-shadow', 'inset 2px 0px 0px 0px ' + recipient_color);
$('#display_recipient').remove();
// Fixing include_sender after rendering groups.
var prev_sender;
$('.recipient_row').each(function () {
if (prev_sender !== undefined) {
var first_group_msg = $(this).find('.message_row').first();
var message_sender = first_group_msg.find('.message_sender');
if (!message_sender.find('.inline_profile_picture').length) {
message_sender.replaceWith(prev_sender.clone());
}
}
var all_senders = $(this).find('.message_sender').has('.inline_profile_picture');
prev_sender = all_senders.last();
});
$('.app').scrollTop($('.app').height());
all_message_timestamps_to_human_readable();
};
return exports;
}());
var current_msg_list = {
selected_row: function () {
return $('.message_row').last();
},
};
var rows = {
get_message_recipient_row: function (message_row) {
return $(message_row).parent('.recipient_row');
},
first_message_in_group: function (message_group) {
return $('div.message_row:first', message_group);
},
id: function (message_row) {
return parseFloat(message_row.attr('zid'));
},
};
if (typeof module !== 'undefined') {
module.exports.current_msg_list = current_msg_list;
module.exports.rows = rows;
module.exports = archive;
}
var scroll_timer;
function scroll_finish() {
clearTimeout(scroll_timer);
scroll_timer = setTimeout(floating_recipient_bar.update, 100);
}
$(function () {
$.fn.safeOuterHeight = function () {
return $(this).outerHeight.apply(this, arguments) || 0;
};
$.fn.safeOuterWidth = function () {
return $(this).outerWidth.apply(this, arguments) || 0;
};
$('.app').scroll(_.throttle(function () {
scroll_finish();
}, 50));
archive.initialize();
});