mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 23:29:57 +08:00
Refactor post components and controls
This commit is contained in:
48
js/forum/src/initializers/post-controls.js
Normal file
48
js/forum/src/initializers/post-controls.js
Normal 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;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user