mirror of
https://github.com/discourse/discourse-category-banners
synced 2025-02-09 04:54:45 +00:00
ac66be7948
- drops jquery - uses new codelayout structure - more explicit site setting - apply code standards - various cleanups - do not show an empty category-title-desription if we don't show the category description
74 lines
1.8 KiB
JavaScript
74 lines
1.8 KiB
JavaScript
import { h } from "virtual-dom";
|
|
import { iconNode } from "discourse-common/lib/icon-library";
|
|
import { createWidget } from "discourse/widgets/widget";
|
|
|
|
function buildCategory(category) {
|
|
const content = [];
|
|
|
|
if (category.read_restricted) {
|
|
content.push(iconNode("lock"));
|
|
}
|
|
|
|
content.push(h("h1.category-title", category.name));
|
|
|
|
if (settings.show_description) {
|
|
content.push(
|
|
h(
|
|
"div.category-title-description",
|
|
h("div.cooked", { innerHTML: category.description })
|
|
)
|
|
);
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
export default createWidget("category-header-widget", {
|
|
tagName: "span.discourse-category-banners",
|
|
|
|
html() {
|
|
const path = window.location.pathname;
|
|
const category = this.register
|
|
.lookup("controller:navigation/category")
|
|
.get("category");
|
|
|
|
if (!category) {
|
|
return;
|
|
}
|
|
|
|
const isException = settings.exceptions
|
|
.split("|")
|
|
.filter(Boolean)
|
|
.includes(category.name);
|
|
|
|
if (/^\/c\//.test(path)) {
|
|
const hideMobile = !settings.show_mobile && this.site.mobileView;
|
|
const isSubCategory =
|
|
!settings.show_subcategory && category.parentCategory;
|
|
const hasNoCategoryDescription =
|
|
settings.hide_if_no_description && !category.description_text;
|
|
|
|
if (
|
|
!isException &&
|
|
!hasNoCategoryDescription &&
|
|
!isSubCategory &&
|
|
!hideMobile
|
|
) {
|
|
document.body.classList.add("category-header");
|
|
|
|
return h(
|
|
`div.category-title-header.category-banner-${category.slug}`,
|
|
{
|
|
attributes: {
|
|
style: `background-color: #${category.color}; color: #${category.text_color};`,
|
|
},
|
|
},
|
|
h("div.category-title-contents", buildCategory(category))
|
|
);
|
|
}
|
|
} else {
|
|
document.body.classList.remove("category-header");
|
|
}
|
|
},
|
|
});
|