annotate lib/redmine/themes.rb @ 1298:4f746d8966dd redmine_2.3_integration

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