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