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