themes.rb 3.25 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2013 the ChiliProject Team
6
#
7 8 9 10
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
11
#
12 13 14
# See doc/COPYRIGHT.rdoc for more details.
#++

15 16
module Redmine
  module Themes
17

18 19 20 21
    # Return an array of installed themes
    def self.themes
      @@installed_themes ||= scan_themes
    end
22

23 24 25 26
    # Rescan themes directory
    def self.rescan
      @@installed_themes = scan_themes
    end
27

28
    # Return theme for given id, or nil if it's not found
29
    def self.theme(id, options={})
30
      return nil if id.blank?
31

32 33 34 35 36 37
      found = themes.find {|t| t.id == id}
      if found.nil? && options[:rescan] != false
        rescan
        found = theme(id, :rescan => false)
      end
      found
38
    end
39

40 41
    # Class used to represent a theme
    class Theme
42
      attr_reader :path, :name, :dir
43

44
      def initialize(path)
45
        @path = path
46 47
        @dir = File.basename(path)
        @name = @dir.humanize
48 49
        @stylesheets = nil
        @javascripts = nil
50
      end
51

52 53
      # Directory name used as the theme id
      def id; dir end
54

55 56 57
      def ==(theme)
        theme.is_a?(Theme) && theme.dir == dir
      end
58

59 60 61
      def <=>(theme)
        name <=> theme.name
      end
62

63 64 65
      def stylesheets
        @stylesheets ||= assets("stylesheets", "css")
      end
66

67 68 69
      def javascripts
        @javascripts ||= assets("javascripts", "js")
      end
70

71 72 73
      def stylesheet_path(source)
        "/themes/#{dir}/stylesheets/#{source}"
      end
74

75 76 77
      def javascript_path(source)
        "/themes/#{dir}/javascripts/#{source}"
      end
78

79
      private
80

81 82 83
      def assets(dir, ext)
        Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
      end
84
    end
85

86
    private
87

88
    def self.scan_themes
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      theme_paths.inject([]) do |themes, path|
        dirs = Dir.glob(File.join(path, '*')).select do |f|
          # A theme should at least override application.css
          File.directory?(f) && File.exist?("#{f}/stylesheets/application.css")
        end
        themes += dirs.collect { |dir| Theme.new(dir) }
      end.sort
    end

    def self.theme_paths
      paths = Redmine::Configuration['themes_storage_path']
      paths = [paths] unless paths.is_a?(Array)
      paths.flatten!; paths.compact!

      paths = ["#{Rails.public_path}/themes"] if paths.empty?
      paths.collect { |p| File.expand_path(p, Rails.root) }
105 106 107 108 109
    end
  end
end

module ApplicationHelper
110 111 112 113 114 115
  def current_theme
    unless instance_variable_defined?(:@current_theme)
      @current_theme = Redmine::Themes.theme(Setting.ui_theme)
    end
    @current_theme
  end
116

117
  def stylesheet_path(source)
118 119 120 121 122
    if current_theme && current_theme.stylesheets.include?(source)
      super current_theme.stylesheet_path(source)
    else
      super
    end
123
  end
124

125 126 127
  def path_to_stylesheet(source)
    stylesheet_path source
  end
128

129 130 131 132 133 134
  # Returns the header tags for the current theme
  def heads_for_theme
    if current_theme && current_theme.javascripts.include?('theme')
      javascript_include_tag current_theme.javascript_path('theme')
    end
  end
135
end