Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 module Redmine
|
Chris@0
|
19 module Themes
|
Chris@909
|
20
|
Chris@0
|
21 # Return an array of installed themes
|
Chris@0
|
22 def self.themes
|
Chris@0
|
23 @@installed_themes ||= scan_themes
|
Chris@0
|
24 end
|
Chris@909
|
25
|
Chris@0
|
26 # Rescan themes directory
|
Chris@0
|
27 def self.rescan
|
Chris@0
|
28 @@installed_themes = scan_themes
|
Chris@0
|
29 end
|
Chris@909
|
30
|
Chris@0
|
31 # Return theme for given id, or nil if it's not found
|
Chris@119
|
32 def self.theme(id, options={})
|
Chris@119
|
33 return nil if id.blank?
|
Chris@909
|
34
|
Chris@119
|
35 found = themes.find {|t| t.id == id}
|
Chris@119
|
36 if found.nil? && options[:rescan] != false
|
Chris@119
|
37 rescan
|
Chris@119
|
38 found = theme(id, :rescan => false)
|
Chris@119
|
39 end
|
Chris@119
|
40 found
|
Chris@0
|
41 end
|
Chris@909
|
42
|
Chris@0
|
43 # Class used to represent a theme
|
Chris@0
|
44 class Theme
|
Chris@119
|
45 attr_reader :path, :name, :dir
|
Chris@909
|
46
|
Chris@0
|
47 def initialize(path)
|
Chris@119
|
48 @path = path
|
Chris@0
|
49 @dir = File.basename(path)
|
Chris@0
|
50 @name = @dir.humanize
|
Chris@119
|
51 @stylesheets = nil
|
Chris@119
|
52 @javascripts = nil
|
Chris@0
|
53 end
|
Chris@909
|
54
|
Chris@0
|
55 # Directory name used as the theme id
|
Chris@0
|
56 def id; dir end
|
Chris@909
|
57
|
Chris@119
|
58 def ==(theme)
|
Chris@119
|
59 theme.is_a?(Theme) && theme.dir == dir
|
Chris@119
|
60 end
|
Chris@909
|
61
|
Chris@0
|
62 def <=>(theme)
|
Chris@0
|
63 name <=> theme.name
|
Chris@0
|
64 end
|
Chris@909
|
65
|
Chris@119
|
66 def stylesheets
|
Chris@119
|
67 @stylesheets ||= assets("stylesheets", "css")
|
Chris@119
|
68 end
|
Chris@909
|
69
|
Chris@119
|
70 def javascripts
|
Chris@119
|
71 @javascripts ||= assets("javascripts", "js")
|
Chris@119
|
72 end
|
Chris@909
|
73
|
Chris@119
|
74 def stylesheet_path(source)
|
Chris@119
|
75 "/themes/#{dir}/stylesheets/#{source}"
|
Chris@119
|
76 end
|
Chris@909
|
77
|
Chris@119
|
78 def javascript_path(source)
|
Chris@119
|
79 "/themes/#{dir}/javascripts/#{source}"
|
Chris@119
|
80 end
|
Chris@909
|
81
|
Chris@119
|
82 private
|
Chris@909
|
83
|
Chris@119
|
84 def assets(dir, ext)
|
Chris@119
|
85 Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
|
Chris@119
|
86 end
|
Chris@0
|
87 end
|
Chris@909
|
88
|
Chris@0
|
89 private
|
Chris@909
|
90
|
Chris@0
|
91 def self.scan_themes
|
Chris@0
|
92 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
|
Chris@0
|
93 # A theme should at least override application.css
|
Chris@0
|
94 File.directory?(f) && File.exist?("#{f}/stylesheets/application.css")
|
Chris@0
|
95 end
|
Chris@0
|
96 dirs.collect {|dir| Theme.new(dir)}.sort
|
Chris@0
|
97 end
|
Chris@0
|
98 end
|
Chris@0
|
99 end
|
Chris@0
|
100
|
Chris@0
|
101 module ApplicationHelper
|
Chris@119
|
102 def current_theme
|
Chris@119
|
103 unless instance_variable_defined?(:@current_theme)
|
Chris@119
|
104 @current_theme = Redmine::Themes.theme(Setting.ui_theme)
|
Chris@119
|
105 end
|
Chris@119
|
106 @current_theme
|
Chris@119
|
107 end
|
Chris@909
|
108
|
Chris@0
|
109 def stylesheet_path(source)
|
Chris@119
|
110 if current_theme && current_theme.stylesheets.include?(source)
|
Chris@119
|
111 super current_theme.stylesheet_path(source)
|
Chris@119
|
112 else
|
Chris@119
|
113 super
|
Chris@119
|
114 end
|
Chris@0
|
115 end
|
Chris@909
|
116
|
Chris@0
|
117 def path_to_stylesheet(source)
|
Chris@0
|
118 stylesheet_path source
|
Chris@0
|
119 end
|
Chris@909
|
120
|
Chris@119
|
121 # Returns the header tags for the current theme
|
Chris@119
|
122 def heads_for_theme
|
Chris@119
|
123 if current_theme && current_theme.javascripts.include?('theme')
|
Chris@119
|
124 javascript_include_tag current_theme.javascript_path('theme')
|
Chris@119
|
125 end
|
Chris@119
|
126 end
|
Chris@0
|
127 end
|