2011-09-20 06:48:17 +04:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
2007-10-10 21:17:37 +04: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 06:48:17 +04:00
|
|
|
#
|
2007-10-10 21:17:37 +04: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 06:48:17 +04:00
|
|
|
#
|
2007-10-10 21:17:37 +04: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 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
# Return an array of installed themes
|
|
|
|
def self.themes
|
|
|
|
@@installed_themes ||= scan_themes
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
# Rescan themes directory
|
|
|
|
def self.rescan
|
|
|
|
@@installed_themes = scan_themes
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
# Return theme for given id, or nil if it's not found
|
2010-11-27 17:06:11 +03:00
|
|
|
def self.theme(id, options={})
|
2010-12-23 00:41:08 +03:00
|
|
|
return nil if id.blank?
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-11-27 17:06:11 +03: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 21:17:37 +04:00
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
# Class used to represent a theme
|
|
|
|
class Theme
|
2010-12-23 00:37:07 +03:00
|
|
|
attr_reader :path, :name, :dir
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
def initialize(path)
|
2010-12-23 00:37:07 +03:00
|
|
|
@path = path
|
2007-10-10 21:17:37 +04:00
|
|
|
@dir = File.basename(path)
|
|
|
|
@name = @dir.humanize
|
2010-12-23 00:37:07 +03:00
|
|
|
@stylesheets = nil
|
|
|
|
@javascripts = nil
|
2007-10-10 21:17:37 +04:00
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
# Directory name used as the theme id
|
|
|
|
def id; dir end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-11-27 17:06:11 +03:00
|
|
|
def ==(theme)
|
|
|
|
theme.is_a?(Theme) && theme.dir == dir
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
def <=>(theme)
|
|
|
|
name <=> theme.name
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
def stylesheets
|
|
|
|
@stylesheets ||= assets("stylesheets", "css")
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
def javascripts
|
|
|
|
@javascripts ||= assets("javascripts", "js")
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
def stylesheet_path(source)
|
|
|
|
"/themes/#{dir}/stylesheets/#{source}"
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
def javascript_path(source)
|
|
|
|
"/themes/#{dir}/javascripts/#{source}"
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
private
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2010-12-23 00:37:07 +03:00
|
|
|
def assets(dir, ext)
|
|
|
|
Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
|
|
|
|
end
|
2007-10-10 21:17:37 +04:00
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
private
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
def self.scan_themes
|
2010-04-30 16:44:19 +04:00
|
|
|
dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
|
2007-10-10 21:17:37 +04: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-23 00:37:07 +03: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 06:48:17 +04:00
|
|
|
|
2007-10-10 21:17:37 +04:00
|
|
|
def stylesheet_path(source)
|
2010-12-23 00:37:07 +03:00
|
|
|
if current_theme && current_theme.stylesheets.include?(source)
|
|
|
|
super current_theme.stylesheet_path(source)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2007-10-10 21:17:37 +04:00
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2008-01-07 23:14:50 +03:00
|
|
|
def path_to_stylesheet(source)
|
|
|
|
stylesheet_path source
|
|
|
|
end
|
2011-09-20 06:48:17 +04:00
|
|
|
|
2012-04-25 21:17:49 +04:00
|
|
|
def stylesheet_link_tag(source, *args)
|
|
|
|
if current_theme && current_theme.stylesheets.include?(source)
|
|
|
|
super current_theme.stylesheet_path(source), *args
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-23 00:37:07 +03: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 21:17:37 +04:00
|
|
|
end
|