1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/stylesheet.rb

116 lines
2.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-01-22 14:34:45 +01:00
2020-02-26 10:19:43 +01:00
# Stylesheet is a cached CSS file that allows to easily customize the interface of Fab-manager with some configurable colors and
2020-01-22 14:34:45 +01:00
# a picture for the background of the user's profile.
# There's only one stylesheet record in the database, which is updated on each colour change.
2020-03-25 10:16:47 +01:00
class Stylesheet < ApplicationRecord
# brightness limits to change the font color to black or white
BRIGHTNESS_HIGH_LIMIT = 160
BRIGHTNESS_LOW_LIMIT = 40
2016-03-23 18:39:41 +01:00
validates_presence_of :contents
## ===== COMMON =====
2020-01-27 17:10:29 +01:00
def rebuild!
if Stylesheet.primary && Stylesheet.secondary && name == 'theme'
update(contents: Stylesheet.theme_css)
2020-01-27 17:10:29 +01:00
elsif name == 'home_page'
update(contents: Stylesheet.home_page_css)
end
2016-03-23 18:39:41 +01:00
end
## ===== THEME =====
def self.build_theme!
2018-12-17 16:02:02 +01:00
return unless Stylesheet.primary && Stylesheet.secondary
2020-01-27 17:10:29 +01:00
if Stylesheet.theme
Stylesheet.theme.rebuild!
2018-12-17 16:02:02 +01:00
else
Stylesheet.create!(contents: Stylesheet.theme_css, name: 'theme')
2016-03-23 18:39:41 +01:00
end
end
def self.primary
2020-05-13 15:02:03 +02:00
Setting.get('main_color')
2016-03-23 18:39:41 +01:00
end
def self.secondary
2020-05-13 15:02:03 +02:00
Setting.get('secondary_color')
2016-03-23 18:39:41 +01:00
end
def self.primary_light
2018-12-17 16:02:02 +01:00
Stylesheet.primary.paint.lighten(10)
2016-03-23 18:39:41 +01:00
end
def self.primary_dark
2018-12-17 16:02:02 +01:00
Stylesheet.primary.paint.darken(20)
2016-03-23 18:39:41 +01:00
end
def self.secondary_light
2018-12-17 16:02:02 +01:00
Stylesheet.secondary.paint.lighten(10)
2016-03-23 18:39:41 +01:00
end
def self.secondary_dark
2018-12-17 16:02:02 +01:00
Stylesheet.secondary.paint.darken(20)
2016-03-23 18:39:41 +01:00
end
def self.primary_with_alpha(alpha)
2018-12-17 16:02:02 +01:00
Stylesheet.primary.paint.to_rgb.insert(3, 'a').insert(-2, ", #{alpha}")
2016-03-23 18:39:41 +01:00
end
2020-01-27 17:10:29 +01:00
def self.theme
Stylesheet.find_by(name: 'theme')
end
def self.primary_text_color
Stylesheet.primary.paint.brightness >= BRIGHTNESS_HIGH_LIMIT ? 'black' : 'white'
end
def self.primary_decoration_color
Stylesheet.primary.paint.brightness <= BRIGHTNESS_LOW_LIMIT ? 'white' : 'black'
end
def self.secondary_text_color
Stylesheet.secondary.paint.brightness <= BRIGHTNESS_LOW_LIMIT ? 'white' : 'black'
end
def self.theme_css
2020-10-29 16:11:19 +01:00
erb_files = Dir['app/themes/casemate/**/*.scss.erb']
scss_files = Dir['app/themes/casemate/**/*.scss']
templates = ''
erb_files.each { |erb_file| templates += ERB.new(File.read(erb_file)).result }
scss_files.each { |scss_file| templates += File.read(scss_file) }
2020-10-29 16:11:19 +01:00
engine = SassC::Engine.new(templates, style: :compressed)
engine.render.presence
2016-03-23 18:39:41 +01:00
end
2020-01-27 17:10:29 +01:00
## ===== HOME PAGE =====
def self.home_style
2020-05-13 15:02:03 +02:00
style = Setting.get('home_css')
2020-01-27 17:10:29 +01:00
".home-page { #{style} }"
end
def self.build_home!
if Stylesheet.home_page
Stylesheet.home_page.rebuild!
else
Stylesheet.create!(contents: Stylesheet.home_page_css, name: 'home_page')
end
end
def self.home_page
Stylesheet.find_by(name: 'home_page')
end
def self.home_page_css
engine = SassC::Engine.new(home_style, style: :compressed)
2020-01-27 17:10:29 +01:00
engine.render.presence || '.home-page {}'
end
2016-03-23 18:39:41 +01:00
end