2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
#
|
|
|
|
# A class that handles interaction between a plugin and the Discourse App.
|
|
|
|
#
|
|
|
|
class DiscoursePluginRegistry
|
|
|
|
|
2020-05-13 06:25:34 -05:00
|
|
|
# Shortcut to create new register in the plugin registry
|
|
|
|
# - Register is created in a class variable using the specified name/type
|
|
|
|
# - Defines singleton method to access the register
|
|
|
|
# - Defines instance method as a shortcut to the singleton method
|
|
|
|
# - Automatically deletes the register on ::clear!
|
|
|
|
def self.define_register(register_name, type)
|
|
|
|
@@register_names ||= Set.new
|
|
|
|
@@register_names << register_name
|
|
|
|
|
|
|
|
define_singleton_method(register_name) do
|
|
|
|
instance_variable_get(:"@#{register_name}") ||
|
|
|
|
instance_variable_set(:"@#{register_name}", type.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method(register_name) do
|
|
|
|
self.class.public_send(register_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-15 08:04:38 -05:00
|
|
|
# Create a new register (see `define_register`) with some additions:
|
|
|
|
# - Register is created in a class variable using the specified name/type
|
|
|
|
# - Defines singleton method to access the register
|
|
|
|
# - Defines instance method as a shortcut to the singleton method
|
|
|
|
# - Automatically deletes the register on ::clear!
|
|
|
|
def self.define_filtered_register(register_name)
|
|
|
|
define_register(register_name, Array)
|
|
|
|
|
|
|
|
singleton_class.alias_method :"_raw_#{register_name}", :"#{register_name}"
|
|
|
|
|
|
|
|
define_singleton_method(register_name) do
|
|
|
|
unfiltered = public_send(:"_raw_#{register_name}")
|
|
|
|
unfiltered
|
|
|
|
.filter { |v| v[:plugin].enabled? }
|
|
|
|
.map { |v| v[:value] }
|
|
|
|
.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
define_singleton_method("register_#{register_name.to_s.singularize}") do |value, plugin|
|
|
|
|
public_send(:"_raw_#{register_name}") << { plugin: plugin, value: value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-13 06:25:34 -05:00
|
|
|
define_register :javascripts, Set
|
|
|
|
define_register :auth_providers, Set
|
|
|
|
define_register :service_workers, Set
|
|
|
|
define_register :admin_javascripts, Set
|
|
|
|
define_register :stylesheets, Hash
|
|
|
|
define_register :mobile_stylesheets, Hash
|
|
|
|
define_register :desktop_stylesheets, Hash
|
|
|
|
define_register :sass_variables, Set
|
|
|
|
define_register :handlebars, Set
|
|
|
|
define_register :serialized_current_user_fields, Set
|
|
|
|
define_register :seed_data, HashWithIndifferentAccess
|
|
|
|
define_register :locales, HashWithIndifferentAccess
|
|
|
|
define_register :svg_icons, Set
|
|
|
|
define_register :custom_html, Hash
|
|
|
|
define_register :asset_globs, Set
|
|
|
|
define_register :html_builders, Hash
|
|
|
|
define_register :seed_path_builders, Set
|
|
|
|
define_register :vendored_pretty_text, Set
|
|
|
|
define_register :vendored_core_pretty_text, Set
|
2013-08-01 00:59:57 -05:00
|
|
|
|
2020-05-15 08:04:38 -05:00
|
|
|
define_filtered_register :staff_user_custom_fields
|
|
|
|
define_filtered_register :public_user_custom_fields
|
|
|
|
|
|
|
|
define_filtered_register :self_editable_user_custom_fields
|
|
|
|
define_filtered_register :staff_editable_user_custom_fields
|
|
|
|
|
|
|
|
define_filtered_register :editable_group_custom_fields
|
|
|
|
|
2020-05-19 04:38:58 -05:00
|
|
|
define_filtered_register :topic_thumbnail_sizes
|
|
|
|
|
2018-07-23 10:51:57 -05:00
|
|
|
def self.register_auth_provider(auth_provider)
|
|
|
|
self.auth_providers << auth_provider
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def register_js(filename, options = {})
|
2013-02-05 13:16:51 -06:00
|
|
|
# If we have a server side option, add that too.
|
|
|
|
self.class.javascripts << filename
|
|
|
|
end
|
|
|
|
|
2017-11-22 19:02:01 -06:00
|
|
|
def self.register_service_worker(filename, options = {})
|
|
|
|
self.service_workers << filename
|
|
|
|
end
|
|
|
|
|
2018-11-26 15:49:57 -06:00
|
|
|
def self.register_svg_icon(icon)
|
|
|
|
self.svg_icons << icon
|
|
|
|
end
|
|
|
|
|
2019-08-20 11:39:52 -05:00
|
|
|
def register_css(filename, plugin_directory_name)
|
|
|
|
self.class.stylesheets[plugin_directory_name] ||= Set.new
|
|
|
|
self.class.stylesheets[plugin_directory_name] << filename
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
def self.register_locale(locale, options = {})
|
|
|
|
self.locales[locale] = options
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def register_archetype(name, options = {})
|
2013-02-05 13:16:51 -06:00
|
|
|
Archetype.register(name, options)
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.register_glob(root, extension, options = nil)
|
2015-08-17 14:03:55 -05:00
|
|
|
self.asset_globs << [root, extension, options || {}]
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.each_globbed_asset(each_options = nil)
|
2015-08-17 14:03:55 -05:00
|
|
|
each_options ||= {}
|
|
|
|
|
|
|
|
self.asset_globs.each do |g|
|
|
|
|
root, ext, options = *g
|
|
|
|
|
2015-08-19 14:11:57 -05:00
|
|
|
if options[:admin]
|
|
|
|
next unless each_options[:admin]
|
|
|
|
else
|
|
|
|
next if each_options[:admin]
|
2015-08-18 20:24:09 -05:00
|
|
|
end
|
2015-08-17 14:03:55 -05:00
|
|
|
|
|
|
|
Dir.glob("#{root}/**/*") do |f|
|
|
|
|
yield f, ext
|
|
|
|
end
|
|
|
|
end
|
2014-12-09 14:23:14 -06:00
|
|
|
end
|
|
|
|
|
2020-03-11 08:43:55 -05:00
|
|
|
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6$/
|
2020-02-11 13:38:12 -06:00
|
|
|
HANDLEBARS_REGEX = /\.(hb[rs]|js\.handlebars)$/
|
2016-11-14 18:42:55 -06:00
|
|
|
|
2019-08-20 11:39:52 -05:00
|
|
|
def self.register_asset(asset, opts = nil, plugin_directory_name = nil)
|
2016-11-14 18:42:55 -06:00
|
|
|
if asset =~ JS_REGEX
|
2014-12-09 13:20:53 -06:00
|
|
|
if opts == :admin
|
|
|
|
self.admin_javascripts << asset
|
2017-04-18 16:49:56 -05:00
|
|
|
elsif opts == :vendored_pretty_text
|
|
|
|
self.vendored_pretty_text << asset
|
2018-04-10 01:37:16 -05:00
|
|
|
elsif opts == :vendored_core_pretty_text
|
|
|
|
self.vendored_core_pretty_text << asset
|
2014-12-09 13:20:53 -06:00
|
|
|
else
|
|
|
|
self.javascripts << asset
|
|
|
|
end
|
|
|
|
elsif asset =~ /\.css$|\.scss$/
|
|
|
|
if opts == :mobile
|
2019-08-20 11:39:52 -05:00
|
|
|
self.mobile_stylesheets[plugin_directory_name] ||= Set.new
|
|
|
|
self.mobile_stylesheets[plugin_directory_name] << asset
|
2014-12-09 13:20:53 -06:00
|
|
|
elsif opts == :desktop
|
2019-08-20 11:39:52 -05:00
|
|
|
self.desktop_stylesheets[plugin_directory_name] ||= Set.new
|
|
|
|
self.desktop_stylesheets[plugin_directory_name] << asset
|
2014-12-09 13:20:53 -06:00
|
|
|
elsif opts == :variables
|
|
|
|
self.sass_variables << asset
|
|
|
|
else
|
2019-08-20 11:39:52 -05:00
|
|
|
self.stylesheets[plugin_directory_name] ||= Set.new
|
|
|
|
self.stylesheets[plugin_directory_name] << asset
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
2016-11-14 18:42:55 -06:00
|
|
|
elsif asset =~ HANDLEBARS_REGEX
|
2014-12-09 13:20:53 -06:00
|
|
|
self.handlebars << asset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-21 22:09:10 -05:00
|
|
|
def self.stylesheets_exists?(plugin_directory_name, target = nil)
|
|
|
|
case target
|
|
|
|
when :desktop
|
|
|
|
self.desktop_stylesheets[plugin_directory_name].present?
|
|
|
|
when :mobile
|
|
|
|
self.mobile_stylesheets[plugin_directory_name].present?
|
|
|
|
else
|
|
|
|
self.stylesheets[plugin_directory_name].present?
|
|
|
|
end
|
2019-08-20 11:39:52 -05:00
|
|
|
end
|
|
|
|
|
2015-06-04 14:56:17 -05:00
|
|
|
def self.register_seed_data(key, value)
|
|
|
|
self.seed_data[key] = value
|
|
|
|
end
|
|
|
|
|
2017-11-16 13:42:38 -06:00
|
|
|
def self.register_seed_path_builder(&block)
|
|
|
|
seed_path_builders << block
|
|
|
|
end
|
|
|
|
|
2017-04-17 14:47:21 -05:00
|
|
|
def self.register_html_builder(name, &block)
|
2017-11-03 10:32:32 -05:00
|
|
|
html_builders[name] ||= []
|
|
|
|
html_builders[name] << block
|
2017-04-17 14:47:21 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.build_html(name, ctx = nil)
|
2017-11-03 10:32:32 -05:00
|
|
|
builders = html_builders[name] || []
|
2017-11-14 15:31:44 -06:00
|
|
|
builders.map { |b| b.call(ctx) }.join("\n").html_safe
|
2017-04-17 14:47:21 -05:00
|
|
|
end
|
|
|
|
|
2017-11-16 13:42:38 -06: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-16 14:22:05 -06:00
|
|
|
result.uniq
|
2017-11-16 13:42:38 -06:00
|
|
|
end
|
|
|
|
|
2018-04-10 01:37:16 -05:00
|
|
|
VENDORED_CORE_PRETTY_TEXT_MAP = {
|
2019-02-12 12:57:52 -06:00
|
|
|
"moment.js" => "vendor/assets/javascripts/moment.js",
|
|
|
|
"moment-timezone.js" => "vendor/assets/javascripts/moment-timezone-with-data.js"
|
2018-04-10 01:37:16 -05: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
|
|
|
|
|
2014-12-09 13:20:53 -06:00
|
|
|
def self.reset!
|
2020-05-13 06:25:34 -05:00
|
|
|
@@register_names.each do |name|
|
|
|
|
instance_variable_set(:"@#{name}", nil)
|
|
|
|
end
|
2014-12-09 13:20:53 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|