2019-07-14 00:41:01 +02:00
|
|
|
/*
|
|
|
|
This library implements two related, similar concepts:
|
|
|
|
|
|
|
|
- condensing, i.e. cutting off messages taller than about a half
|
|
|
|
screen so that they aren't distractingly tall (and offering a button
|
|
|
|
to uncondense them).
|
|
|
|
|
|
|
|
- Collapsing, i.e. taking a message and reducing its height to a
|
|
|
|
single line, with a button to see the content.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-02-01 04:05:55 +01:00
|
|
|
const IntDict = require('./int_dict').IntDict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-01 04:05:55 +01:00
|
|
|
const _message_content_height_cache = new IntDict();
|
2014-03-13 20:22:09 +01:00
|
|
|
|
|
|
|
function show_more_link(row) {
|
|
|
|
row.find(".message_condenser").hide();
|
|
|
|
row.find(".message_expander").show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_condense_link(row) {
|
|
|
|
row.find(".message_expander").hide();
|
|
|
|
row.find(".message_condenser").show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function condense_row(row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const content = row.find(".message_content");
|
2014-03-13 20:22:09 +01:00
|
|
|
content.addClass("condensed");
|
|
|
|
show_more_link(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
function uncondense_row(row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const content = row.find(".message_content");
|
2014-03-13 20:22:09 +01:00
|
|
|
content.removeClass("condensed");
|
|
|
|
show_condense_link(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.uncollapse = function (row) {
|
|
|
|
// Uncollapse a message, restoring the condensed message [More] or
|
|
|
|
// [Condense] link if necessary.
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(rows.id(row));
|
2014-03-13 20:22:09 +01:00
|
|
|
message.collapsed = false;
|
2018-07-03 01:41:31 +02:00
|
|
|
message_flags.save_uncollapsed(message);
|
2014-03-13 20:22:09 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const process_row = function process_row(row) {
|
|
|
|
const content = row.find(".message_content");
|
2016-03-13 19:05:10 +01:00
|
|
|
content.removeClass("collapsed");
|
|
|
|
|
|
|
|
if (message.condensed === true) {
|
|
|
|
// This message was condensed by the user, so re-show the
|
|
|
|
// [More] link.
|
|
|
|
condense_row(row);
|
|
|
|
} else if (message.condensed === false) {
|
|
|
|
// This message was un-condensed by the user, so re-show the
|
|
|
|
// [Condense] link.
|
|
|
|
uncondense_row(row);
|
|
|
|
} else if (content.hasClass("could-be-condensed")) {
|
|
|
|
// By default, condense a long message.
|
|
|
|
condense_row(row);
|
|
|
|
} else {
|
|
|
|
// This was a short message, no more need for a [More] link.
|
|
|
|
row.find(".message_expander").hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We also need to collapse this message in the home view
|
2019-11-02 00:06:25 +01:00
|
|
|
const home_row = home_msg_list.get_row(rows.id(row));
|
2016-03-13 19:05:10 +01:00
|
|
|
|
|
|
|
process_row(row);
|
|
|
|
process_row(home_row);
|
2014-03-13 20:22:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.collapse = function (row) {
|
|
|
|
// Collapse a message, hiding the condensed message [More] or
|
|
|
|
// [Condense] link if necessary.
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(rows.id(row));
|
2014-03-13 20:22:09 +01:00
|
|
|
message.collapsed = true;
|
2017-12-15 23:32:36 +01:00
|
|
|
|
|
|
|
if (message.locally_echoed) {
|
|
|
|
// Trying to collapse a locally echoed message is
|
|
|
|
// very rare, and in our current implementation the
|
|
|
|
// server response overwrites the flag, so we just
|
|
|
|
// punt for now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 01:41:31 +02:00
|
|
|
message_flags.save_collapsed(message);
|
2016-03-13 19:05:10 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const process_row = function process_row(row) {
|
2016-03-13 19:05:10 +01:00
|
|
|
row.find(".message_content").addClass("collapsed");
|
|
|
|
show_more_link(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
// We also need to collapse this message in the home view
|
2019-11-02 00:06:25 +01:00
|
|
|
const home_row = home_msg_list.get_row(rows.id(row));
|
2016-03-13 19:05:10 +01:00
|
|
|
|
|
|
|
process_row(row);
|
|
|
|
process_row(home_row);
|
2014-03-13 20:22:09 +01:00
|
|
|
};
|
2017-06-16 06:46:46 +02:00
|
|
|
|
|
|
|
exports.toggle_collapse = function (message) {
|
2019-03-07 17:55:17 +01:00
|
|
|
if (message.is_me_message) {
|
|
|
|
// Disabled temporarily because /me messages don't have a
|
|
|
|
// styling for collapsing /me messages (they only recently
|
|
|
|
// added multi-line support). See also popovers.js.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-14 00:41:01 +02:00
|
|
|
// This function implements a multi-way toggle, to try to do what
|
|
|
|
// the user wants for messages:
|
|
|
|
//
|
|
|
|
// * If the message is currently showing any [More] link, either
|
|
|
|
// because it was previously condensed or collapsed, fully display it.
|
|
|
|
// * If the message is fully visible, either because it's too short to
|
|
|
|
// condense or because it's already uncondensed, collapse it
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = current_msg_list.get_row(message.id);
|
2017-06-16 06:46:46 +02:00
|
|
|
if (!row) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-14 00:41:01 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const content = row.find(".message_content");
|
|
|
|
const is_condensable = content.hasClass("could-be-condensed");
|
|
|
|
const is_condensed = content.hasClass("condensed");
|
2017-06-16 06:46:46 +02:00
|
|
|
if (message.collapsed) {
|
2019-07-14 00:41:01 +02:00
|
|
|
if (is_condensable) {
|
|
|
|
message.condensed = true;
|
|
|
|
content.addClass("condensed");
|
|
|
|
exports.show_message_expander(row);
|
|
|
|
row.find(".message_condenser").hide();
|
|
|
|
}
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.uncollapse(row);
|
2019-07-14 00:41:01 +02:00
|
|
|
} else {
|
|
|
|
if (is_condensed) {
|
|
|
|
message.condensed = false;
|
|
|
|
content.removeClass("condensed");
|
|
|
|
exports.hide_message_expander(row);
|
|
|
|
row.find(".message_condenser").show();
|
|
|
|
} else {
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.collapse(row);
|
2019-07-14 00:41:01 +02:00
|
|
|
}
|
2017-06-16 06:46:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-13 20:22:09 +01:00
|
|
|
exports.clear_message_content_height_cache = function () {
|
2020-02-01 04:05:55 +01:00
|
|
|
_message_content_height_cache.clear();
|
2014-03-13 20:22:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.un_cache_message_content_height = function (message_id) {
|
|
|
|
_message_content_height_cache.del(message_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
function get_message_height(elem, message_id) {
|
|
|
|
if (_message_content_height_cache.has(message_id)) {
|
|
|
|
return _message_content_height_cache.get(message_id);
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:37:35 +01:00
|
|
|
// shown to be ~2.5x faster than Node.getBoundingClientRect().
|
2019-11-02 00:06:25 +01:00
|
|
|
const height = elem.offsetHeight;
|
2014-03-13 20:22:09 +01:00
|
|
|
_message_content_height_cache.set(message_id, height);
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2017-03-15 15:26:39 +01:00
|
|
|
exports.hide_message_expander = function (row) {
|
|
|
|
if (row.find(".could-be-condensed").length !== 0) {
|
|
|
|
row.find(".message_expander").hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.show_message_expander = function (row) {
|
|
|
|
if (row.find(".could-be-condensed").length !== 0) {
|
|
|
|
row.find(".message_expander").show();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-13 20:22:09 +01:00
|
|
|
exports.condense_and_collapse = function (elems) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const height_cutoff = message_viewport.height() * 0.65;
|
2014-03-13 20:22:09 +01:00
|
|
|
|
|
|
|
_.each(elems, function (elem) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const content = $(elem).find(".message_content");
|
|
|
|
const message = current_msg_list.get(rows.id($(elem)));
|
2014-03-13 20:22:09 +01:00
|
|
|
if (content !== undefined && message !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_height = get_message_height(elem, message.id);
|
|
|
|
const long_message = message_height > height_cutoff;
|
2014-03-13 20:22:09 +01:00
|
|
|
if (long_message) {
|
|
|
|
// All long messages are flagged as such.
|
|
|
|
content.addClass("could-be-condensed");
|
|
|
|
} else {
|
|
|
|
content.removeClass("could-be-condensed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If message.condensed is defined, then the user has manually
|
|
|
|
// specified whether this message should be expanded or condensed.
|
|
|
|
if (message.condensed === true) {
|
|
|
|
condense_row($(elem));
|
|
|
|
return;
|
|
|
|
} else if (message.condensed === false) {
|
|
|
|
uncondense_row($(elem));
|
|
|
|
return;
|
|
|
|
} else if (long_message) {
|
|
|
|
// By default, condense a long message.
|
|
|
|
condense_row($(elem));
|
|
|
|
} else {
|
|
|
|
content.removeClass('condensed');
|
|
|
|
$(elem).find(".message_expander").hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Completely hide the message and replace it with a [More]
|
|
|
|
// link if the user has collapsed it.
|
|
|
|
if (message.collapsed) {
|
|
|
|
content.addClass("collapsed");
|
|
|
|
$(elem).find(".message_expander").show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2016-12-02 14:06:06 +01:00
|
|
|
$("#home").on("click", ".message_expander", function () {
|
2014-03-13 20:22:09 +01:00
|
|
|
// Expanding a message can mean either uncollapsing or
|
|
|
|
// uncondensing it.
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(this).closest(".message_row");
|
|
|
|
const message = current_msg_list.get(rows.id(row));
|
|
|
|
const content = row.find(".message_content");
|
2014-03-13 20:22:09 +01:00
|
|
|
if (message.collapsed) {
|
|
|
|
// Uncollapse.
|
|
|
|
exports.uncollapse(row);
|
|
|
|
} else if (content.hasClass("condensed")) {
|
|
|
|
// Uncondense (show the full long message).
|
|
|
|
message.condensed = false;
|
|
|
|
content.removeClass("condensed");
|
|
|
|
$(this).hide();
|
|
|
|
row.find(".message_condenser").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-02 14:06:06 +01:00
|
|
|
$("#home").on("click", ".message_condenser", function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(this).closest(".message_row");
|
2014-03-13 20:22:09 +01:00
|
|
|
current_msg_list.get(rows.id(row)).condensed = true;
|
|
|
|
condense_row(row);
|
|
|
|
});
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2014-03-13 20:22:09 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.condense = exports;
|