annotate lib/redmine/themes.rb @ 1628:9c5f8e24dadc live tip

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