2013-02-05 14:16:51 -05:00
|
|
|
#
|
|
|
|
# A class that handles interaction between a plugin and the Discourse App.
|
|
|
|
#
|
|
|
|
class DiscoursePluginRegistry
|
|
|
|
|
|
|
|
class << self
|
2014-12-09 15:23:14 -05:00
|
|
|
attr_writer :javascripts
|
|
|
|
attr_writer :admin_javascripts
|
|
|
|
attr_writer :stylesheets
|
|
|
|
attr_writer :mobile_stylesheets
|
|
|
|
attr_writer :desktop_stylesheets
|
|
|
|
attr_writer :sass_variables
|
|
|
|
attr_writer :handlebars
|
|
|
|
attr_writer :serialized_current_user_fields
|
2015-06-04 15:56:17 -04:00
|
|
|
attr_writer :seed_data
|
2014-12-09 15:23:14 -05:00
|
|
|
attr_accessor :custom_html
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-11-14 19:42:55 -05:00
|
|
|
def plugins
|
|
|
|
@plugins ||= []
|
|
|
|
end
|
|
|
|
|
2013-08-01 01:59:57 -04:00
|
|
|
# Default accessor values
|
|
|
|
def javascripts
|
|
|
|
@javascripts ||= Set.new
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2014-12-09 15:23:14 -05:00
|
|
|
def asset_globs
|
|
|
|
@asset_globs ||= Set.new
|
|
|
|
end
|
|
|
|
|
2014-03-17 08:19:08 -04:00
|
|
|
def admin_javascripts
|
|
|
|
@admin_javascripts ||= Set.new
|
|
|
|
end
|
|
|
|
|
2013-11-19 10:42:28 -05:00
|
|
|
def stylesheets
|
|
|
|
@stylesheets ||= Set.new
|
|
|
|
end
|
2013-12-31 10:40:45 -05:00
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
def mobile_stylesheets
|
|
|
|
@mobile_stylesheets ||= Set.new
|
|
|
|
end
|
2014-04-25 04:26:37 -04:00
|
|
|
|
|
|
|
def desktop_stylesheets
|
|
|
|
@desktop_stylesheets ||= Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def sass_variables
|
|
|
|
@sass_variables ||= Set.new
|
|
|
|
end
|
|
|
|
|
2013-12-31 10:40:45 -05:00
|
|
|
def handlebars
|
|
|
|
@handlebars ||= Set.new
|
|
|
|
end
|
2014-06-10 21:57:22 -04:00
|
|
|
|
|
|
|
def serialized_current_user_fields
|
|
|
|
@serialized_current_user_fields ||= Set.new
|
|
|
|
end
|
2015-06-04 15:56:17 -04:00
|
|
|
|
|
|
|
def seed_data
|
|
|
|
@seed_data ||= HashWithIndifferentAccess.new({})
|
|
|
|
end
|
2017-04-17 15:47:21 -04:00
|
|
|
|
|
|
|
def html_builders
|
|
|
|
@html_builders ||= {}
|
|
|
|
end
|
2017-04-18 17:49:56 -04:00
|
|
|
|
|
|
|
def vendored_pretty_text
|
|
|
|
@vendored_pretty_text ||= Set.new
|
|
|
|
end
|
|
|
|
|
2013-11-19 10:42:28 -05:00
|
|
|
end
|
2013-08-01 01:59:57 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def register_js(filename, options = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
# If we have a server side option, add that too.
|
|
|
|
self.class.javascripts << filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_css(filename)
|
|
|
|
self.class.stylesheets << filename
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def register_archetype(name, options = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
Archetype.register(name, options)
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.register_glob(root, extension, options = nil)
|
2015-08-17 15:03:55 -04:00
|
|
|
self.asset_globs << [root, extension, options || {}]
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.each_globbed_asset(each_options = nil)
|
2015-08-17 15:03:55 -04:00
|
|
|
each_options ||= {}
|
|
|
|
|
|
|
|
self.asset_globs.each do |g|
|
|
|
|
root, ext, options = *g
|
|
|
|
|
2015-08-19 15:11:57 -04:00
|
|
|
if options[:admin]
|
|
|
|
next unless each_options[:admin]
|
|
|
|
else
|
|
|
|
next if each_options[:admin]
|
2015-08-18 21:24:09 -04:00
|
|
|
end
|
2015-08-17 15:03:55 -04:00
|
|
|
|
|
|
|
Dir.glob("#{root}/**/*") do |f|
|
|
|
|
yield f, ext
|
|
|
|
end
|
|
|
|
end
|
2014-12-09 15:23:14 -05:00
|
|
|
end
|
|
|
|
|
2016-11-14 19:42:55 -05:00
|
|
|
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6$/
|
|
|
|
HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.register_asset(asset, opts = nil)
|
2016-11-14 19:42:55 -05:00
|
|
|
if asset =~ JS_REGEX
|
2014-12-09 14:20:53 -05:00
|
|
|
if opts == :admin
|
|
|
|
self.admin_javascripts << asset
|
2017-04-18 17:49:56 -04:00
|
|
|
elsif opts == :vendored_pretty_text
|
|
|
|
self.vendored_pretty_text << asset
|
2014-12-09 14:20:53 -05:00
|
|
|
else
|
|
|
|
self.javascripts << asset
|
|
|
|
end
|
|
|
|
elsif asset =~ /\.css$|\.scss$/
|
|
|
|
if opts == :mobile
|
|
|
|
self.mobile_stylesheets << asset
|
|
|
|
elsif opts == :desktop
|
|
|
|
self.desktop_stylesheets << asset
|
|
|
|
elsif opts == :variables
|
|
|
|
self.sass_variables << asset
|
|
|
|
else
|
|
|
|
self.stylesheets << asset
|
|
|
|
end
|
2016-11-14 19:42:55 -05:00
|
|
|
elsif asset =~ HANDLEBARS_REGEX
|
2014-12-09 14:20:53 -05:00
|
|
|
self.handlebars << asset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-04 15:56:17 -04:00
|
|
|
def self.register_seed_data(key, value)
|
|
|
|
self.seed_data[key] = value
|
|
|
|
end
|
|
|
|
|
2017-04-17 15:47:21 -04:00
|
|
|
def self.register_html_builder(name, &block)
|
|
|
|
html_builders[name] = block
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.build_html(name, ctx = nil)
|
2017-04-18 12:35:19 -04:00
|
|
|
html_builders[name]&.call(ctx)
|
2017-04-17 15:47:21 -04:00
|
|
|
end
|
|
|
|
|
2013-11-19 10:42:28 -05:00
|
|
|
def javascripts
|
2013-02-08 02:56:12 -05:00
|
|
|
self.class.javascripts
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-19 10:42:28 -05:00
|
|
|
def stylesheets
|
|
|
|
self.class.stylesheets
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-04-07 10:33:35 -04:00
|
|
|
def mobile_stylesheets
|
|
|
|
self.class.mobile_stylesheets
|
|
|
|
end
|
|
|
|
|
2014-04-25 04:26:37 -04:00
|
|
|
def desktop_stylesheets
|
|
|
|
self.class.desktop_stylesheets
|
|
|
|
end
|
|
|
|
|
|
|
|
def sass_variables
|
|
|
|
self.class.sass_variables
|
|
|
|
end
|
|
|
|
|
2013-12-31 10:40:45 -05:00
|
|
|
def handlebars
|
|
|
|
self.class.handlebars
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.clear
|
2013-02-08 02:56:12 -05:00
|
|
|
self.javascripts = nil
|
2013-11-19 10:42:28 -05:00
|
|
|
self.stylesheets = nil
|
2014-04-07 10:33:35 -04:00
|
|
|
self.mobile_stylesheets = nil
|
2014-04-25 04:26:37 -04:00
|
|
|
self.desktop_stylesheets = nil
|
|
|
|
self.sass_variables = nil
|
2013-12-31 10:40:45 -05:00
|
|
|
self.handlebars = nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-12-09 14:20:53 -05:00
|
|
|
def self.reset!
|
|
|
|
javascripts.clear
|
|
|
|
admin_javascripts.clear
|
|
|
|
stylesheets.clear
|
|
|
|
mobile_stylesheets.clear
|
|
|
|
desktop_stylesheets.clear
|
|
|
|
sass_variables.clear
|
|
|
|
serialized_current_user_fields
|
2014-12-09 15:23:14 -05:00
|
|
|
asset_globs.clear
|
2017-04-17 15:47:21 -04:00
|
|
|
html_builders.clear
|
2017-04-18 17:49:56 -04:00
|
|
|
vendored_pretty_text.clear
|
2014-12-09 14:20:53 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
def self.setup(plugin_class)
|
2013-02-05 14:16:51 -05:00
|
|
|
registry = DiscoursePluginRegistry.new
|
|
|
|
plugin = plugin_class.new(registry)
|
|
|
|
plugin.setup
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|