2013-08-11 21:21:47 +02:00
|
|
|
var compose_fade = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var focused_recipient;
|
2013-08-12 17:40:55 +02:00
|
|
|
var normal_display = false;
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2014-02-12 21:07:22 +01:00
|
|
|
var fade_heuristic = feature_flags.fade_at_stream_granularity ?
|
|
|
|
util.same_major_recipient : util.same_recipient;
|
|
|
|
|
2013-08-11 23:54:50 +02:00
|
|
|
exports.set_focused_recipient = function (msg_type) {
|
2013-08-12 17:40:55 +02:00
|
|
|
if (msg_type === undefined) {
|
|
|
|
focused_recipient = undefined;
|
|
|
|
}
|
|
|
|
|
2013-08-11 23:54:50 +02:00
|
|
|
// Construct focused_recipient as a mocked up element which has all the
|
|
|
|
// fields of a message used by util.same_recipient()
|
|
|
|
focused_recipient = {
|
|
|
|
type: msg_type
|
|
|
|
};
|
|
|
|
|
|
|
|
if (focused_recipient.type === "stream") {
|
|
|
|
focused_recipient.stream = $('#stream').val();
|
|
|
|
focused_recipient.subject = $('#subject').val();
|
|
|
|
} else {
|
|
|
|
// Normalize the recipient list so it matches the one used when
|
2014-01-31 16:27:24 +01:00
|
|
|
// adding the message (see message_store.add_message_metadata()).
|
2013-08-11 23:54:50 +02:00
|
|
|
focused_recipient.reply_to = util.normalize_recipients(
|
|
|
|
$('#private_message_recipient').val());
|
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
};
|
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
function _display_messages_normally() {
|
2013-08-11 21:21:47 +02:00
|
|
|
rows.get_table(current_msg_list.table_name).find(".recipient_row, .message_row")
|
2013-08-12 17:40:55 +02:00
|
|
|
.removeClass("faded").removeClass("unfaded");
|
2013-08-11 21:34:05 +02:00
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
normal_display = true;
|
2013-08-16 00:04:12 +02:00
|
|
|
ui.update_floating_recipient_bar();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2013-09-07 04:22:18 +02:00
|
|
|
function _display_users_normally() {
|
|
|
|
if (!feature_flags.fade_users_when_composing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$('.user_sidebar_entry').removeClass('faded').removeClass('unfaded');
|
|
|
|
}
|
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
function change_fade_state(elt, should_fade_message) {
|
|
|
|
if (should_fade_message) {
|
|
|
|
elt.removeClass("unfaded").addClass("faded");
|
|
|
|
} else {
|
|
|
|
elt.removeClass("faded").addClass("unfaded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
function _fade_messages() {
|
2013-08-11 21:21:47 +02:00
|
|
|
var i;
|
|
|
|
var should_fade_message = false;
|
2013-10-30 19:53:17 +01:00
|
|
|
var visible_messages = viewport.visible_messages(false);
|
2013-08-12 17:40:55 +02:00
|
|
|
|
|
|
|
normal_display = false;
|
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
// Update the visible messages first, before the compose box opens
|
|
|
|
for (i = 0; i < visible_messages.length; i++) {
|
2014-02-12 21:07:22 +01:00
|
|
|
should_fade_message = !fade_heuristic(focused_recipient, visible_messages[i]);
|
2013-10-30 19:53:17 +01:00
|
|
|
var elt = current_msg_list.get_row(visible_messages[i].id);
|
2014-02-05 16:55:24 +01:00
|
|
|
var recipient_row = rows.get_message_recipient_row(elt);
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2013-10-30 19:53:17 +01:00
|
|
|
change_fade_state(elt, should_fade_message);
|
|
|
|
change_fade_state(recipient_row, should_fade_message);
|
2013-08-11 21:21:47 +02:00
|
|
|
}
|
2013-08-16 00:04:12 +02:00
|
|
|
|
|
|
|
ui.update_floating_recipient_bar();
|
2013-10-30 19:53:17 +01:00
|
|
|
|
|
|
|
// Defer updating all messages so that the compose box can open sooner
|
|
|
|
setTimeout(function (expected_msg_list, expected_recipient) {
|
2014-02-05 16:55:24 +01:00
|
|
|
var all_groups = rows.get_table(current_msg_list.table_name).find(".recipient_row");
|
2013-10-30 19:53:17 +01:00
|
|
|
|
|
|
|
if (current_msg_list !== expected_msg_list ||
|
2013-11-05 23:45:44 +01:00
|
|
|
!compose.composing() ||
|
2013-10-30 19:53:17 +01:00
|
|
|
compose.recipient() !== expected_recipient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-02-05 16:55:24 +01:00
|
|
|
for (i = 0; i < all_groups.length; i++) {
|
|
|
|
var group_elt = $(all_groups[i]);
|
|
|
|
should_fade_message = !fade_heuristic(focused_recipient, rows.recipient_from_group(group_elt));
|
|
|
|
change_fade_state(group_elt, should_fade_message);
|
2013-10-30 19:53:17 +01:00
|
|
|
}
|
|
|
|
}, 0, current_msg_list, compose.recipient());
|
2013-08-11 21:21:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:13:11 +02:00
|
|
|
exports.would_receive_message = function (email) {
|
2013-09-07 04:22:18 +02:00
|
|
|
// Given the current focused_recipient, this function returns true if
|
|
|
|
// the user in question would definitely receive this message, false if
|
|
|
|
// they would definitely not receive this message, and undefined if we
|
|
|
|
// don't know (e.g. the recipient is a stream we're not subscribed to).
|
|
|
|
//
|
|
|
|
// Yes it's slightly weird to have three return values, but this will be
|
|
|
|
// helpful if we want to emphasize the '.unfaded' class later (applied
|
|
|
|
// to users who will definitely receive the message).
|
|
|
|
|
|
|
|
if (email === page_params.email) {
|
|
|
|
// We never want to fade you yourself, so pretend it's true even if
|
|
|
|
// it's not.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focused_recipient.type === 'stream') {
|
2014-01-30 22:42:19 +01:00
|
|
|
var user = people.realm_get(email);
|
2013-10-18 16:51:26 +02:00
|
|
|
var sub = stream_data.get_sub(focused_recipient.stream);
|
|
|
|
if (user && sub && user.is_bot && !sub.invite_only) {
|
|
|
|
// Bots may receive messages on public streams even if they are
|
|
|
|
// not subscribed.
|
|
|
|
return undefined;
|
|
|
|
}
|
2013-09-07 04:22:18 +02:00
|
|
|
return stream_data.user_is_subscribed(focused_recipient.stream, email);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PM, so check if the given email is in the recipients list.
|
|
|
|
var recipients = focused_recipient.reply_to.split(',');
|
|
|
|
return recipients.indexOf(email) !== -1;
|
2013-09-16 21:13:11 +02:00
|
|
|
};
|
2013-09-07 04:22:18 +02:00
|
|
|
|
|
|
|
function _fade_users() {
|
|
|
|
if (!feature_flags.fade_users_when_composing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.forEach($('.user_sidebar_entry'), function (elt) {
|
|
|
|
elt = $(elt);
|
2013-09-16 21:13:11 +02:00
|
|
|
var would_receive = exports.would_receive_message(elt.attr('data-email'));
|
2013-09-07 04:22:18 +02:00
|
|
|
if (would_receive === true) {
|
|
|
|
elt.addClass('unfaded').removeClass('faded');
|
|
|
|
} else if (would_receive === false) {
|
|
|
|
elt.addClass('faded').removeClass('unfaded');
|
|
|
|
} else {
|
|
|
|
elt.removeClass('faded').removeClass('unfaded');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
function _want_normal_display() {
|
|
|
|
// If we're not composing show a normal display.
|
|
|
|
if (focused_recipient === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user really hasn't specified anything let, then we want a normal display
|
2013-09-24 17:16:47 +02:00
|
|
|
if (focused_recipient.type === "stream") {
|
|
|
|
// If a stream doesn't exist, there is no real chance of a mix, so fading
|
|
|
|
// is just noise to the user.
|
|
|
|
if (!stream_data.get_sub(focused_recipient.stream)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is kind of debatable. If the topic is empty, it could be that
|
|
|
|
// the user simply hasn't started typing it yet, but disabling fading here
|
|
|
|
// means the feature doesn't help realms where topics aren't mandatory
|
|
|
|
// (which is most realms as of this writing).
|
|
|
|
if (focused_recipient.subject === "") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 17:35:24 +01:00
|
|
|
return focused_recipient.type === "private" && focused_recipient.reply_to === "";
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function _update_faded_messages() {
|
|
|
|
// See also update_faded_messages(), which just wraps this with a debounce.
|
2013-09-07 04:22:18 +02:00
|
|
|
// FIXME: This fades users too now, as well as messages, so should have
|
|
|
|
// a better name.
|
2013-08-12 17:40:55 +02:00
|
|
|
if (_want_normal_display()) {
|
|
|
|
if (!normal_display) {
|
|
|
|
_display_messages_normally();
|
2013-09-07 04:22:18 +02:00
|
|
|
_display_users_normally();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_fade_messages();
|
2013-09-07 04:22:18 +02:00
|
|
|
_fade_users();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 04:22:18 +02:00
|
|
|
// This one only updates the users, not both, like update_faded_messages.
|
|
|
|
// This is for when new presence information comes in, redrawing the presence
|
|
|
|
// list.
|
|
|
|
exports.update_faded_users = function () {
|
|
|
|
if (_want_normal_display()) {
|
|
|
|
_display_users_normally();
|
|
|
|
} else {
|
|
|
|
_fade_users();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-11 21:21:47 +02:00
|
|
|
// See trac #1633. For fast typists, calls to _update_faded_messages can
|
|
|
|
// cause typing sluggishness.
|
2013-08-13 22:15:27 +02:00
|
|
|
exports.update_faded_messages = _.debounce(_update_faded_messages, 50);
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
exports.start_compose = function (msg_type) {
|
2013-08-12 00:08:36 +02:00
|
|
|
exports.set_focused_recipient(msg_type);
|
|
|
|
_update_faded_messages();
|
|
|
|
};
|
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
exports.clear_compose = function () {
|
|
|
|
focused_recipient = undefined;
|
|
|
|
_display_messages_normally();
|
2013-09-07 04:22:18 +02:00
|
|
|
_display_users_normally();
|
2013-08-12 17:40:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_message_list = function () {
|
|
|
|
if (_want_normal_display()) {
|
|
|
|
_display_messages_normally();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_fade_messages();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-14 17:48:55 +02:00
|
|
|
exports.update_rendered_messages = function (messages, get_elements) {
|
2013-08-13 17:18:28 +02:00
|
|
|
if (_want_normal_display()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This loop is superficially similar to some code in _fade_messages, but an
|
|
|
|
// important difference here is that we look at each message individually, whereas
|
|
|
|
// the other code takes advantage of blocks beneath recipient bars.
|
2013-08-14 17:48:55 +02:00
|
|
|
//
|
|
|
|
// get_elements() is plural, because we can get up to two elements:
|
|
|
|
// the message (always)
|
|
|
|
// the recipient bar (sometimes)
|
2013-08-13 17:18:28 +02:00
|
|
|
_.each(messages, function (message) {
|
2013-08-14 17:48:55 +02:00
|
|
|
var elts = get_elements(message);
|
2014-02-12 21:07:22 +01:00
|
|
|
var should_fade_message = !fade_heuristic(focused_recipient, message);
|
2013-08-13 17:18:28 +02:00
|
|
|
|
2013-08-14 17:48:55 +02:00
|
|
|
_.each(elts, function (elt) {
|
2013-10-30 19:53:17 +01:00
|
|
|
change_fade_state(elt, should_fade_message);
|
2013-08-14 17:48:55 +02:00
|
|
|
});
|
2013-08-13 17:18:28 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-16 21:13:11 +02:00
|
|
|
$(function () {
|
|
|
|
$(document).on('peer_subscribe.zulip', function (e) {
|
|
|
|
exports.update_faded_users();
|
|
|
|
});
|
|
|
|
$(document).on('peer_unsubscribe.zulip', function (e) {
|
|
|
|
exports.update_faded_users();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-08-11 21:21:47 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|