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:
Steve Howell
2018-08-04 14:33:28 +00:00
committed by Tim Abbott
parent ab26e27fef
commit 9accc2a3b6
7 changed files with 34 additions and 36 deletions

View File

@@ -77,7 +77,6 @@ zrequire('Handlebars', 'handlebars');
zrequire('templates');
zrequire('unread');
zrequire('hash_util');
zrequire('hashchange');
zrequire('narrow');
zrequire('util');
zrequire('presence');

View File

@@ -33,7 +33,7 @@ run_test('operators_round_trip', () => {
{operator: 'stream', operand: 'devel'},
{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');
narrow = hashchange.parse_narrow(hash.split('/'));
@@ -46,7 +46,7 @@ run_test('operators_round_trip', () => {
{operator: 'stream', operand: 'devel'},
{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');
narrow = hashchange.parse_narrow(hash.split('/'));
@@ -64,7 +64,7 @@ run_test('operators_round_trip', () => {
operators = [
{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');
narrow = hashchange.parse_narrow(hash.split('/'));
assert.deepEqual(narrow, [
@@ -99,7 +99,7 @@ run_test('people_slugs', () => {
operators = [
{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');
narrow = hashchange.parse_narrow(hash.split('/'));
assert.deepEqual(narrow, [
@@ -109,7 +109,7 @@ run_test('people_slugs', () => {
operators = [
{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');
});

View File

@@ -1,7 +1,6 @@
set_global('$', global.make_zjquery());
set_global('i18n', global.stub_i18n);
zrequire('hashchange');
zrequire('hash_util');
zrequire('narrow');
zrequire('narrow_state');

View File

@@ -64,6 +64,28 @@ exports.by_stream_subject_uri = function (stream, 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;

View File

@@ -39,33 +39,11 @@ exports.changehash = function (newhash) {
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) {
if (changing_hash) {
return;
}
var new_hash = exports.operators_to_hash(operators);
var new_hash = hash_util.operators_to_hash(operators);
exports.changehash(new_hash);
};

View File

@@ -768,7 +768,7 @@ exports.hide_empty_narrow_message = function () {
};
exports.pm_with_uri = function (reply_to) {
return hashchange.operators_to_hash([
return hash_util.operators_to_hash([
{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) {
return hashchange.operators_to_hash([
return hash_util.operators_to_hash([
{operator: 'sender', operand: reply_to},
]);
};

View File

@@ -38,11 +38,11 @@ function make_tab_data() {
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
} else if (page_params.narrow !== undefined) {
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'));
if (page_params.narrow_topic !== undefined) {
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));
}
}
@@ -51,7 +51,7 @@ function make_tab_data() {
var stream;
var ops = narrow_state.operators();
// 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")) {
stream = filter.operands("stream")[0];
tabs.push(make_tab(stream, hashed, stream, 'stream'));
@@ -103,7 +103,7 @@ function make_tab_data() {
if (filter.has_operator("stream") &&
filter.has_operator("topic")) {
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));
}