DEV: Make bread-crumb component work with sub-sub-categories

This commit is contained in:
Daniel Waterworth 2019-12-03 12:31:57 +00:00
parent d0bb6314c6
commit af5bcb25d5
3 changed files with 82 additions and 17 deletions

View File

@ -1,15 +1,64 @@
import { alias, filter, or } from "@ember/object/computed";
import Component from "@ember/component";
import { default as discourseComputed } from "discourse-common/utils/decorators";
import deprecated from "discourse-common/lib/deprecated";
// A breadcrumb including category drop downs
export default Component.extend({
classNameBindings: ["hidden:hidden", ":category-breadcrumb"],
tagName: "ol",
parentCategory: alias("category.parentCategory"),
@discourseComputed("categories")
filteredCategories(categories) {
return categories.filter(
category =>
this.siteSettings.allow_uncategorized_topics ||
category.id !== this.site.uncategorized_category_id
);
},
@discourseComputed(
"category.ancestors",
"filteredCategories",
"noSubcategories"
)
categoryBreadcrumbs(categoryAncestors, filteredCategories, noSubcategories) {
categoryAncestors = categoryAncestors || [];
const parentCategories = [undefined, ...categoryAncestors];
const categories = [...categoryAncestors, undefined];
const zipped = parentCategories.map((x, i) => [x, categories[i]]);
return zipped.map(record => {
const [parentCategory, category] = record;
const options = filteredCategories.filter(
c =>
c.get("parentCategory.id") === (parentCategory && parentCategory.id)
);
return {
category,
parentCategory,
options,
isSubcategory: !!parentCategory,
noSubcategories: !category && noSubcategories,
hasOptions: options.length !== 0
};
});
},
@discourseComputed("category")
parentCategory(category) {
deprecated(
"The parentCategory property of the bread-crumbs component is deprecated"
);
return category && category.parentCategory;
},
parentCategories: filter("categories", function(c) {
deprecated(
"The parentCategories property of the bread-crumbs component is deprecated"
);
if (
c.id === this.site.get("uncategorized_category_id") &&
!this.siteSettings.allow_uncategorized_topics
@ -23,6 +72,9 @@ export default Component.extend({
@discourseComputed("parentCategories")
parentCategoriesSorted(parentCategories) {
deprecated(
"The parentCategoriesSorted property of the bread-crumbs component is deprecated"
);
if (this.siteSettings.fixed_category_positions) {
return parentCategories;
}
@ -35,16 +87,27 @@ export default Component.extend({
return this.site.mobileView && !category;
},
firstCategory: or("{parentCategory,category}"),
@discourseComputed("category", "parentCategory")
firstCategory(category, parentCategory) {
deprecated(
"The firstCategory property of the bread-crumbs component is deprecated"
);
return parentCategory || category;
},
@discourseComputed("category", "parentCategory")
secondCategory(category, parentCategory) {
if (parentCategory) return category;
return null;
deprecated(
"The secondCategory property of the bread-crumbs component is deprecated"
);
return parentCategory && category;
},
@discourseComputed("firstCategory", "hideSubcategories")
childCategories(firstCategory, hideSubcategories) {
deprecated(
"The childCategories property of the bread-crumbs component is deprecated"
);
if (hideSubcategories) {
return [];
}

View File

@ -55,6 +55,11 @@ const Category = RestModel.extend({
return { type: "category", id, category: this };
},
@discourseComputed("parentCategory.ancestors")
ancestors(parentAncestors) {
return [...(parentAncestors || []), this];
},
@discourseComputed("notification_level")
isMuted(notificationLevel) {
return notificationLevel === NotificationLevels.MUTED;

View File

@ -1,16 +1,13 @@
{{category-drop
category=firstCategory
categories=parentCategoriesSorted
countSubcategories=true}}
{{#if childCategories}}
{{category-drop
category=secondCategory
parentCategory=firstCategory
categories=childCategories
subCategory=true
noSubcategories=noSubcategories}}
{{/if}}
{{#each categoryBreadcrumbs as |breadcrumb|}}
{{#if breadcrumb.hasOptions}}
{{category-drop
category=breadcrumb.category
parentCategory=breadcrumb.parentCategory
categories=breadcrumb.options
subCategory=breadcrumb.isSubcategory
noSubcategories=breadcrumb.noSubcategories}}
{{/if}}
{{/each}}
{{#if siteSettings.tagging_enabled}}
{{tag-drop