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
|
|
|
|
2017-02-24 01:20:49 +01:00
|
|
|
exports.should_fade_message = function (message) {
|
|
|
|
return !util.same_recipient(focused_recipient, message);
|
|
|
|
};
|
2014-02-12 21:07:22 +01:00
|
|
|
|
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 = {
|
2017-01-12 00:17:43 +01:00
|
|
|
type: msg_type,
|
2013-08-11 23:54:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (focused_recipient.type === "stream") {
|
2017-02-23 03:53:02 +01:00
|
|
|
var stream_name = $('#stream').val();
|
2013-08-11 23:54:50 +02:00
|
|
|
focused_recipient.subject = $('#subject').val();
|
2017-02-23 03:53:02 +01:00
|
|
|
focused_recipient.stream = stream_name;
|
|
|
|
var sub = stream_data.get_sub(stream_name);
|
|
|
|
if (sub) {
|
|
|
|
focused_recipient.stream_id = sub.stream_id;
|
|
|
|
}
|
2013-08-11 23:54:50 +02:00
|
|
|
} 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()).
|
2018-03-20 16:32:05 +01:00
|
|
|
var reply_to = util.normalize_recipients(compose_state.recipient());
|
2017-02-25 00:30:51 +01:00
|
|
|
focused_recipient.reply_to = reply_to;
|
|
|
|
focused_recipient.to_user_ids = people.reply_to_to_user_ids_string(reply_to);
|
2013-08-11 23:54:50 +02:00
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
};
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function display_messages_normally() {
|
2017-03-16 19:13:43 +01:00
|
|
|
var table = rows.get_table(current_msg_list.table_name);
|
2017-03-16 19:26:26 +01:00
|
|
|
table.find('.recipient_row').removeClass("message-fade");
|
2013-08-11 21:34:05 +02:00
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
normal_display = true;
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
function change_fade_state(elt, should_fade_group) {
|
|
|
|
if (should_fade_group) {
|
2017-03-16 19:26:26 +01:00
|
|
|
elt.addClass("message-fade");
|
2013-10-30 19:53:17 +01:00
|
|
|
} else {
|
2017-03-16 19:26:26 +01:00
|
|
|
elt.removeClass("message-fade");
|
2013-10-30 19:53:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function fade_messages() {
|
2016-12-02 17:09:31 +01:00
|
|
|
var i;
|
|
|
|
var first_message;
|
|
|
|
var first_row;
|
2014-02-26 17:22:21 +01:00
|
|
|
var should_fade_group = false;
|
2017-03-10 23:48:51 +01:00
|
|
|
var visible_groups = message_viewport.visible_groups(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
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < visible_groups.length; i += 1) {
|
2014-02-26 17:22:21 +01:00
|
|
|
first_row = rows.first_message_in_group(visible_groups[i]);
|
|
|
|
first_message = current_msg_list.get(rows.id(first_row));
|
2017-02-24 01:20:49 +01:00
|
|
|
should_fade_group = exports.should_fade_message(first_message);
|
2013-08-11 21:21:47 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
change_fade_state($(visible_groups[i]), should_fade_group);
|
2013-08-11 21:21:47 +02:00
|
|
|
}
|
2013-08-16 00:04:12 +02:00
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
// Defer updating all message groups so that the compose box can open sooner
|
2013-10-30 19:53:17 +01:00
|
|
|
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 ||
|
2017-03-18 18:48:43 +01:00
|
|
|
!compose_state.composing() ||
|
|
|
|
compose_state.recipient() !== expected_recipient) {
|
2013-10-30 19:53:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 17:22:21 +01:00
|
|
|
should_fade_group = false;
|
2013-10-30 19:53:17 +01:00
|
|
|
|
|
|
|
// Note: The below algorithm relies on the fact that all_elts is
|
|
|
|
// sorted as it would be displayed in the message view
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < all_groups.length; i += 1) {
|
2014-02-05 16:55:24 +01:00
|
|
|
var group_elt = $(all_groups[i]);
|
2017-02-24 01:20:49 +01:00
|
|
|
should_fade_group = exports.should_fade_message(rows.recipient_from_group(group_elt));
|
2014-02-26 17:22:21 +01:00
|
|
|
change_fade_state(group_elt, should_fade_group);
|
2013-10-30 19:53:17 +01:00
|
|
|
}
|
2014-02-21 21:10:09 +01:00
|
|
|
|
2014-03-13 21:14:33 +01:00
|
|
|
floating_recipient_bar.update();
|
2017-03-18 18:48:43 +01:00
|
|
|
}, 0, current_msg_list, compose_state.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).
|
|
|
|
//
|
2017-03-16 19:13:43 +01:00
|
|
|
// The distinction between undefined and true is historical. We really
|
|
|
|
// only ever fade stuff if would_receive_message() returns false; i.e.
|
|
|
|
// we are **sure** that you would **not** receive the message.
|
2013-09-07 04:22:18 +02:00
|
|
|
|
2017-01-19 20:18:03 +01:00
|
|
|
if (people.is_current_user(email)) {
|
2013-09-07 04:22:18 +02:00
|
|
|
// We never want to fade you yourself, so pretend it's true even if
|
|
|
|
// it's not.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focused_recipient.type === 'stream') {
|
2017-10-26 18:52:42 +02:00
|
|
|
var user = people.get_active_user_for_email(email);
|
2013-10-18 16:51:26 +02:00
|
|
|
var sub = stream_data.get_sub(focused_recipient.stream);
|
2018-04-06 05:22:07 +02:00
|
|
|
if (!sub || !user) {
|
|
|
|
// If the stream or user isn't valid, there is no risk of a mix
|
2017-03-04 22:43:15 +01:00
|
|
|
// yet, so don't fade.
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2017-03-04 22:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user && user.is_bot && !sub.invite_only) {
|
2013-10-18 16:51:26 +02:00
|
|
|
// Bots may receive messages on public streams even if they are
|
|
|
|
// not subscribed.
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2013-10-18 16:51:26 +02:00
|
|
|
}
|
2018-04-06 05:22:07 +02:00
|
|
|
return stream_data.is_user_subscribed(focused_recipient.stream, user.user_id);
|
2013-09-07 04:22:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PM, so check if the given email is in the recipients list.
|
2016-06-08 06:06:57 +02:00
|
|
|
return util.is_pm_recipient(email, focused_recipient);
|
2013-09-16 21:13:11 +02:00
|
|
|
};
|
2013-09-07 04:22:18 +02:00
|
|
|
|
2018-04-22 14:12:08 +02:00
|
|
|
var user_fade_config = {
|
|
|
|
get_user_id: function (li) {
|
|
|
|
return buddy_list.get_key_from_li({li: li});
|
|
|
|
},
|
|
|
|
fade: function (li) {
|
|
|
|
return li.addClass('user-fade');
|
|
|
|
},
|
|
|
|
unfade: function (li) {
|
|
|
|
return li.removeClass('user-fade');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function update_user_row_when_fading(li, conf) {
|
|
|
|
var user_id = conf.get_user_id(li);
|
2017-02-26 23:40:40 +01:00
|
|
|
var email = people.get_person_from_user_id(user_id).email;
|
|
|
|
var would_receive = exports.would_receive_message(email);
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2017-03-16 19:13:43 +01:00
|
|
|
if (would_receive === false) {
|
2018-04-22 14:12:08 +02:00
|
|
|
conf.fade(li);
|
2017-02-26 23:40:40 +01:00
|
|
|
} else {
|
2017-03-16 19:13:43 +01:00
|
|
|
// would_receive is either true (so definitely don't fade) or
|
|
|
|
// undefined (in which case we don't presume to fade)
|
2018-04-22 14:12:08 +02:00
|
|
|
conf.unfade(li);
|
2017-02-26 23:40:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 14:12:08 +02:00
|
|
|
function display_users_normally(items, conf) {
|
|
|
|
_.each(items, function (li) {
|
|
|
|
conf.unfade(li);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fade_users(items, conf) {
|
|
|
|
_.each(items, function (li) {
|
|
|
|
update_user_row_when_fading(li, conf);
|
2013-09-07 04:22:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
function want_normal_display() {
|
2013-08-12 17:40:55 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-04-22 16:25:46 +02:00
|
|
|
function do_update_all() {
|
2018-04-22 14:12:08 +02:00
|
|
|
var user_items = buddy_list.get_items();
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2013-08-12 17:40:55 +02:00
|
|
|
if (!normal_display) {
|
2018-04-22 16:30:58 +02:00
|
|
|
display_messages_normally();
|
2018-04-22 14:12:08 +02:00
|
|
|
display_users_normally(user_items, user_fade_config);
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2018-04-22 16:30:58 +02:00
|
|
|
fade_messages();
|
2018-04-22 14:12:08 +02:00
|
|
|
fade_users(user_items, user_fade_config);
|
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 () {
|
2018-04-22 14:12:08 +02:00
|
|
|
var user_items = buddy_list.get_items();
|
|
|
|
|
2018-04-22 17:46:20 +02:00
|
|
|
exports.update_user_info(user_items, user_fade_config);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_user_info = function (items, conf) {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2018-04-22 17:46:20 +02:00
|
|
|
display_users_normally(items, conf);
|
2013-09-07 04:22:18 +02:00
|
|
|
} else {
|
2018-04-22 17:46:20 +02:00
|
|
|
fade_users(items, conf);
|
2013-09-07 04:22:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-22 16:25:46 +02:00
|
|
|
// This gets called on keyup events, hence the throttling.
|
|
|
|
exports.update_all = _.debounce(do_update_all, 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);
|
2018-04-22 16:25:46 +02:00
|
|
|
do_update_all();
|
2013-08-12 00:08:36 +02:00
|
|
|
};
|
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
exports.clear_compose = function () {
|
|
|
|
focused_recipient = undefined;
|
2018-04-22 16:30:58 +02:00
|
|
|
display_messages_normally();
|
2018-04-22 14:12:08 +02:00
|
|
|
exports.update_faded_users();
|
2013-08-12 17:40:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_message_list = function () {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
|
|
|
display_messages_normally();
|
2016-06-09 23:02:49 +02:00
|
|
|
} else {
|
2018-04-22 16:30:58 +02:00
|
|
|
fade_messages();
|
2013-08-12 17:40:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-21 21:10:09 +01:00
|
|
|
exports.update_rendered_message_groups = function (message_groups, get_element) {
|
2018-04-22 16:30:58 +02:00
|
|
|
if (want_normal_display()) {
|
2013-08-13 17:18:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-22 16:30:58 +02:00
|
|
|
// This loop is superficially similar to some code in fade_messages, but an
|
2013-08-13 17:18:28 +02:00
|
|
|
// important difference here is that we look at each message individually, whereas
|
|
|
|
// the other code takes advantage of blocks beneath recipient bars.
|
2014-02-21 21:10:09 +01:00
|
|
|
_.each(message_groups, function (message_group) {
|
|
|
|
var elt = get_element(message_group);
|
2014-03-18 21:13:04 +01:00
|
|
|
var first_message = message_group.message_containers[0].msg;
|
2017-02-24 01:20:49 +01:00
|
|
|
var should_fade = exports.should_fade_message(first_message);
|
2014-02-21 21:10:09 +01:00
|
|
|
change_fade_state(elt, should_fade);
|
2013-08-13 17:18:28 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-24 22:30:32 +02:00
|
|
|
exports.initialize = function () {
|
2016-12-02 14:06:06 +01:00
|
|
|
$(document).on('peer_subscribe.zulip', function () {
|
2013-09-16 21:13:11 +02:00
|
|
|
exports.update_faded_users();
|
|
|
|
});
|
2016-12-02 14:06:06 +01:00
|
|
|
$(document).on('peer_unsubscribe.zulip', function () {
|
2013-09-16 21:13:11 +02:00
|
|
|
exports.update_faded_users();
|
|
|
|
});
|
2017-05-24 22:30:32 +02:00
|
|
|
};
|
2013-09-16 21:13:11 +02:00
|
|
|
|
|
|
|
|
2013-08-11 21:21:47 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = compose_fade;
|
|
|
|
}
|