framework/extensions/sticky/js/bootstrap.js
2015-05-07 22:26:02 +09:30

61 lines
2.0 KiB
JavaScript

import { extend } from 'flarum/extension-utils';
import Model from 'flarum/model';
import Discussion from 'flarum/models/discussion';
import DiscussionPage from 'flarum/components/discussion-page';
import Badge from 'flarum/components/badge';
import ActionButton from 'flarum/components/action-button';
import SettingsPage from 'flarum/components/settings-page';
import icon from 'flarum/helpers/icon';
import app from 'flarum/app';
import PostDiscussionStickied from 'sticky/components/post-discussion-stickied';
import NotificationDiscussionStickied from 'sticky/components/notification-discussion-stickied';
app.initializers.add('sticky', function() {
// Register components.
app.postComponentRegistry['discussionStickied'] = PostDiscussionStickied;
app.notificationComponentRegistry['discussionStickied'] = NotificationDiscussionStickied;
Discussion.prototype.isSticky = Model.prop('isSticky');
// Add a sticky badge to discussions.
extend(Discussion.prototype, 'badges', function(badges) {
if (this.isSticky()) {
badges.add('sticky', Badge.component({
label: 'Sticky',
icon: 'thumb-tack',
className: 'badge-sticky',
}));
}
});
function toggleSticky() {
this.save({isSticky: !this.isSticky()}).then(discussion => {
if (app.current instanceof DiscussionPage) {
app.current.stream().sync();
}
m.redraw();
});
}
// Add a sticky control to discussions.
extend(Discussion.prototype, 'controls', function(items) {
if (this.canEdit()) {
items.add('sticky', ActionButton.component({
label: this.isSticky() ? 'Unsticky' : 'Sticky',
icon: 'thumb-tack',
onclick: toggleSticky.bind(this)
}), {after: 'rename'});
}
});
// Add a notification preference.
extend(SettingsPage.prototype, 'notificationTypes', function(items) {
items.add('discussionStickied', {
name: 'discussionStickied',
label: [icon('thumb-tack'), ' Someone stickies a discussion I started']
});
});
});