2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-09-20 02:48:17 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- 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
|
|
|
|
|
# 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)
|
2023-06-29 14:42:54 +00:00
|
|
|
return nil unless theme.is_a?(Theme)
|
|
|
|
|
|
2007-10-10 17:17:37 +00:00
|
|
|
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
|
|
|
|
2014-01-11 11:22:46 +00:00
|
|
|
def favicons
|
|
|
|
|
@favicons ||= assets("favicon")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def favicon
|
|
|
|
|
favicons.first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def favicon?
|
|
|
|
|
favicon.present?
|
|
|
|
|
end
|
|
|
|
|
|
2010-12-22 21:37:07 +00:00
|
|
|
def stylesheet_path(source)
|
2024-01-25 05:38:33 +00:00
|
|
|
"#{asset_prefix}#{source}"
|
2010-12-22 21:37:07 +00:00
|
|
|
end
|
2011-09-20 02:48:17 +00:00
|
|
|
|
2012-04-28 09:47:09 +00:00
|
|
|
def image_path(source)
|
2024-01-25 05:38:33 +00:00
|
|
|
"#{asset_prefix}#{source}"
|
2012-04-28 09:47:09 +00:00
|
|
|
end
|
|
|
|
|
|
2010-12-22 21:37:07 +00:00
|
|
|
def javascript_path(source)
|
2024-01-25 05:38:33 +00:00
|
|
|
"#{asset_prefix}#{source}"
|
2010-12-22 21:37:07 +00:00
|
|
|
end
|
2014-01-11 11:22:46 +00:00
|
|
|
|
|
|
|
|
def favicon_path
|
2024-01-25 05:38:33 +00:00
|
|
|
"#{asset_prefix}#{favicon}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def asset_prefix
|
|
|
|
|
"themes/#{dir}/"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def asset_paths
|
|
|
|
|
base_dir = Pathname.new(path)
|
2024-07-11 08:48:35 +00:00
|
|
|
paths = base_dir.children.select do |child|
|
|
|
|
|
child.directory? &&
|
|
|
|
|
child.basename.to_s != 'src' &&
|
|
|
|
|
!child.basename.to_s.start_with?('.')
|
2024-01-27 01:41:44 +00:00
|
|
|
end
|
2024-01-25 05:38:33 +00:00
|
|
|
Redmine::AssetPath.new(base_dir, paths, asset_prefix)
|
2014-01-11 11:22:46 +00:00
|
|
|
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
|
2024-01-08 01:39:27 +00:00
|
|
|
Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f, ".#{ext}")}
|
2012-04-28 09:47:09 +00:00
|
|
|
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
|
|
|
|
2015-10-19 18:03:51 +00:00
|
|
|
module Helper
|
|
|
|
|
def current_theme
|
|
|
|
|
unless instance_variable_defined?(:@current_theme)
|
|
|
|
|
@current_theme = Redmine::Themes.theme(Setting.ui_theme)
|
|
|
|
|
end
|
|
|
|
|
@current_theme
|
|
|
|
|
end
|
2017-07-31 04:30:42 +00:00
|
|
|
|
2015-10-19 18:03:51 +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
|
|
|
|
|
end
|
|
|
|
|
|
2007-10-10 17:17:37 +00:00
|
|
|
def self.scan_themes
|
2024-02-13 23:11:28 +00:00
|
|
|
dirs = Dir.glob(["#{Rails.root}/app/assets/themes/*", "#{Rails.root}/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
|
2019-10-01 05:07:27 +00:00
|
|
|
private_class_method :scan_themes
|
2007-10-10 17:17:37 +00:00
|
|
|
end
|
|
|
|
|
end
|