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-05-17 12:38:43 -04:00
|
|
|
def self.inline_category_stripe(color, styles = '', insert_blank = false)
|
|
|
|
"<span style='background-color: ##{color};#{styles}'>#{insert_blank ? ' ' : ''}</span>"
|
|
|
|
end
|
|
|
|
|
2018-11-15 08:35:50 -05:00
|
|
|
def self.inline_badge_wrapper_style(category)
|
2017-07-27 21:20:09 -04:00
|
|
|
style =
|
|
|
|
case (SiteSetting.category_style || :box).to_sym
|
|
|
|
when :bar then 'line-height: 1.25; margin-right: 5px;'
|
2018-11-15 08:35:50 -05:00
|
|
|
when :box then "background-color:##{category.color}; line-height: 1.5; margin-top: 5px; margin-right: 5px;"
|
2017-07-27 21:20:09 -04:00
|
|
|
when :bullet then 'line-height: 1; margin-right: 10px;'
|
2017-08-24 13:44:59 -04:00
|
|
|
when :none then ''
|
2017-07-27 21:20:09 -04:00
|
|
|
end
|
|
|
|
|
2015-05-17 12:38:43 -04:00
|
|
|
" style='font-size: 0.857em; white-space: nowrap; display: inline-block; position: relative; #{style}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.html_for(category, opts = nil)
|
2015-01-23 15:02:25 -05:00
|
|
|
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-17 12:38:43 -04:00
|
|
|
# parent span
|
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)
|
2017-07-27 21:20:09 -04:00
|
|
|
result <<
|
|
|
|
if opts[:inline_style]
|
|
|
|
case (SiteSetting.category_style || :box).to_sym
|
|
|
|
when :bar
|
|
|
|
inline_category_stripe(parent_category.color, 'display: inline-block; padding: 1px;', true)
|
|
|
|
when :box
|
2018-11-15 08:35:50 -05:00
|
|
|
inline_category_stripe(parent_category.color, 'display: inline-block; padding: 0 1px;', true)
|
2017-07-27 21:20:09 -04:00
|
|
|
when :bullet
|
|
|
|
inline_category_stripe(parent_category.color, 'display: inline-block; width: 5px; height: 10px; line-height: 1;')
|
2017-08-24 13:44:59 -04:00
|
|
|
when :none
|
|
|
|
''
|
2017-07-27 21:20:09 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
category_stripe(parent_category.color, 'badge-category-parent-bg')
|
2015-05-17 12:38:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-09 16:59:54 -05:00
|
|
|
show_parent = category.parent_category_id && !opts[:hide_parent]
|
|
|
|
|
2015-05-17 12:38:43 -04:00
|
|
|
# sub parent or main category span
|
2017-07-27 21:20:09 -04:00
|
|
|
result <<
|
|
|
|
if opts[:inline_style]
|
|
|
|
case (SiteSetting.category_style || :box).to_sym
|
|
|
|
when :bar
|
|
|
|
inline_category_stripe(category.color, 'display: inline-block; padding: 1px;', true)
|
|
|
|
when :box
|
2018-11-15 08:35:50 -05:00
|
|
|
''
|
2017-07-27 21:20:09 -04:00
|
|
|
when :bullet
|
|
|
|
inline_category_stripe(category.color, "display: inline-block; width: #{category.parent_category_id.nil? ? 10 : 5}px; height: 10px;")
|
2017-08-24 13:44:59 -04:00
|
|
|
when :none
|
|
|
|
''
|
2016-12-09 16:59:54 -05:00
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
else
|
|
|
|
category_stripe(category.color, 'badge-category-bg')
|
2015-05-17 12:38:43 -04:00
|
|
|
end
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2015-05-17 12:38:43 -04:00
|
|
|
# category name
|
2015-05-13 07:25:27 -04:00
|
|
|
class_names = 'badge-category clear-badge'
|
2018-06-28 04:14:55 -04:00
|
|
|
description = category.description_text ? "title='#{category.description_text}'" : ''
|
2016-04-26 11:18:34 -04:00
|
|
|
category_url = opts[:absolute_url] ? "#{Discourse.base_url_no_prefix}#{category.url}" : category.url
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
extra_span_classes =
|
|
|
|
if opts[:inline_style]
|
2015-05-17 12:38:43 -04:00
|
|
|
case (SiteSetting.category_style || :box).to_sym
|
2017-07-27 21:20:09 -04:00
|
|
|
when :bar
|
2017-08-15 02:45:56 -04:00
|
|
|
'color: #222222; padding: 3px; vertical-align: text-top; margin-top: -3px; display: inline-block;'
|
2017-07-27 21:20:09 -04:00
|
|
|
when :box
|
2018-11-15 08:35:50 -05:00
|
|
|
"color: ##{category.text_color}; padding: 0 5px;"
|
2017-07-27 21:20:09 -04:00
|
|
|
when :bullet
|
2017-08-15 02:45:56 -04:00
|
|
|
'color: #222222; vertical-align: text-top; line-height: 1; margin-left: 4px; padding-left: 2px; display: inline;'
|
2017-08-24 13:44:59 -04:00
|
|
|
when :none
|
|
|
|
''
|
2015-05-17 12:38:43 -04:00
|
|
|
end + 'max-width: 150px; overflow: hidden; text-overflow: ellipsis;'
|
2018-04-27 14:04:56 -04:00
|
|
|
elsif (SiteSetting.category_style).to_sym == :box
|
|
|
|
"color: ##{category.text_color}"
|
2015-05-17 12:38:43 -04:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2017-08-15 02:45:56 -04:00
|
|
|
result << "<span style='#{extra_span_classes}' data-drop-close='true' class='#{class_names}'
|
2015-05-17 12:38:43 -04:00
|
|
|
#{description}>"
|
2015-01-23 15:02:25 -05:00
|
|
|
|
2018-06-28 04:14:55 -04:00
|
|
|
result << ERB::Util.html_escape(category.name) << '</span>'
|
|
|
|
|
2018-11-15 08:35:50 -05:00
|
|
|
result = "<a class='badge-wrapper #{extra_classes}' href='#{category_url}'" + (opts[:inline_style] ? inline_badge_wrapper_style(category) : '') + ">#{result}</a>"
|
2018-06-28 04:14:55 -04:00
|
|
|
|
|
|
|
result.html_safe
|
2015-05-13 07:25:27 -04:00
|
|
|
end
|
2015-01-23 15:02:25 -05:00
|
|
|
end
|