Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] [extensibility] refactor(core): backport & improve extensibility of DiscussionListItem #4048

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 57 additions & 27 deletions framework/core/js/src/forum/components/DiscussionListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,22 @@ export default class DiscussionListItem<CustomAttrs extends IDiscussionListItemA
}

view() {
const discussion = this.attrs.discussion;
const attrs = this.elementAttrs();

return <div {...attrs}>{this.viewItems().toArray()}</div>;
}

viewItems(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

const discussion = this.attrs.discussion;
const controls = DiscussionControls.controls(discussion, this).toArray();
const attrs = this.elementAttrs();

return (
<div {...attrs}>
{this.controlsView(controls)}
{this.slidableUnderneathView()}
{this.contentView()}
</div>
);
items.add('controls', this.controlsView(controls), 100);
items.add('slidableUnderneath', this.slidableUnderneathView(), 90);
items.add('content', this.contentView(), 80);

return items;
}

controlsView(controls: Mithril.ChildArray): Mithril.Children {
Expand Down Expand Up @@ -113,14 +117,22 @@ export default class DiscussionListItem<CustomAttrs extends IDiscussionListItemA

return (
<div className={classList('DiscussionListItem-content', 'Slidable-content', { unread: isUnread, read: isRead })}>
{this.authorAvatarView()}
{this.badgesView()}
{this.mainView()}
{this.replyCountItem()}
{this.contentItems().toArray()}
</div>
);
}

contentItems(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

items.add('authorAvatar', this.authorAvatarView(), 100);
items.add('badges', this.badgesView(), 90);
items.add('main', this.mainView(), 80);
items.add('replyCount', this.replyCountItem().toArray(), 70);

return items;
}

authorAvatarView(): Mithril.Children {
const discussion = this.attrs.discussion;
const user = discussion.user();
Expand Down Expand Up @@ -149,12 +161,22 @@ export default class DiscussionListItem<CustomAttrs extends IDiscussionListItemA

return (
<Link href={app.route.discussion(discussion, jumpTo)} className="DiscussionListItem-main">
<h2 className="DiscussionListItem-title">{highlight(discussion.title(), this.highlightRegExp)}</h2>
<ul className="DiscussionListItem-info">{listItems(this.infoItems().toArray())}</ul>
{this.mainItems().toArray()}
</Link>
);
}

mainItems(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

const discussion = this.attrs.discussion;

items.add('title', <h2 className="DiscussionListItem-title">{highlight(discussion.title(), this.highlightRegExp)}</h2>, 100);
items.add('info', <ul className="DiscussionListItem-info">{listItems(this.infoItems().toArray())}</ul>, 90);

return items;
}

getJumpTo() {
const discussion = this.attrs.discussion;
let jumpTo = 0;
Expand Down Expand Up @@ -252,30 +274,38 @@ export default class DiscussionListItem<CustomAttrs extends IDiscussionListItemA
return items;
}

replyCountItem() {
replyCountItem(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();

const discussion = this.attrs.discussion;
const showUnread = !this.showRepliesCount() && discussion.isUnread();

if (showUnread) {
return (
items.add(
'unreadCount',
<button className="Button--ua-reset DiscussionListItem-count" onclick={this.markAsRead.bind(this)}>
<span aria-hidden="true">{abbreviateNumber(discussion.unreadCount())}</span>

<span className="visually-hidden">
{app.translator.trans('core.forum.discussion_list.unread_replies_a11y_label', { count: discussion.replyCount() })}
</span>
</button>
</button>,
100
);
}
} else {
items.add(
'count',
<span className="DiscussionListItem-count">
<span aria-hidden="true">{abbreviateNumber(discussion.replyCount())}</span>

return (
<span className="DiscussionListItem-count">
<span aria-hidden="true">{abbreviateNumber(discussion.replyCount())}</span>
<span className="visually-hidden">
{app.translator.trans('core.forum.discussion_list.total_replies_a11y_label', { count: discussion.replyCount() })}
</span>
</span>,
100
);
}

<span className="visually-hidden">
{app.translator.trans('core.forum.discussion_list.total_replies_a11y_label', { count: discussion.replyCount() })}
</span>
</span>
);
return items;
}
}
Loading