2024-03-27 12:20:28 -04:00
|
|
|
import { get } from "@ember/object";
|
|
|
|
import { h } from "virtual-dom";
|
|
|
|
import categoriesBoxes from "discourse/components/categories-boxes";
|
|
|
|
import categoriesBoxesWithTopics from "discourse/components/categories-boxes-with-topics";
|
|
|
|
import categoryTitleLink from "discourse/components/category-title-link";
|
2020-08-04 08:54:50 -04:00
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2024-03-27 12:20:28 -04:00
|
|
|
import { isRTL } from "discourse/lib/text-direction";
|
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
2020-08-04 08:54:50 -04:00
|
|
|
import Category from "discourse/models/category";
|
2024-03-27 12:20:28 -04:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2020-08-04 12:47:52 -04:00
|
|
|
import { helperContext } from "discourse-common/lib/helpers";
|
2020-08-04 08:54:50 -04:00
|
|
|
import { iconHTML, iconNode } from "discourse-common/lib/icon-library";
|
2020-09-16 08:48:02 -04:00
|
|
|
import I18n from "I18n";
|
2020-08-04 08:54:50 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: "category-icons",
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
withPluginApi("0.8.26", (api) => {
|
|
|
|
let categoryThemeList = settings.category_icon_list.split("|");
|
|
|
|
let lockIcon = settings.category_lock_icon || "lock";
|
2020-08-17 16:30:57 -04:00
|
|
|
|
|
|
|
categoryTitleLink.reopen({
|
2022-06-18 10:51:57 -04:00
|
|
|
lockIcon,
|
2020-08-17 16:30:57 -04:00
|
|
|
});
|
|
|
|
|
2021-12-15 13:33:41 -05:00
|
|
|
categoriesBoxes.reopen({
|
2022-06-18 10:51:57 -04:00
|
|
|
lockIcon,
|
2021-12-15 13:33:41 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
categoriesBoxesWithTopics.reopen({
|
2022-06-18 10:51:57 -04:00
|
|
|
lockIcon,
|
2021-12-15 13:33:41 -05:00
|
|
|
});
|
|
|
|
|
2020-08-04 08:54:50 -04:00
|
|
|
function getIconItem(categorySlug) {
|
2022-06-18 10:51:57 -04:00
|
|
|
if (!categorySlug) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-04 08:54:50 -04:00
|
|
|
|
|
|
|
let categoryThemeItem = categoryThemeList.find((str) =>
|
|
|
|
str.indexOf(",") > -1
|
|
|
|
? categorySlug.indexOf(str.substr(0, str.indexOf(","))) > -1
|
|
|
|
: ""
|
|
|
|
);
|
2023-05-25 21:01:41 -04:00
|
|
|
|
2020-08-04 08:54:50 -04:00
|
|
|
if (categoryThemeItem) {
|
|
|
|
let iconItem = categoryThemeItem.split(",");
|
|
|
|
// Test partial/exact match
|
2022-06-18 10:51:57 -04:00
|
|
|
if (iconItem[3] === "partial") {
|
2020-08-04 08:54:50 -04:00
|
|
|
return iconItem;
|
|
|
|
} else if (iconItem[0] === categorySlug) {
|
|
|
|
return iconItem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 08:48:02 -04:00
|
|
|
function buildTopicCount(count) {
|
|
|
|
return `<span class="topic-count" aria-label="${I18n.t(
|
|
|
|
"category_row.topic_count",
|
|
|
|
{ count }
|
|
|
|
)}">× ${count}</span>`;
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:54:50 -04:00
|
|
|
function categoryIconsRenderer(category, opts) {
|
2020-08-04 12:47:52 -04:00
|
|
|
let siteSettings = helperContext().siteSettings;
|
2023-12-06 09:59:42 -05:00
|
|
|
let descriptionText = escapeExpression(
|
|
|
|
get(category, "description_text")
|
|
|
|
);
|
2020-08-04 08:54:50 -04:00
|
|
|
let restricted = get(category, "read_restricted");
|
|
|
|
let url = opts.url
|
|
|
|
? opts.url
|
|
|
|
: getURL(`/c/${Category.slugFor(category)}/${get(category, "id")}`);
|
|
|
|
let href = opts.link === false ? "" : url;
|
|
|
|
let tagName =
|
|
|
|
opts.link === false || opts.link === "false" ? "span" : "a";
|
|
|
|
let extraClasses = opts.extraClasses ? " " + opts.extraClasses : "";
|
|
|
|
let html = "";
|
|
|
|
let parentCat = null;
|
|
|
|
let categoryDir = "";
|
2023-11-13 10:46:33 -05:00
|
|
|
let dataAttributes = category
|
|
|
|
? `data-category-id="${get(category, "id")}"`
|
|
|
|
: "";
|
|
|
|
|
|
|
|
/// Add custom category icon from theme settings
|
|
|
|
let iconItem = getIconItem(category.slug);
|
2020-08-04 08:54:50 -04:00
|
|
|
|
|
|
|
if (!opts.hideParent) {
|
|
|
|
parentCat = Category.findById(get(category, "parent_category_id"));
|
|
|
|
}
|
|
|
|
|
2023-11-13 10:46:33 -05:00
|
|
|
let classNames = `badge-category ${iconItem ? "--has-icon" : ""}`;
|
2020-08-04 08:54:50 -04:00
|
|
|
if (restricted) {
|
|
|
|
classNames += " restricted";
|
|
|
|
}
|
|
|
|
|
2023-11-13 10:46:33 -05:00
|
|
|
if (parentCat) {
|
|
|
|
classNames += ` --has-parent`;
|
|
|
|
dataAttributes += ` data-parent-category-id="${parentCat.id}"`;
|
2020-08-04 08:54:50 -04:00
|
|
|
}
|
|
|
|
|
2023-11-13 10:46:33 -05:00
|
|
|
html += `<span
|
|
|
|
${dataAttributes}
|
|
|
|
data-drop-close="true"
|
|
|
|
class="${classNames}"
|
|
|
|
${descriptionText ? 'title="' + descriptionText + '" ' : ""}
|
|
|
|
>`;
|
2023-05-25 21:01:41 -04:00
|
|
|
|
2020-08-04 08:54:50 -04:00
|
|
|
if (iconItem) {
|
2023-11-13 10:46:33 -05:00
|
|
|
let itemColor = iconItem[2] ? `style="color: ${iconItem[2]}"` : "";
|
2022-06-18 10:51:57 -04:00
|
|
|
let itemIcon = iconItem[1] !== "" ? iconHTML(iconItem[1]) : "";
|
2023-11-13 10:46:33 -05:00
|
|
|
html += `<span ${itemColor} class="badge-category__icon">${itemIcon}</span>`;
|
2020-08-04 08:54:50 -04:00
|
|
|
}
|
|
|
|
/// End custom category icon
|
|
|
|
|
|
|
|
let categoryName = escapeExpression(get(category, "name"));
|
|
|
|
|
2020-08-04 12:47:52 -04:00
|
|
|
if (siteSettings.support_mixed_text_direction) {
|
2020-08-04 08:54:50 -04:00
|
|
|
categoryDir = isRTL(categoryName) ? 'dir="rtl"' : 'dir="ltr"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restricted) {
|
|
|
|
html += iconHTML(lockIcon);
|
|
|
|
}
|
2023-11-13 10:46:33 -05:00
|
|
|
html += `<span class="badge-category__name" ${categoryDir}>${categoryName}</span>`;
|
|
|
|
html += "</span>";
|
2020-08-04 08:54:50 -04:00
|
|
|
|
2023-11-13 10:46:33 -05:00
|
|
|
if (opts.topicCount) {
|
2020-09-16 08:48:02 -04:00
|
|
|
html += buildTopicCount(opts.topicCount);
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:54:50 -04:00
|
|
|
if (href) {
|
|
|
|
href = ` href="${href}" `;
|
|
|
|
}
|
|
|
|
|
2020-09-16 08:48:02 -04:00
|
|
|
let afterBadgeWrapper = "";
|
2023-11-13 10:46:33 -05:00
|
|
|
|
|
|
|
if (opts.plusSubcategories && opts.lastSubcategory) {
|
|
|
|
afterBadgeWrapper += `<span class="plus-subcategories">
|
|
|
|
${I18n.t("category_row.plus_subcategories", {
|
|
|
|
count: opts.plusSubcategories,
|
|
|
|
})}
|
|
|
|
</span>`;
|
2020-09-16 08:48:02 -04:00
|
|
|
}
|
2023-11-13 10:46:33 -05:00
|
|
|
return `<${tagName} class="badge-category__wrapper ${extraClasses}" ${href}>${html}</${tagName}>${afterBadgeWrapper}`;
|
2020-08-04 08:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
api.replaceCategoryLinkRenderer(categoryIconsRenderer);
|
|
|
|
|
|
|
|
api.createWidget("category-icon", {
|
|
|
|
tagName: "div.category-icon-widget",
|
|
|
|
html(attrs) {
|
|
|
|
let iconItem = getIconItem(attrs.category.slug);
|
2020-08-25 13:27:32 -04:00
|
|
|
if (iconItem) {
|
2020-09-28 10:41:15 -04:00
|
|
|
let itemColor = iconItem[2]
|
|
|
|
? iconItem[2].match(/categoryColo(u*)r/g)
|
|
|
|
? `color: #${attrs.category.color}`
|
|
|
|
: `color: ${iconItem[2]}`
|
|
|
|
: "";
|
2022-06-18 10:51:57 -04:00
|
|
|
let itemIcon = iconItem[1] !== "" ? iconNode(iconItem[1]) : "";
|
2020-08-04 08:54:50 -04:00
|
|
|
return h("span.category-icon", { style: itemColor }, itemIcon);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-05-25 21:01:41 -04:00
|
|
|
|
|
|
|
if (api.registerCustomCategorySectionLinkLockIcon) {
|
|
|
|
api.registerCustomCategorySectionLinkLockIcon(lockIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (api.registerCustomCategorySectionLinkPrefix) {
|
|
|
|
const site = api.container.lookup("service:site");
|
|
|
|
|
|
|
|
categoryThemeList.forEach((str) => {
|
|
|
|
const [slug, icon, color, match] = str.split(",");
|
|
|
|
|
2024-06-14 12:05:43 -04:00
|
|
|
if (slug && icon) {
|
2023-05-25 21:01:41 -04:00
|
|
|
const category = site.categories.find((cat) => {
|
|
|
|
if (match === "partial") {
|
|
|
|
return cat.slug.toLowerCase().includes(slug.toLowerCase());
|
|
|
|
} else {
|
|
|
|
return cat.slug.toLowerCase() === slug.toLowerCase();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
const opts = {
|
|
|
|
categoryId: category.id,
|
|
|
|
prefixType: "icon",
|
|
|
|
prefixValue: icon,
|
2024-06-16 21:09:42 -04:00
|
|
|
prefixColor: color,
|
2023-05-25 21:01:41 -04:00
|
|
|
};
|
|
|
|
|
2024-06-16 21:09:42 -04:00
|
|
|
// Fix for legacy color declaration
|
|
|
|
if (color?.match(/categoryColo(u*)r/g)) {
|
|
|
|
opts.prefixColor = category.color;
|
2023-05-25 21:01:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
api.registerCustomCategorySectionLinkPrefix(opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-04 08:54:50 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|