##// END OF EJS Templates
Support for Javascript in Themes (#2803)....
Jean-Philippe Lang -
r4444:523febf9c1b3
parent child
Show More
@@ -0,0 +1,60
1 # Redmine - project management software
2 # Copyright (C) 2006-2010 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 require File.expand_path('../../../../test_helper', __FILE__)
19
20 class ThemesTest < ActionController::IntegrationTest
21 fixtures :all
22
23 def setup
24 @theme = Redmine::Themes.themes.last
25 Setting.ui_theme = @theme.id
26 end
27
28 def teardown
29 Setting.ui_theme = ''
30 end
31
32 def test_application_css
33 get '/'
34
35 assert_response :success
36 assert_tag :tag => 'link',
37 :attributes => {:href => %r{^/themes/#{@theme.dir}/stylesheets/application.css}}
38 end
39
40 def test_without_theme_js
41 get '/'
42
43 assert_response :success
44 assert_no_tag :tag => 'script',
45 :attributes => {:src => %r{^/themes/#{@theme.dir}/javascripts/theme.js}}
46 end
47
48 def test_with_theme_js
49 # Simulates a theme.js
50 @theme.javascripts << 'theme'
51 get '/'
52
53 assert_response :success
54 assert_tag :tag => 'script',
55 :attributes => {:src => %r{^/themes/#{@theme.dir}/javascripts/theme.js}}
56
57 ensure
58 @theme.javascripts.delete 'theme'
59 end
60 end
@@ -9,6 +9,7
9 <%= stylesheet_link_tag 'application', :media => 'all' %>
9 <%= stylesheet_link_tag 'application', :media => 'all' %>
10 <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %>
10 <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %>
11 <%= javascript_include_tag :defaults %>
11 <%= javascript_include_tag :defaults %>
12 <%= heads_for_theme %>
12 <%= heads_for_wiki_formatter %>
13 <%= heads_for_wiki_formatter %>
13 <!--[if IE]>
14 <!--[if IE]>
14 <style type="text/css">
15 <style type="text/css">
@@ -40,12 +40,14 module Redmine
40
40
41 # Class used to represent a theme
41 # Class used to represent a theme
42 class Theme
42 class Theme
43 attr_reader :name, :dir, :stylesheets
43 attr_reader :path, :name, :dir
44
44
45 def initialize(path)
45 def initialize(path)
46 @path = path
46 @dir = File.basename(path)
47 @dir = File.basename(path)
47 @name = @dir.humanize
48 @name = @dir.humanize
48 @stylesheets = Dir.glob("#{path}/stylesheets/*.css").collect {|f| File.basename(f).gsub(/\.css$/, '')}
49 @stylesheets = nil
50 @javascripts = nil
49 end
51 end
50
52
51 # Directory name used as the theme id
53 # Directory name used as the theme id
@@ -58,10 +60,32 module Redmine
58 def <=>(theme)
60 def <=>(theme)
59 name <=> theme.name
61 name <=> theme.name
60 end
62 end
63
64 def stylesheets
65 @stylesheets ||= assets("stylesheets", "css")
66 end
67
68 def javascripts
69 @javascripts ||= assets("javascripts", "js")
70 end
71
72 def stylesheet_path(source)
73 "/themes/#{dir}/stylesheets/#{source}"
74 end
75
76 def javascript_path(source)
77 "/themes/#{dir}/javascripts/#{source}"
78 end
79
80 private
81
82 def assets(dir, ext)
83 Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')}
84 end
61 end
85 end
62
86
63 private
87 private
64
88
65 def self.scan_themes
89 def self.scan_themes
66 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
90 dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f|
67 # A theme should at least override application.css
91 # A theme should at least override application.css
@@ -73,13 +97,29 module Redmine
73 end
97 end
74
98
75 module ApplicationHelper
99 module ApplicationHelper
100 def current_theme
101 unless instance_variable_defined?(:@current_theme)
102 @current_theme = Redmine::Themes.theme(Setting.ui_theme)
103 end
104 @current_theme
105 end
106
76 def stylesheet_path(source)
107 def stylesheet_path(source)
77 @current_theme ||= Redmine::Themes.theme(Setting.ui_theme)
108 if current_theme && current_theme.stylesheets.include?(source)
78 super((@current_theme && @current_theme.stylesheets.include?(source)) ?
109 super current_theme.stylesheet_path(source)
79 "/themes/#{@current_theme.dir}/stylesheets/#{source}" : source)
110 else
111 super
112 end
80 end
113 end
81
114
82 def path_to_stylesheet(source)
115 def path_to_stylesheet(source)
83 stylesheet_path source
116 stylesheet_path source
84 end
117 end
118
119 # Returns the header tags for the current theme
120 def heads_for_theme
121 if current_theme && current_theme.javascripts.include?('theme')
122 javascript_include_tag current_theme.javascript_path('theme')
123 end
124 end
85 end
125 end
General Comments 0
You need to be logged in to leave comments. Login now