mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Always pass ancestors to category-link (#26638)
We want to remove uses of Category.findById. This change pushes the fetching up the chain of responsibility.
This commit is contained in:
parent
4733369f71
commit
56c4804440
@ -36,7 +36,7 @@
|
|||||||
<span class="topic-categories">
|
<span class="topic-categories">
|
||||||
{{bound-category-link
|
{{bound-category-link
|
||||||
t.category
|
t.category
|
||||||
recursive=true
|
ancestors=t.category.predecessors
|
||||||
hideParent=true
|
hideParent=true
|
||||||
link=false
|
link=false
|
||||||
}}
|
}}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
{{#unless this.topic.isPrivateMessage}}
|
{{#unless this.topic.isPrivateMessage}}
|
||||||
{{bound-category-link this.topic.category recursive=true hideParent=true}}
|
{{bound-category-link
|
||||||
|
this.topic.category
|
||||||
|
ancestors=this.topic.category.predecessors
|
||||||
|
hideParent=true
|
||||||
|
}}
|
||||||
{{/unless}}
|
{{/unless}}
|
||||||
<div class="topic-header-extra">
|
<div class="topic-header-extra">
|
||||||
{{#if this.siteSettings.tagging_enabled}}
|
{{#if this.siteSettings.tagging_enabled}}
|
||||||
|
@ -32,6 +32,7 @@ export function addExtraIconRenderer(renderer) {
|
|||||||
@param {Boolean} [opts.recursive] If true, the function will be called recursively for all parent categories
|
@param {Boolean} [opts.recursive] If true, the function will be called recursively for all parent categories
|
||||||
@param {Number} [opts.depth] Current category depth, used for limiting recursive calls
|
@param {Number} [opts.depth] Current category depth, used for limiting recursive calls
|
||||||
@param {Boolean} [opts.previewColor] If true, category color will be set as an inline style.
|
@param {Boolean} [opts.previewColor] If true, category color will be set as an inline style.
|
||||||
|
@param {Array} [opts.ancestors] The ancestors of the category to generate the badge for.
|
||||||
**/
|
**/
|
||||||
export function categoryBadgeHTML(category, opts) {
|
export function categoryBadgeHTML(category, opts) {
|
||||||
const { site, siteSettings } = helperContext();
|
const { site, siteSettings } = helperContext();
|
||||||
@ -47,7 +48,13 @@ 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.ancestors) {
|
||||||
|
const { ancestors, ...otherOpts } = opts;
|
||||||
|
return [category, ...ancestors]
|
||||||
|
.reverse()
|
||||||
|
.map((c) => categoryBadgeHTML(c, otherOpts))
|
||||||
|
.join("");
|
||||||
|
} else 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;
|
const lastSubcategory = !opts.depth;
|
||||||
opts.depth = depth;
|
opts.depth = depth;
|
||||||
@ -87,6 +94,9 @@ export function categoryLinkHTML(category, options) {
|
|||||||
if (options.recursive) {
|
if (options.recursive) {
|
||||||
categoryOptions.recursive = true;
|
categoryOptions.recursive = true;
|
||||||
}
|
}
|
||||||
|
if (options.ancestors) {
|
||||||
|
categoryOptions.ancestors = options.ancestors;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return htmlSafe(categoryBadgeHTML(category, categoryOptions));
|
return htmlSafe(categoryBadgeHTML(category, categoryOptions));
|
||||||
}
|
}
|
||||||
|
@ -492,6 +492,15 @@ export default class Category extends RestModel {
|
|||||||
return [...(parentAncestors || []), this];
|
return [...(parentAncestors || []), this];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@discourseComputed("parentCategory", "parentCategory.predecessors")
|
||||||
|
predecessors(parentCategory, parentPredecessors) {
|
||||||
|
if (parentCategory) {
|
||||||
|
return [parentCategory, ...parentPredecessors];
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@discourseComputed("subcategories")
|
@discourseComputed("subcategories")
|
||||||
descendants() {
|
descendants() {
|
||||||
const descendants = [this];
|
const descendants = [this];
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<div class="topic-categories">
|
<div class="topic-categories">
|
||||||
{{bound-category-link
|
{{bound-category-link
|
||||||
this.item.category
|
this.item.category
|
||||||
recursive=true
|
ancestors=this.item.category.predecessors
|
||||||
hideParent=true
|
hideParent=true
|
||||||
link=false
|
link=false
|
||||||
}}
|
}}
|
||||||
|
Loading…
Reference in New Issue
Block a user