2015-07-29 09:54:33 -07:00
|
|
|
module ThemeHelper
|
2020-05-06 04:36:43 -07:00
|
|
|
ATTRIBUTE_MAP = {
|
|
|
|
'primary_color' => 'primary',
|
|
|
|
'primary_text' => 'primary-text',
|
|
|
|
'danger_color' => 'danger',
|
|
|
|
'danger_text' => 'danger-text',
|
|
|
|
'warning_color' => 'warning',
|
|
|
|
'warning_text' => 'warning-text',
|
|
|
|
'info_color' => 'info',
|
|
|
|
'info_text' => 'info-text',
|
|
|
|
'success_color' => 'success',
|
|
|
|
'success_text' => 'success-text',
|
|
|
|
'dark_color' => 'dark',
|
|
|
|
'dark_text' => 'dark-text',
|
|
|
|
'light_color' => 'light',
|
|
|
|
'light_text' => 'light-text',
|
|
|
|
'raised_background' => 'raised-bg',
|
|
|
|
'raised_accent' => 'raised-accent',
|
|
|
|
'background_color' => 'background',
|
|
|
|
'body_text' => 'body-text',
|
|
|
|
'input_color' => 'input-bg',
|
|
|
|
'input_text' => 'input-text',
|
|
|
|
'muted_text' => 'muted-text'
|
|
|
|
}.freeze
|
2020-05-03 08:28:41 -07:00
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
def render_theme
|
2020-05-03 08:28:41 -07:00
|
|
|
theme = get_active_theme
|
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
return unless theme
|
2020-05-03 08:28:41 -07:00
|
|
|
|
|
|
|
body = ":root {\n"
|
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
theme.attributes.each do |k, v|
|
|
|
|
next unless ATTRIBUTE_MAP.key?(k)
|
2020-05-03 08:28:41 -07:00
|
|
|
|
2020-05-06 04:36:43 -07:00
|
|
|
if k.include? "text"
|
|
|
|
hex = get_hex_color_from_theme_value(v)
|
|
|
|
body += "\t--#{ATTRIBUTE_MAP[k]}: #{get_decimal_triplet_from_hex(hex)};\n"
|
|
|
|
else
|
|
|
|
body += "\t--#{ATTRIBUTE_MAP[k]}: ##{get_hex_color_from_theme_value(v)};\n"
|
|
|
|
end
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
2020-05-06 04:36:43 -07:00
|
|
|
|
|
|
|
body += "}"
|
|
|
|
|
2020-05-06 17:13:07 -07:00
|
|
|
content_tag(:style, body, data: { "turbolinks-track": false })
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_active_theme
|
2020-05-05 11:17:49 -07:00
|
|
|
if @user&.theme
|
2020-05-03 08:28:41 -07:00
|
|
|
if user_signed_in?
|
2020-05-05 11:17:49 -07:00
|
|
|
if current_user&.show_foreign_themes?
|
|
|
|
@user.theme
|
|
|
|
else
|
|
|
|
current_user&.theme
|
|
|
|
end
|
2020-05-03 08:28:41 -07:00
|
|
|
else
|
|
|
|
@user.theme
|
|
|
|
end
|
2020-05-05 11:17:49 -07:00
|
|
|
elsif current_user&.theme
|
|
|
|
current_user.theme
|
2020-05-03 08:28:41 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_hex_color_from_theme_value(value)
|
|
|
|
('0000000' + value.to_s(16))[-6, 6]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_decimal_triplet_from_hex(value)
|
|
|
|
hexes = value.split(/(.{2})/).reject { |c| c.empty? }
|
|
|
|
hexes.map(&:hex).join(", ")
|
|
|
|
end
|
2015-07-29 09:54:33 -07:00
|
|
|
end
|