2021-02-10 17:11:42 +01:00
|
|
|
import _ from "lodash";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-10 17:11:42 +01:00
|
|
|
import render_archive_message_group from "../templates/archive_message_group.hbs";
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2021-02-28 00:52:39 +01:00
|
|
|
import * as color_data from "./color_data";
|
2021-02-28 00:58:04 +01:00
|
|
|
import * as floating_recipient_bar from "./floating_recipient_bar";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2021-02-28 00:52:39 +01:00
|
|
|
|
2021-02-10 17:11:42 +01:00
|
|
|
const {format, isSameDay} = require("date-fns");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2018-02-03 12:57:38 +01:00
|
|
|
function should_separate_into_groups(current_msg_time, next_msg_time) {
|
2021-02-05 21:20:14 +01:00
|
|
|
return isSameDay(current_msg_time * 1000, next_msg_time * 1000);
|
2018-02-03 12:57:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function all_message_timestamps_to_human_readable() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_time").each(function () {
|
2021-02-05 21:20:14 +01:00
|
|
|
$(this).text(format(Number.parseInt($(this).text(), 10) * 1000, "h:mm a"));
|
2018-02-03 12:57:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-10 17:11:42 +01:00
|
|
|
export function initialize() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const all_message_groups = [];
|
|
|
|
let current_message_group = {};
|
2021-02-05 21:20:14 +01:00
|
|
|
const today = new Date();
|
2020-07-15 01:29:15 +02:00
|
|
|
const recipient_and_topic = $("#display_recipient").html();
|
|
|
|
const stream_name = recipient_and_topic.split("-")[0];
|
|
|
|
const topic = recipient_and_topic.split("-")[1];
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipient_color = color_data.pick_color();
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.message_containers = [];
|
2019-02-08 20:24:01 +01:00
|
|
|
current_message_group.show_group_date_divider = false;
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.display_recipient = stream_name;
|
2018-11-15 17:13:49 +01:00
|
|
|
current_message_group.topic = topic;
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.background_color = recipient_color;
|
|
|
|
|
|
|
|
function separate_into_groups(current_message_row, cur_msg_time, next_msg_time) {
|
2021-02-05 21:20:14 +01:00
|
|
|
const time = new Date(next_msg_time * 1000);
|
|
|
|
const prev_time = new Date(cur_msg_time * 1000);
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.message_containers.push(current_message_row[0].outerHTML);
|
2019-11-02 00:06:25 +01:00
|
|
|
const date_element = timerender.render_date(prev_time, undefined, today)[0];
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.date = date_element.outerHTML;
|
|
|
|
all_message_groups.push(current_message_group);
|
|
|
|
current_message_group = {};
|
|
|
|
current_message_group.message_containers = [];
|
2020-07-15 00:34:28 +02:00
|
|
|
current_message_group.group_date_divider_html = timerender.render_date(
|
|
|
|
time,
|
|
|
|
prev_time,
|
|
|
|
today,
|
|
|
|
)[0].outerHTML;
|
2019-02-08 20:24:01 +01:00
|
|
|
current_message_group.show_group_date_divider = true;
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.display_recipient = stream_name;
|
2018-11-15 17:13:49 +01:00
|
|
|
current_message_group.topic = topic;
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.background_color = recipient_color;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_row").each(function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const current_message_row = $(this);
|
2020-10-07 09:17:30 +02:00
|
|
|
const cur_msg_time = Number.parseInt(
|
|
|
|
current_message_row.find(".message_time").first().html(),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
const next_msg_time = Number.parseInt(
|
2020-07-15 00:34:28 +02:00
|
|
|
current_message_row.next().find(".message_time").first().html(),
|
|
|
|
10,
|
|
|
|
);
|
2018-02-03 12:57:38 +01:00
|
|
|
|
|
|
|
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);
|
2021-02-05 21:20:14 +01:00
|
|
|
const time = new Date(cur_msg_time * 1000);
|
2019-11-02 00:06:25 +01:00
|
|
|
const date_element = timerender.render_date(time, undefined, today)[0];
|
2018-02-03 12:57:38 +01:00
|
|
|
current_message_group.date = date_element.outerHTML;
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const context = {
|
2018-02-03 12:57:38 +01:00
|
|
|
message_groups: all_message_groups,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_groups_html = render_archive_message_group(context);
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_row").each(function () {
|
2018-02-03 12:57:38 +01:00
|
|
|
$(this).detach();
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_table").prepend(message_groups_html);
|
|
|
|
$(".messagebox").css("box-shadow", "inset 2px 0px 0px 0px " + recipient_color);
|
|
|
|
$("#display_recipient").remove();
|
2018-02-03 12:57:38 +01:00
|
|
|
|
|
|
|
// Fixing include_sender after rendering groups.
|
2019-11-02 00:06:25 +01:00
|
|
|
let prev_sender;
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".recipient_row").each(function () {
|
2018-02-03 12:57:38 +01:00
|
|
|
if (prev_sender !== undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const first_group_msg = $(this).find(".message_row").first();
|
|
|
|
const message_sender = first_group_msg.find(".message_sender");
|
|
|
|
if (!message_sender.find(".inline_profile_picture").length) {
|
2018-02-03 12:57:38 +01:00
|
|
|
message_sender.replaceWith(prev_sender.clone());
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
const all_senders = $(this).find(".message_sender").has(".inline_profile_picture");
|
2018-02-03 12:57:38 +01:00
|
|
|
prev_sender = all_senders.last();
|
|
|
|
});
|
2018-02-05 17:15:03 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".app").scrollTop($(".app").height());
|
2018-02-03 12:57:38 +01:00
|
|
|
all_message_timestamps_to_human_readable();
|
2021-02-10 17:11:42 +01:00
|
|
|
}
|
2018-02-03 12:57:38 +01:00
|
|
|
|
2021-02-10 17:11:42 +01:00
|
|
|
export const current_msg_list = {
|
2020-07-20 22:18:43 +02:00
|
|
|
selected_row() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $(".message_row").last();
|
2018-02-05 17:15:03 +01:00
|
|
|
},
|
|
|
|
};
|
2021-02-10 17:11:42 +01:00
|
|
|
|
|
|
|
export const rows = {
|
2020-07-20 22:18:43 +02:00
|
|
|
get_message_recipient_row(message_row) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $(message_row).parent(".recipient_row");
|
2018-02-05 17:15:03 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
first_message_in_group(message_group) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return $("div.message_row", message_group).first();
|
2018-02-05 17:15:03 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
id(message_row) {
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseFloat(message_row.attr("zid"));
|
2018-02-05 17:15:03 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let scroll_timer;
|
2018-02-05 17:15:03 +01:00
|
|
|
function scroll_finish() {
|
|
|
|
clearTimeout(scroll_timer);
|
|
|
|
scroll_timer = setTimeout(floating_recipient_bar.update, 100);
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$(() => {
|
2020-02-12 01:35:16 +01:00
|
|
|
$.fn.safeOuterHeight = function (...args) {
|
|
|
|
return this.outerHeight(...args) || 0;
|
2018-02-05 17:15:03 +01:00
|
|
|
};
|
2020-02-12 01:35:16 +01:00
|
|
|
$.fn.safeOuterWidth = function (...args) {
|
|
|
|
return this.outerWidth(...args) || 0;
|
2018-02-05 17:15:03 +01:00
|
|
|
};
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".app").on(
|
|
|
|
"scroll",
|
2020-07-15 00:34:28 +02:00
|
|
|
_.throttle(() => {
|
|
|
|
scroll_finish();
|
|
|
|
}, 50),
|
|
|
|
);
|
2021-02-10 17:11:42 +01:00
|
|
|
initialize();
|
2018-02-03 12:57:38 +01:00
|
|
|
});
|