2015-09-23 02:47:17 -04:00
|
|
|
require_dependency 'distributed_cache'
|
2014-11-20 16:32:16 -05:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
class ColorScheme < ActiveRecord::Base
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
CUSTOM_SCHEMES = {
|
|
|
|
dark: {
|
|
|
|
"primary" => 'dddddd',
|
|
|
|
"secondary" => '222222',
|
|
|
|
"tertiary" => '0f82af',
|
|
|
|
"quaternary" => 'c14924',
|
|
|
|
"header_background" => '111111',
|
2017-10-03 13:01:05 -04:00
|
|
|
"header_primary" => 'dddddd',
|
2017-04-12 10:52:52 -04:00
|
|
|
"highlight" => 'a87137',
|
|
|
|
"danger" => 'e45735',
|
|
|
|
"success" => '1ca551',
|
|
|
|
"love" => 'fa6c8d'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.base_color_scheme_colors
|
2016-09-02 11:42:14 -04:00
|
|
|
base_with_hash = {}
|
|
|
|
base_colors.each do |name, color|
|
2017-04-12 10:52:52 -04:00
|
|
|
base_with_hash[name] = "#{color}"
|
2016-09-02 11:42:14 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
list = [
|
|
|
|
{ id: 'default', colors: base_with_hash }
|
2016-09-02 11:42:14 -04:00
|
|
|
]
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
CUSTOM_SCHEMES.each do |k, v|
|
|
|
|
list.push(id: k.to_s, colors: v)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
list
|
2016-09-02 11:42:14 -04:00
|
|
|
end
|
|
|
|
|
2015-09-23 02:47:17 -04:00
|
|
|
def self.hex_cache
|
|
|
|
@hex_cache ||= DistributedCache.new("scheme_hex_for_name")
|
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
attr_accessor :is_base
|
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
has_many :color_scheme_colors, -> { order('id ASC') }, dependent: :destroy
|
|
|
|
|
|
|
|
alias_method :colors, :color_scheme_colors
|
|
|
|
|
2018-03-15 03:26:54 -04:00
|
|
|
before_save :bump_version
|
2014-11-10 23:32:44 -05:00
|
|
|
after_save :publish_discourse_stylesheet
|
2015-09-23 02:47:17 -04:00
|
|
|
after_save :dump_hex_cache
|
|
|
|
after_destroy :dump_hex_cache
|
2017-04-17 15:56:13 -04:00
|
|
|
belongs_to :theme
|
2014-04-16 09:49:06 -04:00
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
validates_associated :color_scheme_colors
|
|
|
|
|
|
|
|
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
|
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
|
|
|
|
def self.base_colors
|
|
|
|
@mutex.synchronize do
|
|
|
|
return @base_colors if @base_colors
|
|
|
|
@base_colors = {}
|
2016-09-02 11:42:14 -04:00
|
|
|
File.readlines(BASE_COLORS_FILE).each do |line|
|
2014-05-02 17:46:03 -04:00
|
|
|
matches = /\$([\w]+):\s*#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?:[;]|\s)/.match(line.strip)
|
|
|
|
@base_colors[matches[1]] = matches[2] if matches
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@base_colors
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def self.base_color_schemes
|
|
|
|
base_color_scheme_colors.map do |hash|
|
|
|
|
scheme = new(name: I18n.t("color_schemes.#{hash[:id]}"), base_scheme_id: hash[:id])
|
2017-07-27 21:20:09 -04:00
|
|
|
scheme.colors = hash[:colors].map { |k, v| { name: k.to_s, hex: v.sub("#", "") } }
|
2017-04-12 10:52:52 -04:00
|
|
|
scheme.is_base = true
|
|
|
|
scheme
|
|
|
|
end
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
def self.base
|
2014-06-03 12:36:34 -04:00
|
|
|
return @base_color_scheme if @base_color_scheme
|
2017-04-12 10:52:52 -04:00
|
|
|
@base_color_scheme = new(name: I18n.t('color_schemes.base_theme_name'))
|
2017-07-27 21:20:09 -04:00
|
|
|
@base_color_scheme.colors = base_colors.map { |name, hex| { name: name, hex: hex } }
|
2014-06-03 12:36:34 -04:00
|
|
|
@base_color_scheme.is_base = true
|
|
|
|
@base_color_scheme
|
|
|
|
end
|
|
|
|
|
|
|
|
# create_from_base will create a new ColorScheme that overrides Discourse's base color scheme with the given colors.
|
|
|
|
def self.create_from_base(params)
|
|
|
|
new_color_scheme = new(name: params[:name])
|
2017-05-03 11:31:16 -04:00
|
|
|
new_color_scheme.via_wizard = true if params[:via_wizard]
|
|
|
|
new_color_scheme.base_scheme_id = params[:base_scheme_id]
|
|
|
|
|
|
|
|
colors = CUSTOM_SCHEMES[params[:base_scheme_id].to_sym]&.map do |name, hex|
|
2017-07-27 21:20:09 -04:00
|
|
|
{ name: name, hex: hex }
|
2017-05-03 11:31:16 -04:00
|
|
|
end if params[:base_scheme_id]
|
|
|
|
colors ||= base.colors_hashes
|
2014-06-03 12:36:34 -04:00
|
|
|
|
|
|
|
# Override base values
|
|
|
|
params[:colors].each do |name, hex|
|
2017-07-27 21:20:09 -04:00
|
|
|
c = colors.find { |x| x[:name].to_s == name.to_s }
|
2014-06-03 12:36:34 -04:00
|
|
|
c[:hex] = hex
|
2017-05-03 11:31:16 -04:00
|
|
|
end if params[:colors]
|
2014-06-03 12:36:34 -04:00
|
|
|
|
|
|
|
new_color_scheme.colors = colors
|
|
|
|
new_color_scheme.save
|
|
|
|
new_color_scheme
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2017-01-12 15:43:09 -05:00
|
|
|
def self.lookup_hex_for_name(name)
|
2017-05-03 14:35:13 -04:00
|
|
|
enabled_color_scheme = Theme.where(key: SiteSetting.default_theme_key).first&.color_scheme
|
2017-07-27 21:20:09 -04:00
|
|
|
(enabled_color_scheme || base).colors.find { |c| c.name == name }.try(:hex) || :nil
|
2017-01-12 15:43:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.hex_for_name(name)
|
|
|
|
hex_cache[name] ||= lookup_hex_for_name(name)
|
|
|
|
hex_cache[name] == :nil ? nil : hex_cache[name]
|
2014-07-17 12:31:37 -04:00
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
def colors=(arr)
|
|
|
|
@colors_by_name = nil
|
|
|
|
arr.each do |c|
|
2017-07-27 21:20:09 -04:00
|
|
|
self.color_scheme_colors << ColorSchemeColor.new(name: c[:name], hex: c[:hex])
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_by_name
|
2017-07-27 21:20:09 -04:00
|
|
|
@colors_by_name ||= self.colors.inject({}) { |sum, c| sum[c.name] = c; sum; }
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
def clear_colors_cache
|
|
|
|
@colors_by_name = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def colors_hashes
|
|
|
|
color_scheme_colors.map do |c|
|
2017-07-27 21:20:09 -04:00
|
|
|
{ name: c.name, hex: c.hex }
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def base_colors
|
|
|
|
colors = nil
|
|
|
|
if base_scheme_id && base_scheme_id != "default"
|
|
|
|
colors = CUSTOM_SCHEMES[base_scheme_id.to_sym]
|
|
|
|
end
|
|
|
|
colors || ColorScheme.base_colors
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
def resolved_colors
|
|
|
|
resolved = ColorScheme.base_colors.dup
|
|
|
|
if base_scheme_id && base_scheme_id != "default"
|
|
|
|
if scheme = CUSTOM_SCHEMES[base_scheme_id.to_sym]
|
|
|
|
scheme.each do |name, value|
|
|
|
|
resolved[name] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
colors.each do |c|
|
|
|
|
resolved[c.name] = c.hex
|
|
|
|
end
|
|
|
|
resolved
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
|
|
|
|
2014-11-10 23:32:44 -05:00
|
|
|
def publish_discourse_stylesheet
|
2017-04-12 10:52:52 -04:00
|
|
|
if self.id
|
|
|
|
themes = Theme.where(color_scheme_id: self.id).to_a
|
|
|
|
if themes.present?
|
|
|
|
Stylesheet::Manager.cache.clear
|
|
|
|
themes.each do |theme|
|
|
|
|
theme.notify_scheme_change(_clear_manager_cache = false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-11-10 23:32:44 -05:00
|
|
|
end
|
|
|
|
|
2015-09-23 02:47:17 -04:00
|
|
|
def dump_hex_cache
|
|
|
|
self.class.hex_cache.clear
|
|
|
|
end
|
|
|
|
|
2018-03-15 03:26:54 -04:00
|
|
|
def bump_version
|
|
|
|
if self.id
|
|
|
|
self.version += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-16 09:49:06 -04:00
|
|
|
end
|
2014-05-08 02:45:49 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: color_schemes
|
|
|
|
#
|
2017-04-12 10:52:52 -04:00
|
|
|
# id :integer not null, primary key
|
2018-02-20 01:28:58 -05:00
|
|
|
# name :string not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# version :integer default(1), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# via_wizard :boolean default(FALSE), not null
|
|
|
|
# base_scheme_id :string
|
2017-04-20 15:47:25 -04:00
|
|
|
# theme_id :integer
|
2014-05-08 02:45:49 -04:00
|
|
|
#
|