2015-07-07 00:29:02 -04:00
|
|
|
import PostMenuComponent from 'discourse/components/post-menu';
|
2015-05-19 01:45:19 -04:00
|
|
|
import PostView from 'discourse/views/post';
|
2015-07-07 00:29:02 -04:00
|
|
|
import { Button } from 'discourse/components/post-menu';
|
2015-05-19 01:45:19 -04:00
|
|
|
import Topic from 'discourse/models/topic';
|
2015-05-19 02:26:22 -04:00
|
|
|
import User from 'discourse/models/user';
|
2015-06-19 02:08:43 -04:00
|
|
|
import TopicStatus from 'discourse/views/topic-status';
|
2015-08-14 06:36:06 -04:00
|
|
|
import computed from 'ember-addons/ember-computed-decorators';
|
2015-05-19 01:45:19 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'extend-for-solved-button',
|
|
|
|
initialize: function() {
|
|
|
|
|
2015-06-09 16:09:20 -04:00
|
|
|
Discourse.Category.reopen({
|
2015-08-14 06:36:06 -04:00
|
|
|
|
|
|
|
@computed("custom_fields")
|
|
|
|
enable_accepted_answers: {
|
|
|
|
get() {
|
|
|
|
const fields = this.get("custom_fields");
|
|
|
|
return fields && fields.enable_accepted_answers === "true";
|
|
|
|
},
|
|
|
|
set(value) {
|
2015-08-17 12:29:12 -04:00
|
|
|
value = value ? "true" : "false";
|
|
|
|
this.set("custom_fields.enable_accepted_answers", value);
|
|
|
|
return value;
|
2015-06-09 16:09:20 -04:00
|
|
|
}
|
2015-08-14 06:36:06 -04:00
|
|
|
}
|
|
|
|
|
2015-06-19 02:08:43 -04:00
|
|
|
});
|
2015-06-09 16:09:20 -04:00
|
|
|
|
2015-05-19 01:45:19 -04:00
|
|
|
Topic.reopen({
|
2015-05-19 02:26:22 -04:00
|
|
|
|
2015-05-19 01:45:19 -04:00
|
|
|
// keeping this here cause there is complex localization
|
|
|
|
acceptedAnswerHtml: function(){
|
2015-08-14 06:36:06 -04:00
|
|
|
const username = this.get('accepted_answer.username');
|
2015-05-19 02:26:22 -04:00
|
|
|
var postNumber = this.get('accepted_answer.post_number');
|
|
|
|
|
|
|
|
if (!username || !postNumber) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-06-15 00:24:49 -04:00
|
|
|
return I18n.t("solved.accepted_html", {
|
2015-05-19 02:26:22 -04:00
|
|
|
username_lower: username.toLowerCase(),
|
2015-08-14 06:36:06 -04:00
|
|
|
username,
|
2015-05-19 02:26:22 -04:00
|
|
|
post_path: this.get('url') + "/" + postNumber,
|
|
|
|
post_number: postNumber,
|
|
|
|
user_path: User.create({username: username}).get('path')
|
|
|
|
});
|
|
|
|
}.property('accepted_answer', 'id')
|
2015-05-19 01:45:19 -04:00
|
|
|
});
|
|
|
|
|
2015-06-15 02:26:40 -04:00
|
|
|
TopicStatus.reopen({
|
|
|
|
statuses: function(){
|
|
|
|
var results = this._super();
|
|
|
|
if (this.topic.has_accepted_answer) {
|
|
|
|
results.push({
|
|
|
|
openTag: 'span',
|
|
|
|
closeTag: 'span',
|
|
|
|
title: I18n.t('solved.has_accepted_answer'),
|
2015-06-15 03:06:49 -04:00
|
|
|
icon: 'check-square-o'
|
2015-06-15 02:26:40 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}.property()
|
|
|
|
});
|
|
|
|
|
2015-05-19 01:45:19 -04:00
|
|
|
PostView.reopen({
|
|
|
|
classNameBindings: ['post.accepted_answer:accepted-answer']
|
|
|
|
});
|
|
|
|
|
2015-07-07 00:29:02 -04:00
|
|
|
PostMenuComponent.registerButton(function(visibleButtons){
|
2015-06-10 04:31:49 -04:00
|
|
|
var position = 0;
|
|
|
|
|
|
|
|
var canAccept = this.get('post.can_accept_answer');
|
|
|
|
var canUnaccept = this.get('post.can_unaccept_answer');
|
|
|
|
var accepted = this.get('post.accepted_answer');
|
2015-06-10 05:39:37 -04:00
|
|
|
var isOp = Discourse.User.currentProp("id") === this.get('post.topic.user_id');
|
2015-06-10 04:31:49 -04:00
|
|
|
|
2015-06-10 05:39:37 -04:00
|
|
|
if (!accepted && canAccept && !isOp) {
|
2015-06-10 04:31:49 -04:00
|
|
|
// first hidden position
|
|
|
|
if (this.get('collapsed')) { return; }
|
|
|
|
position = visibleButtons.length - 2;
|
|
|
|
}
|
|
|
|
if (canAccept) {
|
2015-06-15 17:34:27 -04:00
|
|
|
visibleButtons.splice(position,0,new Button('acceptAnswer', 'solved.accept_answer', 'check-square-o', {className: 'unaccepted'}));
|
2015-05-19 01:45:19 -04:00
|
|
|
}
|
2015-06-10 04:31:49 -04:00
|
|
|
if (canUnaccept || accepted) {
|
2015-06-15 17:34:27 -04:00
|
|
|
var locale = canUnaccept ? 'solved.unaccept_answer' : 'solved.accepted_answer';
|
2015-06-10 04:31:49 -04:00
|
|
|
visibleButtons.splice(position,0,new Button(
|
|
|
|
'unacceptAnswer',
|
|
|
|
locale,
|
|
|
|
'check-square',
|
2015-07-14 02:18:56 -04:00
|
|
|
{className: 'accepted fade-out', prefixHTML: '<span class="accepted-text">' + I18n.t('solved.solution') + '</span>'})
|
2015-06-10 04:31:49 -04:00
|
|
|
);
|
2015-05-19 01:45:19 -04:00
|
|
|
}
|
2015-06-10 04:31:49 -04:00
|
|
|
|
2015-05-19 01:45:19 -04:00
|
|
|
});
|
|
|
|
|
2015-07-07 00:29:02 -04:00
|
|
|
PostMenuComponent.reopen({
|
2015-05-19 01:45:19 -04:00
|
|
|
acceptedChanged: function(){
|
|
|
|
this.rerender();
|
|
|
|
}.observes('post.accepted_answer'),
|
|
|
|
|
|
|
|
clickUnacceptAnswer: function(){
|
2015-06-10 04:31:49 -04:00
|
|
|
if (!this.get('post.can_unaccept_answer')) { return; }
|
|
|
|
|
2015-05-19 01:45:19 -04:00
|
|
|
this.set('post.can_accept_answer', true);
|
|
|
|
this.set('post.can_unaccept_answer', false);
|
2015-05-19 02:26:22 -04:00
|
|
|
this.set('post.accepted_answer', false);
|
|
|
|
this.set('post.topic.accepted_answer', undefined);
|
2015-05-19 01:45:19 -04:00
|
|
|
|
|
|
|
Discourse.ajax("/solution/unaccept", {
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
id: this.get('post.id')
|
|
|
|
}
|
|
|
|
}).then(function(){
|
|
|
|
//
|
|
|
|
}).catch(function(error){
|
|
|
|
var message = I18n.t("generic_error");
|
|
|
|
try {
|
|
|
|
message = $.parseJSON(error.responseText).errors;
|
|
|
|
} catch (e) {
|
|
|
|
// nothing we can do
|
|
|
|
}
|
|
|
|
bootbox.alert(message);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearAcceptedAnswer: function(){
|
|
|
|
const posts = this.get('post.topic.postStream.posts');
|
|
|
|
posts.forEach(function(post){
|
|
|
|
if (post.get('post_number') > 1 ) {
|
|
|
|
post.set('accepted_answer',false);
|
|
|
|
post.set('can_accept_answer',true);
|
|
|
|
post.set('can_unaccept_answer',false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clickAcceptAnswer: function(){
|
|
|
|
|
|
|
|
this.clearAcceptedAnswer();
|
|
|
|
|
|
|
|
this.set('post.can_unaccept_answer', true);
|
|
|
|
this.set('post.can_accept_answer', false);
|
|
|
|
this.set('post.accepted_answer', true);
|
|
|
|
|
|
|
|
this.set('post.topic.accepted_answer', {
|
|
|
|
username: this.get('post.username'),
|
|
|
|
post_number: this.get('post.post_number')
|
|
|
|
});
|
|
|
|
|
|
|
|
Discourse.ajax("/solution/accept", {
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
id: this.get('post.id')
|
|
|
|
}
|
|
|
|
}).then(function(){
|
|
|
|
//
|
|
|
|
}).catch(function(error){
|
|
|
|
var message = I18n.t("generic_error");
|
|
|
|
try {
|
|
|
|
message = $.parseJSON(error.responseText).errors;
|
|
|
|
} catch (e) {
|
|
|
|
// nothing we can do
|
|
|
|
}
|
|
|
|
bootbox.alert(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|