annotate .svn/pristine/8d/8d137ddbd4cf6f8f24fe80f23c9c81f3f5e0f6a8.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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