2017-04-12 10:52:52 -04:00
|
|
|
class ThemeField < ActiveRecord::Base
|
|
|
|
|
2017-05-08 11:38:48 -04:00
|
|
|
belongs_to :upload
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(html: 0,
|
|
|
|
scss: 1,
|
|
|
|
theme_upload_var: 2,
|
|
|
|
theme_color_var: 3,
|
|
|
|
theme_var: 4)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.theme_var_type_ids
|
|
|
|
@theme_var_type_ids ||= [2,3,4]
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
COMPILER_VERSION = 5
|
|
|
|
|
|
|
|
belongs_to :theme
|
|
|
|
|
|
|
|
def transpile(es6_source, version)
|
|
|
|
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
|
|
|
wrapped = <<PLUGIN_API_JS
|
|
|
|
Discourse._registerPluginCode('#{version}', api => {
|
|
|
|
#{es6_source}
|
|
|
|
});
|
|
|
|
PLUGIN_API_JS
|
|
|
|
|
|
|
|
template.babel_transpile(wrapped)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_html(html)
|
2017-04-19 16:46:28 -04:00
|
|
|
errors = nil
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
doc.css('script[type="text/x-handlebars"]').each do |node|
|
|
|
|
name = node["name"] || node["data-template-name"] || "broken"
|
|
|
|
is_raw = name =~ /\.raw$/
|
|
|
|
if is_raw
|
2017-07-05 14:14:30 -04:00
|
|
|
template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(node.inner_html)})"
|
2017-04-12 10:52:52 -04:00
|
|
|
node.replace <<COMPILED
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
Discourse.RAW_TEMPLATES[#{name.sub(/\.raw$/, '').inspect}] = #{template};
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
COMPILED
|
|
|
|
else
|
|
|
|
template = "Ember.HTMLBars.template(#{Barber::Ember::Precompiler.compile(node.inner_html)})"
|
|
|
|
node.replace <<COMPILED
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
Ember.TEMPLATES[#{name.inspect}] = #{template};
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
COMPILED
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
doc.css('script[type="text/discourse-plugin"]').each do |node|
|
|
|
|
if node['version'].present?
|
|
|
|
begin
|
|
|
|
code = transpile(node.inner_html, node['version'])
|
|
|
|
node.replace("<script>#{code}</script>")
|
|
|
|
rescue MiniRacer::RuntimeError => ex
|
|
|
|
node.replace("<script type='text/discourse-js-error'>#{ex.message}</script>")
|
2017-04-19 16:46:28 -04:00
|
|
|
errors ||= []
|
|
|
|
errors << ex.message
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-19 16:46:28 -04:00
|
|
|
[doc.to_s, errors&.join("\n")]
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 16:01:01 -04:00
|
|
|
def self.guess_type(name)
|
|
|
|
if html_fields.include?(name.to_s)
|
|
|
|
types[:html]
|
|
|
|
elsif scss_fields.include?(name.to_s)
|
|
|
|
types[:scss]
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
def self.html_fields
|
2017-05-02 16:01:01 -04:00
|
|
|
@html_fields ||= %w(body_tag head_tag header footer after_header)
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2017-04-19 16:46:28 -04:00
|
|
|
def self.scss_fields
|
2017-05-02 16:01:01 -04:00
|
|
|
@scss_fields ||= %w(scss embedded_scss)
|
2017-04-19 16:46:28 -04:00
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
def ensure_baked!
|
|
|
|
if ThemeField.html_fields.include?(self.name)
|
|
|
|
if !self.value_baked || compiler_version != COMPILER_VERSION
|
|
|
|
|
2017-04-19 16:46:28 -04:00
|
|
|
self.value_baked, self.error = process_html(self.value)
|
2017-04-12 10:52:52 -04:00
|
|
|
self.compiler_version = COMPILER_VERSION
|
|
|
|
|
2017-04-19 16:46:28 -04:00
|
|
|
if self.value_baked_changed? || compiler_version.changed? || self.error_changed?
|
|
|
|
self.update_columns(value_baked: value_baked,
|
|
|
|
compiler_version: compiler_version,
|
|
|
|
error: error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_scss_compiles!
|
|
|
|
if ThemeField.scss_fields.include?(self.name)
|
|
|
|
begin
|
|
|
|
Stylesheet::Compiler.compile("@import \"theme_variables\"; @import \"theme_field\";",
|
|
|
|
"theme.scss",
|
2017-05-09 17:20:28 -04:00
|
|
|
theme_field: self.value.dup,
|
|
|
|
theme: self.theme
|
|
|
|
)
|
2017-04-19 16:46:28 -04:00
|
|
|
self.error = nil unless error.nil?
|
|
|
|
rescue SassC::SyntaxError => e
|
|
|
|
self.error = e.message
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
2017-04-20 16:55:09 -04:00
|
|
|
|
|
|
|
if error_changed?
|
|
|
|
update_columns(error: self.error)
|
|
|
|
end
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_name
|
2017-05-02 16:01:01 -04:00
|
|
|
Theme.targets.invert[target_id].to_s
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before_save do
|
|
|
|
if value_changed? && !value_baked_changed?
|
|
|
|
self.value_baked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_commit do
|
|
|
|
ensure_baked!
|
2017-04-19 16:46:28 -04:00
|
|
|
ensure_scss_compiles!
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
Stylesheet::Manager.clear_theme_cache! if self.name.include?("scss")
|
|
|
|
|
|
|
|
# TODO message for mobile vs desktop
|
2017-04-19 16:46:28 -04:00
|
|
|
MessageBus.publish "/header-change/#{theme.key}", self.value if theme && self.name == "header"
|
|
|
|
MessageBus.publish "/footer-change/#{theme.key}", self.value if theme && self.name == "footer"
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: theme_fields
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# theme_id :integer not null
|
2017-05-02 16:01:01 -04:00
|
|
|
# target_id :integer not null
|
|
|
|
# name :string(30) not null
|
2017-04-12 10:52:52 -04:00
|
|
|
# value :text not null
|
|
|
|
# value_baked :text
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# compiler_version :integer default(0), not null
|
2017-04-20 15:47:25 -04:00
|
|
|
# error :string
|
2017-05-02 16:01:01 -04:00
|
|
|
# upload_id :integer
|
|
|
|
# type_id :integer default(0), not null
|
2017-04-12 10:52:52 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-05-02 16:01:01 -04:00
|
|
|
# theme_field_unique_index (theme_id,target_id,type_id,name) UNIQUE
|
2017-04-12 10:52:52 -04:00
|
|
|
#
|