Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

templates: Add map_entries helper.

Anders Kaseorg0a28fcee8bf1
diff --git a/web/src/templates.ts b/web/src/templates.ts
index f81b04bab28..518c433f2b2 100644
--- a/web/src/templates.ts
+++ b/web/src/templates.ts
@@ -89,6 +89,15 @@ Handlebars.registerHelper({
},
});
+Handlebars.registerHelper("map_entries", (m: unknown) => {
+ /* istanbul ignore if */
+ if (!(typeof m === "object" && m instanceof Map)) {
+ blueslip.error("map_entries requires a Map");
+ return m;
+ }
+ return [...m];
+});
+
type Context = Record<string, unknown>;
Handlebars.registerHelper("t", function (this: Context, message: string) {
diff --git a/web/templates/inbox_view/inbox_folder_with_channels.hbs b/web/templates/inbox_view/inbox_folder_with_channels.hbs
index 34cbc11ce07..a824bcc476f 100644
--- a/web/templates/inbox_view/inbox_folder_with_channels.hbs
+++ b/web/templates/inbox_view/inbox_folder_with_channels.hbs
@@ -8,13 +8,13 @@
unread_count=unread_count
}}
<div class="inbox-streams-container inbox-folder-components">
- {{#each topics_dict as |key_value_list _index|}}
- {{#each ../streams_dict as |stream_key_value _stream_index|}}
+ {{#each (map_entries topics_dict) as |key_value_list|}}
+ {{#each (map_entries ../streams_dict) as |stream_key_value|}}
{{#if (and (eq stream_key_value.[1].folder_id ../../id) (eq stream_key_value.[0] key_value_list.[0]))}}
<div id="{{key_value_list.[0]}}" class="inbox-folder-channel">
{{> inbox_row stream_key_value.[1]}}
<div class="inbox-topic-container">
- {{#each key_value_list.[1]}}
+ {{#each (map_entries key_value_list.[1])}}
{{> inbox_row this.[1]}}
{{/each}}
</div>
diff --git a/web/templates/inbox_view/inbox_list.hbs b/web/templates/inbox_view/inbox_list.hbs
index cac3009d21b..d96c87c99a9 100644
--- a/web/templates/inbox_view/inbox_list.hbs
+++ b/web/templates/inbox_view/inbox_list.hbs
@@ -7,11 +7,11 @@
unread_count=unread_dms_count
}}
<div id="inbox-direct-messages-container" class="inbox-folder-components">
- {{#each dms_dict}}
+ {{#each (map_entries dms_dict)}}
{{> inbox_row this.[1]}}
{{/each}}
</div>
-{{#each channel_folders_dict as |channel_folder_key_value_list _index|}}
+{{#each (map_entries channel_folders_dict) as |channel_folder_key_value_list|}}
{{> inbox_folder_with_channels
id=channel_folder_key_value_list.[1].id
name=channel_folder_key_value_list.[1].name
diff --git a/web/templates/inbox_view/inbox_stream_container.hbs b/web/templates/inbox_view/inbox_stream_container.hbs
index 8aaa8af28f1..61ccba414e9 100644
--- a/web/templates/inbox_view/inbox_stream_container.hbs
+++ b/web/templates/inbox_view/inbox_stream_container.hbs
@@ -1,12 +1,12 @@
-{{#each topics_dict as |key_value_list _index|}}
+{{#each (map_entries topics_dict) as |key_value_list|}}
<div id="{{key_value_list.[0]}}">
- {{#each ../streams_dict as |stream_key_value _stream_index|}}
+ {{#each (map_entries ../streams_dict) as |stream_key_value|}}
{{#if (eq stream_key_value.[0] key_value_list.[0])}}
{{> inbox_row stream_key_value.[1]}}
{{/if}}
{{/each}}
<div class="inbox-topic-container">
- {{#each key_value_list.[1]}}
+ {{#each (map_entries key_value_list.[1])}}
{{>inbox_row this.[1]}}
{{/each}}
</div>
diff --git a/web/tests/templates.test.cjs b/web/tests/templates.test.cjs
index 2fa88b65656..ee115f33fd4 100644
--- a/web/tests/templates.test.cjs
+++ b/web/tests/templates.test.cjs
@@ -36,6 +36,16 @@ run_test("or", () => {
assert.equal(html, "\n<p>last or</p>\n<p>true or</p>\n");
});
+run_test("map_entries", () => {
+ const html = require("./templates/map_entries.hbs")({
+ m: new Map([
+ ["a", "b"],
+ ["c", "d"],
+ ]),
+ });
+ assert.equal(html, "a:b\nc:d\n");
+});
+
run_test("rendered_markdown", () => {
const html = require("./templates/rendered_markdown.hbs")();
const expected_html =
diff --git a/web/tests/templates/map_entries.hbs b/web/tests/templates/map_entries.hbs
new file mode 100644
index 00000000000..7dedb159c75
--- /dev/null
+++ b/web/tests/templates/map_entries.hbs
@@ -0,0 +1,3 @@
+{{#each (map_entries m)~}}
+ {{0}}:{{1}}
+{{/each}}
diff --git a/web/webpack.config.ts b/web/webpack.config.ts
index ad2514066a7..ca7397a8827 100644
--- a/web/webpack.config.ts
+++ b/web/webpack.config.ts
@@ -176,6 +176,7 @@ const config = (
"not",
"t",
"tr",
+ "map_entries",
"rendered_markdown",
"numberFormat",
"tooltip_hotkey_hints",