Skip to content

Commit

Permalink
REFACTOR: full component refactoring
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jjaffeux committed Sep 27, 2020
1 parent 6dd00a8 commit ac66be7
Show file tree
Hide file tree
Showing 11 changed files with 1,711 additions and 73 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "eslint-config-discourse",
"globals": {
"settings": true
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn-error.log
4 changes: 4 additions & 0 deletions .template-lintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
plugins: ["ember-template-lint-plugin-discourse"],
extends: "discourse:recommended"
};
9 changes: 7 additions & 2 deletions common/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ div[class^="category-title-header"] {
text-align: center;
width: 100%;
justify-content: center;

.category-title-contents {
max-width: 500px;
padding: 40px;
svg {

.d-icon-lock {
height: 1.5em;
width: 1.1em;
margin-right: 0.25em;
}
h1 {

.category-title {
display: inline;
}
}

.category-title-description {
a,
a:visited {
Expand All @@ -24,6 +28,7 @@ div[class^="category-title-header"] {
}
}
}

.category-header {
#main-outlet {
padding-top: 20px;
Expand Down
70 changes: 0 additions & 70 deletions common/header.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{mount-widget widget="category-header-widget"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { withPluginApi } from "discourse/lib/plugin-api";

export default {
name: "discourse-category-banners",

initialize() {
withPluginApi("0.8", (api) => {
api.decorateWidget("category-header-widget:after", (helper) => {
helper.widget.appEvents.on("page:changed", () => {
helper.widget.scheduleRerender();
});
});
});
},
};
73 changes: 73 additions & 0 deletions javascripts/discourse/widgets/category-header-widget.js.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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");
}
},
});
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"author": "Discourse",
"license": "MIT",
"devDependencies": {
"eslint-config-discourse": "latest"
}
}
2 changes: 1 addition & 1 deletion settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ exceptions:
default: ""
type: list
list_type: simple
description: "Banner will not show for these categories"
description: "Banner will not show for these categories NAMES. This is case sensitive."
Loading

0 comments on commit ac66be7

Please sign in to comment.