2013-02-06 03:16:51 +08:00
|
|
|
#
|
|
|
|
# A class that handles interaction between a plugin and the Discourse App.
|
|
|
|
#
|
|
|
|
class DiscoursePluginRegistry
|
|
|
|
|
|
|
|
class << self
|
2014-12-10 04:23:14 +08:00
|
|
|
attr_writer :javascripts
|
2018-07-23 23:51:57 +08:00
|
|
|
attr_writer :auth_providers
|
2017-11-23 09:02:01 +08:00
|
|
|
attr_writer :service_workers
|
2014-12-10 04:23:14 +08:00
|
|
|
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-05 03:56:17 +08:00
|
|
|
attr_writer :seed_data
|
2018-11-27 05:49:57 +08:00
|
|
|
attr_writer :svg_icons
|
2018-01-25 19:09:18 +08:00
|
|
|
attr_writer :locales
|
2014-12-10 04:23:14 +08:00
|
|
|
attr_accessor :custom_html
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-11-15 08:42:55 +08:00
|
|
|
def plugins
|
|
|
|
@plugins ||= []
|
|
|
|
end
|
|
|
|
|
2013-08-01 13:59:57 +08:00
|
|
|
# Default accessor values
|
|
|
|
def javascripts
|
|
|
|
@javascripts ||= Set.new
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def auth_providers
|
|
|
|
@auth_providers ||= Set.new
|
|
|
|
end
|
|
|
|
|
2017-11-23 09:02:01 +08:00
|
|
|
def service_workers
|
|
|
|
@service_workers ||= Set.new
|
|
|
|
end
|
|
|
|
|
2014-12-10 04:23:14 +08:00
|
|
|
def asset_globs
|
|
|
|
@asset_globs ||= Set.new
|
|
|
|
end
|
|
|
|
|
2014-03-17 20:19:08 +08:00
|
|
|
def admin_javascripts
|
|
|
|
@admin_javascripts ||= Set.new
|
|
|
|
end
|
|
|
|
|
2013-11-19 23:42:28 +08:00
|
|
|
def stylesheets
|
|
|
|
@stylesheets ||= Set.new
|
|
|
|
end
|
2013-12-31 23:40:45 +08:00
|
|
|
|
2014-04-07 22:33:35 +08:00
|
|
|
def mobile_stylesheets
|
|
|
|
@mobile_stylesheets ||= Set.new
|
|
|
|
end
|
2014-04-25 16:26:37 +08:00
|
|
|
|
|
|
|
def desktop_stylesheets
|
|
|
|
@desktop_stylesheets ||= Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def sass_variables
|
|
|
|
@sass_variables ||= Set.new
|
|
|
|
end
|
|
|
|
|
2018-11-27 05:49:57 +08:00
|
|
|
def svg_icons
|
|
|
|
@svg_icons ||= []
|
|
|
|
end
|
|
|
|
|
2013-12-31 23:40:45 +08:00
|
|
|
def handlebars
|
|
|
|
@handlebars ||= Set.new
|
|
|
|
end
|
2014-06-11 09:57:22 +08:00
|
|
|
|
|
|
|
def serialized_current_user_fields
|
|
|
|
@serialized_current_user_fields ||= Set.new
|
|
|
|
end
|
2015-06-05 03:56:17 +08:00
|
|
|
|
|
|
|
def seed_data
|
|
|
|
@seed_data ||= HashWithIndifferentAccess.new({})
|
|
|
|
end
|
2017-04-18 03:47:21 +08:00
|
|
|
|
2018-01-25 19:09:18 +08:00
|
|
|
def locales
|
|
|
|
@locales ||= HashWithIndifferentAccess.new({})
|
|
|
|
end
|
|
|
|
|
2017-04-18 03:47:21 +08:00
|
|
|
def html_builders
|
|
|
|
@html_builders ||= {}
|
|
|
|
end
|
2017-04-19 05:49:56 +08:00
|
|
|
|
2017-11-17 03:42:38 +08:00
|
|
|
def seed_path_builders
|
|
|
|
@seed_path_builders ||= Set.new
|
|
|
|
end
|
|
|
|
|
2017-04-19 05:49:56 +08:00
|
|
|
def vendored_pretty_text
|
|
|
|
@vendored_pretty_text ||= Set.new
|
|
|
|
end
|
|
|
|
|
2018-04-10 14:37:16 +08:00
|
|
|
def vendored_core_pretty_text
|
|
|
|
@vendored_core_pretty_text ||= Set.new
|
|
|
|
end
|
2013-11-19 23:42:28 +08:00
|
|
|
end
|
2013-08-01 13:59:57 +08:00
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def self.register_auth_provider(auth_provider)
|
|
|
|
self.auth_providers << auth_provider
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def register_js(filename, options = {})
|
2013-02-06 03:16:51 +08:00
|
|
|
# If we have a server side option, add that too.
|
|
|
|
self.class.javascripts << filename
|
|
|
|
end
|
|
|
|
|
2017-11-23 09:02:01 +08:00
|
|
|
def self.register_service_worker(filename, options = {})
|
|
|
|
self.service_workers << filename
|
|
|
|
end
|
|
|
|
|
2018-11-27 05:49:57 +08:00
|
|
|
def self.register_svg_icon(icon)
|
|
|
|
self.svg_icons << icon
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def register_css(filename)
|
|
|
|
self.class.stylesheets << filename
|
|
|
|
end
|
|
|
|
|
2018-01-25 19:09:18 +08:00
|
|
|
def self.register_locale(locale, options = {})
|
|
|
|
self.locales[locale] = options
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def register_archetype(name, options = {})
|
2013-02-06 03:16:51 +08:00
|
|
|
Archetype.register(name, options)
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.register_glob(root, extension, options = nil)
|
2015-08-18 03:03:55 +08:00
|
|
|
self.asset_globs << [root, extension, options || {}]
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.each_globbed_asset(each_options = nil)
|
2015-08-18 03:03:55 +08:00
|
|
|
each_options ||= {}
|
|
|
|
|
|
|
|
self.asset_globs.each do |g|
|
|
|
|
root, ext, options = *g
|
|
|
|
|
2015-08-20 03:11:57 +08:00
|
|
|
if options[:admin]
|
|
|
|
next unless each_options[:admin]
|
|
|
|
else
|
|
|
|
next if each_options[:admin]
|
2015-08-19 09:24:09 +08:00
|
|
|
end
|
2015-08-18 03:03:55 +08:00
|
|
|
|
|
|
|
Dir.glob("#{root}/**/*") do |f|
|
|
|
|
yield f, ext
|
|
|
|
end
|
|
|
|
end
|
2014-12-10 04:23:14 +08:00
|
|
|
end
|
|
|
|
|
2018-11-26 16:01:28 +08:00
|
|
|
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6|\.js\.no-module\.es6$/
|
2016-11-15 08:42:55 +08:00
|
|
|
HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.register_asset(asset, opts = nil)
|
2016-11-15 08:42:55 +08:00
|
|
|
if asset =~ JS_REGEX
|
2014-12-10 03:20:53 +08:00
|
|
|
if opts == :admin
|
|
|
|
self.admin_javascripts << asset
|
2017-04-19 05:49:56 +08:00
|
|
|
elsif opts == :vendored_pretty_text
|
|
|
|
self.vendored_pretty_text << asset
|
2018-04-10 14:37:16 +08:00
|
|
|
elsif opts == :vendored_core_pretty_text
|
|
|
|
self.vendored_core_pretty_text << asset
|
2014-12-10 03:20:53 +08: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-15 08:42:55 +08:00
|
|
|
elsif asset =~ HANDLEBARS_REGEX
|
2014-12-10 03:20:53 +08:00
|
|
|
self.handlebars << asset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-05 03:56:17 +08:00
|
|
|
def self.register_seed_data(key, value)
|
|
|
|
self.seed_data[key] = value
|
|
|
|
end
|
|
|
|
|
2017-11-17 03:42:38 +08:00
|
|
|
def self.register_seed_path_builder(&block)
|
|
|
|
seed_path_builders << block
|
|
|
|
end
|
|
|
|
|
2017-04-18 03:47:21 +08:00
|
|
|
def self.register_html_builder(name, &block)
|
2017-11-03 23:32:32 +08:00
|
|
|
html_builders[name] ||= []
|
|
|
|
html_builders[name] << block
|
2017-04-18 03:47:21 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.build_html(name, ctx = nil)
|
2017-11-03 23:32:32 +08:00
|
|
|
builders = html_builders[name] || []
|
2017-11-15 05:31:44 +08:00
|
|
|
builders.map { |b| b.call(ctx) }.join("\n").html_safe
|
2017-04-18 03:47:21 +08:00
|
|
|
end
|
|
|
|
|
2017-11-17 03:42:38 +08:00
|
|
|
def self.seed_paths
|
|
|
|
result = SeedFu.fixture_paths.dup
|
|
|
|
unless Rails.env.test? && ENV['LOAD_PLUGINS'] != "1"
|
|
|
|
seed_path_builders.each { |b| result += b.call }
|
|
|
|
end
|
2017-11-17 04:22:05 +08:00
|
|
|
result.uniq
|
2017-11-17 03:42:38 +08:00
|
|
|
end
|
|
|
|
|
2018-04-10 14:37:16 +08:00
|
|
|
VENDORED_CORE_PRETTY_TEXT_MAP = {
|
2019-02-13 02:57:52 +08:00
|
|
|
"moment.js" => "vendor/assets/javascripts/moment.js",
|
|
|
|
"moment-timezone.js" => "vendor/assets/javascripts/moment-timezone-with-data.js"
|
2018-04-10 14:37:16 +08:00
|
|
|
}
|
|
|
|
def self.core_asset_for_name(name)
|
|
|
|
asset = VENDORED_CORE_PRETTY_TEXT_MAP[name]
|
|
|
|
raise KeyError, "Asset #{name} not found in #{VENDORED_CORE_PRETTY_TEXT_MAP}" unless asset
|
|
|
|
asset
|
|
|
|
end
|
|
|
|
|
2018-01-25 19:09:18 +08:00
|
|
|
def locales
|
|
|
|
self.class.locales
|
|
|
|
end
|
|
|
|
|
2013-11-19 23:42:28 +08:00
|
|
|
def javascripts
|
2013-02-08 15:56:12 +08:00
|
|
|
self.class.javascripts
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
def auth_providers
|
|
|
|
self.class.auth_providers
|
|
|
|
end
|
|
|
|
|
2017-11-23 09:02:01 +08:00
|
|
|
def service_workers
|
|
|
|
self.class.service_workers
|
|
|
|
end
|
|
|
|
|
2013-11-19 23:42:28 +08:00
|
|
|
def stylesheets
|
|
|
|
self.class.stylesheets
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-04-07 22:33:35 +08:00
|
|
|
def mobile_stylesheets
|
|
|
|
self.class.mobile_stylesheets
|
|
|
|
end
|
|
|
|
|
2014-04-25 16:26:37 +08:00
|
|
|
def desktop_stylesheets
|
|
|
|
self.class.desktop_stylesheets
|
|
|
|
end
|
|
|
|
|
|
|
|
def sass_variables
|
|
|
|
self.class.sass_variables
|
|
|
|
end
|
|
|
|
|
2013-12-31 23:40:45 +08:00
|
|
|
def handlebars
|
|
|
|
self.class.handlebars
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.clear
|
2013-02-08 15:56:12 +08:00
|
|
|
self.javascripts = nil
|
2018-07-23 23:51:57 +08:00
|
|
|
self.auth_providers = nil
|
2017-11-23 09:02:01 +08:00
|
|
|
self.service_workers = nil
|
2013-11-19 23:42:28 +08:00
|
|
|
self.stylesheets = nil
|
2014-04-07 22:33:35 +08:00
|
|
|
self.mobile_stylesheets = nil
|
2014-04-25 16:26:37 +08:00
|
|
|
self.desktop_stylesheets = nil
|
|
|
|
self.sass_variables = nil
|
2013-12-31 23:40:45 +08:00
|
|
|
self.handlebars = nil
|
2018-01-25 19:09:18 +08:00
|
|
|
self.locales = nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-12-10 03:20:53 +08:00
|
|
|
def self.reset!
|
|
|
|
javascripts.clear
|
2018-07-23 23:51:57 +08:00
|
|
|
auth_providers.clear
|
2017-11-23 09:02:01 +08:00
|
|
|
service_workers.clear
|
2014-12-10 03:20:53 +08:00
|
|
|
admin_javascripts.clear
|
|
|
|
stylesheets.clear
|
|
|
|
mobile_stylesheets.clear
|
|
|
|
desktop_stylesheets.clear
|
|
|
|
sass_variables.clear
|
|
|
|
serialized_current_user_fields
|
2014-12-10 04:23:14 +08:00
|
|
|
asset_globs.clear
|
2017-04-18 03:47:21 +08:00
|
|
|
html_builders.clear
|
2017-04-19 05:49:56 +08:00
|
|
|
vendored_pretty_text.clear
|
2018-04-10 14:37:16 +08:00
|
|
|
vendored_core_pretty_text.clear
|
2017-11-17 03:42:38 +08:00
|
|
|
seed_path_builders.clear
|
2018-01-25 19:09:18 +08:00
|
|
|
locales.clear
|
2014-12-10 03:20:53 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
def self.setup(plugin_class)
|
2013-02-06 03:16:51 +08:00
|
|
|
registry = DiscoursePluginRegistry.new
|
|
|
|
plugin = plugin_class.new(registry)
|
|
|
|
plugin.setup
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|