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