hashchange: Add hash_util.get_hash_category().

This replaces hashchange.get_main_hash(), which had
a slightly misleading name.  Also, moving this to
hash_util forces us to keep 100% coverage on it.
This commit is contained in:
Steve Howell
2018-12-04 22:48:07 +00:00
committed by Tim Abbott
parent 67fba69b0c
commit 44ef8baff1
3 changed files with 27 additions and 7 deletions

View File

@@ -63,6 +63,25 @@ run_test('hash_util', () => {
encode_decode_operand(operator, operand, 'testing.20123');
});
run_test('test_get_hash_category', () => {
assert.deepEqual(
hash_util.get_hash_category('streams/subscribed'),
'streams'
);
assert.deepEqual(
hash_util.get_hash_category('#settings/display-settings'),
'settings'
);
assert.deepEqual(
hash_util.get_hash_category('#drafts'),
'drafts'
);
assert.deepEqual(
hash_util.get_hash_category('invites'),
'invites'
);
});
run_test('test_parse_narrow', () => {
assert.deepEqual(
hash_util.parse_narrow(['narrow', 'stream', '11-social']),

View File

@@ -2,6 +2,11 @@ var hash_util = (function () {
var exports = {};
exports.get_hash_category = function (hash) {
// given "#streams/subscribed", returns "streams"
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
};
// Some browsers zealously URI-decode the contents of
// window.location.hash. So we hide our URI-encoding
// by replacing % with . (like MediaWiki).

View File

@@ -59,10 +59,6 @@ var state = {
old_hash: typeof window !== "undefined" ? window.location.hash : "#",
};
function get_main_hash(hash) {
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
}
function get_hash_components() {
var hash = window.location.hash.split(/\//);
@@ -75,7 +71,7 @@ function get_hash_components() {
function is_overlay_hash(hash) {
// Hash changes within this list are overlays and should not unnarrow (etc.)
var overlay_list = ["streams", "drafts", "settings", "organization", "invite"];
var main_hash = get_main_hash(hash);
var main_hash = hash_util.get_hash_category(hash);
return overlay_list.indexOf(main_hash) > -1;
}
@@ -141,8 +137,8 @@ function do_hashchange_normal(from_reload) {
}
function do_hashchange_overlay(old_hash) {
var base = get_main_hash(window.location.hash);
var old_base = get_main_hash(old_hash);
var base = hash_util.get_hash_category(window.location.hash);
var old_base = hash_util.get_hash_category(old_hash);
var coming_from_overlay = is_overlay_hash(old_hash || '#');