Extracted code into compose_fade.js.

The compose_fade has three public exports:

    set_focused_recipient
    unfade_messages
    update_faded_messages

All code was pulled directly from compose.js, except for the
one-line setter of set_focused_recipient.  The focused_recipients
variable that used to be in compose.js was moved to compose_fade.js,
hence the need for the setter.

(imported from commit 462ca5d0d0bd58612d0197f3734a8c78de8c6d30)
This commit is contained in:
Steve Howell
2013-08-11 15:21:47 -04:00
parent 0dcaf9ca3d
commit 37f8cc9294
7 changed files with 85 additions and 71 deletions

71
static/js/compose_fade.js Normal file
View File

@@ -0,0 +1,71 @@
var compose_fade = (function () {
var exports = {};
var focused_recipient;
exports.set_focused_recipient = function (recipient) {
focused_recipient = recipient;
};
exports.unfade_messages = function (clear_state) {
if (focused_recipient === undefined) {
return;
}
rows.get_table(current_msg_list.table_name).find(".recipient_row, .message_row")
.removeClass("faded").addClass("unfaded");
if (clear_state === true) {
focused_recipient = undefined;
}
ui.update_floating_recipient_bar();
};
function _update_faded_messages() {
// See also update_faded_messages(), which just wraps this with a debounce.
if (focused_recipient === undefined) {
return;
}
if ((focused_recipient.type === "stream" && focused_recipient.subject === "") ||
(focused_recipient.type === "private" && focused_recipient.reply_to === "")) {
exports.unfade_messages();
return;
}
var i;
var all_elts = rows.get_table(current_msg_list.table_name).find(".recipient_row, .message_row");
var should_fade_message = false;
// Note: The below algorithm relies on the fact that all_elts is
// sorted as it would be displayed in the message view
for (i = 0; i < all_elts.length; i++) {
var elt = $(all_elts[i]);
if (elt.hasClass("recipient_row")) {
should_fade_message = !util.same_recipient(focused_recipient, current_msg_list.get(rows.id(elt)));
}
// Usually we are not actually switching up the classes here, so the hasClass()
// calls here will usually short circuit two function calls that are more expensive.
// So, while the hasClass() checks are semantically unnecessary, they should improve
// performance. See trac #1633 for more context.
if (should_fade_message) {
if (!elt.hasClass("faded")) {
elt.removeClass("unfaded").addClass("faded");
}
} else {
if (!elt.hasClass("unfaded")) {
elt.removeClass("faded").addClass("unfaded");
}
}
}
ui.update_floating_recipient_bar();
}
// See trac #1633. For fast typists, calls to _update_faded_messages can
// cause typing sluggishness.
exports.update_faded_messages = _.debounce(_update_faded_messages, 150);
return exports;
}());