Refactor post components and controls

This commit is contained in:
Toby Zerner
2015-05-05 17:07:12 +09:30
parent 762137c6df
commit 11b39605e2
7 changed files with 126 additions and 128 deletions

View File

@ -0,0 +1,48 @@
import Post from 'flarum/models/post';
import ComposerEdit from 'flarum/components/composer-edit';
import ActionButton from 'flarum/components/action-button';
import Separator from 'flarum/components/separator';
import ItemList from 'flarum/utils/item-list';
export default function(app) {
function editAction() {
app.composer.load(new ComposerEdit({ post: this }));
app.composer.show();
}
function hideAction() {
this.save({ isHidden: true });
this.pushData({ hideTime: new Date(), hideUser: app.session.user() });
}
function restoreAction() {
this.save({ isHidden: false });
this.pushData({ hideTime: null, hideUser: null });
}
function deleteAction() {
this.delete();
if (app.current instanceof DiscussionPage) {
app.current.stream().removePost(this.id());
}
}
Post.prototype.controls = function(context) {
var items = new ItemList();
if (this.contentType() === 'comment' && this.canEdit()) {
if (this.isHidden()) {
items.add('restore', ActionButton.component({ icon: 'reply', label: 'Restore', onclick: restoreAction.bind(this) }));
} else {
items.add('edit', ActionButton.component({ icon: 'pencil', label: 'Edit', onclick: editAction.bind(this) }));
items.add('hide', ActionButton.component({ icon: 'times', label: 'Delete', onclick: hideAction.bind(this) }));
}
}
if ((this.contentType() !== 'comment' || this.isHidden()) && this.canDelete()) {
items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: deleteAction.bind(this) }));
}
return items;
}
};