Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: add features for tag banner integration, clean up #32

Merged
merged 4 commits into from May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 23 additions & 21 deletions javascripts/discourse/components/discourse-category-banners.hbs
Expand Up @@ -2,32 +2,34 @@
<div
{{did-insert this.getCategory}}
{{did-update this.getCategory this.isVisible}}
{{will-destroy this.teardownComponent}}
class="category-title-header
{{if this.category (concat 'category-banner-' this.category.slug)}}"
style={{if this.category this.safeStyle}}
>

<div class="category-title-contents">
<h1 class="category-title">
{{#if (and (theme-setting "show_category_icon") this.category)}}
{{#if this.hasIconComponent}}
{{! For compatibility with https://meta.discourse.org/t/category-icons/104683}}
<CategoryIcon @category={{this.category}} />
{{else}}
{{this.consoleWarn}}
{{#if this.category}}
<div class="category-title-contents">
<h1 class="category-title">
{{#if (and (theme-setting "show_category_icon") this.category)}}
{{#if this.hasIconComponent}}
{{! For compatibility with https://meta.discourse.org/t/category-icons/104683}}
<CategoryIcon @category={{this.category}} />
{{else}}
{{this.consoleWarn}}
{{/if}}
{{/if}}
{{#if this.category.read_restricted}}
{{d-icon "lock"}}
{{/if}}
{{this.category.name}}
<PluginOutlet @name="category-banners-after-title" />
</h1>
{{#if (theme-setting "show_description")}}
<div class="category-title-description">
<div class="cooked" innerHTML={{this.category.description}}></div>
</div>
{{/if}}
{{#if this.category.read_restricted}}
{{d-icon "lock"}}
{{/if}}
{{this.category.name}}
</h1>

{{#if (theme-setting "show_description")}}
<div class="category-title-description">
<div class="cooked" innerHTML={{this.category.description}}></div>
</div>
{{/if}}
</div>
</div>
{{/if}}
</div>
{{/if}}
72 changes: 40 additions & 32 deletions javascripts/discourse/components/discourse-category-banners.js
Expand Up @@ -9,45 +9,33 @@ import { getOwner } from "@ember/application";
export default class DiscourseCategoryBanners extends Component {
@service router;
@service site;
@service categoryBannerPresence;
@tracked category = null;
@tracked keepDuringLoadingRoute = false;

get hasIconComponent() {
return getOwner(this).hasRegistration("component:category-icon");
}

get consoleWarn() {
// eslint-disable-next-line no-console
return console.warn(
"The category banners component is trying to use the category icons component, but it is not available. https://meta.discourse.org/t/category-icons/104683"
);
}

get isVisible() {
if (this.categorySlugPathWithID) {
this.keepDuringLoadingRoute = true;
return true;
} else {
if (this.router.currentRoute.name.includes("loading")) {
return this.keepDuringLoadingRoute;
} else {
this.keepDuringLoadingRoute = false;
return false;
}
}
}

get categorySlugPathWithID() {
return this.router?.currentRoute?.params?.category_slug_path_with_id;
}

get shouldRender() {
if (this.isVisible && this.keepDuringLoadingRoute) {
return (
this.categorySlugPathWithID ||
(this.keepDuringLoadingRoute &&
this.router.currentRoute.name.includes("loading"))
);
}

get isVisible() {
if (this.categorySlugPathWithID) {
return true;
} else {
document.body.classList.remove("category-header");
return false;
} else if (this.router.currentRoute.name.includes("loading")) {
return this.keepDuringLoadingRoute;
}
return false;
}

get safeStyle() {
Expand All @@ -56,6 +44,13 @@ export default class DiscourseCategoryBanners extends Component {
);
}

get consoleWarn() {
// eslint-disable-next-line no-console
return console.warn(
"The category banners component is trying to use the category icons component, but it is not available. https://meta.discourse.org/t/category-icons/104683"
);
}

#parseCategories(categoriesStr) {
const categories = {};
categoriesStr.split("|").forEach((item) => {
Expand All @@ -78,21 +73,28 @@ export default class DiscourseCategoryBanners extends Component {
}

#checkTargetCategory(categories) {
const currentCategoryName = this.category.name.toLowerCase();
const parentCategoryName = this.category.parentCategory
const currentCategoryName = this.category?.name.toLowerCase();
const parentCategoryName = this.category?.parentCategory
? this.category.parentCategory.name.toLowerCase()
: null;

return (
Object.keys(categories).length === 0 ||
categories[currentCategoryName] === "all" ||
categories[currentCategoryName] === "no_sub" ||
(this.category.parentCategory &&
(this.category?.parentCategory &&
(categories[parentCategoryName] === "all" ||
categories[parentCategoryName] === "only_sub"))
);
}

@action
teardownComponent() {
document.body.classList.remove("category-header");
this.category = null;
this.categoryBannerPresence.setTo(false);
}

@action
getCategory() {
if (!this.isVisible) {
Expand All @@ -103,18 +105,23 @@ export default class DiscourseCategoryBanners extends Component {
this.category = Category.findBySlugPathWithID(
this.categorySlugPathWithID
);
this.categoryBannerPresence.setTo(true);
this.keepDuringLoadingRoute = true;
} else {
if (!this.router.currentRoute.name.includes("loading")) {
return (this.keepDuringLoadingRoute = false);
}
}

const categories = this.#parseCategories(settings.categories);
const exceptions = this.#parseExceptions(settings.exceptions);

const isException = exceptions.includes(this.category.name.toLowerCase());
const isException = exceptions.includes(this.category?.name.toLowerCase());
const isTarget = this.#checkTargetCategory(categories);
const hideMobile = !settings.show_mobile && this.site.mobileView;
const isSubCategory =
!settings.show_subcategory && this.category.parentCategory;
!settings.show_subcategory && this.category?.parentCategory;
const hasNoCategoryDescription =
settings.hide_if_no_description && !this.category.description_text;
settings.hide_if_no_description && !this.category?.description_text;

if (
isTarget &&
Expand All @@ -126,6 +133,7 @@ export default class DiscourseCategoryBanners extends Component {
document.body.classList.add("category-header");
} else {
document.body.classList.remove("category-header");
this.categoryBannerPresence.setTo(false);
}
}
}
10 changes: 10 additions & 0 deletions javascripts/discourse/services/category-banner-presence.js
@@ -0,0 +1,10 @@
import Service from "@ember/service";
import { tracked } from "@glimmer/tracking";

export default class CategoryBannerPresence extends Service {
@tracked isPresent = false;

setTo(value) {
this.isPresent = value;
}
}