annotate .svn/pristine/59/59827011d12236cea3e69be00d123a9b5835aeb5.svn-base @ 1480:75fd8eace091 issue_556

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