2018-08-01 10:05:46 -04:00
|
|
|
import Topic from "discourse/models/topic";
|
|
|
|
import User from "discourse/models/user";
|
|
|
|
import TopicStatus from "discourse/raw-views/topic-status";
|
2019-03-13 13:10:54 -04:00
|
|
|
import TopicStatusIcons from "discourse/helpers/topic-status-icons";
|
2018-08-01 10:05:46 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import PostCooked from "discourse/widgets/post-cooked";
|
|
|
|
import { formatUsername } from "discourse/lib/utilities";
|
2018-11-12 13:26:28 -05:00
|
|
|
import { iconHTML } from "discourse-common/lib/icon-library";
|
2016-02-17 15:58:32 -05:00
|
|
|
|
|
|
|
function clearAccepted(topic) {
|
2018-08-01 10:05:46 -04:00
|
|
|
const posts = topic.get("postStream.posts");
|
2016-02-17 15:58:32 -05:00
|
|
|
posts.forEach(post => {
|
2018-08-01 10:05:46 -04:00
|
|
|
if (post.get("post_number") > 1) {
|
|
|
|
post.set("accepted_answer", false);
|
|
|
|
post.set("can_accept_answer", true);
|
|
|
|
post.set("can_unaccept_answer", false);
|
2016-02-17 15:58:32 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function unacceptPost(post) {
|
2018-08-01 10:05:46 -04:00
|
|
|
if (!post.get("can_unaccept_answer")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-17 15:58:32 -05:00
|
|
|
const topic = post.topic;
|
|
|
|
|
|
|
|
post.setProperties({
|
|
|
|
can_accept_answer: true,
|
|
|
|
can_unaccept_answer: false,
|
|
|
|
accepted_answer: false
|
|
|
|
});
|
2018-08-01 10:05:46 -04:00
|
|
|
topic.set("accepted_answer", undefined);
|
2016-02-17 15:58:32 -05:00
|
|
|
|
2016-11-01 09:25:57 -04:00
|
|
|
ajax("/solution/unaccept", {
|
2018-08-01 10:05:46 -04:00
|
|
|
type: "POST",
|
|
|
|
data: { id: post.get("id") }
|
2016-02-17 15:58:32 -05:00
|
|
|
}).catch(popupAjaxError);
|
|
|
|
}
|
|
|
|
|
|
|
|
function acceptPost(post) {
|
|
|
|
const topic = post.topic;
|
|
|
|
|
|
|
|
clearAccepted(topic);
|
|
|
|
|
|
|
|
post.setProperties({
|
|
|
|
can_unaccept_answer: true,
|
|
|
|
can_accept_answer: false,
|
|
|
|
accepted_answer: true
|
|
|
|
});
|
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
topic.set("accepted_answer", {
|
|
|
|
username: post.get("username"),
|
|
|
|
post_number: post.get("post_number"),
|
|
|
|
excerpt: post.get("cooked")
|
2016-02-17 15:58:32 -05:00
|
|
|
});
|
|
|
|
|
2016-11-01 09:25:57 -04:00
|
|
|
ajax("/solution/accept", {
|
2018-08-01 10:05:46 -04:00
|
|
|
type: "POST",
|
|
|
|
data: { id: post.get("id") }
|
2016-02-17 15:58:32 -05:00
|
|
|
}).catch(popupAjaxError);
|
|
|
|
}
|
|
|
|
|
2019-07-15 10:30:58 -04:00
|
|
|
function initializeWithApi(api, container) {
|
2019-06-28 18:12:41 -04:00
|
|
|
const currentUser = api.getCurrentUser();
|
2019-07-15 10:30:58 -04:00
|
|
|
const { mobileView } = container.lookup("site:main");
|
2016-02-17 15:58:32 -05:00
|
|
|
|
2019-06-28 18:12:41 -04:00
|
|
|
TopicStatusIcons.addObject([
|
|
|
|
"has_accepted_answer",
|
|
|
|
"far-check-square",
|
|
|
|
"solved"
|
|
|
|
]);
|
2019-03-13 13:10:54 -04:00
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
api.includePostAttributes(
|
|
|
|
"can_accept_answer",
|
|
|
|
"can_unaccept_answer",
|
|
|
|
"accepted_answer"
|
|
|
|
);
|
2016-02-17 15:58:32 -05:00
|
|
|
|
2017-02-27 17:30:37 -05:00
|
|
|
if (api.addDiscoveryQueryParam) {
|
2018-08-01 10:05:46 -04:00
|
|
|
api.addDiscoveryQueryParam("solved", { replace: true, refreshModel: true });
|
2017-02-27 17:30:37 -05:00
|
|
|
}
|
2017-02-27 17:15:05 -05:00
|
|
|
|
2019-06-28 18:12:41 -04:00
|
|
|
api.addPostMenuButton("solved", attrs => {
|
|
|
|
const isOp = currentUser && currentUser.id === attrs.topicCreatedById;
|
|
|
|
const position =
|
2019-07-15 10:30:58 -04:00
|
|
|
!attrs.accepted_answer && attrs.can_accept_answer && !isOp
|
|
|
|
? "second-last-hidden"
|
|
|
|
: "first";
|
2019-06-28 18:12:41 -04:00
|
|
|
|
2019-07-15 10:30:58 -04:00
|
|
|
if (!mobileView && !attrs.accepted_answer && attrs.can_accept_answer) {
|
2019-06-28 18:12:41 -04:00
|
|
|
return {
|
|
|
|
action: "acceptAnswer",
|
|
|
|
icon: "far-check-square",
|
|
|
|
title: "solved.accept_answer",
|
2019-07-15 10:30:58 -04:00
|
|
|
className: "unaccepted",
|
|
|
|
position,
|
2019-07-16 11:08:05 -04:00
|
|
|
label: "solved.mark_as_solution"
|
2019-06-28 18:12:41 -04:00
|
|
|
};
|
2019-07-15 10:30:58 -04:00
|
|
|
} else if (attrs.accepted_answer) {
|
2019-06-28 18:12:41 -04:00
|
|
|
return {
|
2019-07-15 10:30:58 -04:00
|
|
|
action: attrs.can_unaccept_answer ? "unacceptAnswer" : "",
|
2019-06-28 18:12:41 -04:00
|
|
|
icon: "check-square",
|
2019-07-15 10:30:58 -04:00
|
|
|
title: "solved.accept_answer",
|
|
|
|
className: "accepted",
|
|
|
|
position: "first",
|
2019-07-16 11:08:05 -04:00
|
|
|
label: "solved.solution"
|
2019-06-28 18:12:41 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2016-02-17 15:58:32 -05:00
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
api.decorateWidget("post-contents:after-cooked", dec => {
|
2016-02-23 12:09:36 -05:00
|
|
|
if (dec.attrs.post_number === 1) {
|
2016-11-23 10:41:02 -05:00
|
|
|
const postModel = dec.getModel();
|
|
|
|
if (postModel) {
|
2018-08-01 10:05:46 -04:00
|
|
|
const topic = postModel.get("topic");
|
|
|
|
if (topic.get("accepted_answer")) {
|
|
|
|
const hasExcerpt = !!topic.get("accepted_answer").excerpt;
|
2017-03-07 19:38:15 -05:00
|
|
|
|
2017-08-06 17:22:30 -04:00
|
|
|
const withExcerpt = `
|
2019-03-14 02:19:58 -04:00
|
|
|
<aside class='quote accepted-answer' data-post="${
|
2018-08-01 10:05:46 -04:00
|
|
|
topic.get("accepted_answer").post_number
|
|
|
|
}" data-topic="${topic.get("id")}">
|
2017-03-07 19:38:15 -05:00
|
|
|
<div class='title'>
|
2018-08-01 10:05:46 -04:00
|
|
|
${topic.get(
|
|
|
|
"acceptedAnswerHtml"
|
|
|
|
)} <div class="quote-controls"><\/div>
|
2017-03-07 18:33:10 -05:00
|
|
|
</div>
|
2017-03-08 10:21:41 -05:00
|
|
|
<blockquote>
|
2018-08-01 10:05:46 -04:00
|
|
|
${topic.get("accepted_answer").excerpt}
|
2017-03-08 10:21:41 -05:00
|
|
|
</blockquote>
|
2017-07-05 14:18:26 -04:00
|
|
|
</aside>`;
|
2017-03-07 18:33:10 -05:00
|
|
|
|
2017-08-06 17:22:30 -04:00
|
|
|
const withoutExcerpt = `
|
2019-03-14 02:19:58 -04:00
|
|
|
<aside class='quote accepted-answer'>
|
2017-08-06 17:22:30 -04:00
|
|
|
<div class='title title-only'>
|
2018-08-01 10:05:46 -04:00
|
|
|
${topic.get("acceptedAnswerHtml")}
|
2017-08-06 17:22:30 -04:00
|
|
|
</div>
|
|
|
|
</aside>`;
|
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
var cooked = new PostCooked({
|
|
|
|
cooked: hasExcerpt ? withExcerpt : withoutExcerpt
|
|
|
|
});
|
2017-03-07 18:33:10 -05:00
|
|
|
|
|
|
|
var html = cooked.init();
|
|
|
|
|
|
|
|
return dec.rawHtml(html);
|
2016-11-23 10:41:02 -05:00
|
|
|
}
|
2016-02-23 12:09:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-15 10:30:58 -04:00
|
|
|
if (mobileView) {
|
|
|
|
api.decorateWidget("post-contents:after-cooked", dec => {
|
|
|
|
const model = dec.getModel();
|
|
|
|
const result = [];
|
|
|
|
if (!model.accepted_answer && model.can_accept_answer) {
|
|
|
|
result.push(
|
|
|
|
dec.attach("button", {
|
|
|
|
label: "solved.mark_as_solution",
|
|
|
|
title: "solved.mark_as_solution",
|
|
|
|
icon: "far-check-square",
|
|
|
|
action: "acceptAnswer",
|
|
|
|
className: "solve"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return dec.h("div.solved-container", result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
api.attachWidgetAction("post", "acceptAnswer", function() {
|
2016-02-17 15:58:32 -05:00
|
|
|
const post = this.model;
|
2018-08-01 10:05:46 -04:00
|
|
|
const current = post.get("topic.postStream.posts").filter(p => {
|
|
|
|
return p.get("post_number") === 1 || p.get("accepted_answer");
|
2016-02-23 12:09:36 -05:00
|
|
|
});
|
2016-02-17 15:58:32 -05:00
|
|
|
acceptPost(post);
|
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
current.forEach(p =>
|
|
|
|
this.appEvents.trigger("post-stream:refresh", { id: p.id })
|
|
|
|
);
|
2016-02-17 15:58:32 -05:00
|
|
|
});
|
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
api.attachWidgetAction("post", "unacceptAnswer", function() {
|
2016-02-23 12:09:36 -05:00
|
|
|
const post = this.model;
|
2018-08-01 10:05:46 -04:00
|
|
|
const op = post
|
|
|
|
.get("topic.postStream.posts")
|
|
|
|
.find(p => p.get("post_number") === 1);
|
2016-02-23 12:09:36 -05:00
|
|
|
unacceptPost(post);
|
2018-08-01 10:05:46 -04:00
|
|
|
this.appEvents.trigger("post-stream:refresh", { id: op.get("id") });
|
2016-02-17 15:58:32 -05:00
|
|
|
});
|
2016-12-19 22:04:23 -05:00
|
|
|
|
|
|
|
if (api.registerConnectorClass) {
|
2018-08-01 10:05:46 -04:00
|
|
|
api.registerConnectorClass("user-activity-bottom", "solved-list", {
|
2017-02-02 12:20:01 -05:00
|
|
|
shouldRender(args, component) {
|
|
|
|
return component.siteSettings.solved_enabled;
|
2018-08-01 10:05:46 -04:00
|
|
|
}
|
2017-02-02 12:20:01 -05:00
|
|
|
});
|
2018-08-01 10:05:46 -04:00
|
|
|
api.registerConnectorClass("user-summary-stat", "solved-count", {
|
2017-02-02 12:20:01 -05:00
|
|
|
shouldRender(args, component) {
|
2018-08-01 10:05:46 -04:00
|
|
|
return (
|
|
|
|
component.siteSettings.solved_enabled && args.model.solved_count > 0
|
|
|
|
);
|
2016-12-19 22:04:23 -05:00
|
|
|
},
|
|
|
|
setupComponent() {
|
2018-08-01 10:05:46 -04:00
|
|
|
this.set("classNames", ["linked-stat"]);
|
2016-12-19 22:04:23 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-02-17 15:58:32 -05:00
|
|
|
}
|
2015-05-19 01:45:19 -04:00
|
|
|
|
|
|
|
export default {
|
2018-08-01 10:05:46 -04:00
|
|
|
name: "extend-for-solved-button",
|
2019-07-15 10:30:58 -04:00
|
|
|
initialize(container) {
|
2015-05-19 01:45:19 -04:00
|
|
|
Topic.reopen({
|
|
|
|
// keeping this here cause there is complex localization
|
2016-02-17 15:58:32 -05:00
|
|
|
acceptedAnswerHtml: function() {
|
2018-08-01 10:05:46 -04:00
|
|
|
const username = this.get("accepted_answer.username");
|
|
|
|
const postNumber = this.get("accepted_answer.post_number");
|
2015-05-19 02:26:22 -04:00
|
|
|
|
|
|
|
if (!username || !postNumber) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-06-15 00:24:49 -04:00
|
|
|
return I18n.t("solved.accepted_html", {
|
2019-06-28 18:12:41 -04:00
|
|
|
icon: iconHTML("check-square", { class: "accepted" }),
|
2015-05-19 02:26:22 -04:00
|
|
|
username_lower: username.toLowerCase(),
|
2018-08-01 10:05:46 -04:00
|
|
|
username: formatUsername(username),
|
|
|
|
post_path: this.get("url") + "/" + postNumber,
|
2015-05-19 02:26:22 -04:00
|
|
|
post_number: postNumber,
|
2018-08-01 10:05:46 -04:00
|
|
|
user_path: User.create({ username }).get("path")
|
2015-05-19 02:26:22 -04:00
|
|
|
});
|
2018-08-01 10:05:46 -04:00
|
|
|
}.property("accepted_answer", "id")
|
2015-05-19 01:45:19 -04:00
|
|
|
});
|
|
|
|
|
2015-06-15 02:26:40 -04:00
|
|
|
TopicStatus.reopen({
|
2018-08-01 10:05:46 -04:00
|
|
|
statuses: function() {
|
2016-02-17 15:58:32 -05:00
|
|
|
const results = this._super();
|
2015-06-15 02:26:40 -04:00
|
|
|
if (this.topic.has_accepted_answer) {
|
|
|
|
results.push({
|
2018-08-01 10:05:46 -04:00
|
|
|
openTag: "span",
|
|
|
|
closeTag: "span",
|
2019-03-13 13:10:54 -04:00
|
|
|
title: I18n.t("topic_statuses.solved.help"),
|
2019-06-28 18:12:41 -04:00
|
|
|
icon: "far-check-square"
|
2015-06-15 02:26:40 -04:00
|
|
|
});
|
2018-08-01 10:05:46 -04:00
|
|
|
} else if (
|
|
|
|
this.topic.can_have_answer &&
|
|
|
|
this.siteSettings.solved_enabled &&
|
|
|
|
this.siteSettings.empty_box_on_unsolved
|
|
|
|
) {
|
2017-02-27 10:00:04 -05:00
|
|
|
results.push({
|
2018-08-01 10:05:46 -04:00
|
|
|
openTag: "span",
|
|
|
|
closeTag: "span",
|
|
|
|
title: I18n.t("solved.has_no_accepted_answer"),
|
|
|
|
icon: "square-o"
|
2017-02-27 10:00:04 -05:00
|
|
|
});
|
2015-06-15 02:26:40 -04:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}.property()
|
|
|
|
});
|
|
|
|
|
2019-07-15 10:30:58 -04:00
|
|
|
withPluginApi("0.1", api => initializeWithApi(api, container));
|
2017-09-15 13:55:08 -04:00
|
|
|
|
2018-08-01 10:05:46 -04:00
|
|
|
withPluginApi("0.8.10", api => {
|
|
|
|
api.replaceIcon(
|
|
|
|
"notification.solved.accepted_notification",
|
2019-07-15 10:30:58 -04:00
|
|
|
"far-check-square"
|
2018-08-01 10:05:46 -04:00
|
|
|
);
|
2017-09-15 13:55:08 -04:00
|
|
|
});
|
2015-05-19 01:45:19 -04:00
|
|
|
}
|
|
|
|
};
|