comparison lib/redmine/.svn/text-base/themes.rb.svn-base @ 117:af80e5618e9b redmine-1.1

* Update to Redmine 1.1-stable branch (Redmine SVN rev 4707)
author Chris Cannam
date Thu, 13 Jan 2011 12:53:21 +0000
parents 513646585e45
children
comparison
equal deleted inserted replaced
39:150ceac17a8d 117:af80e5618e9b
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) 32 def self.theme(id, options={})
33 themes.find {|t| t.id == id} 33 return nil if id.blank?
34
35 found = themes.find {|t| t.id == id}
36 if found.nil? && options[:rescan] != false
37 rescan
38 found = theme(id, :rescan => false)
39 end
40 found
34 end 41 end
35 42
36 # Class used to represent a theme 43 # Class used to represent a theme
37 class Theme 44 class Theme
38 attr_reader :name, :dir, :stylesheets 45 attr_reader :path, :name, :dir
39 46
40 def initialize(path) 47 def initialize(path)
48 @path = path
41 @dir = File.basename(path) 49 @dir = File.basename(path)
42 @name = @dir.humanize 50 @name = @dir.humanize
43 @stylesheets = Dir.glob("#{path}/stylesheets/*.css").collect {|f| File.basename(f).gsub(/\.css$/, '')} 51 @stylesheets = nil
52 @javascripts = nil
44 end 53 end
45 54
46 # Directory name used as the theme id 55 # Directory name used as the theme id
47 def id; dir end 56 def id; dir end
48 57
58 def ==(theme)
59 theme.is_a?(Theme) && theme.dir == dir
60 end
61
49 def <=>(theme) 62 def <=>(theme)
50 name <=> theme.name 63 name <=> theme.name
64 end
65
66 def stylesheets
67 @stylesheets ||= assets("stylesheets", "css")
68 end
69
70 def javascripts
71 @javascripts ||= assets("javascripts", "js")
72 end
73
74 def stylesheet_path(source)
75 "/themes/#{dir}/stylesheets/#{source}"
76 end
77
78 def javascript_path(source)
79 "/themes/#{dir}/javascripts/#{source}"
80 end
81
82 private
83
84 def assets(dir, ext)
85 Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
51 end 86 end
52 end 87 end
53 88
54 private 89 private
55 90
56 def self.scan_themes 91 def self.scan_themes
57 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f| 92 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
58 # A theme should at least override application.css 93 # A theme should at least override application.css
59 File.directory?(f) && File.exist?("#{f}/stylesheets/application.css") 94 File.directory?(f) && File.exist?("#{f}/stylesheets/application.css")
60 end 95 end
62 end 97 end
63 end 98 end
64 end 99 end
65 100
66 module ApplicationHelper 101 module ApplicationHelper
102 def current_theme
103 unless instance_variable_defined?(:@current_theme)
104 @current_theme = Redmine::Themes.theme(Setting.ui_theme)
105 end
106 @current_theme
107 end
108
67 def stylesheet_path(source) 109 def stylesheet_path(source)
68 @current_theme ||= Redmine::Themes.theme(Setting.ui_theme) 110 if current_theme && current_theme.stylesheets.include?(source)
69 super((@current_theme && @current_theme.stylesheets.include?(source)) ? 111 super current_theme.stylesheet_path(source)
70 "/themes/#{@current_theme.dir}/stylesheets/#{source}" : source) 112 else
113 super
114 end
71 end 115 end
72 116
73 def path_to_stylesheet(source) 117 def path_to_stylesheet(source)
74 stylesheet_path source 118 stylesheet_path source
75 end 119 end
120
121 # Returns the header tags for the current theme
122 def heads_for_theme
123 if current_theme && current_theme.javascripts.include?('theme')
124 javascript_include_tag current_theme.javascript_path('theme')
125 end
126 end
76 end 127 end