Skip to content

Commit

Permalink
REFACTOR: update for core badge style removal
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomerobot committed Nov 2, 2023
1 parent 7afb20e commit 736f4a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 59 deletions.
23 changes: 14 additions & 9 deletions common/common.scss
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
span.category-badge-icon {
.badge-category.--has-icon {
// hide default bullet when icon is set
&:before {
display: none;
}
}

.badge-category__icon {
display: inline-flex;
align-self: center;
color: var(--category-badge-color);
.d-icon {
opacity: 1;
font-size: 1.3em;
color: inherit;
font-size: var(--font-up-2);
}
}

.categories-list .category h3 .d-icon,
.badge-wrapper .badge-category .d-icon,
.select-kit .select-kit-header .selected-name .name .badge-wrapper .d-icon {
margin-right: 5px;
margin-left: 0px;
.categories-list .category h3 .d-icon {
margin-right: 0.25em;
margin-left: 0;
}

.badge-wrapper.bullet span.category-badge-icon .d-icon,
.categories-list .category .category-icon .d-icon {
color: inherit;
}
Expand Down
82 changes: 32 additions & 50 deletions javascripts/discourse/initializers/category-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export default {
lockIcon,
});

function categoryStripe(color, classes) {
let style = color ? "style='background-color: #" + color + ";'" : "";
return "<span class='" + classes + "' " + style + "></span>";
}

function getIconItem(categorySlug) {
if (!categorySlug) {
return;
Expand All @@ -45,7 +40,7 @@ export default {
let categoryThemeItem = categoryThemeList.find((str) =>
str.indexOf(",") > -1
? categorySlug.indexOf(str.substr(0, str.indexOf(","))) > -1
: ""
: "",
);

if (categoryThemeItem) {
Expand All @@ -62,13 +57,13 @@ export default {
function buildTopicCount(count) {
return `<span class="topic-count" aria-label="${I18n.t(
"category_row.topic_count",
{ count }
{ count },
)}">&times; ${count}</span>`;
}

function categoryIconsRenderer(category, opts) {
let siteSettings = helperContext().siteSettings;
let description = get(category, "description_text");
let descriptionText = get(category, "description_text");
let restricted = get(category, "read_restricted");
let url = opts.url
? opts.url
Expand All @@ -81,53 +76,38 @@ export default {
let html = "";
let parentCat = null;
let categoryDir = "";
let dataAttributes = category
? `data-category-id="${get(category, "id")}"`
: "";

/// Add custom category icon from theme settings
let iconItem = getIconItem(category.slug);

if (!opts.hideParent) {
parentCat = Category.findById(get(category, "parent_category_id"));
}

const categoryStyle = opts.categoryStyle || siteSettings.category_style;
if (categoryStyle !== "none") {
if (parentCat && parentCat !== category) {
html += categoryStripe(
get(parentCat, "color"),
"badge-category-parent-bg"
);
}
html += categoryStripe(color, "badge-category-bg");
}

let classNames = "badge-category clear-badge";
let classNames = `badge-category ${iconItem ? "--has-icon" : ""}`;
if (restricted) {
classNames += " restricted";
}

let style = "";
if (categoryStyle === "box") {
style = `style="color: #${get(category, "text_color")};"`;
if (parentCat) {
classNames += ` --has-parent`;
dataAttributes += ` data-parent-category-id="${parentCat.id}"`;
}

html +=
`<span ${style} ` +
'data-drop-close="true" class="' +
classNames +
'"' +
(description
? 'title="' + escapeExpression(description) + '" '
: "") +
">";

/// Add custom category icon from theme settings
let iconItem = getIconItem(category.slug);
html += `<span
${dataAttributes}
data-drop-close="true"
class="${classNames}"
${descriptionText ? 'title="' + descriptionText + '" ' : ""}
>`;

if (iconItem) {
let itemColor = iconItem[2]
? iconItem[2].match(/categoryColo(u*)r/)
? `style="color: #${color}"`
: `style="color: ${iconItem[2]}"`
: "";
let itemColor = iconItem[2] ? `style="color: ${iconItem[2]}"` : "";
let itemIcon = iconItem[1] !== "" ? iconHTML(iconItem[1]) : "";
html += `<span ${itemColor} class="category-badge-icon">${itemIcon}</span>`;
html += `<span ${itemColor} class="badge-category__icon">${itemIcon}</span>`;
}
/// End custom category icon

Expand All @@ -140,25 +120,27 @@ export default {
if (restricted) {
html += iconHTML(lockIcon);
}
html += `<span class="category-name" ${categoryDir}>${categoryName}</span></span>`;
html += `<span class="badge-category__name" ${categoryDir}>${categoryName}</span>`;
html += "</span>";

if (opts.topicCount && categoryStyle !== "box") {
if (opts.topicCount) {
html += buildTopicCount(opts.topicCount);
}

if (href) {
href = ` href="${href}" `;
}

extraClasses = categoryStyle
? categoryStyle + extraClasses
: extraClasses;

let afterBadgeWrapper = "";
if (opts.topicCount && categoryStyle === "box") {
afterBadgeWrapper += buildTopicCount(opts.topicCount);

if (opts.plusSubcategories && opts.lastSubcategory) {
afterBadgeWrapper += `<span class="plus-subcategories">
${I18n.t("category_row.plus_subcategories", {
count: opts.plusSubcategories,
})}
</span>`;
}
return `<${tagName} class="badge-wrapper ${extraClasses}" ${href}>${html}</${tagName}>${afterBadgeWrapper}`;
return `<${tagName} class="badge-category__wrapper ${extraClasses}" ${href}>${html}</${tagName}>${afterBadgeWrapper}`;
}

api.replaceCategoryLinkRenderer(categoryIconsRenderer);
Expand Down

0 comments on commit 736f4a1

Please sign in to comment.