2012-10-18 20:12:04 +02:00
|
|
|
var narrow = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
function Filter(operators) {
|
2013-04-25 21:13:06 +02:00
|
|
|
if (operators === undefined) {
|
|
|
|
this._operators = [];
|
|
|
|
} else {
|
|
|
|
this._operators = this._canonicalize_operators(operators);
|
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 22:52:02 +02:00
|
|
|
var canonical_operators = {"from": "sender", "subject": "topic"};
|
2013-07-16 22:52:42 +02:00
|
|
|
|
|
|
|
exports.canonicalize_operator = function (operator) {
|
|
|
|
operator = operator.toLowerCase();
|
|
|
|
if (canonical_operators[operator] !== undefined) {
|
|
|
|
return canonical_operators[operator];
|
|
|
|
} else {
|
|
|
|
return operator;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
Filter.prototype = {
|
|
|
|
predicate: function Filter_predicate() {
|
|
|
|
if (this._predicate === undefined) {
|
2013-04-25 16:40:53 +02:00
|
|
|
this._predicate = this._build_predicate();
|
2013-04-24 21:40:08 +02:00
|
|
|
}
|
|
|
|
return this._predicate;
|
|
|
|
},
|
|
|
|
|
|
|
|
operators: function Filter_operators() {
|
|
|
|
return this._operators;
|
|
|
|
},
|
|
|
|
|
|
|
|
public_operators: function Filter_public_operators() {
|
2013-07-30 00:35:44 +02:00
|
|
|
var safe_to_return = _.filter(this._operators, function (value) {
|
2013-04-24 21:40:08 +02:00
|
|
|
// Currently just filter out the "in" keyword.
|
2013-07-30 00:35:44 +02:00
|
|
|
return value[0] !== 'in';
|
2013-04-24 21:40:08 +02:00
|
|
|
});
|
2013-08-01 17:47:48 +02:00
|
|
|
if (safe_to_return.length !== 0) {
|
2013-04-24 21:40:08 +02:00
|
|
|
return safe_to_return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
},
|
|
|
|
|
2013-05-03 22:17:20 +02:00
|
|
|
operands: function Filter_get_operands(operator) {
|
2013-07-30 00:35:44 +02:00
|
|
|
return _.chain(this._operators)
|
|
|
|
.filter(function (elem) { return elem[0] === operator; })
|
|
|
|
.map(function (elem) { return elem[1]; })
|
|
|
|
.value();
|
2013-05-03 22:17:20 +02:00
|
|
|
},
|
|
|
|
|
2013-06-13 20:29:58 +02:00
|
|
|
has_operand: function Filter_has_operand(operator, operand) {
|
2013-07-31 21:17:43 +02:00
|
|
|
return _.any(this._operators, function (elem) {
|
2013-07-30 00:35:44 +02:00
|
|
|
return elem[0] === operator && elem[1] === operand;
|
2013-06-13 20:29:58 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-07-30 00:35:44 +02:00
|
|
|
has_operator: function Filter_has_operator(operator) {
|
2013-07-31 21:17:43 +02:00
|
|
|
return _.any(this._operators, function (elem) {
|
2013-07-30 00:35:44 +02:00
|
|
|
return elem[0] === operator;
|
2013-06-13 20:29:58 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-29 22:56:50 +02:00
|
|
|
is_search: function Filter_is_search() {
|
2013-07-30 00:35:44 +02:00
|
|
|
return this.has_operator('search');
|
2013-04-25 19:38:21 +02:00
|
|
|
},
|
|
|
|
|
2013-04-29 22:56:50 +02:00
|
|
|
can_apply_locally: function Filter_can_apply_locally() {
|
|
|
|
return ! this.is_search();
|
|
|
|
},
|
|
|
|
|
2013-04-25 16:40:53 +02:00
|
|
|
_canonicalize_operators: function Filter__canonicalize_operators(operators_mixed_case) {
|
2013-07-29 23:35:36 +02:00
|
|
|
return _.map(operators_mixed_case, function (operator) {
|
2013-04-25 16:40:53 +02:00
|
|
|
// We may want to consider allowing mixed-case operators at some point
|
2013-07-29 23:35:36 +02:00
|
|
|
return [exports.canonicalize_operator(operator[0]),
|
|
|
|
subs.canonicalized_name(operator[1])];
|
2013-04-24 21:40:08 +02:00
|
|
|
});
|
2013-04-25 16:40:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Build a filter function from a list of operators.
|
|
|
|
_build_predicate: function Filter__build_predicate() {
|
|
|
|
var operators = this._operators;
|
2013-04-24 21:40:08 +02:00
|
|
|
|
2013-04-25 21:13:06 +02:00
|
|
|
if (! this.can_apply_locally()) {
|
|
|
|
return function () { return true; };
|
|
|
|
}
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
// FIXME: This is probably pretty slow.
|
|
|
|
// We could turn it into something more like a compiler:
|
|
|
|
// build JavaScript code in a string and then eval() it.
|
|
|
|
|
|
|
|
return function (message) {
|
2013-07-31 21:11:25 +02:00
|
|
|
var operand, i, index, m, related_regexp;
|
2013-04-24 21:40:08 +02:00
|
|
|
for (i = 0; i < operators.length; i++) {
|
|
|
|
operand = operators[i][1];
|
|
|
|
switch (operators[i][0]) {
|
|
|
|
case 'is':
|
2013-07-10 03:22:34 +02:00
|
|
|
if (operand === 'private') {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (message.type !== 'private') {
|
2013-04-24 21:40:08 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
} else if (operand === 'starred') {
|
|
|
|
if (!message.starred) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-10 03:07:01 +02:00
|
|
|
} else if (operand === 'mentioned') {
|
2013-05-29 00:33:03 +02:00
|
|
|
if (!message.mentioned) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'in':
|
|
|
|
if (operand === 'home') {
|
2013-04-25 20:45:14 +02:00
|
|
|
return exports.message_in_home(message);
|
2013-04-24 21:40:08 +02:00
|
|
|
}
|
|
|
|
else if (operand === 'all') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-07-31 20:33:38 +02:00
|
|
|
case 'near':
|
|
|
|
return true;
|
|
|
|
|
2013-07-31 20:54:51 +02:00
|
|
|
case 'id':
|
|
|
|
return message.id.toString() === operand;
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
case 'stream':
|
2013-08-01 17:47:48 +02:00
|
|
|
if (message.type !== 'stream') {
|
2013-04-24 21:40:08 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-06-25 23:12:59 +02:00
|
|
|
|
|
|
|
if (page_params.domain === "mit.edu") {
|
|
|
|
// MIT users expect narrowing to "social" to also show messages to /^(un)*social(.d)*$/
|
|
|
|
// (unsocial, ununsocial, social.d, etc)
|
2013-07-16 22:37:37 +02:00
|
|
|
// TODO: hoist the regex compiling out of the closure
|
2013-07-31 21:11:25 +02:00
|
|
|
m = /^(?:un)*(.+?)(?:\.d)*$/i.exec(operand);
|
2013-07-16 22:37:37 +02:00
|
|
|
var base_stream_name = operand;
|
|
|
|
if (m !== null && m[1] !== undefined) {
|
|
|
|
base_stream_name = m[1];
|
|
|
|
}
|
2013-07-31 21:11:25 +02:00
|
|
|
related_regexp = new RegExp(/^(un)*/.source + util.escape_regexp(base_stream_name) + /(\.d)*$/.source, 'i');
|
2013-07-04 00:39:44 +02:00
|
|
|
if (! related_regexp.test(message.stream)) {
|
2013-06-25 23:12:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (message.stream.toLowerCase() !== operand) {
|
2013-07-31 21:09:08 +02:00
|
|
|
return false;
|
2013-06-25 23:12:59 +02:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
break;
|
|
|
|
|
2013-07-16 22:52:02 +02:00
|
|
|
case 'topic':
|
2013-08-01 17:47:48 +02:00
|
|
|
if (message.type !== 'stream') {
|
2013-04-24 21:40:08 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-07-31 21:11:25 +02:00
|
|
|
|
|
|
|
if (page_params.domain === "mit.edu") {
|
|
|
|
// MIT users expect narrowing to topic "foo" to also show messages to /^foo(.d)*$/
|
|
|
|
// (foo, foo.d, foo.d.d, etc)
|
|
|
|
// TODO: hoist the regex compiling out of the closure
|
|
|
|
m = /^(.*?)(?:\.d)*$/i.exec(operand);
|
|
|
|
var base_topic = operand;
|
|
|
|
if (m !== null && m[1] !== undefined) {
|
|
|
|
base_topic = m[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally, MIT users expect the empty instance and
|
|
|
|
// instance "personal" to be the same.
|
|
|
|
if (base_topic === ''
|
|
|
|
|| base_topic.toLowerCase() === 'personal'
|
|
|
|
|| base_topic.toLowerCase() === '(instance "")')
|
|
|
|
{
|
|
|
|
related_regexp = /^(|personal|\(instance ""\))(\.d)*$/i;
|
|
|
|
} else {
|
|
|
|
related_regexp = new RegExp(/^/.source + util.escape_regexp(base_topic) + /(\.d)*$/.source, 'i');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! related_regexp.test(message.subject)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (message.subject.toLowerCase() !== operand) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'sender':
|
2013-08-01 17:47:48 +02:00
|
|
|
if ((message.sender_email.toLowerCase() !== operand)) {
|
2013-04-24 21:40:08 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'pm-with':
|
|
|
|
if ((message.type !== 'private') ||
|
2013-08-01 17:47:48 +02:00
|
|
|
message.reply_to.toLowerCase() !== operand.split(',').sort().join(',')) {
|
2013-04-24 21:40:08 +02:00
|
|
|
return false;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All filters passed.
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-25 21:13:06 +02:00
|
|
|
exports.Filter = Filter;
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
var current_filter;
|
2012-10-24 00:29:06 +02:00
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.active = function () {
|
2013-04-24 21:40:08 +02:00
|
|
|
return current_filter !== undefined;
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
2013-04-25 19:38:21 +02:00
|
|
|
exports.filter = function () {
|
|
|
|
return current_filter;
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
exports.predicate = function () {
|
2013-04-25 21:22:48 +02:00
|
|
|
if (current_filter === undefined) {
|
2012-10-18 20:12:04 +02:00
|
|
|
return function () { return true; };
|
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
return current_filter.predicate();
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
2012-12-19 23:58:02 +01:00
|
|
|
exports.operators = function () {
|
2013-04-24 21:40:08 +02:00
|
|
|
if (current_filter === undefined) {
|
2013-05-31 17:43:32 +02:00
|
|
|
return [];
|
2013-04-24 21:40:08 +02:00
|
|
|
}
|
|
|
|
return current_filter.operators();
|
2012-12-19 23:58:02 +01:00
|
|
|
};
|
|
|
|
|
2013-02-05 19:25:30 +01:00
|
|
|
/* Operators we should send to the server. */
|
|
|
|
exports.public_operators = function () {
|
2013-04-24 21:40:08 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return undefined;
|
2013-02-05 19:25:30 +01:00
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
return current_filter.public_operators();
|
2013-02-05 19:25:30 +01:00
|
|
|
};
|
|
|
|
|
2013-03-19 23:12:50 +01:00
|
|
|
/* We use a variant of URI encoding which looks reasonably
|
|
|
|
nice and still handles unambiguously cases such as
|
|
|
|
spaces in operands.
|
|
|
|
|
|
|
|
This is just for the search bar, not for saving the
|
|
|
|
narrow in the URL fragment. There we do use full
|
|
|
|
URI encoding to avoid problematic characters. */
|
|
|
|
function encodeOperand(operand) {
|
|
|
|
return operand.replace(/%/g, '%25')
|
|
|
|
.replace(/\+/g, '%2B')
|
|
|
|
.replace(/ /g, '+');
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeOperand(encoded) {
|
2013-03-28 22:32:17 +01:00
|
|
|
return util.robust_uri_decode(encoded.replace(/\+/g, ' '));
|
2013-03-19 23:12:50 +01:00
|
|
|
}
|
|
|
|
|
2012-12-12 19:36:18 +01:00
|
|
|
/* Convert a list of operators to a string.
|
2012-12-12 19:00:50 +01:00
|
|
|
Each operator is a key-value pair like
|
|
|
|
|
|
|
|
['subject', 'my amazing subject']
|
|
|
|
|
|
|
|
These are not keys in a JavaScript object, because we
|
2013-03-04 23:42:30 +01:00
|
|
|
might need to support multiple operators of the same type.
|
|
|
|
*/
|
2013-07-18 22:12:49 +02:00
|
|
|
exports.unparse = function (operators) {
|
2013-07-30 00:35:44 +02:00
|
|
|
var parts = _.map(operators, function (elem) {
|
2012-12-12 19:36:18 +01:00
|
|
|
var operator = elem[0];
|
|
|
|
if (operator === 'search') {
|
|
|
|
// Search terms are the catch-all case.
|
|
|
|
// All tokens that don't start with a known operator and
|
|
|
|
// a colon are glued together to form a search term.
|
2013-07-30 00:35:44 +02:00
|
|
|
return elem[1];
|
2012-12-12 19:36:18 +01:00
|
|
|
} else {
|
2013-07-30 00:35:44 +02:00
|
|
|
return elem[0] + ':' + encodeOperand(elem[1].toString().toLowerCase());
|
2012-12-12 19:36:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return parts.join(' ');
|
2013-07-18 22:12:49 +02:00
|
|
|
};
|
2012-12-12 19:36:18 +01:00
|
|
|
|
2013-07-23 03:41:21 +02:00
|
|
|
exports.search_string = function () {
|
|
|
|
return exports.unparse(exports.operators());
|
|
|
|
};
|
|
|
|
|
2013-02-27 19:24:56 +01:00
|
|
|
// Collect operators which appear only once into an object,
|
|
|
|
// and discard those which appear more than once.
|
|
|
|
function collect_single(operators) {
|
|
|
|
var seen = {};
|
|
|
|
var result = {};
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(operators, function (elem) {
|
2013-02-27 19:24:56 +01:00
|
|
|
var key = elem[0];
|
|
|
|
if (seen.hasOwnProperty(key)) {
|
|
|
|
delete result[key];
|
|
|
|
} else {
|
|
|
|
result[key] = elem[1];
|
|
|
|
seen [key] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify default compose parameters (stream etc.) based on
|
|
|
|
// the current narrowed view.
|
|
|
|
//
|
|
|
|
// This logic is here and not in the 'compose' module because
|
|
|
|
// it will get more complicated as we add things to the narrow
|
|
|
|
// operator language.
|
|
|
|
exports.set_compose_defaults = function (opts) {
|
|
|
|
var single = collect_single(exports.operators());
|
|
|
|
|
|
|
|
// Set the stream, subject, and/or PM recipient if they are
|
|
|
|
// uniquely specified in the narrow view.
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(['stream', 'topic'], function (key) {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (single[key] !== undefined) {
|
2013-02-27 19:24:56 +01:00
|
|
|
opts[key] = single[key];
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-02-27 19:24:56 +01:00
|
|
|
});
|
|
|
|
|
2013-08-01 17:47:48 +02:00
|
|
|
if (single['pm-with'] !== undefined) {
|
2013-02-27 19:24:56 +01:00
|
|
|
opts.private_message_recipient = single['pm-with'];
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-02-27 19:24:56 +01:00
|
|
|
};
|
|
|
|
|
2012-12-17 23:18:16 +01:00
|
|
|
// Parse a string into a list of operators (see below).
|
|
|
|
exports.parse = function (str) {
|
|
|
|
var operators = [];
|
|
|
|
var search_term = [];
|
2013-05-30 23:47:32 +02:00
|
|
|
var matches = str.match(/"[^"]+"|\S+/g);
|
|
|
|
if (matches === null) {
|
|
|
|
return operators;
|
|
|
|
}
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(matches, function (token) {
|
2012-12-17 23:18:16 +01:00
|
|
|
var parts, operator;
|
2013-08-01 17:47:48 +02:00
|
|
|
if (token.length === 0) {
|
2012-12-17 23:18:16 +01:00
|
|
|
return;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-12-17 23:18:16 +01:00
|
|
|
parts = token.split(':');
|
2013-05-13 23:35:55 +02:00
|
|
|
if (token[0] === '"' || parts.length === 1) {
|
|
|
|
// Looks like a normal search term.
|
|
|
|
search_term.push(token);
|
|
|
|
} else {
|
2012-12-17 23:18:16 +01:00
|
|
|
// Looks like an operator.
|
|
|
|
// FIXME: Should we skip unknown operator names here?
|
|
|
|
operator = parts.shift();
|
2013-03-19 23:12:50 +01:00
|
|
|
operators.push([operator, decodeOperand(parts.join(':'))]);
|
2012-12-17 23:18:16 +01:00
|
|
|
}
|
|
|
|
});
|
2012-12-18 00:28:31 +01:00
|
|
|
// NB: Callers of 'parse' can assume that the 'search' operator is last.
|
2013-08-01 17:47:48 +02:00
|
|
|
if (search_term.length > 0) {
|
2012-12-18 21:34:59 +01:00
|
|
|
operators.push(['search', search_term.join(' ')]);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2012-12-17 23:18:16 +01:00
|
|
|
return operators;
|
|
|
|
};
|
|
|
|
|
2013-02-27 19:18:21 +01:00
|
|
|
exports.message_in_home = function (message) {
|
|
|
|
if (message.type === "private") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 21:13:19 +02:00
|
|
|
return subs.in_home_view(message.stream);
|
2013-02-27 19:18:21 +01:00
|
|
|
};
|
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
exports.stream = function () {
|
2013-04-24 21:40:08 +02:00
|
|
|
if (current_filter === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2013-06-13 21:03:01 +02:00
|
|
|
var stream_operands = current_filter.operands("stream");
|
|
|
|
if (stream_operands.length === 1) {
|
|
|
|
return stream_operands[0];
|
2013-04-10 23:38:30 +02:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2012-12-12 20:22:18 +01:00
|
|
|
exports.activate = function (operators, opts) {
|
2013-07-19 21:03:46 +02:00
|
|
|
if (operators.length === 0) {
|
|
|
|
return exports.deactivate();
|
|
|
|
}
|
2013-07-31 20:33:38 +02:00
|
|
|
var filter = new Filter(operators);
|
2013-07-19 21:03:46 +02:00
|
|
|
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.defaults({}, opts, {
|
2013-03-14 18:27:58 +01:00
|
|
|
then_select_id: home_msg_list.selected_id(),
|
2013-03-19 23:45:51 +01:00
|
|
|
select_first_unread: false,
|
2013-05-21 19:34:15 +02:00
|
|
|
change_hash: true,
|
|
|
|
trigger: 'unknown'
|
2013-07-30 05:11:50 +02:00
|
|
|
});
|
2013-07-31 20:33:38 +02:00
|
|
|
if (filter.has_operator("near")) {
|
|
|
|
opts.then_select_id = filter.operands("near")[0];
|
|
|
|
opts.select_first_unread = false;
|
|
|
|
}
|
2013-07-31 20:54:51 +02:00
|
|
|
if (filter.has_operator("id")) {
|
|
|
|
opts.then_select_id = filter.operands("id")[0];
|
|
|
|
opts.select_first_unread = false;
|
|
|
|
}
|
2012-12-07 20:52:39 +01:00
|
|
|
|
2013-04-11 00:13:28 +02:00
|
|
|
if (opts.then_select_id === -1) {
|
|
|
|
// If we're loading the page via a narrowed URL, we may not
|
|
|
|
// have setup the home view yet. In that case, use the
|
|
|
|
// initial pointer. We can remove this code if we later move
|
|
|
|
// to a model where home_msg_list.selected_id() is always
|
|
|
|
// initialized early.
|
|
|
|
opts.then_select_id = page_params.initial_pointer;
|
2013-07-17 18:07:06 +02:00
|
|
|
opts.select_first_unread = false;
|
2013-04-11 00:13:28 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 21:30:17 +01:00
|
|
|
// Unfade the home view before we switch tables.
|
|
|
|
compose.unfade_messages();
|
|
|
|
|
2013-07-24 22:33:06 +02:00
|
|
|
var was_narrowed_already = exports.active();
|
2013-07-01 18:16:05 +02:00
|
|
|
var then_select_id = opts.then_select_id;
|
|
|
|
var then_select_offset;
|
2013-07-10 17:06:28 +02:00
|
|
|
if (!opts.select_first_unread && rows.get(then_select_id, current_msg_list.table_name).length > 0) {
|
2013-07-01 18:16:05 +02:00
|
|
|
then_select_offset = rows.get(then_select_id, current_msg_list.table_name).offset().top -
|
|
|
|
viewport.scrollTop();
|
|
|
|
}
|
2012-10-27 02:58:21 +02:00
|
|
|
|
2013-07-24 22:33:06 +02:00
|
|
|
if (!was_narrowed_already) {
|
|
|
|
message_tour.start_tour(current_msg_list.selected_id());
|
|
|
|
}
|
|
|
|
|
2013-07-31 20:33:38 +02:00
|
|
|
current_filter = filter;
|
2012-12-12 19:00:50 +01:00
|
|
|
|
2013-07-03 21:39:41 +02:00
|
|
|
// Save how far from the pointer the top of the message list was.
|
|
|
|
if (current_msg_list.selected_id() !== -1) {
|
|
|
|
current_msg_list.pre_narrow_offset = current_msg_list.selected_row().offset().top - viewport.scrollTop();
|
|
|
|
}
|
2013-04-25 21:13:06 +02:00
|
|
|
narrowed_msg_list = new MessageList('zfilt', current_filter,
|
2013-06-13 19:44:36 +02:00
|
|
|
{collapse_messages: ! current_filter.is_search()});
|
2013-07-03 21:39:41 +02:00
|
|
|
|
|
|
|
|
2013-02-12 20:01:24 +01:00
|
|
|
current_msg_list = narrowed_msg_list;
|
|
|
|
|
2013-02-20 18:18:15 +01:00
|
|
|
function maybe_select_closest() {
|
|
|
|
if (! narrowed_msg_list.empty()) {
|
2013-03-14 18:27:58 +01:00
|
|
|
if (opts.select_first_unread) {
|
|
|
|
then_select_id = narrowed_msg_list.last().id;
|
2013-07-31 20:35:48 +02:00
|
|
|
var first_unread = _.find(narrowed_msg_list.all(),
|
|
|
|
unread.message_unread);
|
2013-08-01 17:47:48 +02:00
|
|
|
if (first_unread) {
|
2013-07-31 20:35:48 +02:00
|
|
|
then_select_id = first_unread.id;
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-03-14 18:27:58 +01:00
|
|
|
}
|
2013-07-01 18:16:05 +02:00
|
|
|
|
2013-07-09 04:54:23 +02:00
|
|
|
var preserve_pre_narrowing_screen_position =
|
|
|
|
!opts.select_first_unread &&
|
|
|
|
(narrowed_msg_list.get(then_select_id) !== undefined) &&
|
|
|
|
(then_select_offset !== undefined);
|
|
|
|
|
|
|
|
var then_scroll = !preserve_pre_narrowing_screen_position;
|
|
|
|
|
|
|
|
narrowed_msg_list.select_id(then_select_id, {then_scroll: then_scroll,
|
2013-07-11 17:14:11 +02:00
|
|
|
use_closest: true
|
|
|
|
});
|
2013-07-09 04:54:23 +02:00
|
|
|
|
|
|
|
if (preserve_pre_narrowing_screen_position) {
|
2013-07-01 18:16:05 +02:00
|
|
|
// Scroll so that the selected message is in the same
|
|
|
|
// position in the viewport as it was prior to
|
|
|
|
// narrowing
|
|
|
|
viewport.scrollTop(rows.get(then_select_id, narrowed_msg_list.table_name).offset().top
|
|
|
|
- then_select_offset);
|
|
|
|
}
|
2013-02-20 18:18:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 22:59:56 +01:00
|
|
|
// Don't bother populating a message list when it won't contain
|
2013-04-25 19:38:21 +02:00
|
|
|
// the message we want anyway or if the filter can't be applied
|
|
|
|
// locally.
|
|
|
|
if (all_msg_list.get(then_select_id) !== undefined && current_filter.can_apply_locally()) {
|
2013-02-20 22:59:56 +01:00
|
|
|
add_messages(all_msg_list.all(), narrowed_msg_list);
|
|
|
|
}
|
|
|
|
|
2013-04-10 17:55:02 +02:00
|
|
|
var defer_selecting_closest = narrowed_msg_list.empty();
|
|
|
|
load_old_messages({
|
|
|
|
anchor: then_select_id,
|
2013-06-14 23:47:37 +02:00
|
|
|
num_before: 50,
|
|
|
|
num_after: 50,
|
2013-04-10 17:55:02 +02:00
|
|
|
msg_list: narrowed_msg_list,
|
|
|
|
cont: function (messages) {
|
|
|
|
if (defer_selecting_closest) {
|
2013-03-14 19:42:37 +01:00
|
|
|
maybe_select_closest();
|
2013-04-10 17:55:02 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cont_will_add_messages: false
|
|
|
|
});
|
2013-02-20 19:35:15 +01:00
|
|
|
|
2012-10-03 20:49:58 +02:00
|
|
|
// Show the new set of messages.
|
2013-07-17 00:52:18 +02:00
|
|
|
$("body").addClass("narrowed_view");
|
2013-01-09 23:22:21 +01:00
|
|
|
$("#zfilt").addClass("focused_table");
|
2012-10-03 20:49:58 +02:00
|
|
|
$("#zhome").removeClass("focused_table");
|
2013-01-09 23:22:21 +01:00
|
|
|
|
Process message condensing in narrow.activate rather than hashchange.
Previously, we were having this problem where:
* You narrow to something
* That causes message_list.js:process_collapsing to run on all of the
elements in the view, which changes some of their sizes
* That causes the pane to scroll and either push the content up or
down, depending (since stuff on top of where you were is now a
different size)
* That triggers keep_pointer_in_view, which moves your pointer
Moving process_collapsing into narrow.activate doesn't obviously
fix any of this, but it does seem to mitigate the issue a bit.
In particular, we (a) process it less frequently, and (b) process it
immediately after we show the narrowed view table, which seems to
reduce the raciness of the overall experience.
This does, however, introduce a regression:
* If you receive a long message when you're on
#settings, e.g., and then go back to Home,
the message does not properly get a [More] appended
to it.
(imported from commit b1440d656cc7b71eca8af736f2f7b3aa7e0cca14)
2013-05-01 21:36:04 +02:00
|
|
|
// Deal with message condensing/uncondensing.
|
|
|
|
// In principle, this code causes us to scroll around because divs
|
|
|
|
// above us could change size -- which is problematic, because it
|
|
|
|
// could cause us to lose our position. But doing this here, right
|
|
|
|
// after showing the table, seems to cause us to win the race.
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each($("tr.message_row"), ui.process_condensing);
|
Process message condensing in narrow.activate rather than hashchange.
Previously, we were having this problem where:
* You narrow to something
* That causes message_list.js:process_collapsing to run on all of the
elements in the view, which changes some of their sizes
* That causes the pane to scroll and either push the content up or
down, depending (since stuff on top of where you were is now a
different size)
* That triggers keep_pointer_in_view, which moves your pointer
Moving process_collapsing into narrow.activate doesn't obviously
fix any of this, but it does seem to mitigate the issue a bit.
In particular, we (a) process it less frequently, and (b) process it
immediately after we show the narrowed view table, which seems to
reduce the raciness of the overall experience.
This does, however, introduce a regression:
* If you receive a long message when you're on
#settings, e.g., and then go back to Home,
the message does not properly get a [More] appended
to it.
(imported from commit b1440d656cc7b71eca8af736f2f7b3aa7e0cca14)
2013-05-01 21:36:04 +02:00
|
|
|
|
2013-01-09 23:22:21 +01:00
|
|
|
reset_load_more_status();
|
2013-04-10 17:55:02 +02:00
|
|
|
if (! defer_selecting_closest) {
|
|
|
|
maybe_select_closest();
|
|
|
|
}
|
2012-10-26 16:59:38 +02:00
|
|
|
|
2013-03-19 23:45:51 +01:00
|
|
|
// Put the narrow operators in the URL fragment.
|
|
|
|
// Disabled when the URL fragment was the source
|
|
|
|
// of this narrow.
|
2013-08-01 17:47:48 +02:00
|
|
|
if (opts.change_hash) {
|
2013-03-19 23:45:51 +01:00
|
|
|
hashchange.save_narrow(operators);
|
2013-08-01 17:47:48 +02:00
|
|
|
}
|
2013-03-19 23:45:51 +01:00
|
|
|
|
|
|
|
// Put the narrow operators in the search bar.
|
2013-07-18 22:12:49 +02:00
|
|
|
$('#search_query').val(exports.unparse(operators));
|
2012-12-18 23:38:55 +01:00
|
|
|
search.update_button_visibility();
|
2013-03-12 20:53:54 +01:00
|
|
|
compose.update_recipient_on_narrow();
|
2013-03-06 23:47:49 +01:00
|
|
|
compose.update_faded_messages();
|
2013-02-06 19:48:26 +01:00
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('narrow_activated.zulip', {msg_list: narrowed_msg_list,
|
2013-05-21 19:34:15 +02:00
|
|
|
filter: current_filter,
|
|
|
|
trigger: opts.trigger}));
|
2012-12-12 19:00:50 +01:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2013-01-02 19:21:39 +01:00
|
|
|
// Activate narrowing with a single operator.
|
|
|
|
// This is just for syntactic convenience.
|
|
|
|
exports.by = function (operator, operand, opts) {
|
|
|
|
exports.activate([[operator, operand]], opts);
|
2013-02-09 08:27:20 +01:00
|
|
|
};
|
|
|
|
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by_subject = function (target_id, opts) {
|
2013-02-12 20:01:24 +01:00
|
|
|
var original = current_msg_list.get(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.
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by_recipient(target_id, opts);
|
2012-10-03 20:49:58 +02:00
|
|
|
return;
|
2012-10-24 05:04:42 +02:00
|
|
|
}
|
2013-07-08 20:27:48 +02:00
|
|
|
mark_message_as_read(original);
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.defaults({}, opts, {then_select_id: target_id});
|
2012-12-12 20:34:47 +01:00
|
|
|
exports.activate([
|
2013-04-18 17:13:43 +02:00
|
|
|
['stream', original.stream],
|
2013-07-16 22:52:02 +02:00
|
|
|
['topic', original.subject]
|
2013-05-21 19:34:15 +02:00
|
|
|
], opts);
|
2012-11-15 16:57:59 +01:00
|
|
|
};
|
|
|
|
|
2012-10-10 23:24:11 +02:00
|
|
|
// Called for the 'narrow by stream' hotkey.
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by_recipient = function (target_id, opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.defaults({}, opts, {then_select_id: target_id});
|
2013-02-12 20:01:24 +01:00
|
|
|
var message = current_msg_list.get(target_id);
|
2013-07-08 20:33:37 +02:00
|
|
|
mark_message_as_read(message);
|
2012-10-19 19:00:46 +02:00
|
|
|
switch (message.type) {
|
2012-12-03 19:49:12 +01:00
|
|
|
case 'private':
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by('pm-with', message.reply_to, opts);
|
2012-10-19 17:11:31 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by('stream', message.stream, opts);
|
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
|
|
|
|
2013-05-21 19:34:15 +02:00
|
|
|
exports.by_time_travel = function (target_id, opts) {
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.defaults({}, opts, {then_select_id: target_id});
|
2013-07-31 20:33:38 +02:00
|
|
|
narrow.activate([["near", target_id]], opts);
|
2013-02-28 23:21:37 +01:00
|
|
|
};
|
|
|
|
|
2012-12-12 20:36:05 +01:00
|
|
|
exports.deactivate = function () {
|
2013-04-24 21:40:08 +02:00
|
|
|
if (current_filter === undefined) {
|
2012-10-03 20:49:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-04-24 21:40:08 +02:00
|
|
|
|
2013-07-16 20:00:47 +02:00
|
|
|
if (ui.actively_scrolling()) {
|
|
|
|
// There is no way to intercept in-flight scroll events, and they will
|
|
|
|
// cause you to end up in the wrong place if you are actively scrolling
|
|
|
|
// on an unnarrow. Wait a bit and try again once the scrolling is over.
|
|
|
|
setTimeout(exports.deactivate, 50);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
current_filter = undefined;
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2013-03-29 19:55:28 +01:00
|
|
|
exports.hide_empty_narrow_message();
|
2013-02-23 19:38:25 +01:00
|
|
|
|
2013-07-17 00:52:18 +02:00
|
|
|
$("body").removeClass('narrowed_view');
|
2013-01-09 23:22:21 +01:00
|
|
|
$("#zfilt").removeClass('focused_table');
|
|
|
|
$("#zhome").addClass('focused_table');
|
2013-01-09 23:46:32 +01:00
|
|
|
|
2012-12-12 19:36:18 +01:00
|
|
|
$('#search_query').val('');
|
2013-01-09 23:22:21 +01:00
|
|
|
reset_load_more_status();
|
2013-02-14 23:48:37 +01:00
|
|
|
|
2013-07-24 22:33:06 +02:00
|
|
|
var visited_messages = message_tour.get_tour();
|
|
|
|
home_msg_list.advance_past_messages(visited_messages);
|
|
|
|
message_tour.finish_tour();
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
current_msg_list = home_msg_list;
|
2013-07-11 20:45:19 +02:00
|
|
|
var preserve_pre_narrowing_screen_position =
|
|
|
|
(current_msg_list.selected_row().length > 0) &&
|
|
|
|
(current_msg_list.pre_narrow_offset !== undefined);
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
// We fall back to the closest selected id, if the user has removed a stream from the home
|
|
|
|
// view since leaving it the old selected id might no longer be there
|
2013-07-11 20:45:19 +02:00
|
|
|
current_msg_list.select_id(current_msg_list.selected_id(), {
|
|
|
|
then_scroll: !preserve_pre_narrowing_screen_position,
|
2013-07-11 17:14:11 +02:00
|
|
|
use_closest: true
|
2013-07-03 22:14:32 +02:00
|
|
|
});
|
2013-07-11 20:45:19 +02:00
|
|
|
|
|
|
|
if (preserve_pre_narrowing_screen_position) {
|
|
|
|
// We scroll the user back to exactly the offset from the selected
|
|
|
|
// message that he was at the time that he narrowed.
|
|
|
|
// TODO: Make this correctly handle the case of resizing while narrowed.
|
2013-07-03 21:39:41 +02:00
|
|
|
viewport.scrollTop(current_msg_list.selected_row().offset().top - current_msg_list.pre_narrow_offset);
|
|
|
|
}
|
2013-01-11 16:57:17 +01:00
|
|
|
|
2012-12-12 19:00:50 +01:00
|
|
|
hashchange.save_narrow();
|
2013-07-10 19:16:35 +02:00
|
|
|
compose.update_faded_messages();
|
2013-01-11 16:57:17 +01:00
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('narrow_deactivated.zulip', {msg_list: current_msg_list}));
|
2012-10-18 20:12:04 +02:00
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.restore_home_state = function () {
|
2012-11-04 02:16:15 +01:00
|
|
|
// 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.
|
2013-03-13 05:39:33 +01:00
|
|
|
if (!ui.home_tab_obscured()) {
|
2012-12-12 20:36:05 +01:00
|
|
|
exports.deactivate();
|
2012-11-04 02:16:15 +01:00
|
|
|
}
|
2013-02-12 22:32:14 +01:00
|
|
|
maybe_scroll_to_selected();
|
2012-11-04 02:16:15 +01:00
|
|
|
};
|
|
|
|
|
2013-03-29 19:55:28 +01:00
|
|
|
function pick_empty_narrow_banner() {
|
|
|
|
var default_banner = $('#empty_narrow_message');
|
2013-04-24 21:40:08 +02:00
|
|
|
if (current_filter === undefined) {
|
2013-03-29 19:55:28 +01:00
|
|
|
return default_banner;
|
|
|
|
}
|
|
|
|
|
2013-04-24 21:40:08 +02:00
|
|
|
var first_operator = current_filter.operators()[0][0];
|
|
|
|
var first_operand = current_filter.operators()[0][1];
|
2013-03-29 19:55:28 +01:00
|
|
|
|
|
|
|
if (first_operator === "is") {
|
|
|
|
if (first_operand === "starred") {
|
|
|
|
// You have no starred messages.
|
|
|
|
return $("#empty_star_narrow_message");
|
2013-07-10 03:07:01 +02:00
|
|
|
} else if (first_operand === "mentioned") {
|
2013-05-29 00:33:03 +02:00
|
|
|
return $("#empty_narrow_all_mentioned");
|
2013-07-10 03:22:34 +02:00
|
|
|
} else if (first_operand === "private") {
|
2013-03-29 19:55:28 +01:00
|
|
|
// You have no private messages.
|
2013-04-02 20:57:53 +02:00
|
|
|
return $("#empty_narrow_all_private_message");
|
2013-03-29 19:55:28 +01:00
|
|
|
}
|
2013-06-12 21:13:19 +02:00
|
|
|
} else if ((first_operator === "stream") && !subs.is_subscribed(first_operand)) {
|
2013-03-29 19:55:28 +01:00
|
|
|
// You are narrowed to a stream to which you aren't subscribed.
|
|
|
|
return $("#nonsubbed_stream_narrow_message");
|
|
|
|
} else if (first_operator === "search") {
|
|
|
|
// You are narrowed to empty search results.
|
|
|
|
return $("#empty_search_narrow_message");
|
2013-04-02 20:57:53 +02:00
|
|
|
} else if (first_operator === "pm-with") {
|
|
|
|
if (first_operand.indexOf(',') === -1) {
|
|
|
|
// You have no private messages with this person
|
|
|
|
return $("#empty_narrow_private_message");
|
|
|
|
} else {
|
|
|
|
return $("#empty_narrow_multi_private_message");
|
|
|
|
}
|
2013-03-29 19:55:28 +01:00
|
|
|
}
|
|
|
|
return default_banner;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.show_empty_narrow_message = function () {
|
|
|
|
$(".empty_feed_notice").hide();
|
|
|
|
pick_empty_narrow_banner().show();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.hide_empty_narrow_message = function () {
|
|
|
|
$(".empty_feed_notice").hide();
|
|
|
|
};
|
|
|
|
|
2013-05-09 21:12:53 +02:00
|
|
|
exports.by_stream_uri = function (stream) {
|
|
|
|
return "#narrow/stream/" + hashchange.encodeHashComponent(stream);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.by_stream_subject_uri = function (stream, subject) {
|
|
|
|
return "#narrow/stream/" + hashchange.encodeHashComponent(stream) +
|
|
|
|
"/subject/" + hashchange.encodeHashComponent(subject);
|
|
|
|
};
|
|
|
|
|
2013-06-13 19:44:36 +02:00
|
|
|
// Are we narrowed to PMs: all PMs or PMs with particular people.
|
2013-05-24 22:00:23 +02:00
|
|
|
exports.narrowed_to_pms = function () {
|
2013-06-13 21:03:01 +02:00
|
|
|
if (current_filter === undefined) {
|
2013-05-24 22:00:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-13 21:03:01 +02:00
|
|
|
return (current_filter.has_operator("pm-with") ||
|
2013-07-10 03:22:34 +02:00
|
|
|
current_filter.has_operand("is", "private"));
|
2013-05-24 22:00:23 +02:00
|
|
|
};
|
|
|
|
|
2013-05-16 23:16:15 +02:00
|
|
|
// We auto-reply under certain conditions, namely when you're narrowed
|
|
|
|
// to a PM (or huddle), and when you're narrowed to some stream/subject pair
|
|
|
|
exports.narrowed_by_reply = function () {
|
2013-06-13 21:03:01 +02:00
|
|
|
if (current_filter === undefined) {
|
2013-05-16 23:16:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-13 21:03:01 +02:00
|
|
|
var operators = current_filter.operators();
|
2013-06-13 19:44:36 +02:00
|
|
|
return ((operators.length === 1 &&
|
2013-06-13 21:03:01 +02:00
|
|
|
current_filter.operands("pm-with").length === 1) ||
|
2013-06-13 19:44:36 +02:00
|
|
|
(operators.length === 2 &&
|
2013-06-13 21:03:01 +02:00
|
|
|
current_filter.operands("stream").length === 1 &&
|
2013-07-16 22:52:02 +02:00
|
|
|
current_filter.operands("topic").length === 1));
|
2013-05-16 23:16:15 +02:00
|
|
|
};
|
|
|
|
|
2012-10-18 20:12:04 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2013-07-28 23:03:43 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = narrow;
|
|
|
|
}
|