subs: Move active_stream function to hash_util.

This function better belongs in hash_util since it relies on
the current window hash.
This commit is contained in:
Aman Agrawal
2021-03-15 05:57:14 +00:00
committed by Tim Abbott
parent c6a934da4c
commit b6f14c54e5
5 changed files with 28 additions and 18 deletions

View File

@@ -119,6 +119,15 @@ run_test("build_reload_url", () => {
assert.equal(hash_util.build_reload_url(), "+oldhash="); assert.equal(hash_util.build_reload_url(), "+oldhash=");
}); });
run_test("test_active_stream", () => {
location.hash = "#streams/1/announce";
assert.equal(hash_util.active_stream().id, 1);
assert.equal(hash_util.active_stream().name, "announce");
location.hash = "#test/narrow";
assert.equal(hash_util.active_stream(), undefined);
});
run_test("test_parse_narrow", () => { run_test("test_parse_narrow", () => {
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend"]), [ assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend"]), [
{negated: false, operator: "stream", operand: "frontend"}, {negated: false, operator: "stream", operand: "frontend"},

View File

@@ -21,6 +21,7 @@ mock_esm("../../static/js/browser_history", {update: noop});
mock_esm("../../static/js/hash_util", { mock_esm("../../static/js/hash_util", {
stream_edit_uri: noop, stream_edit_uri: noop,
by_stream_uri: noop, by_stream_uri: noop,
active_stream: noop,
}); });
mock_esm("../../static/js/list_widget", { mock_esm("../../static/js/list_widget", {
create: () => ({init: noop}), create: () => ({init: noop}),

View File

@@ -249,3 +249,20 @@ export function is_overlay_hash(hash) {
return overlay_list.includes(main_hash); return overlay_list.includes(main_hash);
} }
// this finds the stream that is actively open in the settings and focused in
// the left side.
export function active_stream() {
const hash_components = window.location.hash.slice(1).split(/\//);
// if the string casted to a number is valid, and another component
// after exists then it's a stream name/id pair.
if (typeof Number.parseFloat(hash_components[1]) === "number" && hash_components[2]) {
return {
id: Number.parseFloat(hash_components[1]),
name: hash_components[2],
};
}
return undefined;
}

View File

@@ -78,7 +78,7 @@ export function is_sub_settings_active(sub) {
// currently being viewed/edited in the stream edit UI. This is // currently being viewed/edited in the stream edit UI. This is
// used to determine whether we need to rerender the stream edit // used to determine whether we need to rerender the stream edit
// UI when a sub object is modified by an event. // UI when a sub object is modified by an event.
const active_stream = subs.active_stream(); const active_stream = hash_util.active_stream();
if (active_stream !== undefined && active_stream.id === sub.stream_id) { if (active_stream !== undefined && active_stream.id === sub.stream_id) {
return true; return true;
} }

View File

@@ -145,23 +145,6 @@ function should_list_all_streams() {
return !page_params.realm_is_zephyr_mirror_realm; return !page_params.realm_is_zephyr_mirror_realm;
} }
// this finds the stream that is actively open in the settings and focused in
// the left side.
export function active_stream() {
const hash_components = window.location.hash.slice(1).split(/\//);
// if the string casted to a number is valid, and another component
// after exists then it's a stream name/id pair.
if (typeof Number.parseFloat(hash_components[1]) === "number" && hash_components[2]) {
return {
id: Number.parseFloat(hash_components[1]),
name: hash_components[2],
};
}
return undefined;
}
export function set_muted(sub, is_muted, status_element) { export function set_muted(sub, is_muted, status_element) {
stream_muting.update_is_muted(sub, is_muted); stream_muting.update_is_muted(sub, is_muted);
stream_edit.set_stream_property(sub, "is_muted", sub.is_muted, status_element); stream_edit.set_stream_property(sub, "is_muted", sub.is_muted, status_element);