To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / themes.rb @ 912:5e80956cc792
History | View | Annotate | Download (3.25 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
||
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 21 | 0:513646585e45 | Chris | # Return an array of installed themes
|
| 22 | def self.themes |
||
| 23 | @@installed_themes ||= scan_themes
|
||
| 24 | end
|
||
| 25 | 909:cbb26bc654de | Chris | |
| 26 | 0:513646585e45 | Chris | # Rescan themes directory
|
| 27 | def self.rescan |
||
| 28 | @@installed_themes = scan_themes
|
||
| 29 | end
|
||
| 30 | 909:cbb26bc654de | Chris | |
| 31 | 0:513646585e45 | Chris | # Return theme for given id, or nil if it's not found
|
| 32 | 119:8661b858af72 | Chris | def self.theme(id, options={}) |
| 33 | return nil if id.blank? |
||
| 34 | 909:cbb26bc654de | Chris | |
| 35 | 119:8661b858af72 | Chris | 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 | 0:513646585e45 | Chris | end
|
| 42 | 909:cbb26bc654de | Chris | |
| 43 | 0:513646585e45 | Chris | # Class used to represent a theme
|
| 44 | class Theme |
||
| 45 | 119:8661b858af72 | Chris | attr_reader :path, :name, :dir |
| 46 | 909:cbb26bc654de | Chris | |
| 47 | 0:513646585e45 | Chris | def initialize(path) |
| 48 | 119:8661b858af72 | Chris | @path = path
|
| 49 | 0:513646585e45 | Chris | @dir = File.basename(path) |
| 50 | @name = @dir.humanize |
||
| 51 | 119:8661b858af72 | Chris | @stylesheets = nil |
| 52 | @javascripts = nil |
||
| 53 | 0:513646585e45 | Chris | end
|
| 54 | 909:cbb26bc654de | Chris | |
| 55 | 0:513646585e45 | Chris | # Directory name used as the theme id
|
| 56 | def id; dir end |
||
| 57 | 909:cbb26bc654de | Chris | |
| 58 | 119:8661b858af72 | Chris | def ==(theme) |
| 59 | theme.is_a?(Theme) && theme.dir == dir
|
||
| 60 | end
|
||
| 61 | 909:cbb26bc654de | Chris | |
| 62 | 0:513646585e45 | Chris | def <=>(theme) |
| 63 | name <=> theme.name |
||
| 64 | end
|
||
| 65 | 909:cbb26bc654de | Chris | |
| 66 | 119:8661b858af72 | Chris | def stylesheets |
| 67 | @stylesheets ||= assets("stylesheets", "css") |
||
| 68 | end
|
||
| 69 | 909:cbb26bc654de | Chris | |
| 70 | 119:8661b858af72 | Chris | def javascripts |
| 71 | @javascripts ||= assets("javascripts", "js") |
||
| 72 | end
|
||
| 73 | 909:cbb26bc654de | Chris | |
| 74 | 119:8661b858af72 | Chris | def stylesheet_path(source) |
| 75 | "/themes/#{dir}/stylesheets/#{source}"
|
||
| 76 | end
|
||
| 77 | 909:cbb26bc654de | Chris | |
| 78 | 119:8661b858af72 | Chris | def javascript_path(source) |
| 79 | "/themes/#{dir}/javascripts/#{source}"
|
||
| 80 | end
|
||
| 81 | 909:cbb26bc654de | Chris | |
| 82 | 119:8661b858af72 | Chris | private |
| 83 | 909:cbb26bc654de | Chris | |
| 84 | 119:8661b858af72 | Chris | def assets(dir, ext) |
| 85 | Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')} |
||
| 86 | end
|
||
| 87 | 0:513646585e45 | Chris | end
|
| 88 | 909:cbb26bc654de | Chris | |
| 89 | 0:513646585e45 | Chris | private |
| 90 | 909:cbb26bc654de | Chris | |
| 91 | 0:513646585e45 | Chris | def self.scan_themes |
| 92 | dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f| |
||
| 93 | # A theme should at least override application.css
|
||
| 94 | File.directory?(f) && File.exist?("#{f}/stylesheets/application.css") |
||
| 95 | end
|
||
| 96 | dirs.collect {|dir| Theme.new(dir)}.sort
|
||
| 97 | end
|
||
| 98 | end
|
||
| 99 | end
|
||
| 100 | |||
| 101 | module ApplicationHelper |
||
| 102 | 119:8661b858af72 | Chris | def current_theme |
| 103 | unless instance_variable_defined?(:@current_theme) |
||
| 104 | @current_theme = Redmine::Themes.theme(Setting.ui_theme) |
||
| 105 | end
|
||
| 106 | @current_theme
|
||
| 107 | end
|
||
| 108 | 909:cbb26bc654de | Chris | |
| 109 | 0:513646585e45 | Chris | def stylesheet_path(source) |
| 110 | 119:8661b858af72 | Chris | if current_theme && current_theme.stylesheets.include?(source)
|
| 111 | super current_theme.stylesheet_path(source)
|
||
| 112 | else
|
||
| 113 | super
|
||
| 114 | end
|
||
| 115 | 0:513646585e45 | Chris | end
|
| 116 | 909:cbb26bc654de | Chris | |
| 117 | 0:513646585e45 | Chris | def path_to_stylesheet(source) |
| 118 | stylesheet_path source |
||
| 119 | end
|
||
| 120 | 909:cbb26bc654de | Chris | |
| 121 | 119:8661b858af72 | Chris | # 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 | 0:513646585e45 | Chris | end |