mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 23:29:57 +08:00
Move discussion controls out of lib and into forum
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import App from 'flarum/utils/app';
|
import App from 'flarum/utils/app';
|
||||||
import store from 'flarum/initializers/store';
|
import store from 'flarum/initializers/store';
|
||||||
|
import discussionControls from 'flarum/initializers/discussion-controls';
|
||||||
import preload from 'flarum/initializers/preload';
|
import preload from 'flarum/initializers/preload';
|
||||||
import session from 'flarum/initializers/session';
|
import session from 'flarum/initializers/session';
|
||||||
import routes from 'flarum/initializers/routes';
|
import routes from 'flarum/initializers/routes';
|
||||||
@ -10,6 +11,7 @@ import boot from 'flarum/initializers/boot';
|
|||||||
var app = new App();
|
var app = new App();
|
||||||
|
|
||||||
app.initializers.add('store', store);
|
app.initializers.add('store', store);
|
||||||
|
app.initializers.add('discussion-controls', discussionControls);
|
||||||
app.initializers.add('session', session);
|
app.initializers.add('session', session);
|
||||||
app.initializers.add('routes', routes);
|
app.initializers.add('routes', routes);
|
||||||
app.initializers.add('components', components);
|
app.initializers.add('components', components);
|
||||||
|
75
js/forum/src/initializers/discussion-controls.js
Normal file
75
js/forum/src/initializers/discussion-controls.js
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import Discussion from 'flarum/models/discussion';
|
||||||
|
import DiscussionPage from 'flarum/components/discussion-page';
|
||||||
|
import ComposerReply from 'flarum/components/composer-reply';
|
||||||
|
import LoginModal from 'flarum/components/login-modal';
|
||||||
|
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 replyAction() {
|
||||||
|
if (app.session.user() && this.canReply()) {
|
||||||
|
if (app.current.discussion && app.current.discussion().id() === this.id()) {
|
||||||
|
app.current.streamContent.goToLast();
|
||||||
|
}
|
||||||
|
app.composer.load(new ComposerReply({
|
||||||
|
user: app.session.user(),
|
||||||
|
discussion: this
|
||||||
|
}));
|
||||||
|
app.composer.show();
|
||||||
|
} else if (!app.session.user()) {
|
||||||
|
app.modal.show(new LoginModal({
|
||||||
|
message: 'You must be logged in to do that.',
|
||||||
|
callback: this.replyAction.bind(this)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteAction() {
|
||||||
|
if (confirm('Are you sure you want to delete this discussion?')) {
|
||||||
|
this.delete();
|
||||||
|
if (app.cache.discussionList) {
|
||||||
|
app.cache.discussionList.removeDiscussion(this);
|
||||||
|
}
|
||||||
|
if (app.current instanceof DiscussionPage && app.current.discussion().id() === this.id()) {
|
||||||
|
app.history.back();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function renameAction() {
|
||||||
|
var currentTitle = this.title();
|
||||||
|
var title = prompt('Enter a new title for this discussion:', currentTitle);
|
||||||
|
if (title && title !== currentTitle) {
|
||||||
|
this.save({title}).then(discussion => {
|
||||||
|
if (app.current instanceof DiscussionPage) {
|
||||||
|
app.current.stream().sync();
|
||||||
|
}
|
||||||
|
m.redraw();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Discussion.prototype.controls = function(context) {
|
||||||
|
var items = new ItemList();
|
||||||
|
|
||||||
|
if (context instanceof DiscussionPage) {
|
||||||
|
items.add('reply', !app.session.user() || this.canReply()
|
||||||
|
? ActionButton.component({ icon: 'reply', label: app.session.user() ? 'Reply' : 'Log In to Reply', onclick: replyAction.bind(this) })
|
||||||
|
: ActionButton.component({ icon: 'reply', label: 'Can\'t Reply', className: 'disabled', title: 'You don\'t have permission to reply to this discussion.' })
|
||||||
|
);
|
||||||
|
|
||||||
|
items.add('separator', Separator.component());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.canEdit()) {
|
||||||
|
items.add('rename', ActionButton.component({ icon: 'pencil', label: 'Rename', onclick: renameAction.bind(this) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.canDelete()) {
|
||||||
|
items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: deleteAction.bind(this) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
};
|
@ -1,11 +1,6 @@
|
|||||||
import Model from 'flarum/model';
|
import Model from 'flarum/model';
|
||||||
import computed from 'flarum/utils/computed';
|
import computed from 'flarum/utils/computed';
|
||||||
import ItemList from 'flarum/utils/item-list';
|
import ItemList from 'flarum/utils/item-list';
|
||||||
import DiscussionPage from 'flarum/components/discussion-page';
|
|
||||||
import ActionButton from 'flarum/components/action-button';
|
|
||||||
import Separator from 'flarum/components/separator';
|
|
||||||
import ComposerReply from 'flarum/components/composer-reply';
|
|
||||||
import LoginModal from 'flarum/components/login-modal';
|
|
||||||
|
|
||||||
class Discussion extends Model {
|
class Discussion extends Model {
|
||||||
unreadCount() {
|
unreadCount() {
|
||||||
@ -19,72 +14,6 @@ class Discussion extends Model {
|
|||||||
badges() {
|
badges() {
|
||||||
return new ItemList();
|
return new ItemList();
|
||||||
}
|
}
|
||||||
|
|
||||||
controls(context) {
|
|
||||||
var items = new ItemList();
|
|
||||||
|
|
||||||
if (context instanceof DiscussionPage) {
|
|
||||||
items.add('reply', !app.session.user() || this.canReply()
|
|
||||||
? ActionButton.component({ icon: 'reply', label: app.session.user() ? 'Reply' : 'Log In to Reply', onclick: this.replyAction.bind(this) })
|
|
||||||
: ActionButton.component({ icon: 'reply', label: 'Can\'t Reply', className: 'disabled', title: 'You don\'t have permission to reply to this discussion.' })
|
|
||||||
);
|
|
||||||
|
|
||||||
items.add('separator', Separator.component());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.canEdit()) {
|
|
||||||
items.add('rename', ActionButton.component({ icon: 'pencil', label: 'Rename', onclick: this.renameAction.bind(this) }));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.canDelete()) {
|
|
||||||
items.add('delete', ActionButton.component({ icon: 'times', label: 'Delete', onclick: this.deleteAction.bind(this) }));
|
|
||||||
}
|
|
||||||
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
replyAction() {
|
|
||||||
if (app.session.user() && this.canReply()) {
|
|
||||||
if (app.current.discussion && app.current.discussion().id() === this.id()) {
|
|
||||||
app.current.streamContent.goToLast();
|
|
||||||
}
|
|
||||||
app.composer.load(new ComposerReply({
|
|
||||||
user: app.session.user(),
|
|
||||||
discussion: this
|
|
||||||
}));
|
|
||||||
app.composer.show();
|
|
||||||
} else if (!app.session.user()) {
|
|
||||||
app.modal.show(new LoginModal({
|
|
||||||
message: 'You must be logged in to do that.',
|
|
||||||
callback: this.replyAction.bind(this)
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteAction() {
|
|
||||||
if (confirm('Are you sure you want to delete this discussion?')) {
|
|
||||||
this.delete();
|
|
||||||
if (app.cache.discussionList) {
|
|
||||||
app.cache.discussionList.removeDiscussion(this);
|
|
||||||
}
|
|
||||||
if (app.current.discussion && app.current.discussion().id() === this.id()) {
|
|
||||||
app.history.back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
renameAction() {
|
|
||||||
var currentTitle = this.title();
|
|
||||||
var title = prompt('Enter a new title for this discussion:', currentTitle);
|
|
||||||
if (title && title !== currentTitle) {
|
|
||||||
this.save({title}).then(discussion => {
|
|
||||||
if (app.current instanceof DiscussionPage) {
|
|
||||||
app.current.stream().sync();
|
|
||||||
}
|
|
||||||
m.redraw();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Discussion.prototype.id = Model.prop('id');
|
Discussion.prototype.id = Model.prop('id');
|
||||||
|
Reference in New Issue
Block a user