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 object_entries helper.

Anders Kaseorg13612ff69826
diff --git a/web/src/templates.ts b/web/src/templates.ts
index 4e0c2297a76..34187f5efcb 100644
--- a/web/src/templates.ts
+++ b/web/src/templates.ts
@@ -98,6 +98,21 @@ Handlebars.registerHelper("map_entries", (m: unknown) => {
return [...m];
});
+Handlebars.registerHelper("object_entries", (o: unknown) => {
+ /* istanbul ignore if */
+ if (typeof o !== "object" || o === null) {
+ blueslip.error("object_entries requires a plain object");
+ return [];
+ }
+ /* istanbul ignore if */
+ if (Symbol.iterator in o) {
+ blueslip.error("object_entries requires a plain object");
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ return [...[...(o as Iterable<unknown>)].entries()];
+ }
+ return Object.entries(o);
+});
+
Handlebars.registerHelper("object_values", (o: unknown): unknown => {
/* istanbul ignore if */
if (typeof o !== "object" || o === null || Symbol.iterator in o) {
diff --git a/web/templates/popovers/schedule_message_popover.hbs b/web/templates/popovers/schedule_message_popover.hbs
index ddd9c85ce9f..d208579c3a0 100644
--- a/web/templates/popovers/schedule_message_popover.hbs
+++ b/web/templates/popovers/schedule_message_popover.hbs
@@ -19,28 +19,28 @@
{{/if}}
{{#if possible_send_later_today}}
<li role="separator" class="popover-menu-separator"></li>
- {{#each possible_send_later_today}}
+ {{#each (object_entries possible_send_later_today) as |entry|}}
<li role="none" class="link-item popover-menu-list-item">
- <a role="menuitem" id="{{@key}}" class="send_later_today send_later_option popover-menu-link" data-send-stamp="{{this.stamp}}" tabindex="0">
- <span class="popover-menu-label">{{this.text}}</span>
+ <a role="menuitem" id="{{entry.[0]}}" class="send_later_today send_later_option popover-menu-link" data-send-stamp="{{entry.[1].stamp}}" tabindex="0">
+ <span class="popover-menu-label">{{entry.[1].text}}</span>
</a>
</li>
{{/each}}
{{/if}}
<li role="separator" class="popover-menu-separator"></li>
- {{#each send_later_tomorrow}}
+ {{#each (object_entries send_later_tomorrow) as |entry|}}
<li role="none" class="link-item popover-menu-list-item">
- <a role="menuitem" id="{{@key}}" class="send_later_tomorrow send_later_option popover-menu-link" data-send-stamp="{{this.stamp}}" tabindex="0">
- <span class="popover-menu-label">{{this.text}}</span>
+ <a role="menuitem" id="{{entry.[0]}}" class="send_later_tomorrow send_later_option popover-menu-link" data-send-stamp="{{entry.[1].stamp}}" tabindex="0">
+ <span class="popover-menu-label">{{entry.[1].text}}</span>
</a>
</li>
{{/each}}
{{#if possible_send_later_monday}}
<li role="separator" class="popover-menu-separator"></li>
- {{#each possible_send_later_monday}}
+ {{#each (object_entries possible_send_later_monday) as |entry|}}
<li role="none" class="link-item popover-menu-list-item">
- <a role="menuitem" id="{{@key}}" class="send_later_monday send_later_option popover-menu-link" data-send-stamp="{{this.stamp}}" tabindex="0">
- <span class="popover-menu-label">{{this.text}}</span>
+ <a role="menuitem" id="{{entry.[0]}}" class="send_later_monday send_later_option popover-menu-link" data-send-stamp="{{entry.[1].stamp}}" tabindex="0">
+ <span class="popover-menu-label">{{entry.[1].text}}</span>
</a>
</li>
{{/each}}
diff --git a/web/templates/settings/add_new_bot_form.hbs b/web/templates/settings/add_new_bot_form.hbs
index a608741fe89..f583ad2c7b0 100644
--- a/web/templates/settings/add_new_bot_form.hbs
+++ b/web/templates/settings/add_new_bot_form.hbs
@@ -52,8 +52,8 @@
</div>
<div id="config_inputbox">
{{#each realm_embedded_bots}}
- {{#each config}}
- {{> ../embedded_bot_config_item botname=../name key=@key value=this}}
+ {{#each (object_entries config) as |entry|}}
+ {{> ../embedded_bot_config_item botname=../name key=entry.[0] value=entry.[1]}}
{{/each}}
{{/each}}
</div>
diff --git a/web/templates/settings/edit_custom_profile_field_form.hbs b/web/templates/settings/edit_custom_profile_field_form.hbs
index 8ea9d7f265a..d4d2cff608a 100644
--- a/web/templates/settings/edit_custom_profile_field_form.hbs
+++ b/web/templates/settings/edit_custom_profile_field_form.hbs
@@ -30,8 +30,8 @@
<div class="input-group profile_field_external_accounts_edit" >
<label for="external_acc_field_type" class="modal-field-label">{{t "External account type" }}</label>
<select name="external_acc_field_type" class="modal_select" disabled>
- {{#each ../realm_default_external_accounts}}
- <option value='{{@key}}'>{{this.text}}</option>
+ {{#each (object_entries ../realm_default_external_accounts) as |entry|}}
+ <option value='{{entry.[0]}}'>{{entry.[1].text}}</option>
{{/each}}
<option value="custom">{{t 'Custom' }}</option>
</select>
diff --git a/web/templates/settings/edit_embedded_bot_service.hbs b/web/templates/settings/edit_embedded_bot_service.hbs
index 5f01eb39ab1..754e8323cf2 100644
--- a/web/templates/settings/edit_embedded_bot_service.hbs
+++ b/web/templates/settings/edit_embedded_bot_service.hbs
@@ -1,11 +1,11 @@
{{#if service}}
{{#if service.config_data}}
<div id="config_edit_inputbox">
- {{#each service.config_data}}
+ {{#each (object_entries service.config_data) as |entry|}}
<div class="input-group">
- <label for="embedded_bot_{{@key}}_edit" class="modal-field-label">{{@key}}</label>
- <input type="text" name="{{@key}}" id="embedded_bot_{{@key}}_edit" class="modal_text_input"
- maxlength=1000 value="{{this}}" />
+ <label for="embedded_bot_{{entry.[0]}}_edit" class="modal-field-label">{{entry.[0]}}</label>
+ <input type="text" name="{{entry.[0]}}" id="embedded_bot_{{entry.[0]}}_edit" class="modal_text_input"
+ maxlength=1000 value="{{entry.[1]}}" />
</div>
{{/each}}
</div>
diff --git a/web/tests/templates.test.cjs b/web/tests/templates.test.cjs
index 01e81fc73c9..6cd7f8145ce 100644
--- a/web/tests/templates.test.cjs
+++ b/web/tests/templates.test.cjs
@@ -46,6 +46,13 @@ run_test("map_entries", () => {
assert.equal(html, "a:b\nc:d\n");
});
+run_test("object_entries", () => {
+ const html = require("./templates/object_entries.hbs")({
+ o: {a: "b", c: "d"},
+ });
+ assert.equal(html, "a:b\nc:d\n");
+});
+
run_test("object_values", () => {
const html = require("./templates/object_values.hbs")({
o: {a: "b", c: "d"},
diff --git a/web/tests/templates/object_entries.hbs b/web/tests/templates/object_entries.hbs
new file mode 100644
index 00000000000..9778c0016fb
--- /dev/null
+++ b/web/tests/templates/object_entries.hbs
@@ -0,0 +1,3 @@
+{{#each (object_entries o)~}}
+ {{0}}:{{1}}
+{{/each}}
diff --git a/web/webpack.config.ts b/web/webpack.config.ts
index 98684d93f88..4dcdad54553 100644
--- a/web/webpack.config.ts
+++ b/web/webpack.config.ts
@@ -177,6 +177,7 @@ const config = (
"t",
"tr",
"map_entries",
+ "object_entries",
"object_values",
"rendered_markdown",
"numberFormat",