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-11-09 01:06:07 +01:00
|
|
|
var narrowdata = false;
|
|
|
|
var filter_function = false;
|
2012-10-24 00:29:06 +02:00
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.active = function () {
|
|
|
|
// Cast to bool
|
2012-11-09 01:06:07 +01:00
|
|
|
return !!narrowdata;
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.predicate = function () {
|
2012-11-09 01:06:07 +01:00
|
|
|
if (filter_function) {
|
|
|
|
return filter_function;
|
2012-10-18 20:12:04 +02:00
|
|
|
} else {
|
|
|
|
return function () { return true; };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-24 00:29:06 +02:00
|
|
|
exports.narrowing_type = function () {
|
2012-11-09 01:06:07 +01:00
|
|
|
if (narrowdata) {
|
|
|
|
return narrowdata.type;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-18 22:53:50 +01:00
|
|
|
exports.allow_collapse = function () {
|
|
|
|
if (narrowdata && narrowdata.allow_collapse !== undefined) {
|
|
|
|
return narrowdata.allow_collapse;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-09 01:06:07 +01:00
|
|
|
exports.data = function () {
|
|
|
|
return narrowdata;
|
2012-10-24 00:29:06 +02:00
|
|
|
};
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.hashchanged = function (hash) {
|
|
|
|
var full_names, decoded, emails;
|
|
|
|
|
|
|
|
if (hash[1] === "all_private_messages") {
|
|
|
|
exports.all_private_messages();
|
|
|
|
}
|
|
|
|
else if (hash[1] === "stream" && hash[3] === "subject") {
|
|
|
|
exports.by_stream_and_subject_names(decodeURIComponent(hash[2]),
|
|
|
|
decodeURIComponent(hash[4]));
|
|
|
|
}
|
|
|
|
else if (hash[1] === "stream") {
|
|
|
|
exports.by_stream_name(decodeURIComponent(hash[2]));
|
|
|
|
}
|
|
|
|
else if (hash[1] === "private_messages") {
|
|
|
|
decoded = decodeURIComponent(hash[2]);
|
|
|
|
emails = decoded.split(", ");
|
|
|
|
|
|
|
|
$.each(emails, function (index, email) {
|
|
|
|
$.each(people_list, function (index, person) {
|
|
|
|
if (person.email === email) {
|
|
|
|
if (full_names === undefined) {
|
|
|
|
full_names = person.full_name;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
full_names += ", " + person.full_name;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
exports.by_private_message_group(full_names, decoded);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-19 17:43:46 +01:00
|
|
|
function do_narrow(new_narrow, bar, time_travel, new_filter) {
|
2012-10-27 02:58:21 +02:00
|
|
|
var was_narrowed = exports.active();
|
|
|
|
|
2012-11-09 01:06:07 +01:00
|
|
|
narrowdata = new_narrow;
|
|
|
|
filter_function = new_filter;
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
// Your pointer isn't changed when narrowed.
|
2012-10-31 16:31:57 +01:00
|
|
|
if (! was_narrowed) {
|
|
|
|
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.
|
2012-11-14 20:52:53 +01:00
|
|
|
var highlighted = search.something_is_highlighted();
|
2012-10-26 16:59:38 +02:00
|
|
|
|
2012-10-03 20:49:58 +02:00
|
|
|
// Empty the filtered table right before we fill it again
|
2012-11-19 17:43:46 +01:00
|
|
|
if (time_travel) {
|
2012-11-27 22:30:47 +01:00
|
|
|
load_old_messages(target_id, 200, 200, function (messages) {
|
|
|
|
// We do this work inside the load_old_messages
|
|
|
|
// continuation, to shorten the window with just 1 visible message
|
|
|
|
clear_table('zfilt');
|
|
|
|
add_to_table([message_dict[target_id]], 'zfilt', filter_function, 'bottom', exports.allow_collapse());
|
|
|
|
// Select target_id so that we will correctly arrange messages
|
|
|
|
// surrounding the target message.
|
|
|
|
select_message_by_id(target_id, {then_scroll: false});
|
|
|
|
add_messages(messages, false);
|
|
|
|
}, true, true);
|
2012-11-19 17:43:46 +01:00
|
|
|
} else {
|
2012-11-27 22:30:47 +01:00
|
|
|
clear_table('zfilt');
|
2012-11-19 17:43:46 +01:00
|
|
|
add_to_table(message_array, 'zfilt', filter_function, 'bottom', exports.allow_collapse());
|
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
// Show the new set of messages.
|
|
|
|
$("#zfilt").addClass("focused_table");
|
|
|
|
|
2012-11-27 23:17:30 +01:00
|
|
|
reset_load_more_status();
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#show_all_messages").removeAttr("disabled");
|
2012-10-15 21:11:16 +02:00
|
|
|
$(".narrowed_to_bar").show();
|
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-31 20:36:03 +01:00
|
|
|
$("#currently_narrowed_to").remove();
|
|
|
|
$("#narrowlabel").append(templates.narrowbar(bar));
|
|
|
|
|
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-31 16:31:57 +01:00
|
|
|
select_message_by_id(persistent_message_id,
|
2012-11-27 21:15:29 +01:00
|
|
|
{then_scroll: false, for_narrow: false});
|
|
|
|
select_message_by_id(target_id, {then_scroll: true});
|
2012-10-26 16:59:38 +02:00
|
|
|
|
|
|
|
// If anything was highlighted before, try to rehighlight it.
|
|
|
|
if (highlighted) {
|
2012-11-14 20:52:53 +01:00
|
|
|
search.update_highlight_on_narrow();
|
2012-10-26 16:59:38 +02:00
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
}
|
|
|
|
|
2012-11-19 17:43:46 +01:00
|
|
|
exports.time_travel = function () {
|
|
|
|
var bar = {
|
|
|
|
icon: 'time',
|
|
|
|
description: 'Messages around time ' + message_dict[target_id].full_date_str
|
|
|
|
};
|
|
|
|
do_narrow({}, bar, true, function (other) {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-12-03 19:49:12 +01:00
|
|
|
exports.all_private_messages = function () {
|
2012-12-07 20:52:39 +01:00
|
|
|
hashchange.changehash("#narrow/all_private_messages");
|
|
|
|
|
2012-12-03 19:49:12 +01:00
|
|
|
var new_narrow = {type: "all_private_messages"};
|
2012-11-19 17:43:46 +01:00
|
|
|
var bar = {icon: 'user', description: 'You and anyone else'};
|
|
|
|
do_narrow(new_narrow, bar, false, function (other) {
|
2012-12-03 19:49:12 +01:00
|
|
|
return other.type === "private";
|
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.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-12-07 20:52:39 +01:00
|
|
|
exports.by_stream_and_subject_names(original.display_recipient,
|
|
|
|
original.subject);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.by_stream_and_subject_names = function (stream, subject) {
|
|
|
|
hashchange.changehash("#narrow/stream/" +
|
|
|
|
encodeURIComponent(stream) +
|
|
|
|
"/subject/" +
|
|
|
|
encodeURIComponent(subject));
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
var new_narrow = {type: "subject", stream: stream, subject: subject};
|
2012-10-31 20:36:03 +01:00
|
|
|
var bar = {
|
2012-12-07 20:52:39 +01:00
|
|
|
icon: 'bullhorn',
|
|
|
|
description: stream,
|
|
|
|
subject: subject
|
2012-10-31 20:36:03 +01:00
|
|
|
};
|
2012-11-19 17:43:46 +01:00
|
|
|
do_narrow(new_narrow, bar, false, function (other) {
|
2012-11-07 19:33:40 +01:00
|
|
|
return ((other.type === 'stream') &&
|
2012-12-07 20:52:39 +01:00
|
|
|
(other.display_recipient === stream &&
|
|
|
|
other.subject.toLowerCase() === subject.toLowerCase()));
|
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-11-15 05:21:20 +01:00
|
|
|
exports.by_stream_name = function (name) {
|
2012-12-07 20:52:39 +01:00
|
|
|
hashchange.changehash("#narrow/stream/" +
|
|
|
|
encodeURIComponent(name));
|
|
|
|
|
2012-11-15 05:21:20 +01:00
|
|
|
var new_narrow = {type: "stream", stream: name};
|
|
|
|
var bar = {icon: 'bullhorn', description: name};
|
2012-11-19 17:43:46 +01:00
|
|
|
do_narrow(new_narrow, bar, false, function (other) {
|
2012-11-15 05:21:20 +01:00
|
|
|
return (other.type === 'stream' &&
|
|
|
|
name === other.display_recipient);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.by_private_message_group = function (names, emails) {
|
|
|
|
hashchange.changehash("#narrow/private_messages/" +
|
|
|
|
encodeURIComponent(emails));
|
|
|
|
|
|
|
|
var new_narrow = {type: "private", emails: emails.split(", ")};
|
|
|
|
var bar = {icon: 'user', description: "You and " + names};
|
2012-11-15 16:57:59 +01:00
|
|
|
var my_email = email;
|
2012-11-19 17:43:46 +01:00
|
|
|
do_narrow(new_narrow, bar, false, function (other) {
|
2012-12-03 19:49:12 +01:00
|
|
|
return (other.type === 'private' &&
|
2012-12-07 20:52:39 +01:00
|
|
|
other.reply_to === emails);
|
2012-11-15 16:57:59 +01: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];
|
2012-12-07 20:52:39 +01:00
|
|
|
var bar, new_narrow, emails;
|
2012-10-19 19:00:46 +02:00
|
|
|
switch (message.type) {
|
2012-12-03 19:49:12 +01:00
|
|
|
case 'private':
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.by_private_message_group(message.display_reply_to,
|
|
|
|
message.reply_to);
|
2012-10-19 17:11:31 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
2012-12-07 20:52:39 +01:00
|
|
|
exports.by_stream_name(message.display_recipient);
|
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-11-18 22:53:50 +01:00
|
|
|
exports.by_search_term = function (term) {
|
2012-12-07 20:52:39 +01:00
|
|
|
hashchange.changehash("#narrow/searchterm/" + encodeURIComponent(term));
|
|
|
|
|
2012-11-18 22:53:50 +01:00
|
|
|
var new_narrow = {type: "searchterm", searchterm: term, allow_collapse: false};
|
|
|
|
var bar = {icon: 'search', description: 'Messages containing "' + term + '"'};
|
|
|
|
var term_lowercase = term.toLowerCase();
|
2012-11-19 17:43:46 +01:00
|
|
|
do_narrow(new_narrow, bar, false, function (other) {
|
2012-11-18 22:53:50 +01:00
|
|
|
return other.subject.toLowerCase().indexOf(term_lowercase) !== -1 ||
|
|
|
|
other.content.toLowerCase().indexOf(term_lowercase) !== -1;
|
|
|
|
});
|
|
|
|
load_more_messages();
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.show_all_messages = function () {
|
2012-12-07 20:52:39 +01:00
|
|
|
hashchange.changehash("");
|
|
|
|
|
2012-11-09 01:06:07 +01:00
|
|
|
if (!narrowdata) {
|
2012-10-03 20:49:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-11-09 01:06:07 +01:00
|
|
|
narrowdata = false;
|
|
|
|
filter_function = false;
|
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-11-27 23:17:30 +01:00
|
|
|
reset_load_more_status();
|
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");
|
2012-10-31 23:13:01 +01:00
|
|
|
$("#currently_narrowed_to").empty();
|
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
|
|
|
|
2012-11-14 20:52:53 +01:00
|
|
|
search.update_highlight_on_narrow();
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
2012-11-04 02:16:15 +01:00
|
|
|
exports.restore_home_state = function() {
|
|
|
|
// If we click on the Home link while already at Home, unnarrow.
|
|
|
|
// If we click on the Home link from another nav pane, just go
|
|
|
|
// back to the state you were in (possibly still narrowed) before
|
|
|
|
// you left the Home pane.
|
|
|
|
if ($('#sidebar li[title="Home"]').hasClass("active")) {
|
|
|
|
exports.show_all_messages();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|