FEATURE: avatar flair on user cards

This commit is contained in:
Neil Lalonde 2016-09-15 16:15:08 -04:00
parent cd571b26ba
commit e3e15182df
7 changed files with 95 additions and 56 deletions

View File

@ -28,10 +28,10 @@ export function transformBasicPost(post) {
isDeleted: post.deleted_at || post.user_deleted, isDeleted: post.deleted_at || post.user_deleted,
deletedByAvatarTemplate: null, deletedByAvatarTemplate: null,
deletedByUsername: null, deletedByUsername: null,
primaryGroupName: post.primary_group_name, primary_group_name: post.primary_group_name,
primaryGroupFlairUrl: post.primary_group_flair_url, primary_group_flair_url: post.primary_group_flair_url,
primaryGroupFlairBgColor: post.primary_group_flair_bg_color, primary_group_flair_bg_color: post.primary_group_flair_bg_color,
primaryGroupFlairColor: post.primary_group_flair_color, primary_group_flair_color: post.primary_group_flair_color,
wiki: post.wiki, wiki: post.wiki,
firstPost: post.post_number === 1, firstPost: post.post_number === 1,
post_number: post.post_number, post_number: post.post_number,

View File

@ -1,7 +1,10 @@
{{#if visible}} {{#if visible}}
<div class="card-content"> <div class="card-content">
<div class="user-card-avatar">
<a href={{user.path}} {{action "showUser"}} class="card-huge-avatar">{{bound-avatar avatar "huge"}}</a> <a href={{user.path}} {{action "showUser"}} class="card-huge-avatar">{{bound-avatar avatar "huge"}}</a>
{{mount-widget widget="avatar-flair" args=user}}
</div>
<div class="names"> <div class="names">
<span> <span>

View File

@ -0,0 +1,40 @@
import { createWidget, applyDecorators } from 'discourse/widgets/widget';
import { h } from 'virtual-dom';
createWidget('avatar-flair', {
tagName: 'div.avatar-flair',
isIcon(attrs) {
return (attrs.primary_group_flair_url && attrs.primary_group_flair_url.substr(0,3) === 'fa-');
},
title(attrs) {
return attrs.primary_group_name;
},
buildClasses(attrs) {
return 'avatar-flair-' + attrs.primary_group_name + (attrs.primary_group_flair_bg_color ? ' rounded' : '');
},
buildAttributes(attrs) {
var style = '';
if (!this.isIcon(attrs)) {
style += 'background-image: url(' + Handlebars.Utils.escapeExpression(attrs.primary_group_flair_url) + '); ';
}
if (attrs.primary_group_flair_bg_color) {
style += 'background-color: #' + Handlebars.Utils.escapeExpression(attrs.primary_group_flair_bg_color) + '; ';
}
if (attrs.primary_group_flair_color) {
style += 'color: #' + Handlebars.Utils.escapeExpression(attrs.primary_group_flair_color) + '; ';
}
return {style: style};
},
html(attrs) {
if (this.isIcon(attrs)) {
return [h('i', { className: 'fa ' + attrs.primary_group_flair_url })];
} else {
return [];
}
}
});

View File

@ -77,43 +77,6 @@ createWidget('reply-to-tab', {
} }
}); });
createWidget('post-avatar-flair', {
tagName: 'div.avatar-flair',
isIcon(attrs) {
return (attrs.primaryGroupFlairUrl && attrs.primaryGroupFlairUrl.substr(0,3) === 'fa-');
},
title(attrs) {
return attrs.primaryGroupName;
},
buildClasses(attrs) {
return 'avatar-flair-' + attrs.primaryGroupName + (attrs.primaryGroupFlairBgColor ? ' rounded' : '');
},
buildAttributes(attrs) {
var style = '';
if (!this.isIcon(attrs)) {
style += 'background-image: url(' + Handlebars.Utils.escapeExpression(attrs.primaryGroupFlairUrl) + '); ';
}
if (attrs.primaryGroupFlairBgColor) {
style += 'background-color: #' + Handlebars.Utils.escapeExpression(attrs.primaryGroupFlairBgColor) + '; ';
}
if (attrs.primaryGroupFlairColor) {
style += 'color: #' + Handlebars.Utils.escapeExpression(attrs.primaryGroupFlairColor) + '; ';
}
return {style: style};
},
html(attrs) {
if (this.isIcon(attrs)) {
return [h('i', { className: 'fa ' + attrs.primaryGroupFlairUrl })];
} else {
return [];
}
}
});
createWidget('post-avatar', { createWidget('post-avatar', {
tagName: 'div.topic-avatar', tagName: 'div.topic-avatar',
@ -131,16 +94,14 @@ createWidget('post-avatar', {
template: attrs.avatar_template, template: attrs.avatar_template,
username: attrs.username, username: attrs.username,
url: attrs.usernameUrl, url: attrs.usernameUrl,
className: 'main-avatar', className: 'main-avatar'
flairUrl: attrs.primaryGroupFlairUrl,
flairBgColor: attrs.primaryGroupFlairBgColor
}); });
} }
const result = [body]; const result = [body];
if (attrs.primaryGroupFlairUrl || attrs.primaryGroupFlairBgColor) { if (attrs.primary_group_flair_url || attrs.primary_group_flair_bg_color) {
result.push(this.attach('post-avatar-flair', attrs)); result.push(this.attach('avatar-flair', attrs));
} }
result.push(h('div.poster-avatar-extra')); result.push(h('div.poster-avatar-extra'));
@ -454,7 +415,7 @@ export default createWidget('post', {
if (attrs.topicOwner) { classNames.push('topic-owner'); } if (attrs.topicOwner) { classNames.push('topic-owner'); }
if (attrs.hidden) { classNames.push('post-hidden'); } if (attrs.hidden) { classNames.push('post-hidden'); }
if (attrs.deleted) { classNames.push('deleted'); } if (attrs.deleted) { classNames.push('deleted'); }
if (attrs.primaryGroupName) { classNames.push(`group-${attrs.primaryGroupName}`); } if (attrs.primary_group_name) { classNames.push(`group-${attrs.primary_group_name}`); }
if (attrs.wiki) { classNames.push(`wiki`); } if (attrs.wiki) { classNames.push(`wiki`); }
if (attrs.isWhisper) { classNames.push('whisper'); } if (attrs.isWhisper) { classNames.push('whisper'); }
if (attrs.isModeratorAction || (attrs.isWarning && attrs.firstPost)) { if (attrs.isModeratorAction || (attrs.isWarning && attrs.firstPost)) {

View File

@ -153,22 +153,24 @@ aside.quote {
} }
} }
.topic-avatar { .topic-avatar, .user-card-avatar {
position: relative; position: relative;
} }
.topic-avatar .avatar-flair, .avatar-flair-preview .avatar-flair { .topic-avatar .avatar-flair, .avatar-flair-preview .avatar-flair, .user-card-avatar .avatar-flair {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
background-size: 20px 20px;
background-repeat: no-repeat; background-repeat: no-repeat;
background-position: center; background-position: center;
width: 20px;
height: 20px;
position: absolute; position: absolute;
bottom: 0; bottom: 0;
right: -6px; right: -6px;
}
.topic-avatar .avatar-flair, .avatar-flair-preview .avatar-flair {
background-size: 20px 20px;
width: 20px;
height: 20px;
&.rounded { &.rounded {
background-size: 18px 18px; background-size: 18px 18px;
border-radius: 12px; border-radius: 12px;
@ -178,6 +180,19 @@ aside.quote {
right: -8px; right: -8px;
} }
} }
.user-card-avatar .avatar-flair {
background-size: 40px 40px;
width: 40px;
height: 40px;
&.rounded {
background-size: 30px 30px;
border-radius: 24px;
width: 40px;
height: 40px;
bottom: -2px;
right: -4px;
}
}
.topic-avatar .poster-avatar-extra { .topic-avatar .poster-avatar-extra {
display: none; display: none;
} }

View File

@ -172,7 +172,7 @@ $user_card_background: $secondary;
} }
} }
img.avatar { .user-card-avatar {
float: left; float: left;
margin-right: 10px; margin-right: 10px;
margin-top: -53px; margin-top: -53px;

View File

@ -65,7 +65,11 @@ class UserSerializer < BasicUserSerializer
:user_fields, :user_fields,
:topic_post_count, :topic_post_count,
:pending_count, :pending_count,
:profile_view_count :profile_view_count,
:primary_group_name,
:primary_group_flair_url,
:primary_group_flair_bg_color,
:primary_group_flair_color
has_one :invited_by, embed: :object, serializer: BasicUserSerializer has_one :invited_by, embed: :object, serializer: BasicUserSerializer
has_many :groups, embed: :object, serializer: BasicGroupSerializer has_many :groups, embed: :object, serializer: BasicGroupSerializer
@ -253,6 +257,22 @@ class UserSerializer < BasicUserSerializer
object.suspended? object.suspended?
end end
def primary_group_name
object.primary_group.try(:name)
end
def primary_group_flair_url
object.try(:primary_group).try(:flair_url)
end
def primary_group_flair_bg_color
object.try(:primary_group).try(:flair_bg_color)
end
def primary_group_flair_color
object.try(:primary_group).try(:flair_color)
end
### ###
### STAFF ATTRIBUTES ### STAFF ATTRIBUTES
### ###