| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2012-05-05 12:56:53 +00:00
										 |  |  | # Copyright (C) 2006-2012  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Redmine | 
					
						
							|  |  |  |   module Themes | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     # Return an array of installed themes | 
					
						
							|  |  |  |     def self.themes | 
					
						
							|  |  |  |       @@installed_themes ||= scan_themes | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     # Rescan themes directory | 
					
						
							|  |  |  |     def self.rescan | 
					
						
							|  |  |  |       @@installed_themes = scan_themes | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     # Return theme for given id, or nil if it's not found | 
					
						
							| 
									
										
										
										
											2010-11-27 14:06:11 +00:00
										 |  |  |     def self.theme(id, options={}) | 
					
						
							| 
									
										
										
										
											2010-12-22 21:41:08 +00:00
										 |  |  |       return nil if id.blank? | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-27 14:06:11 +00:00
										 |  |  |       found = themes.find {|t| t.id == id} | 
					
						
							|  |  |  |       if found.nil? && options[:rescan] != false | 
					
						
							|  |  |  |         rescan | 
					
						
							|  |  |  |         found = theme(id, :rescan => false) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       found | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     # Class used to represent a theme | 
					
						
							|  |  |  |     class Theme | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       attr_reader :path, :name, :dir | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |       def initialize(path) | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |         @path = path | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |         @dir = File.basename(path) | 
					
						
							|  |  |  |         @name = @dir.humanize | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |         @stylesheets = nil | 
					
						
							|  |  |  |         @javascripts = nil | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |       # Directory name used as the theme id | 
					
						
							|  |  |  |       def id; dir end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-27 14:06:11 +00:00
										 |  |  |       def ==(theme) | 
					
						
							|  |  |  |         theme.is_a?(Theme) && theme.dir == dir | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |       def <=>(theme) | 
					
						
							|  |  |  |         name <=> theme.name | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       def stylesheets | 
					
						
							|  |  |  |         @stylesheets ||= assets("stylesheets", "css") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-28 09:47:09 +00:00
										 |  |  |       def images | 
					
						
							|  |  |  |         @images ||= assets("images") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       def javascripts | 
					
						
							|  |  |  |         @javascripts ||= assets("javascripts", "js") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       def stylesheet_path(source) | 
					
						
							|  |  |  |         "/themes/#{dir}/stylesheets/#{source}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-28 09:47:09 +00:00
										 |  |  |       def image_path(source) | 
					
						
							|  |  |  |         "/themes/#{dir}/images/#{source}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       def javascript_path(source) | 
					
						
							|  |  |  |         "/themes/#{dir}/javascripts/#{source}" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-28 09:47:09 +00:00
										 |  |  |       def assets(dir, ext=nil) | 
					
						
							|  |  |  |         if ext | 
					
						
							|  |  |  |           Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')} | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Dir.glob("#{path}/#{dir}/*").collect {|f| File.basename(f)} | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     private | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |     def self.scan_themes | 
					
						
							| 
									
										
										
										
											2010-04-30 12:44:19 +00:00
										 |  |  |       dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f| | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  |         # A theme should at least override application.css | 
					
						
							|  |  |  |         File.directory?(f) && File.exist?("#{f}/stylesheets/application.css") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       dirs.collect {|dir| Theme.new(dir)}.sort | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module ApplicationHelper | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |   def current_theme | 
					
						
							|  |  |  |     unless instance_variable_defined?(:@current_theme) | 
					
						
							|  |  |  |       @current_theme = Redmine::Themes.theme(Setting.ui_theme) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     @current_theme | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-09-20 02:48:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-22 21:37:07 +00:00
										 |  |  |   # 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 | 
					
						
							| 
									
										
										
										
											2007-10-10 17:17:37 +00:00
										 |  |  | end |