debian-mirror-gitlab/lib/gitlab/themes.rb

88 lines
1.9 KiB
Ruby
Raw Normal View History

2015-09-11 14:41:01 +05:30
module Gitlab
# Module containing GitLab's application theme definitions and helper methods
# for accessing them.
module Themes
2016-09-13 17:45:13 +05:30
extend self
2015-09-11 14:41:01 +05:30
# Theme ID used when no `default_theme` configuration setting is provided.
APPLICATION_DEFAULT = 2
# Struct class representing a single Theme
Theme = Struct.new(:id, :name, :css_class)
# All available Themes
THEMES = [
Theme.new(1, 'Graphite', 'ui_graphite'),
Theme.new(2, 'Charcoal', 'ui_charcoal'),
Theme.new(3, 'Green', 'ui_green'),
Theme.new(4, 'Gray', 'ui_gray'),
Theme.new(5, 'Violet', 'ui_violet'),
Theme.new(6, 'Blue', 'ui_blue')
].freeze
# Convenience method to get a space-separated String of all the theme
# classes that might be applied to the `body` element
#
# Returns a String
2016-09-13 17:45:13 +05:30
def body_classes
2015-09-11 14:41:01 +05:30
THEMES.collect(&:css_class).uniq.join(' ')
end
# Get a Theme by its ID
#
# If the ID is invalid, returns the default Theme.
#
# id - Integer ID
#
# Returns a Theme
2016-09-13 17:45:13 +05:30
def by_id(id)
2015-09-11 14:41:01 +05:30
THEMES.detect { |t| t.id == id } || default
end
2015-09-25 12:07:36 +05:30
# Returns the number of defined Themes
2016-09-13 17:45:13 +05:30
def count
2015-09-25 12:07:36 +05:30
THEMES.size
end
2015-09-11 14:41:01 +05:30
# Get the default Theme
#
# Returns a Theme
2016-09-13 17:45:13 +05:30
def default
2015-09-11 14:41:01 +05:30
by_id(default_id)
end
# Iterate through each Theme
#
# Yields the Theme object
2016-09-13 17:45:13 +05:30
def each(&block)
2015-09-11 14:41:01 +05:30
THEMES.each(&block)
end
2015-09-25 12:07:36 +05:30
# Get the Theme for the specified user, or the default
#
# user - User record
#
# Returns a Theme
2016-09-13 17:45:13 +05:30
def for_user(user)
2015-09-25 12:07:36 +05:30
if user
by_id(user.theme_id)
else
default
end
end
2015-09-11 14:41:01 +05:30
private
2016-09-13 17:45:13 +05:30
def default_id
2015-09-11 14:41:01 +05:30
id = Gitlab.config.gitlab.default_theme.to_i
# Prevent an invalid configuration setting from causing an infinite loop
if id < THEMES.first.id || id > THEMES.last.id
APPLICATION_DEFAULT
else
id
end
end
end
end