Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwatsrt committed Oct 28, 2016
1 parent 088bf81 commit dd47ccb
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 122 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
Thumbs.db
node_modules
2 changes: 1 addition & 1 deletion js/admin/Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ var gulp = require('flarum-gulp');

gulp({
modules: {
'wiwatSrt/bestAnswer/': 'src/**/*.js'
'wiwatSrt/bestAnswer': 'src/**/*.js'
}
});
4 changes: 1 addition & 3 deletions js/forum/Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ var gulp = require('flarum-gulp');

gulp({
modules: {
'wiwatsrt/best-answer': [
'src/**/*.js'
]
'wiwatSrt/bestAnswer': 'src/**/*.js'
}
});
140 changes: 52 additions & 88 deletions js/forum/dist/extension.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

System.register('wiwatSrt/bestAnswer/addBestAnswerAction', ['flarum/extend', 'flarum/app', 'flarum/components/Button', 'flarum/components/DiscussionPage', 'flarum/components/CommentPost'], function (_export, _context) {
System.register('wiwatSrt/bestAnswer/addBestAnswerAction', ['flarum/app', 'flarum/extend', 'flarum/components/Button', 'flarum/components/CommentPost', 'flarum/components/DiscussionPage'], function (_export, _context) {
"use strict";

var extend, app, Button, DiscussionPage, CommentPost;
var app, extend, Button, CommentPost, DiscussionPage;

_export('default', function () {
extend(CommentPost.prototype, 'actionItems', function (items) {
Expand All @@ -12,8 +12,8 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerAction', ['flarum/extend', 'fl

if (post.isHidden() || !discussion.canSelectBestAnswer() || !app.session.user || discussion.attribute('startUserId') != app.session.user.id()) return;

var isBestAnswer = (discussion.bestAnswerPost() && discussion.bestAnswerPost() == post);
post.pushAttributes({isBestAnswer});
var isBestAnswer = discussion.bestAnswerPost() && discussion.bestAnswerPost() == post;
post.pushAttributes({ isBestAnswer: isBestAnswer });

items.add('bestAnswer', Button.component({
children: app.translator.trans(isBestAnswer ? 'flarum-best-answer.forum.remove_best_answer' : 'flarum-best-answer.forum.this_best_answer'),
Expand All @@ -22,14 +22,14 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerAction', ['flarum/extend', 'fl
isBestAnswer = !isBestAnswer;
discussion.save({
bestAnswerPostId: isBestAnswer ? post.id() : 0,
relationships: isBestAnswer ? {bestAnswerPost: post} : delete discussion.data.relationships.bestAnswerPost
}).then(() => {
relationships: isBestAnswer ? { bestAnswerPost: post } : delete discussion.data.relationships.bestAnswerPost
}).then(function () {
if (app.current instanceof DiscussionPage) {
app.current.stream.update();
}
m.redraw();
});
if(isBestAnswer) {
if (isBestAnswer) {
m.route(app.route.post(discussion.posts()[0]));
}
}
Expand All @@ -38,27 +38,26 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerAction', ['flarum/extend', 'fl
});

return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumApp) {
setters: [function (_flarumApp) {
app = _flarumApp.default;
}, function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumComponentsButton) {
Button = _flarumComponentsButton.default;
}, function (_flarumComponentsDiscussionPage) {
DiscussionPage = _flarumComponentsDiscussionPage.default;
}, function (_flarumComponentsCommentPost) {
CommentPost = _flarumComponentsCommentPost.default;
}, function (_flarumComponentsDiscussionPage) {
DiscussionPage = _flarumComponentsDiscussionPage.default;
}],
execute: function () {
}
execute: function () {}
};
});
});;
'use strict';

System.register('wiwatSrt/bestAnswer/addBestAnswerAttribute', ['flarum/extend', 'flarum/app', 'flarum/components/Post', 'flarum/components/CommentPost'], function (_export, _context) {
System.register('wiwatSrt/bestAnswer/addBestAnswerAttribute', ['flarum/app', 'flarum/extend', 'flarum/components/CommentPost', 'flarum/components/Post'], function (_export, _context) {
"use strict";

var extend, app, PostComponent, CommentPost;
var app, extend, CommentPost, PostComponent;

_export('default', function () {
extend(CommentPost.prototype, 'headerItems', function (items) {
Expand All @@ -75,25 +74,25 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerAttribute', ['flarum/extend',
});

return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumApp) {
setters: [function (_flarumApp) {
app = _flarumApp.default;
}, function (_flarumComponentsPost) {
PostComponent = _flarumComponentsPost.default;
}, function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumComponentsCommentPost) {
CommentPost = _flarumComponentsCommentPost.default;
}, function (_flarumComponentsPost) {
PostComponent = _flarumComponentsPost.default;
}],
execute: function () {
}
execute: function () {}
};
});
});;
'use strict';

System.register('wiwatSrt/bestAnswer/addBadgeBestAnswer', ['flarum/extend', 'flarum/app', 'flarum/models/Discussion', 'flarum/components/Badge'], function (_export, _context) {
System.register('wiwatSrt/bestAnswer/addBestAnswerBadge', ['flarum/app', 'flarum/extend', 'flarum/components/Badge', 'flarum/models/Discussion'], function (_export, _context) {
"use strict";

var extend, app, Discussion, Badge;
var app, extend, Badge, Discussion;

_export('default', function () {
extend(Discussion.prototype, 'badges', function (items) {
if (this.bestAnswerPost() && !items.has('hidden')) {
Expand All @@ -107,26 +106,26 @@ System.register('wiwatSrt/bestAnswer/addBadgeBestAnswer', ['flarum/extend', 'fla
});

return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumApp) {
setters: [function (_flarumApp) {
app = _flarumApp.default;
}, function (_flarumModelsDiscussion) {
Discussion = _flarumModelsDiscussion.default;
}, function (_flarumExtend) {
extend = _flarumExtend.extend;
}, function (_flarumComponentsBadge) {
Badge = _flarumComponentsBadge.default;
}, function (_flarumModelsDiscussion) {
Discussion = _flarumModelsDiscussion.default;
}],
execute: function () {
}
execute: function () {}
};
});
});;
'use strict';

System.register('wiwatSrt/bestAnswer/addBestAnswerFirstPost', ['flarum/app', 'flarum/extend', 'flarum/components/CommentPost', 'flarum/components/PostMeta', 'flarum/helpers/icon', 'flarum/helpers/username', 'flarum/helpers/userOnline'], function (_export, _context) {
System.register('wiwatSrt/bestAnswer/addBestAnswerFirstPost', ['flarum/app', 'flarum/extend', 'flarum/components/CommentPost', 'flarum/helpers/icon', 'flarum/components/PostMeta', 'flarum/helpers/username', 'flarum/helpers/userOnline'], function (_export, _context) {
"use strict";

var app, extend, icon, username, userOnline, CommentPost, PostMeta;
function addBestAnswerFirstPost() {
var app, extend, CommentPost, icon, PostMeta, username, userOnline;

_export('default', function () {
extend(CommentPost.prototype, 'footerItems', function (items) {

var discussion = this.props.post.discussion();
Expand All @@ -135,48 +134,14 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerFirstPost', ['flarum/app', 'fl
var post = discussion.bestAnswerPost();
if (post.isHidden()) return;
var user = discussion.bestAnswerPost().user();
items.add('bestAnswerPost', m(
'div',
{className: 'CommentPost'},
m(".Post-header",
m('ul',
m('li',
{className: 'item-user'},
m('.PostUser',
userOnline(user),
m('h3',
m(
'a',
{href: app.route.user(user), config: m.route},
username(user)
)
)
)
), m('li',
{className: 'item-meta'},
PostMeta.component({post})
), m('li',
{className: 'item-bestAnswerButton'}, m('a',
{
href: app.route.post(post),
config: m.route,
'data-number': post.number()
},
icon('check'),
app.translator.trans('flarum-best-answer.forum.best_answer_button')
)
)
)
),
m(".Post-body",
m.trust(discussion.bestAnswerPost().contentHtml())
)
), -10);
items.add('bestAnswerPost', m('div', { className: 'CommentPost' }, m(".Post-header", m('ul', m('li', { className: 'item-user' }, m('.PostUser', userOnline(user), m('h3', m('a', { href: app.route.user(user), config: m.route }, username(user))))), m('li', { className: 'item-meta' }, PostMeta.component({ post: post })), m('li', { className: 'item-bestAnswerButton' }, m('a', {
href: app.route.post(post),
config: m.route,
'data-number': post.number()
}, icon('check'), app.translator.trans('flarum-best-answer.forum.best_answer_button'))))), m(".Post-body", m.trust(discussion.bestAnswerPost().contentHtml()))), -10);
}
});
}

_export('default', addBestAnswerFirstPost);
});

return {
setters: [function (_flarumApp) {
Expand All @@ -185,25 +150,24 @@ System.register('wiwatSrt/bestAnswer/addBestAnswerFirstPost', ['flarum/app', 'fl
extend = _flarumExtend.extend;
}, function (_flarumComponentsCommentPost) {
CommentPost = _flarumComponentsCommentPost.default;
}, function (_flarumComponentsPostMeta) {
PostMeta = _flarumComponentsPostMeta.default;
}, function (_flarumHelpersIcon) {
icon = _flarumHelpersIcon.default;
}, function (_flarumComponentsPostMeta) {
PostMeta = _flarumComponentsPostMeta.default;
}, function (_flarumHelpersUsername) {
username = _flarumHelpersUsername.default;
}, function (_flarumHelpersUserOnline) {
userOnline = _flarumHelpersUserOnline.default;
}],
execute: function () {}
};
});
});;
'use strict';

System.register('wiwatSrt/bestAnswer/main', ['flarum/app', 'flarum/extend', 'flarum/Model', 'flarum/models/Discussion', 'wiwatSrt/bestAnswer/addBestAnswerAction', 'wiwatSrt/bestAnswer/addBestAnswerAttribute', 'wiwatSrt/bestAnswer/addBadgeBestAnswer', 'wiwatSrt/bestAnswer/addBestAnswerFirstPost'], function (_export, _context) {
System.register('wiwatSrt/bestAnswer/main', ['flarum/app', 'flarum/extend', 'flarum/models/Discussion', 'flarum/Model', 'wiwatSrt/bestAnswer/addBestAnswerAction', 'wiwatSrt/bestAnswer/addBestAnswerAttribute', 'wiwatSrt/bestAnswer/addBestAnswerBadge', 'wiwatSrt/bestAnswer/addBestAnswerFirstPost'], function (_export, _context) {
"use strict";

var app, extend, Model, Discussion, addBestAnswerAction, addBestAnswerAttribute, addBadgeBestAnswer, addBestAnswerFirstPost;

var app, extend, Discussion, Model, addBestAnswerAction, addBestAnswerAttribute, addBestAnswerBadge, addBestAnswerFirstPost;
return {
setters: [function (_flarumApp) {
app = _flarumApp.default;
Expand All @@ -217,20 +181,20 @@ System.register('wiwatSrt/bestAnswer/main', ['flarum/app', 'flarum/extend', 'fla
addBestAnswerAction = _wiwatSrtBestAnswerAddBestAnswerAction.default;
}, function (_wiwatSrtBestAnswerAddBestAnswerAttribute) {
addBestAnswerAttribute = _wiwatSrtBestAnswerAddBestAnswerAttribute.default;
}, function (_wiwatSrtBestAnswerAddBadgeBestAnswer) {
addBadgeBestAnswer = _wiwatSrtBestAnswerAddBadgeBestAnswer.default;
}, function (_wiwatSrtBestAnswerAddBestAnswerBadge) {
addBestAnswerBadge = _wiwatSrtBestAnswerAddBestAnswerBadge.default;
}, function (_wiwatSrtBestAnswerAddBestAnswerFirstPost) {
addBestAnswerFirstPost = _wiwatSrtBestAnswerAddBestAnswerFirstPost.default;
}],

execute: function () {

app.initializers.add('wiwatSrt-bestAnswer', function () {
Discussion.prototype.bestAnswerPost = Model.hasOne('bestAnswerPost');
Discussion.prototype.canSelectBestAnswer = Model.attribute('canSelectBestAnswer');

addBestAnswerAction();
addBestAnswerAttribute();
addBadgeBestAnswer();
addBestAnswerBadge();
addBestAnswerFirstPost();
});
}
Expand Down
23 changes: 17 additions & 6 deletions js/forum/src/addBestAnswerAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@ import app from 'flarum/app';
import { extend } from 'flarum/extend';
import Button from 'flarum/components/Button';
import CommentPost from 'flarum/components/CommentPost';
import DiscussionPage from 'flarum/components/DiscussionPage';

export default function() {
extend(CommentPost.prototype, 'actionItems', function (items) {
var post = this.props.post;
var discussion = this.props.post.discussion();
var startUserId = discussion.attribute('startUserId');
var canSelectBestAnswer = discussion.attribute('canSelectBestAnswer');
var isBestAnswer = post.attribute('isBestAnswer');

if (post.isHidden() || !canSelectBestAnswer || post.number() == 1 || !app.session.user || startUserId != app.session.user.id()) return;
if (post.isHidden() || !discussion.canSelectBestAnswer() || !app.session.user || discussion.attribute('startUserId') != app.session.user.id()) return;

var isBestAnswer = (discussion.bestAnswerPost() && discussion.bestAnswerPost() == post);
post.pushAttributes({isBestAnswer});

items.add('bestAnswer', Button.component({
children: app.translator.trans(isBestAnswer ? 'flarum-best-answer.forum.remove_best_answer' : 'flarum-best-answer.forum.this_best_answer'),
className: 'Button Button--link',
onclick: function onclick() {
isBestAnswer = !isBestAnswer;
post.save({isBestAnswer: isBestAnswer});
discussion.pushAttributes({isBestAnswer: isBestAnswer});
discussion.save({
bestAnswerPostId: isBestAnswer ? post.id() : 0,
relationships: isBestAnswer ? {bestAnswerPost: post} : delete discussion.data.relationships.bestAnswerPost
}).then(() => {
if (app.current instanceof DiscussionPage) {
app.current.stream.update();
}
m.redraw();
});
if(isBestAnswer) {
m.route(app.route.post(discussion.posts()[0]));
}
}
}));
});
Expand Down
6 changes: 3 additions & 3 deletions js/forum/src/addBestAnswerAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import PostComponent from 'flarum/components/Post';

export default function() {
extend(CommentPost.prototype, 'headerItems', function (items) {
if (this.props.post.attribute('isBestAnswer') && !this.props.post.isHidden()) {
if (this.props.post.discussion().bestAnswerPost() == this.props.post && !this.props.post.isHidden()) {
items.add('isBestAnswer', app.translator.trans('flarum-best-answer.forum.best_answer_button'));
}
});

extend(PostComponent.prototype, 'attrs', function (attrs) {
if (this.props.post.attribute('isBestAnswer') && !this.props.post.isHidden()) {
attrs.className += ' Post--best-answer';
if (this.props.post.discussion().bestAnswerPost() == this.props.post && !this.props.post.isHidden()) {
attrs.className += ' Post--bestAnswer';
}
});
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import app from 'flarum/app';
import { extend } from 'flarum/extend';
import Discussion from 'flarum/models/Discussion';
import Badge from 'flarum/components/Badge';
import Discussion from 'flarum/models/Discussion';

export default function() {
extend(Discussion.prototype, 'badges', function (items) {
if (this.attribute('hasBestAnswer') && !items.has('hidden')) {
if (this.bestAnswerPost() && !items.has('hidden')) {
items.add('bestAnswer', m(Badge, {
type: 'bestAnswer',
icon: 'check',
Expand Down
Loading

0 comments on commit dd47ccb

Please sign in to comment.