REFACTOR: new code layout/removes jquery/coding standards

This commit is contained in:
jjaffeux 2020-09-23 17:00:33 +02:00
parent 41bc9b1ae1
commit a22d636551
7 changed files with 1694 additions and 66 deletions

6
.eslintrc Normal file
View File

@ -0,0 +1,6 @@
{
"extends": "eslint-config-discourse",
"globals": {
"settings": true
}
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
yarn-error.log

4
.template-lintrc.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = {
plugins: ["ember-template-lint-plugin-discourse"],
extends: "discourse:recommended"
};

View File

@ -1,66 +0,0 @@
<script type="text/discourse-plugin" version="0.8.20">
const customHeaderLinks = settings.Custom_header_links;
const linksPosition = (settings.links_position === "right") ? "header-buttons:before" : "home-logo:after";
if (!customHeaderLinks.length) return;
const h = require("virtual-dom").h;
const headerLinks = [];
customHeaderLinks.split("|").map(i => {
const seg = $.map(i.split(","), $.trim);
const linkText = seg[0];
const linkTitle = seg[1];
const linkHref = seg[2];
const deviceClass = `.${seg[3]}`;
const linkTarget = seg[4] === "self" ? "" : "_blank";
const keepOnScrollClass = seg[5] === "keep" ? ".keep" : "";
const linkClass = `.${linkText.trim().toLowerCase().replace(/\s/gi, '-')}-custom-header-links`;
if (!linkTarget) {
headerLinks.push(
h(
`li.headerLink${deviceClass}${keepOnScrollClass}${linkClass}`,
h(
"a",
{
title: linkTitle,
href: linkHref
},
linkText
)
)
);
} else {
headerLinks.push(
h(
`li.headerLink${deviceClass}${keepOnScrollClass}${linkClass}`,
h(
"a",
{
title: linkTitle,
href: linkHref,
target: linkTarget
},
linkText
)
)
);
}
});
api.decorateWidget(linksPosition, helper => {
return helper.h(
"ul.custom-header-links", headerLinks
);
});
api.decorateWidget("home-logo:after", helper => {
let titleVisible = helper.attrs.minimized;
if (titleVisible) {
$(".d-header").addClass("hide-menus");
} else {
$(".d-header").removeClass("hide-menus");
}
});
</script>

View File

@ -0,0 +1,80 @@
import { h } from "virtual-dom";
import { withPluginApi } from "discourse/lib/plugin-api";
export default {
name: "discourse-custom-header-links",
initialize() {
withPluginApi("0.8.20", (api) => {
const customHeaderLinks = settings.Custom_header_links;
if (!customHeaderLinks.length) {
return;
}
const linksPosition =
settings.links_position === "right"
? "header-buttons:before"
: "home-logo:after";
const headerLinks = [];
customHeaderLinks
.split("|")
.filter(Boolean)
.map((customHeaderLinksArray) => {
const [
linkText,
linkTitle,
linkHref,
device,
target,
keepOnScroll,
] = customHeaderLinksArray
.split(",")
.filter(Boolean)
.map((x) => x.trim());
const deviceClass = `.${device}`;
const linkTarget = target === "self" ? "" : "_blank";
const keepOnScrollClass = keepOnScroll === "keep" ? ".keep" : "";
const linkClass = `.${linkText
.toLowerCase()
.replace(/\s/gi, "-")}-custom-header-links`;
const anchorAttributes = {
title: linkTitle,
href: linkHref,
};
if (linkTarget) {
anchorAttributes.target = linkTarget;
}
headerLinks.push(
h(
`li.headerLink${deviceClass}${keepOnScrollClass}${linkClass}`,
h("a", anchorAttributes, linkText)
)
);
});
api.decorateWidget(linksPosition, (helper) => {
return helper.h("ul.custom-header-links", headerLinks);
});
api.decorateWidget("home-logo:after", (helper) => {
const dHeader = document.querySelector(".d-header");
if (!dHeader) {
return;
}
const isTitleVisible = helper.attrs.minimized;
if (isTitleVisible) {
dHeader.classList.add("hide-menus");
} else {
dHeader.classList.remove("hide-menus");
}
});
});
},
};

7
package.json Normal file
View File

@ -0,0 +1,7 @@
{
"author": "Discourse",
"license": "MIT",
"devDependencies": {
"eslint-config-discourse": "latest"
}
}

1595
yarn.lock Normal file

File diff suppressed because it is too large Load Diff