REFACTOR: old patterns and deprecations (#81)

This commit is contained in:
Joffrey JAFFEUX 2019-10-02 07:36:35 +02:00 committed by Sam
parent be6ecbf667
commit c630c14d6c
4 changed files with 47 additions and 46 deletions

View File

@ -42,9 +42,7 @@ export default {
? queryStrings.substr(1).split("&") ? queryStrings.substr(1).split("&")
: []; : [];
params = params.filter(param => { params = params.filter(param => !param.startsWith("solved="));
return !param.startsWith("solved=");
});
if (newStatus && newStatus !== "all") { if (newStatus && newStatus !== "all") {
newStatus = newStatus === "solved" ? "yes" : "no"; newStatus = newStatus === "solved" ? "yes" : "no";

View File

@ -14,15 +14,17 @@ function clearAccepted(topic) {
const posts = topic.get("postStream.posts"); const posts = topic.get("postStream.posts");
posts.forEach(post => { posts.forEach(post => {
if (post.get("post_number") > 1) { if (post.get("post_number") > 1) {
post.set("accepted_answer", false); post.setProperties({
post.set("can_accept_answer", true); accepted_answer: false,
post.set("can_unaccept_answer", false); can_accept_answer: true,
can_unaccept_answer: false
});
} }
}); });
} }
function unacceptPost(post) { function unacceptPost(post) {
if (!post.get("can_unaccept_answer")) { if (!post.can_unaccept_answer) {
return; return;
} }
const topic = post.topic; const topic = post.topic;
@ -36,7 +38,7 @@ function unacceptPost(post) {
ajax("/solution/unaccept", { ajax("/solution/unaccept", {
type: "POST", type: "POST",
data: { id: post.get("id") } data: { id: post.id }
}).catch(popupAjaxError); }).catch(popupAjaxError);
} }
@ -52,14 +54,14 @@ function acceptPost(post) {
}); });
topic.set("accepted_answer", { topic.set("accepted_answer", {
username: post.get("username"), username: post.username,
post_number: post.get("post_number"), post_number: post.post_number,
excerpt: post.get("cooked") excerpt: post.cooked
}); });
ajax("/solution/accept", { ajax("/solution/accept", {
type: "POST", type: "POST",
data: { id: post.get("id") } data: { id: post.id }
}).catch(popupAjaxError); }).catch(popupAjaxError);
} }
@ -136,38 +138,34 @@ function initializeWithApi(api) {
if (dec.attrs.post_number === 1) { if (dec.attrs.post_number === 1) {
const postModel = dec.getModel(); const postModel = dec.getModel();
if (postModel) { if (postModel) {
const topic = postModel.get("topic"); const topic = postModel.topic;
if (topic.get("accepted_answer")) { if (topic.accepted_answer) {
const hasExcerpt = !!topic.get("accepted_answer").excerpt; const hasExcerpt = !!topic.accepted_answer.excerpt;
const withExcerpt = ` const withExcerpt = `
<aside class='quote accepted-answer' data-post="${ <aside class='quote accepted-answer' data-post="${
topic.get("accepted_answer").post_number topic.get("accepted_answer").post_number
}" data-topic="${topic.get("id")}"> }" data-topic="${topic.id}">
<div class='title'> <div class='title'>
${topic.get( ${topic.acceptedAnswerHtml} <div class="quote-controls"><\/div>
"acceptedAnswerHtml"
)} <div class="quote-controls"><\/div>
</div> </div>
<blockquote> <blockquote>
${topic.get("accepted_answer").excerpt} ${topic.accepted_answer.excerpt}
</blockquote> </blockquote>
</aside>`; </aside>`;
const withoutExcerpt = ` const withoutExcerpt = `
<aside class='quote accepted-answer'> <aside class='quote accepted-answer'>
<div class='title title-only'> <div class='title title-only'>
${topic.get("acceptedAnswerHtml")} ${topic.acceptedAnswerHtml}
</div> </div>
</aside>`; </aside>`;
var cooked = new PostCooked({ const cooked = new PostCooked({
cooked: hasExcerpt ? withExcerpt : withoutExcerpt cooked: hasExcerpt ? withExcerpt : withoutExcerpt
}); });
var html = cooked.init(); return dec.rawHtml(cooked.init());
return dec.rawHtml(html);
} }
} }
} }
@ -176,7 +174,7 @@ function initializeWithApi(api) {
api.attachWidgetAction("post", "acceptAnswer", function() { api.attachWidgetAction("post", "acceptAnswer", function() {
const post = this.model; const post = this.model;
const current = post.get("topic.postStream.posts").filter(p => { const current = post.get("topic.postStream.posts").filter(p => {
return p.get("post_number") === 1 || p.get("accepted_answer"); return p.post_number === 1 || p.accepted_answer;
}); });
acceptPost(post); acceptPost(post);
@ -189,9 +187,9 @@ function initializeWithApi(api) {
const post = this.model; const post = this.model;
const op = post const op = post
.get("topic.postStream.posts") .get("topic.postStream.posts")
.find(p => p.get("post_number") === 1); .find(p => p.post_number === 1);
unacceptPost(post); unacceptPost(post);
this.appEvents.trigger("post-stream:refresh", { id: op.get("id") }); this.appEvents.trigger("post-stream:refresh", { id: op.id });
}); });
if (api.registerConnectorClass) { if (api.registerConnectorClass) {
@ -218,7 +216,7 @@ export default {
initialize() { initialize() {
Topic.reopen({ Topic.reopen({
// keeping this here cause there is complex localization // keeping this here cause there is complex localization
acceptedAnswerHtml: function() { acceptedAnswerHtml: Ember.computed("accepted_answer", "id", function() {
const username = this.get("accepted_answer.username"); const username = this.get("accepted_answer.username");
const postNumber = this.get("accepted_answer.post_number"); const postNumber = this.get("accepted_answer.post_number");
@ -230,16 +228,17 @@ export default {
icon: iconHTML("check-square", { class: "accepted" }), icon: iconHTML("check-square", { class: "accepted" }),
username_lower: username.toLowerCase(), username_lower: username.toLowerCase(),
username: formatUsername(username), username: formatUsername(username),
post_path: this.get("url") + "/" + postNumber, post_path: `${this.url}/${postNumber}`,
post_number: postNumber, post_number: postNumber,
user_path: User.create({ username }).get("path") user_path: User.create({ username }).path
}); });
}.property("accepted_answer", "id") })
}); });
TopicStatus.reopen({ TopicStatus.reopen({
statuses: function() { statuses: Ember.computed(function() {
const results = this._super(); const results = this._super(...arguments);
if (this.topic.has_accepted_answer) { if (this.topic.has_accepted_answer) {
results.push({ results.push({
openTag: "span", openTag: "span",
@ -260,7 +259,7 @@ export default {
}); });
} }
return results; return results;
}.property() })
}); });
withPluginApi("0.1", initializeWithApi); withPluginApi("0.1", initializeWithApi);

View File

@ -1,22 +1,25 @@
import property from "ember-addons/ember-computed-decorators";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
export default { export default {
name: "extend-category-for-solved", name: "extend-category-for-solved",
before: "inject-discourse-objects", before: "inject-discourse-objects",
initialize() { initialize() {
Category.reopen({ Category.reopen({
@property("custom_fields.enable_accepted_answers") enable_accepted_answers: Ember.computed(
enable_accepted_answers: { "custom_fields.enable_accepted_answers",
get(enableField) { {
return enableField === "true"; get(enableField) {
}, return enableField === "true";
set(value) { },
value = value ? "true" : "false"; set(value) {
this.set("custom_fields.enable_accepted_answers", value); value = value ? "true" : "false";
return value; this.set("custom_fields.enable_accepted_answers", value);
return value;
}
} }
} )
}); });
} }
}; };

View File

@ -1,5 +1,6 @@
export default { export default {
resource: "user.userActivity", resource: "user.userActivity",
map() { map() {
this.route("solved"); this.route("solved");
} }