From 9accc2a3b6e192f44e3e284cb784fb16a6eee1fa Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 4 Aug 2018 14:33:28 +0000 Subject: [PATCH] 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. --- frontend_tests/node_tests/activity.js | 1 - frontend_tests/node_tests/hashchange.js | 10 +++++----- frontend_tests/node_tests/popovers.js | 1 - static/js/hash_util.js | 22 ++++++++++++++++++++++ static/js/hashchange.js | 24 +----------------------- static/js/narrow.js | 4 ++-- static/js/tab_bar.js | 8 ++++---- 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js index 86662a8d5e..ca5e9017f7 100644 --- a/frontend_tests/node_tests/activity.js +++ b/frontend_tests/node_tests/activity.js @@ -77,7 +77,6 @@ zrequire('Handlebars', 'handlebars'); zrequire('templates'); zrequire('unread'); zrequire('hash_util'); -zrequire('hashchange'); zrequire('narrow'); zrequire('util'); zrequire('presence'); diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js index 5f8367e07c..a81f203d92 100644 --- a/frontend_tests/node_tests/hashchange.js +++ b/frontend_tests/node_tests/hashchange.js @@ -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'); }); diff --git a/frontend_tests/node_tests/popovers.js b/frontend_tests/node_tests/popovers.js index 5c26e88491..045d888fe6 100644 --- a/frontend_tests/node_tests/popovers.js +++ b/frontend_tests/node_tests/popovers.js @@ -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'); diff --git a/static/js/hash_util.js b/static/js/hash_util.js index 054380c087..b0d4f8859f 100644 --- a/static/js/hash_util.js +++ b/static/js/hash_util.js @@ -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; diff --git a/static/js/hashchange.js b/static/js/hashchange.js index 28bece4793..b21c19a316 100644 --- a/static/js/hashchange.js +++ b/static/js/hashchange.js @@ -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); }; diff --git a/static/js/narrow.js b/static/js/narrow.js index 589c9ac388..44a1700396 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -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}, ]); }; diff --git a/static/js/tab_bar.js b/static/js/tab_bar.js index 032202dc94..3e7fc1f707 100644 --- a/static/js/tab_bar.js +++ b/static/js/tab_bar.js @@ -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)); }