2012-10-18 20:12:04 +02:00
|
|
|
var narrow = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-10-04 07:13:28 +02:00
|
|
|
// For tracking where you were before you narrowed.
|
2012-10-10 00:10:05 +02:00
|
|
|
var persistent_message_id = 0;
|
2012-10-04 07:13:28 +02:00
|
|
|
|
2012-10-15 18:57:24 +02:00
|
|
|
// For narrowing based on a particular message
|
2012-10-18 20:12:04 +02:00
|
|
|
var target_id = 0;
|
2012-10-15 18:57:24 +02:00
|
|
|
|
2012-10-03 20:49:58 +02:00
|
|
|
// Narrowing predicate, or 'false' for the home view.
|
|
|
|
var narrowed = false;
|
|
|
|
|
2012-10-24 00:29:06 +02:00
|
|
|
// What sort of narrowing is currently active
|
|
|
|
var narrow_type = '';
|
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.active = function () {
|
|
|
|
// Cast to bool
|
|
|
|
return !!narrowed;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.predicate = function () {
|
|
|
|
if (narrowed) {
|
|
|
|
return narrowed;
|
|
|
|
} else {
|
|
|
|
return function () { return true; };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-24 00:29:06 +02:00
|
|
|
exports.narrowing_type = function () {
|
|
|
|
return narrow_type;
|
|
|
|
};
|
|
|
|
|
2012-10-24 02:21:51 +02:00
|
|
|
function do_narrow(icon, description, filter_function) {
|
2012-10-03 20:49:58 +02:00
|
|
|
narrowed = filter_function;
|
|
|
|
|
|
|
|
// Your pointer isn't changed when narrowed.
|
2012-10-10 00:10:05 +02:00
|
|
|
persistent_message_id = selected_message_id;
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-26 16:59:38 +02:00
|
|
|
// Before we clear the table, check if anything was highlighted.
|
|
|
|
var highlighted = something_is_highlighted();
|
|
|
|
|
2012-10-03 20:49:58 +02:00
|
|
|
// Empty the filtered table right before we fill it again
|
|
|
|
clear_table('zfilt');
|
2012-10-09 23:48:41 +02:00
|
|
|
add_to_table(message_array, 'zfilt', filter_function, 'bottom');
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
// Show the new set of messages.
|
|
|
|
$("#zfilt").addClass("focused_table");
|
|
|
|
|
|
|
|
$("#show_all_messages").removeAttr("disabled");
|
2012-10-15 21:11:16 +02:00
|
|
|
$(".narrowed_to_bar").show();
|
2012-10-23 19:24:18 +02:00
|
|
|
$("#loading_control").hide();
|
2012-10-26 16:59:38 +02:00
|
|
|
$("#top_narrowed_whitespace").show();
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#main_div").addClass("narrowed_view");
|
2012-10-26 19:55:39 +02:00
|
|
|
$("#searchbox").addClass("narrowed_view");
|
2012-10-24 02:30:58 +02:00
|
|
|
$("#currently_narrowed_to").html(icon + " " + description).attr("title", description.replace(/ /g, ""));
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#zhome").removeClass("focused_table");
|
2012-10-15 19:24:34 +02:00
|
|
|
// Indicate both which message is persistently selected and which
|
|
|
|
// is temporarily selected
|
2012-10-26 17:44:22 +02:00
|
|
|
select_message_by_id(selected_message_id,
|
|
|
|
{then_scroll: false, update_server: false});
|
2012-10-15 19:24:34 +02:00
|
|
|
selected_message_class = "narrowed_selected_message";
|
2012-10-26 17:44:22 +02:00
|
|
|
select_message_by_id(target_id,
|
|
|
|
{then_scroll: true, update_server: false});
|
2012-10-26 16:59:38 +02:00
|
|
|
|
|
|
|
// If anything was highlighted before, try to rehighlight it.
|
|
|
|
if (highlighted) {
|
|
|
|
update_highlight_on_narrow();
|
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 18:57:04 +02:00
|
|
|
// This is the message we're about to select, within the narrowed view.
|
|
|
|
// But it won't necessarily be selected once the user un-narrows.
|
|
|
|
//
|
|
|
|
// FIXME: We probably don't need this variable, selected_message_id, *and*
|
|
|
|
// persistent_message_id.
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.target = function (id) {
|
|
|
|
target_id = id;
|
|
|
|
};
|
2012-10-15 18:57:24 +02:00
|
|
|
|
2012-10-24 02:43:23 +02:00
|
|
|
exports.all_huddles = function () {
|
|
|
|
narrow_type = "all_huddles";
|
2012-10-24 02:21:51 +02:00
|
|
|
do_narrow("<i class='icon-user'></i>", "You and anyone else", function (other) {
|
2012-10-03 20:49:58 +02:00
|
|
|
return other.type === "personal" || other.type === "huddle";
|
|
|
|
});
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.by_subject = function () {
|
|
|
|
var original = message_dict[target_id];
|
2012-10-24 05:04:42 +02:00
|
|
|
if (original.type !== 'stream') {
|
|
|
|
// Only stream messages have subjects, but the
|
|
|
|
// user wants us to narrow in some way.
|
|
|
|
exports.by_recipient();
|
2012-10-03 20:49:58 +02:00
|
|
|
return;
|
2012-10-24 05:04:42 +02:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-24 02:21:51 +02:00
|
|
|
var icon = "<i class='icon-bullhorn'></i>";
|
|
|
|
var message = original.display_recipient + " | " + original.subject;
|
2012-10-24 00:29:06 +02:00
|
|
|
narrow_type = "subject";
|
2012-10-24 02:21:51 +02:00
|
|
|
do_narrow(icon, message, function (other) {
|
2012-10-10 23:09:16 +02:00
|
|
|
return (other.type === 'stream' &&
|
2012-10-03 20:49:58 +02:00
|
|
|
original.recipient_id === other.recipient_id &&
|
2012-10-11 00:01:39 +02:00
|
|
|
original.subject === other.subject);
|
2012-10-03 20:49:58 +02:00
|
|
|
});
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-10 23:24:11 +02:00
|
|
|
// Called for the 'narrow by stream' hotkey.
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.by_recipient = function () {
|
2012-10-19 19:00:46 +02:00
|
|
|
var message = message_dict[target_id];
|
|
|
|
switch (message.type) {
|
2012-10-19 17:11:31 +02:00
|
|
|
case 'personal':
|
|
|
|
// Narrow to personals with a specific user
|
2012-10-24 00:29:06 +02:00
|
|
|
narrow_type = "huddle";
|
2012-10-24 02:21:51 +02:00
|
|
|
do_narrow("<i class='icon-user'></i>", "You and " + message.display_reply_to, function (other) {
|
2012-10-19 17:11:31 +02:00
|
|
|
return (other.type === 'personal') &&
|
2012-10-19 19:00:46 +02:00
|
|
|
(((other.display_recipient.email === message.display_recipient.email)
|
|
|
|
&& (other.sender_email === message.sender_email)) ||
|
|
|
|
((other.display_recipient.email === message.sender_email)
|
|
|
|
&& (other.sender_email === message.display_recipient.email)));
|
2012-10-19 17:11:31 +02:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'huddle':
|
2012-10-24 00:29:06 +02:00
|
|
|
narrow_type = "huddle";
|
2012-10-24 02:21:51 +02:00
|
|
|
do_narrow("<i class='icon-user'></i>", "You and " + message.display_reply_to, function (other) {
|
2012-10-19 17:11:31 +02:00
|
|
|
return (other.type === "personal" || other.type === "huddle")
|
2012-10-19 19:00:46 +02:00
|
|
|
&& other.reply_to === message.reply_to;
|
2012-10-19 17:11:31 +02:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
2012-10-24 00:29:06 +02:00
|
|
|
narrow_type = "stream";
|
2012-10-24 02:21:51 +02:00
|
|
|
do_narrow("<i class='icon-bullhorn'></i>", message.display_recipient, function (other) {
|
2012-10-19 17:11:31 +02:00
|
|
|
return (other.type === 'stream' &&
|
2012-10-19 19:00:46 +02:00
|
|
|
message.recipient_id === other.recipient_id);
|
2012-10-19 17:11:31 +02:00
|
|
|
});
|
|
|
|
break;
|
2012-10-03 20:49:58 +02:00
|
|
|
}
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.show_all_messages = function () {
|
2012-10-03 20:49:58 +02:00
|
|
|
if (!narrowed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
narrowed = false;
|
2012-10-24 00:29:06 +02:00
|
|
|
narrow_type = "";
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
$("#zfilt").removeClass('focused_table');
|
|
|
|
$("#zhome").addClass('focused_table');
|
2012-10-15 21:11:16 +02:00
|
|
|
$(".narrowed_to_bar").hide();
|
2012-10-23 19:24:18 +02:00
|
|
|
$("#loading_control").show();
|
2012-10-26 16:59:38 +02:00
|
|
|
$("#top_narrowed_whitespace").hide();
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#main_div").removeClass('narrowed_view');
|
2012-10-26 19:55:39 +02:00
|
|
|
$("#searchbox").removeClass('narrowed_view');
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#show_all_messages").attr("disabled", "disabled");
|
|
|
|
$("#currently_narrowed_to").html("");
|
2012-10-15 19:24:34 +02:00
|
|
|
selected_message_class = "selected_message";
|
2012-10-03 20:49:58 +02:00
|
|
|
// Includes scrolling.
|
2012-10-22 19:05:06 +02:00
|
|
|
select_message_by_id(persistent_message_id, {then_scroll: true});
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
scroll_to_selected();
|
2012-10-26 16:59:38 +02:00
|
|
|
|
|
|
|
update_highlight_on_narrow();
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|