Skip to content

Commit

Permalink
Move replies preview dropdown out of Post-footer (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 authored May 12, 2021
1 parent 62134c0 commit 78bdecf
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions js/src/forum/addMentionedByList.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,74 @@ import icon from 'flarum/helpers/icon';
export default function addMentionedByList() {
Post.prototype.mentionedBy = Model.hasMany('mentionedBy');

extend(CommentPost.prototype, 'footerItems', function(items) {
function hidePreview() {
this.$('.Post-mentionedBy-preview')
.removeClass('in')
.one('transitionend', function() { $(this).hide(); });
}

extend(CommentPost.prototype, 'oncreate', function() {
let timeout;
const post = this.attrs.post;
const replies = post.mentionedBy();

if (replies && replies.length) {
const hidePreview = () => {
this.$('.Post-mentionedBy-preview')
.removeClass('in')
.one('transitionend', function() { $(this).hide(); });
};
const $preview = $('<ul class="Dropdown-menu Post-mentionedBy-preview fade"/>');
this.$().append($preview);

const $parentPost = this.$();
const $this = this.$('.Post-mentionedBy');

const showPreview = () => {
if (!$preview.hasClass('in') && $preview.is(':visible')) return;

// When the user hovers their mouse over the list of people who have
// replied to the post, render a list of reply previews into a
// popup.
m.render($preview[0], replies.map(reply => (
<li data-number={reply.number()}>
{PostPreview.component({
post: reply,
onclick: hidePreview.bind(this)
})}
</li>
)));

const oncreate = function(vnode) {
const $this = $(vnode.dom);
let timeout;
$preview.show()
.css('top', $this.offset().top - $parentPost.offset().top + $this.outerHeight(true))
.css('left', $this.offsetParent().offset().left - $parentPost.offset().left)
.css('max-width', $parentPost.width());

const $preview = $('<ul class="Dropdown-menu Post-mentionedBy-preview fade"/>');
$this.append($preview);
setTimeout(() => $preview.off('transitionend').addClass('in'));
}

$this.children().hover(function() {
$this.add($preview).hover(
() => {
clearTimeout(timeout);
timeout = setTimeout(function() {
if (!$preview.hasClass('in') && $preview.is(':visible')) return;

// When the user hovers their mouse over the list of people who have
// replied to the post, render a list of reply previews into a
// popup.
m.render($preview[0], replies.map(reply => (
<li data-number={reply.number()}>
{PostPreview.component({
post: reply,
onclick: hidePreview
})}
</li>
)));
$preview.show();
setTimeout(() => $preview.off('transitionend').addClass('in'));
}, 500);
}, function() {
timeout = setTimeout(showPreview, 250);
},
() => {
clearTimeout(timeout);
timeout = setTimeout(hidePreview, 250);
});
}
);

// Whenever the user hovers their mouse over a particular name in the
// list of repliers, highlight the corresponding post in the preview
// popup.
$this.find('.Post-mentionedBy-summary a').hover(function() {
$preview.find('[data-number="' + $(this).data('number') + '"]').addClass('active');
}, function() {
$preview.find('[data-number]').removeClass('active');
});
};
// Whenever the user hovers their mouse over a particular name in the
// list of repliers, highlight the corresponding post in the preview
// popup.
this.$().find('.Post-mentionedBy-summary a').hover(function() {
$preview.find('[data-number="' + $(this).data('number') + '"]').addClass('active');
}, function() {
$preview.find('[data-number]').removeClass('active');
});
}
});

extend(CommentPost.prototype, 'footerItems', function(items) {
const post = this.attrs.post;
const replies = post.mentionedBy();

if (replies && replies.length) {
const users = [];
const repliers = replies
.sort(reply => reply.user() === app.session.user ? -1 : 0)
Expand All @@ -86,7 +102,7 @@ export default function addMentionedByList() {

return (
<Link href={app.route.post(reply)}
onclick={hidePreview}
onclick={hidePreview.bind(this)}
data-number={reply.number()}>
{app.session.user === user ? app.translator.trans('flarum-mentions.forum.post.you_text') : username(user)}
</Link>
Expand All @@ -105,7 +121,7 @@ export default function addMentionedByList() {
}

items.add('replies',
<div className="Post-mentionedBy" oncreate={oncreate}>
<div className="Post-mentionedBy">
<span className="Post-mentionedBy-summary">
{icon('fas fa-reply')}
{app.translator.transChoice('flarum-mentions.forum.post.mentioned_by' + (repliers[0].user() === app.session.user ? '_self' : '') + '_text', names.length, {
Expand Down

0 comments on commit 78bdecf

Please sign in to comment.