annotate lib/redmine/.svn/text-base/themes.rb.svn-base @ 1174:928c0d0f624e bug_365

Close obsolete branch bug_365
author Chris Cannam
date Fri, 03 Feb 2012 14:03:51 +0000
parents af80e5618e9b
children
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 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@0 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@0 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@0 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@0 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@0 30
Chris@0 31 # Return theme for given id, or nil if it's not found
Chris@117 32 def self.theme(id, options={})
Chris@117 33 return nil if id.blank?
Chris@117 34
Chris@117 35 found = themes.find {|t| t.id == id}
Chris@117 36 if found.nil? && options[:rescan] != false
Chris@117 37 rescan
Chris@117 38 found = theme(id, :rescan => false)
Chris@117 39 end
Chris@117 40 found
Chris@0 41 end
Chris@0 42
Chris@0 43 # Class used to represent a theme
Chris@0 44 class Theme
Chris@117 45 attr_reader :path, :name, :dir
Chris@0 46
Chris@0 47 def initialize(path)
Chris@117 48 @path = path
Chris@0 49 @dir = File.basename(path)
Chris@0 50 @name = @dir.humanize
Chris@117 51 @stylesheets = nil
Chris@117 52 @javascripts = nil
Chris@0 53 end
Chris@0 54
Chris@0 55 # Directory name used as the theme id
Chris@0 56 def id; dir end
Chris@117 57
Chris@117 58 def ==(theme)
Chris@117 59 theme.is_a?(Theme) && theme.dir == dir
Chris@117 60 end
Chris@117 61
Chris@0 62 def <=>(theme)
Chris@0 63 name <=> theme.name
Chris@0 64 end
Chris@117 65
Chris@117 66 def stylesheets
Chris@117 67 @stylesheets ||= assets("stylesheets", "css")
Chris@117 68 end
Chris@117 69
Chris@117 70 def javascripts
Chris@117 71 @javascripts ||= assets("javascripts", "js")
Chris@117 72 end
Chris@117 73
Chris@117 74 def stylesheet_path(source)
Chris@117 75 "/themes/#{dir}/stylesheets/#{source}"
Chris@117 76 end
Chris@117 77
Chris@117 78 def javascript_path(source)
Chris@117 79 "/themes/#{dir}/javascripts/#{source}"
Chris@117 80 end
Chris@117 81
Chris@117 82 private
Chris@117 83
Chris@117 84 def assets(dir, ext)
Chris@117 85 Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
Chris@117 86 end
Chris@0 87 end
Chris@0 88
Chris@0 89 private
Chris@117 90
Chris@0 91 def self.scan_themes
Chris@0 92 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
Chris@0 93 # A theme should at least override application.css
Chris@0 94 File.directory?(f) && File.exist?("#{f}/stylesheets/application.css")
Chris@0 95 end
Chris@0 96 dirs.collect {|dir| Theme.new(dir)}.sort
Chris@0 97 end
Chris@0 98 end
Chris@0 99 end
Chris@0 100
Chris@0 101 module ApplicationHelper
Chris@117 102 def current_theme
Chris@117 103 unless instance_variable_defined?(:@current_theme)
Chris@117 104 @current_theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@117 105 end
Chris@117 106 @current_theme
Chris@117 107 end
Chris@117 108
Chris@0 109 def stylesheet_path(source)
Chris@117 110 if current_theme && current_theme.stylesheets.include?(source)
Chris@117 111 super current_theme.stylesheet_path(source)
Chris@117 112 else
Chris@117 113 super
Chris@117 114 end
Chris@0 115 end
Chris@0 116
Chris@0 117 def path_to_stylesheet(source)
Chris@0 118 stylesheet_path source
Chris@0 119 end
Chris@117 120
Chris@117 121 # Returns the header tags for the current theme
Chris@117 122 def heads_for_theme
Chris@117 123 if current_theme && current_theme.javascripts.include?('theme')
Chris@117 124 javascript_include_tag current_theme.javascript_path('theme')
Chris@117 125 end
Chris@117 126 end
Chris@0 127 end