mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Add 'all subcategories' option in category-selector (#11099)
This commit is contained in:
parent
c540bf5e2b
commit
4638c6fc8d
@ -54,8 +54,11 @@ export function categoryBadgeHTML(category, opts) {
|
|||||||
const depth = (opts.depth || 1) + 1;
|
const depth = (opts.depth || 1) + 1;
|
||||||
if (opts.recursive && depth <= siteSettings.max_category_nesting) {
|
if (opts.recursive && depth <= siteSettings.max_category_nesting) {
|
||||||
const parentCategory = Category.findById(category.parent_category_id);
|
const parentCategory = Category.findById(category.parent_category_id);
|
||||||
|
const lastSubcategory = !opts.depth;
|
||||||
opts.depth = depth;
|
opts.depth = depth;
|
||||||
return categoryBadgeHTML(parentCategory, opts) + _renderer(category, opts);
|
const parentBadges = categoryBadgeHTML(parentCategory, opts);
|
||||||
|
opts.lastSubcategory = lastSubcategory;
|
||||||
|
return parentBadges + _renderer(category, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _renderer(category, opts);
|
return _renderer(category, opts);
|
||||||
@ -183,5 +186,11 @@ function defaultCategoryLinkRenderer(category, opts) {
|
|||||||
if (opts.topicCount && categoryStyle === "box") {
|
if (opts.topicCount && categoryStyle === "box") {
|
||||||
afterBadgeWrapper += buildTopicCount(opts.topicCount);
|
afterBadgeWrapper += buildTopicCount(opts.topicCount);
|
||||||
}
|
}
|
||||||
|
if (opts.plusSubcategories && opts.lastSubcategory) {
|
||||||
|
afterBadgeWrapper += `<span class="plus-subcategories">${I18n.t(
|
||||||
|
"category_row.plus_subcategories",
|
||||||
|
{ count: opts.plusSubcategories }
|
||||||
|
)}</span>`;
|
||||||
|
}
|
||||||
return `<${tagName} class="badge-wrapper ${extraClasses}" ${href}>${html}</${tagName}>${afterBadgeWrapper}`;
|
return `<${tagName} class="badge-wrapper ${extraClasses}" ${href}>${html}</${tagName}>${afterBadgeWrapper}`;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { get, computed } from "@ember/object";
|
import I18n from "I18n";
|
||||||
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
||||||
|
import EmberObject, { get, computed } from "@ember/object";
|
||||||
import { mapBy } from "@ember/object/computed";
|
import { mapBy } from "@ember/object/computed";
|
||||||
import { makeArray } from "discourse-common/lib/helpers";
|
import { makeArray } from "discourse-common/lib/helpers";
|
||||||
import MultiSelectComponent from "select-kit/components/multi-select";
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
||||||
@ -52,6 +54,57 @@ export default MultiSelectComponent.extend({
|
|||||||
return "category-row";
|
return "category-row";
|
||||||
},
|
},
|
||||||
|
|
||||||
|
search(filter) {
|
||||||
|
const result = this._super(filter);
|
||||||
|
if (result.length === 1) {
|
||||||
|
const subcategoryIds = new Set([result[0].id]);
|
||||||
|
for (let i = 0; i < this.siteSettings.max_category_nesting; ++i) {
|
||||||
|
subcategoryIds.forEach((categoryId) => {
|
||||||
|
this.content.forEach((category) => {
|
||||||
|
if (category.parent_category_id === categoryId) {
|
||||||
|
subcategoryIds.add(category.id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (subcategoryIds.size > 1) {
|
||||||
|
result.push(
|
||||||
|
EmberObject.create({
|
||||||
|
multicategory: [...subcategoryIds],
|
||||||
|
category: result[0],
|
||||||
|
title: I18n.t("category_row.plus_subcategories_title", {
|
||||||
|
name: result[0].name,
|
||||||
|
count: subcategoryIds.size - 1,
|
||||||
|
}),
|
||||||
|
label: categoryBadgeHTML(result[0], {
|
||||||
|
link: false,
|
||||||
|
recursive: true,
|
||||||
|
plusSubcategories: subcategoryIds.size - 1,
|
||||||
|
}).htmlSafe(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
select(value, item) {
|
||||||
|
if (item.multicategory) {
|
||||||
|
const items = item.multicategory.map((id) =>
|
||||||
|
Category.findById(parseInt(id, 10))
|
||||||
|
);
|
||||||
|
|
||||||
|
const newValues = makeArray(this.value).concat(items.map((i) => i.id));
|
||||||
|
const newContent = makeArray(this.selectedContent).concat(items);
|
||||||
|
|
||||||
|
this.selectKit.change(newValues, newContent);
|
||||||
|
} else {
|
||||||
|
this._super(value, item);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
onChange(values) {
|
onChange(values) {
|
||||||
this.attrs.onChange(
|
this.attrs.onChange(
|
||||||
|
@ -16,6 +16,10 @@
|
|||||||
.topic-count {
|
.topic-count {
|
||||||
margin-left: 0.25em;
|
margin-left: 0.25em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.plus-subcategories {
|
||||||
|
font-size: $font-down-2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1763,6 +1763,12 @@ en:
|
|||||||
topic_count:
|
topic_count:
|
||||||
one: "%{count} topic in this category"
|
one: "%{count} topic in this category"
|
||||||
other: "%{count} topics in this category"
|
other: "%{count} topics in this category"
|
||||||
|
plus_subcategories_title:
|
||||||
|
one: "%{name} and one subcategory"
|
||||||
|
other: "%{name} and %{count} subcategories"
|
||||||
|
plus_subcategories:
|
||||||
|
one: "+ %{count} subcategory"
|
||||||
|
other: "+ %{count} subcategories"
|
||||||
|
|
||||||
select_kit:
|
select_kit:
|
||||||
default_header_text: Select...
|
default_header_text: Select...
|
||||||
|
Loading…
Reference in New Issue
Block a user