DEV: Upgrade our widget handlebars compiler

Now supports subexpressions such as i18n and concat, plus automatic
attaching of widgets similar to ember.
This commit is contained in:
Robin Ward
2019-05-01 18:31:01 -04:00
parent e696903c31
commit 3cb0d27d38
9 changed files with 142 additions and 110 deletions

View File

@@ -222,7 +222,7 @@ export default function transformPost(
acted,
count,
canUndo: a.can_undo,
canDeferFlags: a.can_defer_flags,
canIgnoreFlags: a.can_defer_flags,
description: actionDescription(action, acted, count)
};
});

View File

@@ -63,15 +63,12 @@ createWidget("small-user-list", {
createWidget("action-link", {
tagName: "span.action-link",
template: hbs`<a>{{attrs.text}}. </a>`,
buildClasses(attrs) {
return attrs.className;
},
html(attrs) {
return h("a", [attrs.text, ". "]);
},
click() {
this.sendWidgetAction(this.attrs.action);
}
@@ -82,56 +79,24 @@ createWidget("actions-summary-item", {
buildKey: attrs => `actions-summary-item-${attrs.id}`,
defaultState() {
return { users: [] };
return { users: null };
},
html(attrs, state) {
const users = state.users;
template: hbs`
{{#if state.users}}
{{small-user-list users=state.users description=(concat "post.actions.people." attrs.action)}}
{{else}}
{{action-link action="whoActed" text=attrs.description}}
{{/if}}
const result = [];
const action = attrs.action;
{{#if attrs.canUndo}}
{{action-link action="undo" className="undo" text=(i18n (concat "post.actions.undo." attrs.action))}}
{{/if}}
if (users.length === 0) {
result.push(
this.attach("action-link", {
action: "whoActed",
text: attrs.description
})
);
} else {
result.push(
this.attach("small-user-list", {
users,
description: `post.actions.people.${action}`
})
);
}
if (attrs.canUndo) {
result.push(
this.attach("action-link", {
action: "undo",
className: "undo",
text: I18n.t(`post.actions.undo.${action}`)
})
);
}
if (attrs.canDeferFlags) {
const flagsDesc = I18n.t(`post.actions.defer_flags`, {
count: attrs.count
});
result.push(
this.attach("action-link", {
action: "deferFlags",
className: "defer-flags",
text: flagsDesc
})
);
}
return result;
},
{{#if attrs.canIgnoreFlags}}
{{action-link action="deferFlags" className="defer-flags" text=(i18n "post.actions.defer_flags" count=attrs.count)}}
{{/if}}
`,
whoActed() {
const attrs = this.attrs;
@@ -159,7 +124,7 @@ export default createWidget("actions-summary", {
tagName: "section.post-actions",
template: hbs`
{{#each attrs.actionsSummary as |as|}}
{{attach widget="actions-summary-item" attrs=as}}
{{actions-summary-item attrs=as}}
<div class='clearfix'></div>
{{/each}}
{{#if attrs.deleted_at}}

View File

@@ -4,9 +4,9 @@ import hbs from "discourse/widgets/hbs-compiler";
createWidget("header-contents", {
tagName: "div.contents.clearfix",
template: hbs`
{{attach widget="home-logo" attrs=attrs}}
{{home-logo attrs=attrs}}
{{#if attrs.topic}}
{{attach widget="header-topic-info" attrs=attrs}}
{{header-topic-info attrs=attrs}}
{{/if}}
<div class="panel clearfix">{{yield}}</div>
`

View File

@@ -34,9 +34,9 @@ createWidget("pm-map-user-group", {
<span class="group-name">{{attrs.group.name}}</span>
</a>
{{#if attrs.isEditing}}
{{#if attrs.canRemoveAllowedUsers}}
{{attach widget="pm-remove-group-link" attrs=attrs.group}}
{{/if}}
{{#if attrs.canRemoveAllowedUsers}}
{{pm-remove-group-link attrs=attrs.group}}
{{/if}}
{{/if}}
`
});

View File

@@ -109,15 +109,12 @@ createWidget("user-menu-dismiss-link", {
template: hbs`
<ul class='menu-links'>
<li>
{{attach
widget="link"
attrs=(hash
action="dismissNotifications"
className="dismiss"
tabindex="0"
icon="check"
label="user.dismiss"
title="user.dismiss_notifications_tooltip")}}
{{link action="dismissNotifications"
className="dismiss"
tabindex="0"
icon="check"
label="user.dismiss"
title="user.dismiss_notifications_tooltip"}}
</li>
</ul>
`