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