mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
refactor: Move operators_to_hash to hash_utils.
This breaks some unnecessary dependencies on hashchange.js, in favor of hash_util, which has fewer dependencies.
This commit is contained in:
@@ -77,7 +77,6 @@ zrequire('Handlebars', 'handlebars');
|
|||||||
zrequire('templates');
|
zrequire('templates');
|
||||||
zrequire('unread');
|
zrequire('unread');
|
||||||
zrequire('hash_util');
|
zrequire('hash_util');
|
||||||
zrequire('hashchange');
|
|
||||||
zrequire('narrow');
|
zrequire('narrow');
|
||||||
zrequire('util');
|
zrequire('util');
|
||||||
zrequire('presence');
|
zrequire('presence');
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ run_test('operators_round_trip', () => {
|
|||||||
{operator: 'stream', operand: 'devel'},
|
{operator: 'stream', operand: 'devel'},
|
||||||
{operator: 'topic', operand: 'algol'},
|
{operator: 'topic', operand: 'algol'},
|
||||||
];
|
];
|
||||||
hash = hashchange.operators_to_hash(operators);
|
hash = hash_util.operators_to_hash(operators);
|
||||||
assert.equal(hash, '#narrow/stream/devel/topic/algol');
|
assert.equal(hash, '#narrow/stream/devel/topic/algol');
|
||||||
|
|
||||||
narrow = hashchange.parse_narrow(hash.split('/'));
|
narrow = hashchange.parse_narrow(hash.split('/'));
|
||||||
@@ -46,7 +46,7 @@ run_test('operators_round_trip', () => {
|
|||||||
{operator: 'stream', operand: 'devel'},
|
{operator: 'stream', operand: 'devel'},
|
||||||
{operator: 'topic', operand: 'visual c++', negated: true},
|
{operator: 'topic', operand: 'visual c++', negated: true},
|
||||||
];
|
];
|
||||||
hash = hashchange.operators_to_hash(operators);
|
hash = hash_util.operators_to_hash(operators);
|
||||||
assert.equal(hash, '#narrow/stream/devel/-topic/visual.20c.2B.2B');
|
assert.equal(hash, '#narrow/stream/devel/-topic/visual.20c.2B.2B');
|
||||||
|
|
||||||
narrow = hashchange.parse_narrow(hash.split('/'));
|
narrow = hashchange.parse_narrow(hash.split('/'));
|
||||||
@@ -64,7 +64,7 @@ run_test('operators_round_trip', () => {
|
|||||||
operators = [
|
operators = [
|
||||||
{operator: 'stream', operand: 'Florida, USA'},
|
{operator: 'stream', operand: 'Florida, USA'},
|
||||||
];
|
];
|
||||||
hash = hashchange.operators_to_hash(operators);
|
hash = hash_util.operators_to_hash(operators);
|
||||||
assert.equal(hash, '#narrow/stream/987-Florida.2C-USA');
|
assert.equal(hash, '#narrow/stream/987-Florida.2C-USA');
|
||||||
narrow = hashchange.parse_narrow(hash.split('/'));
|
narrow = hashchange.parse_narrow(hash.split('/'));
|
||||||
assert.deepEqual(narrow, [
|
assert.deepEqual(narrow, [
|
||||||
@@ -99,7 +99,7 @@ run_test('people_slugs', () => {
|
|||||||
operators = [
|
operators = [
|
||||||
{operator: 'sender', operand: 'alice@example.com'},
|
{operator: 'sender', operand: 'alice@example.com'},
|
||||||
];
|
];
|
||||||
hash = hashchange.operators_to_hash(operators);
|
hash = hash_util.operators_to_hash(operators);
|
||||||
assert.equal(hash, '#narrow/sender/42-alice');
|
assert.equal(hash, '#narrow/sender/42-alice');
|
||||||
narrow = hashchange.parse_narrow(hash.split('/'));
|
narrow = hashchange.parse_narrow(hash.split('/'));
|
||||||
assert.deepEqual(narrow, [
|
assert.deepEqual(narrow, [
|
||||||
@@ -109,7 +109,7 @@ run_test('people_slugs', () => {
|
|||||||
operators = [
|
operators = [
|
||||||
{operator: 'pm-with', operand: 'alice@example.com'},
|
{operator: 'pm-with', operand: 'alice@example.com'},
|
||||||
];
|
];
|
||||||
hash = hashchange.operators_to_hash(operators);
|
hash = hash_util.operators_to_hash(operators);
|
||||||
assert.equal(hash, '#narrow/pm-with/42-alice');
|
assert.equal(hash, '#narrow/pm-with/42-alice');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
set_global('$', global.make_zjquery());
|
set_global('$', global.make_zjquery());
|
||||||
set_global('i18n', global.stub_i18n);
|
set_global('i18n', global.stub_i18n);
|
||||||
|
|
||||||
zrequire('hashchange');
|
|
||||||
zrequire('hash_util');
|
zrequire('hash_util');
|
||||||
zrequire('narrow');
|
zrequire('narrow');
|
||||||
zrequire('narrow_state');
|
zrequire('narrow_state');
|
||||||
|
|||||||
@@ -64,6 +64,28 @@ exports.by_stream_subject_uri = function (stream, subject) {
|
|||||||
"/subject/" + exports.encodeHashComponent(subject);
|
"/subject/" + exports.encodeHashComponent(subject);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Encodes an operator list into the
|
||||||
|
// corresponding hash: the # component
|
||||||
|
// of the narrow URL
|
||||||
|
exports.operators_to_hash = function (operators) {
|
||||||
|
var hash = '#';
|
||||||
|
|
||||||
|
if (operators !== undefined) {
|
||||||
|
hash = '#narrow';
|
||||||
|
_.each(operators, function (elem) {
|
||||||
|
// Support legacy tuples.
|
||||||
|
var operator = elem.operator;
|
||||||
|
var operand = elem.operand;
|
||||||
|
|
||||||
|
var sign = elem.negated ? '-' : '';
|
||||||
|
hash += '/' + sign + exports.encodeHashComponent(operator)
|
||||||
|
+ '/' + exports.encode_operand(operator, operand);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
|
|
||||||
|
|||||||
@@ -39,33 +39,11 @@ exports.changehash = function (newhash) {
|
|||||||
favicon.reset();
|
favicon.reset();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Encodes an operator list into the
|
|
||||||
// corresponding hash: the # component
|
|
||||||
// of the narrow URL
|
|
||||||
exports.operators_to_hash = function (operators) {
|
|
||||||
var hash = '#';
|
|
||||||
|
|
||||||
if (operators !== undefined) {
|
|
||||||
hash = '#narrow';
|
|
||||||
_.each(operators, function (elem) {
|
|
||||||
// Support legacy tuples.
|
|
||||||
var operator = elem.operator;
|
|
||||||
var operand = elem.operand;
|
|
||||||
|
|
||||||
var sign = elem.negated ? '-' : '';
|
|
||||||
hash += '/' + sign + hash_util.encodeHashComponent(operator)
|
|
||||||
+ '/' + hash_util.encode_operand(operator, operand);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return hash;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.save_narrow = function (operators) {
|
exports.save_narrow = function (operators) {
|
||||||
if (changing_hash) {
|
if (changing_hash) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var new_hash = exports.operators_to_hash(operators);
|
var new_hash = hash_util.operators_to_hash(operators);
|
||||||
exports.changehash(new_hash);
|
exports.changehash(new_hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -768,7 +768,7 @@ exports.hide_empty_narrow_message = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.pm_with_uri = function (reply_to) {
|
exports.pm_with_uri = function (reply_to) {
|
||||||
return hashchange.operators_to_hash([
|
return hash_util.operators_to_hash([
|
||||||
{operator: 'pm-with', operand: reply_to},
|
{operator: 'pm-with', operand: reply_to},
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
@@ -782,7 +782,7 @@ exports.huddle_with_uri = function (user_ids_string) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.by_sender_uri = function (reply_to) {
|
exports.by_sender_uri = function (reply_to) {
|
||||||
return hashchange.operators_to_hash([
|
return hash_util.operators_to_hash([
|
||||||
{operator: 'sender', operand: reply_to},
|
{operator: 'sender', operand: reply_to},
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -38,11 +38,11 @@ function make_tab_data() {
|
|||||||
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
|
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
|
||||||
} else if (page_params.narrow !== undefined) {
|
} else if (page_params.narrow !== undefined) {
|
||||||
tabs.push(make_tab("Stream " + page_params.narrow_stream,
|
tabs.push(make_tab("Stream " + page_params.narrow_stream,
|
||||||
hashchange.operators_to_hash([page_params.narrow[0]]),
|
hash_util.operators_to_hash([page_params.narrow[0]]),
|
||||||
page_params.narrow_stream, 'stream'));
|
page_params.narrow_stream, 'stream'));
|
||||||
if (page_params.narrow_topic !== undefined) {
|
if (page_params.narrow_topic !== undefined) {
|
||||||
tabs.push(make_tab("Topic " + page_params.narrow_topic,
|
tabs.push(make_tab("Topic " + page_params.narrow_topic,
|
||||||
hashchange.operators_to_hash(page_params.narrow),
|
hash_util.operators_to_hash(page_params.narrow),
|
||||||
null));
|
null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ function make_tab_data() {
|
|||||||
var stream;
|
var stream;
|
||||||
var ops = narrow_state.operators();
|
var ops = narrow_state.operators();
|
||||||
// Second breadcrumb item
|
// Second breadcrumb item
|
||||||
var hashed = hashchange.operators_to_hash(ops.slice(0, 1));
|
var hashed = hash_util.operators_to_hash(ops.slice(0, 1));
|
||||||
if (filter.has_operator("stream")) {
|
if (filter.has_operator("stream")) {
|
||||||
stream = filter.operands("stream")[0];
|
stream = filter.operands("stream")[0];
|
||||||
tabs.push(make_tab(stream, hashed, stream, 'stream'));
|
tabs.push(make_tab(stream, hashed, stream, 'stream'));
|
||||||
@@ -103,7 +103,7 @@ function make_tab_data() {
|
|||||||
if (filter.has_operator("stream") &&
|
if (filter.has_operator("stream") &&
|
||||||
filter.has_operator("topic")) {
|
filter.has_operator("topic")) {
|
||||||
var subject = filter.operands("topic")[0];
|
var subject = filter.operands("topic")[0];
|
||||||
hashed = hashchange.operators_to_hash(ops.slice(0, 2));
|
hashed = hash_util.operators_to_hash(ops.slice(0, 2));
|
||||||
|
|
||||||
tabs.push(make_tab(subject, hashed, null));
|
tabs.push(make_tab(subject, hashed, null));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user