Allow plugins to ship custom styles only for mobile

- adds another :mobile-flag to register_assets
- adds test for plugin registering of assets
- load plugins when on desktop and plugins_mobile when on mobile
This commit is contained in:
Benjamin Kampmann 2014-04-07 16:33:35 +02:00
parent 4a2438edc3
commit d22df7731d
7 changed files with 97 additions and 5 deletions

View File

@ -0,0 +1,7 @@
<%
# TODO this is very tricky, we want to add a dependency here on files that may not yet exist
# otherwise in dev we are often stuck nuking the tmp/cache directory
DiscoursePluginRegistry.mobile_stylesheets.each do |css|
require_asset(css)
end
%>

View File

@ -1,13 +1,13 @@
<%- unless SiteCustomization.override_default_style(session[:preview_style]) %>
<% if mobile_view? %>
<%= stylesheet_link_tag "mobile" %>
<%= stylesheet_link_tag "plugins_mobile" %>
<% else %>
<%= stylesheet_link_tag "desktop" %>
<%= stylesheet_link_tag "plugins" %>
<% end %>
<%- end %>
<%= stylesheet_link_tag "plugins" %>
<%- if staff? %>
<%= stylesheet_link_tag "admin"%>
<%- end %>

View File

@ -68,7 +68,7 @@ module Discourse
path =~ /assets\/images/ && !%w(.js .css).include?(File.extname(filename))
end]
config.assets.precompile += ['vendor.js', 'common.css', 'desktop.css', 'mobile.css', 'admin.js', 'admin.css', 'shiny/shiny.css', 'preload_store.js', 'browser-update.js', 'embed.css', 'break_string.js', 'plugins.css']
config.assets.precompile += ['vendor.js', 'common.css', 'desktop.css', 'mobile.css', 'admin.js', 'admin.css', 'shiny/shiny.css', 'preload_store.js', 'browser-update.js', 'embed.css', 'break_string.js', 'plugins.css', 'plugins_mobile.css']
# Precompile all defer
Dir.glob("#{config.root}/app/assets/javascripts/defer/*.js").each do |file|

View File

@ -8,6 +8,7 @@ class DiscoursePluginRegistry
attr_accessor :server_side_javascripts
attr_accessor :admin_javascripts
attr_accessor :stylesheets
attr_accessor :mobile_stylesheets
attr_accessor :handlebars
# Default accessor values
@ -27,6 +28,9 @@ class DiscoursePluginRegistry
@stylesheets ||= Set.new
end
def mobile_stylesheets
@mobile_stylesheets ||= Set.new
end
def handlebars
@handlebars ||= Set.new
end
@ -58,6 +62,10 @@ class DiscoursePluginRegistry
self.class.stylesheets
end
def mobile_stylesheets
self.class.mobile_stylesheets
end
def handlebars
self.class.handlebars
end
@ -66,6 +74,7 @@ class DiscoursePluginRegistry
self.javascripts = nil
self.server_side_javascripts = nil
self.stylesheets = nil
self.mobile_stylesheets = nil
self.handlebars = nil
end

View File

@ -5,7 +5,8 @@ require_dependency 'plugin/auth_provider'
class Plugin::Instance
attr_reader :auth_providers, :assets
attr_reader :auth_providers, :assets, :admin_javascripts,
:server_side_javascripts, :styles, :mobile_styles
attr_accessor :path, :metadata
def self.find_all(parent_path)
@ -93,11 +94,14 @@ class Plugin::Instance
@javascripts << js
end
def register_asset(file,opts=nil)
def register_asset(file, opts=nil)
full_path = File.dirname(path) << "/assets/" << file
if opts == :admin
@admin_javascripts ||= []
@admin_javascripts << full_path
elsif opts == :mobile
@mobile_styles ||= []
@mobile_styles << full_path
else
assets << full_path
end
@ -178,6 +182,12 @@ class Plugin::Instance
end
end
if @mobile_styles
@mobile_styles.each do |style|
DiscoursePluginRegistry.mobile_stylesheets << style
end
end
if @server_side_javascripts
@server_side_javascripts.each do |js|
DiscoursePluginRegistry.server_side_javascripts << js

View File

@ -15,6 +15,13 @@ describe DiscoursePluginRegistry do
end
end
context '#mobile_stylesheets' do
it 'defaults to an empty Set' do
registry.mobile_stylesheets = nil
registry.mobile_stylesheets.should == Set.new
end
end
context '#javascripts' do
it 'defaults to an empty Set' do
registry.javascripts = nil

View File

@ -5,7 +5,10 @@ describe Plugin::Instance do
after do
DiscoursePluginRegistry.javascripts.clear
DiscoursePluginRegistry.admin_javascripts.clear
DiscoursePluginRegistry.server_side_javascripts.clear
DiscoursePluginRegistry.stylesheets.clear
DiscoursePluginRegistry.mobile_stylesheets.clear
end
context "find_all" do
@ -24,6 +27,37 @@ describe Plugin::Instance do
end
end
context "register asset" do
it "does register css properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css")
plugin.assets.count.should == 1
plugin.register_asset("test2.css")
plugin.assets.count.should == 2
end
it "does register mobile css properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("test.css", :mobile)
plugin.assets.count.should == 0
plugin.mobile_styles.count.should == 1
end
it "does register admin javascript properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("my_admin.js", :admin)
plugin.assets.count.should == 0
plugin.admin_javascripts.count.should == 1
end
it "does register server side javascript properly" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb"
plugin.register_asset("my_admin.js", :server_side)
# server side is both in assets and in server_side
plugin.assets.count.should == 1
plugin.server_side_javascripts.count.should == 1
end
end
context "activate!" do
it "can activate plugins correctly" do
plugin = Plugin::Instance.new
@ -47,6 +81,31 @@ describe Plugin::Instance do
# ensure it cleans up all crap in autogenerated directory
File.exists?(junk_file).should be_false
end
it "finds all the custom assets" do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
# two styles
plugin.register_asset("test.css")
plugin.register_asset("test2.scss")
# one javascript
plugin.register_asset("code.js")
# one mobile
plugin.register_asset("test.css", :mobile)
# a server side
plugin.register_asset("server_side.js", :server_side)
# and two admin
plugin.register_asset("my_admin.js", :admin)
plugin.register_asset("my_admin2.js", :admin)
plugin.activate!
DiscoursePluginRegistry.javascripts.count.should == 3
DiscoursePluginRegistry.admin_javascripts.count.should == 2
DiscoursePluginRegistry.server_side_javascripts.count.should == 1
DiscoursePluginRegistry.stylesheets.count.should == 2
DiscoursePluginRegistry.mobile_stylesheets.count.should == 1
end
end
end