Module: Gitlab::Themes

Extended by:
Themes
Included in:
Themes
Defined in:
lib/gitlab/themes.rb

Overview

Module containing GitLab’s application theme definitions and helper methods for accessing them.

Defined Under Namespace

Classes: Theme

Constant Summary collapse

APPLICATION_DEFAULT =

Theme ID used when no default_theme configuration setting is provided.

3
DEPRECATED_THEME_IDS =

Theme IDs previously used

[
  6, # Light indigo
  7, # Light blue
  8, # Light green
  10, # Light red
  11 # Dark mode theme
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid_idsObject



104
105
106
# File 'lib/gitlab/themes.rb', line 104

def self.valid_ids
  available_themes.map(&:id) + DEPRECATED_THEME_IDS
end

Instance Method Details

#available_themesObject

All available Themes



25
26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/themes.rb', line 25

def available_themes
  [
    Theme.new(3, s_('NavigationTheme|Default'), 'ui-neutral', '#F1F0F6', '#232128'),
    Theme.new(1, s_('NavigationTheme|Indigo'), 'ui-indigo', '#F4F0FF', '#25202F'),
    Theme.new(4, s_('NavigationTheme|Blue'), 'ui-blue', '#E9F3FC', '#1B202F'),
    Theme.new(5, s_('NavigationTheme|Green'), 'ui-green', '#ECF4EE', '#1C2527'),
    Theme.new(9, s_('NavigationTheme|Red'), 'ui-red', '#FCF1EF', '#2C1D1F'),
    Theme.new(2, s_('NavigationTheme|Gray'), 'ui-gray', '#ECECEF', '#232227')
  ]
end

#body_classesObject

Convenience method to get a space-separated String of all the theme classes that might be applied to the body element

Returns a String



40
41
42
# File 'lib/gitlab/themes.rb', line 40

def body_classes
  available_themes.collect(&:css_class).uniq.join(' ')
end

#by_id(id) ⇒ Object

Get a Theme by its ID

If the ID is invalid, returns the default Theme.

id - Integer ID

Returns a Theme



65
66
67
68
69
70
# File 'lib/gitlab/themes.rb', line 65

def by_id(id)
  # Map deprecated IDs to new values
  mapped_id = map_deprecated_themes[id] || id

  available_themes.detect { |t| t.id == mapped_id } || default
end

#countObject

Returns the number of defined Themes



73
74
75
# File 'lib/gitlab/themes.rb', line 73

def count
  available_themes.size
end

#defaultObject

Get the default Theme

Returns a Theme



80
81
82
# File 'lib/gitlab/themes.rb', line 80

def default
  by_id(default_id)
end

#each(&block) ⇒ Object

Iterate through each Theme

Yields the Theme object



87
88
89
# File 'lib/gitlab/themes.rb', line 87

def each(&block)
  available_themes.each(&block)
end

#for_user(user) ⇒ Object

Get the Theme for the specified user, or the default

user - User record

Returns a Theme



96
97
98
99
100
101
102
# File 'lib/gitlab/themes.rb', line 96

def for_user(user)
  if user
    by_id(user.theme_id)
  else
    default
  end
end

#map_deprecated_themesObject

Maps deprecated light themes to their default counterpart



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/themes.rb', line 45

def map_deprecated_themes
  {
    # Light indigo to indigo
    6 => 1,
    # Light blue to blue
    7 => 4,
    # Light green to green
    8 => 5,
    # Light red to red
    10 => 9
  }
end