2015-01-23 15:02:25 -05:00
|
|
|
module CategoryBadge
|
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
def self.category_stripe(color, classes)
|
|
|
|
style = color ? "style='background-color: ##{color};'" : ''
|
|
|
|
"<span class='#{classes}' #{style}></span>"
|
|
|
|
end
|
|
|
|
|
2015-01-23 15:02:25 -05:00
|
|
|
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]
|
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
extra_classes = "#{opts[:extra_classes]} #{SiteSetting.category_style}"
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
result = ''
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
unless category.parent_category_id.nil? || opts[:hide_parent]
|
2015-01-23 15:02:25 -05:00
|
|
|
parent_category = Category.find_by(id: category.parent_category_id)
|
2015-05-13 07:25:27 -04:00
|
|
|
result << category_stripe(parent_category.color, 'badge-category-parent-bg')
|
2015-01-23 15:02:25 -05:00
|
|
|
end
|
2015-05-13 07:25:27 -04:00
|
|
|
result << category_stripe(category.color, 'badge-category-bg')
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
class_names = 'badge-category clear-badge'
|
|
|
|
text_color = "##{category.text_color}"
|
|
|
|
description = category.description_text ? "title='#{category.description_text.html_safe}'" : ''
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
result << "<span style='color: #{text_color};' data-drop-close='true' class='#{class_names}'
|
|
|
|
#{description}'>"
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-13 07:25:27 -04:00
|
|
|
result << category.name.html_safe << '</span>'
|
|
|
|
"<a class='badge-wrapper #{extra_classes}' href='#{Discourse.base_url}#{category.url}'>#{result}</a>"
|
|
|
|
end
|
2015-01-23 15:02:25 -05:00
|
|
|
end
|