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