about summary refs log tree commit diff
path: root/app/controllers/concerns/theming_concern.rb
blob: f993a81d720fcb9ab16bf639f558d552853004b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

module ThemingConcern
  extend ActiveSupport::Concern

  def use_pack(pack_name)
    @core = resolve_pack_with_common(Themes.instance.core, pack_name)
    @theme = resolve_pack_with_common(Themes.instance.flavour(current_flavour), pack_name, current_skin)
  end

  private

  def current_flavour
    [current_user&.setting_flavour, Setting.flavour, 'glitch', 'vanilla'].find { |flavour| Themes.instance.flavours.include?(flavour) }
  end

  def current_skin
    skins = Themes.instance.skins_for(current_flavour)
    [current_user&.setting_skin, Setting.skin, 'default'].find { |skin| skins.include?(skin) }
  end

  def valid_pack_data?(data, pack_name)
    data['pack'].is_a?(Hash) && data['pack'][pack_name].present?
  end

  def nil_pack(data)
    {
      use_common: true,
      flavour: data['name'],
      pack: nil,
      preload: nil,
      skin: nil,
      supported_locales: data['locales'],
    }
  end

  def pack(data, pack_name, skin)
    pack_data = {
      use_common: true,
      flavour: data['name'],
      pack: pack_name,
      preload: nil,
      skin: nil,
      supported_locales: data['locales'],
    }

    return pack_data unless data['pack'][pack_name].is_a?(Hash)

    pack_data[:use_common] = false if data['pack'][pack_name]['use_common'] == false
    pack_data[:pack] = nil unless data['pack'][pack_name]['filename']

    preloads = data['pack'][pack_name]['preload']
    pack_data[:preload] = [preloads] if preloads.is_a?(String)
    pack_data[:preload] = preloads if preloads.is_a?(Array)

    if skin != 'default' && data['skin'][skin]
      pack_data[:skin] = skin if data['skin'][skin].include?(pack_name)
    elsif data['pack'][pack_name]['stylesheet']
      pack_data[:skin] = 'default'
    end

    pack_data
  end

  def resolve_pack(data, pack_name, skin)
    return pack(data, pack_name, skin) if valid_pack_data?(data, pack_name)
    return if data['name'].blank?

    fallbacks = []
    if data.key?('fallback')
      fallbacks = data['fallback'] if data['fallback'].is_a?(Array)
      fallbacks = [data['fallback']] if data['fallback'].is_a?(String)
    elsif data['name'] != Setting.default_settings['flavour']
      fallbacks = [Setting.default_settings['flavour']]
    end

    fallbacks.each do |fallback|
      return resolve_pack(Themes.instance.flavour(fallback), pack_name) if Themes.instance.flavour(fallback)
    end

    nil
  end

  def resolve_pack_with_common(data, pack_name, skin = 'default')
    result = resolve_pack(data, pack_name, skin) || nil_pack(data)
    result[:common] = resolve_pack(data, 'common', skin) if result.delete(:use_common)
    result
  end
end