FEATURE: Allow truncated group posts to be expanded

This commit is contained in:
Robin Ward 2017-06-21 12:56:38 -04:00
parent 32756060bb
commit 287cb4bfc5
9 changed files with 65 additions and 49 deletions

View File

@ -0,0 +1,19 @@
import { ajax } from 'discourse/lib/ajax';
export default Ember.Component.extend({
tagName: '',
actions: {
expandItem() {
const item = this.get('item');
const topicId = item.get('topic_id');
const postNumber = item.get('post_number');
return ajax(`/posts/by_number/${topicId}/${postNumber}.json`).then(result => {
item.set('truncated', false);
item.set('excerpt', result.cooked);
});
}
}
});

View File

@ -1,22 +1,8 @@
import { propertyEqual } from 'discourse/lib/computed';
import { actionDescription } from "discourse/components/small-action";
import { ajax } from 'discourse/lib/ajax';
export default Ember.Component.extend({
classNameBindings: [":item", "item.hidden", "item.deleted:deleted", "moderatorAction"],
moderatorAction: propertyEqual("item.post_type", "site.post_types.moderator_action"),
actionDescription: actionDescription("item.action_code", "item.created_at", "item.username"),
actions: {
expandItem() {
const item = this.get('item');
const topicId = item.get('topic_id');
const postNumber = item.get('post_number');
return ajax(`/posts/by_number/${topicId}/${postNumber}.json`).then(result => {
item.set('truncated', false);
item.set('excerpt', result.cooked);
});
}
}
});

View File

@ -0,0 +1,5 @@
{{#if item.truncated}}
<a class="expand-item" href {{action "expandItem"}} title={{i18n "post.expand_collapse"}}>
{{fa-icon "chevron-down"}}
</a>
{{/if}}

View File

@ -2,6 +2,7 @@
<div class='clearfix info'>
<a href="{{unbound post.user.userUrl}}" data-user-card="{{unbound post.user.username}}" class='avatar-link'><div class='avatar-wrapper'>{{avatar post.user imageSize="large" extraClasses="actor" ignoreTitle="true"}}</div></a>
<span class='time'>{{format-date post.created_at leaveAgo="true"}}</span>
{{expand-post item=post}}
<span class="title">
<a href={{post.url}}>{{{post.topic.fancyTitle}}}</a>
</span>
@ -13,6 +14,6 @@
</div>
</div>
<p class='excerpt'>
{{{unbound post.excerpt}}}
{{{post.excerpt}}}
</p>
</div>

View File

@ -1,11 +1,7 @@
<div class='clearfix info'>
<a href={{item.userUrl}} data-user-card={{item.username}} class='avatar-link'><div class='avatar-wrapper'>{{avatar item imageSize="large" extraClasses="actor" ignoreTitle="true"}}</div></a>
<span class='time'>{{format-date item.created_at}}</span>
{{#if item.truncated}}
<a class="expand-item" href {{action "expandItem"}} title={{i18n "post.expand_collapse"}}>
{{fa-icon "chevron-down"}}
</a>
{{/if}}
{{expand-post item=item}}
{{topic-status topic=item disableActions=true}}
<span class="title">
<a href={{item.postUrl}}>{{{item.title}}}</a>

View File

@ -1,4 +1,7 @@
require_relative 'post_item_excerpt'
class AdminUserActionSerializer < ApplicationSerializer
include PostItemExcerpt
attributes(
:id,
@ -13,7 +16,6 @@ class AdminUserActionSerializer < ApplicationSerializer
:title,
:category_id,
:truncated,
:excerpt,
:hidden,
:moderator_action,
:deleted,
@ -23,18 +25,10 @@ class AdminUserActionSerializer < ApplicationSerializer
:action_type
)
def truncated
true
end
def post_id
object.id
end
def include_truncated?
object.excerpt != object.cooked
end
def deleted
deleted_at.present?
end

View File

@ -1,10 +1,15 @@
require_relative 'post_item_excerpt'
class GroupPostSerializer < ApplicationSerializer
include PostItemExcerpt
attributes :id,
:excerpt,
:created_at,
:title,
:url,
:category
:category,
:post_number,
:topic_id
has_one :user, serializer: GroupPostUserSerializer, embed: :object
has_one :topic, serializer: BasicTopicSerializer, embed: :object

View File

@ -0,0 +1,25 @@
module PostItemExcerpt
def self.included(base)
base.attributes(:excerpt, :truncated)
end
def cooked
@cooked ||= object.cooked || PrettyText.cook(object.raw)
end
def excerpt
return nil unless cooked
@excerpt ||= PrettyText.excerpt(cooked, 300, keep_emoji_images: true)
end
def truncated
true
end
def include_truncated?
cooked.length > 300
end
end

View File

@ -1,9 +1,10 @@
require_relative 'post_item_excerpt'
class UserActionSerializer < ApplicationSerializer
include PostItemExcerpt
attributes :action_type,
:created_at,
:excerpt,
:truncated,
:avatar_template,
:acting_avatar_template,
:slug,
@ -30,22 +31,6 @@ class UserActionSerializer < ApplicationSerializer
:closed,
:archived
def cooked
@cooked ||= object.cooked || PrettyText.cook(object.raw)
end
def excerpt
return nil unless cooked
@excerpt ||= PrettyText.excerpt(cooked, 300, keep_emoji_images: true)
end
def truncated
true
end
def include_truncated?
cooked.length > 300
end
def avatar_template
User.avatar_template(object.username, object.uploaded_avatar_id)