DEV: minor choose topic refactoring (#15233)

* DEV: minor choose topic refactoring

- prevents category to be clickable to make clicking topics easier
- drops jQuery
- uses @action
- uses ? operator where possible
- drops un-needed next/schedule usage

* uses topic-status component and prevents pinned icon to be focusable

* Update app/assets/javascripts/discourse/app/templates/components/choose-topic.hbs

Co-authored-by: Jarek Radosz <jradosz@gmail.com>

Co-authored-by: Jarek Radosz <jradosz@gmail.com>
This commit is contained in:
Joffrey JAFFEUX 2021-12-09 04:08:21 +01:00 committed by GitHub
parent 4d1c84f15f
commit 828e75c2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 27 deletions

View File

@ -1,8 +1,8 @@
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import Component from "@ember/component";
import { action } from "@ember/object";
import discourseDebounce from "discourse-common/lib/debounce";
import { isEmpty } from "@ember/utils";
import { next, schedule } from "@ember/runloop";
import { searchForTerm } from "discourse/lib/search";
import { INPUT_DELAY } from "discourse-common/config/environment";
@ -26,7 +26,7 @@ export default Component.extend({
if (this.loadOnInit && !isEmpty(this.additionalFilters)) {
searchForTerm(this.additionalFilters, {}).then((results) => {
if (results && results.posts && results.posts.length > 0) {
if (results?.posts?.length > 0) {
this.set(
"topics",
results.posts
@ -42,18 +42,18 @@ export default Component.extend({
didInsertElement() {
this._super(...arguments);
schedule("afterRender", () => {
$("#choose-topic-title").keydown((e) => {
if (e.key === "Enter") {
return false;
}
});
});
document
.getElementById("choose-topic-title")
.addEventListener("keydown", this._handleEnter);
},
willDestroyElement() {
this._super(...arguments);
$("#choose-topic-title").off("keydown");
document
.getElementById("choose-topic-title")
.removeEventListener("keydown", this._handleEnter);
},
@observes("topicTitle")
@ -102,7 +102,7 @@ export default Component.extend({
const currentTopicId = this.currentTopicId;
const titleWithFilters = `${title} ${this.additionalFilters}`;
let searchParams = {};
const searchParams = {};
if (!isEmpty(title)) {
searchParams.typeFilter = "topic";
@ -116,7 +116,7 @@ export default Component.extend({
if (title !== this.topicTitle) {
return;
}
if (results && results.posts && results.posts.length > 0) {
if (results?.posts?.length > 0) {
this.set(
"topics",
results.posts.mapBy("topic").filter((t) => t.id !== currentTopicId)
@ -130,15 +130,18 @@ export default Component.extend({
});
},
actions: {
@action
chooseTopic(topic) {
this.set("selectedTopicId", topic.id);
next(() => {
document.getElementById(`choose-topic-${topic.id}`).checked = true;
});
if (this.topicChangedCallback) {
this.topicChangedCallback(topic);
}
},
_handleEnter(event) {
if (event.key === "Enter") {
event.preventDefault();
}
},
});

View File

@ -4,6 +4,8 @@ import discourseComputed from "discourse-common/utils/decorators";
import { iconHTML } from "discourse-common/lib/icon-library";
export default Component.extend({
disableActions: false,
classNames: ["topic-statuses"],
click(e) {

View File

@ -1,8 +1,15 @@
<label for="choose-topic-title">
{{#if labelIcon}}{{d-icon labelIcon}}{{/if}}{{i18n labelText}}
{{#if labelIcon}}
{{d-icon labelIcon}}
{{/if}}
<span>{{i18n labelText}}</span>
</label>
{{text-field value=topicTitle placeholderKey="choose_topic.title.placeholder" id="choose-topic-title"}}
{{text-field
value=topicTitle
placeholderKey="choose_topic.title.placeholder"
id="choose-topic-title"
}}
{{#if loading}}
<p>{{i18n "loading"}}</p>
@ -13,13 +20,23 @@
{{#each topics as |t|}}
<div class="controls existing-topic">
<label class="radio">
<input id="choose-topic-{{t.id}}" {{action "chooseTopic" t}} type="radio" name="choose_topic_id">
{{raw "topic-status" topic=t}}
{{input
id=(concat "choose-topic-" t.id)
checked=(eq t.id selectedTopicId)
click=(action "chooseTopic" t)
type="radio"
name="choose_topic_id"
}}
{{topic-status topic=t disableActions=true}}
<span class="topic-title">
{{replace-emoji t.fancy_title}}
</span>
<span class="topic-categories">
{{bound-category-link t.category recursive=true hideParent=true}}
{{bound-category-link t.category
recursive=true
hideParent=true
link=false
}}
</span>
</label>
</div>