mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 02:11:08 -06:00
52 lines
1.7 KiB
Ruby
52 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module CategoryBadge
|
|
def self.html_for(category, opts = nil)
|
|
opts = opts || {}
|
|
|
|
# If there is no category, bail
|
|
return "" if category.blank?
|
|
|
|
# By default hide uncategorized
|
|
return "" if category.uncategorized? && !opts[:show_uncategorized]
|
|
|
|
extra_classes = "#{opts[:extra_classes]}"
|
|
|
|
result = +""
|
|
|
|
# parent class
|
|
parent_category =
|
|
Category.find_by(id: category.parent_category_id) unless category.parent_category_id.nil?
|
|
has_parent_class = parent_category ? "--has-parent" : ""
|
|
|
|
# category name
|
|
class_names = "badge-category #{has_parent_class}"
|
|
description = category.description_text ? "title='#{category.description_text}'" : ""
|
|
category_url =
|
|
opts[:absolute_url] ? "#{Discourse.base_url_no_prefix}#{category.url}" : category.url
|
|
|
|
# styles
|
|
styles = {
|
|
"--category-badge-color": "##{category.color}",
|
|
"--category-badge-text-color": "##{category.text_color}",
|
|
}
|
|
styles["--parent-category-badge-color"] = "##{parent_category.color}" if parent_category
|
|
style_value = styles.map { |k, v| "#{k}: #{ERB::Util.html_escape(v)};" }.join(" ")
|
|
|
|
# category badge structure
|
|
result << "<span data-category-id='#{category.id}'"
|
|
result << " style='#{style_value}'"
|
|
result << " data-parent-category-id='#{parent_category.id}'" if parent_category
|
|
result << " data-drop-close='true' class='#{class_names}' #{description}>"
|
|
result << "<span class='badge-category__name'>"
|
|
result << ERB::Util.html_escape(category.name)
|
|
result << "</span></span>"
|
|
|
|
# wrapping link
|
|
result =
|
|
"<a class='badge-category__wrapper #{extra_classes}' href='#{category_url}'>#{result}</a>"
|
|
|
|
result.html_safe
|
|
end
|
|
end
|