FEATURE: theme settings in RAW hbs templates

This commit is contained in:
osamasayegh 2018-04-12 21:30:36 +03:00 committed by Sam
parent 42fc116af4
commit 4ebf46dc08
2 changed files with 19 additions and 3 deletions

View File

@ -9,12 +9,28 @@ function inject(context, key, value) {
value = value.replace(/\\u0022/g, '"'); value = value.replace(/\\u0022/g, '"');
} }
if (!(context instanceof Ember.Object)) {
injectPlainObject(context, key, value);
return;
}
if (!context.get("themeSettings")) { if (!context.get("themeSettings")) {
context.set("themeSettings", {}); context.set("themeSettings", {});
} }
context.set(`themeSettings.${key}`, value); context.set(`themeSettings.${key}`, value);
} }
function injectPlainObject(context, key, value) {
if (!context.themeSettings) {
Object.assign(context, { themeSettings: {} });
}
Object.assign(context.themeSettings, { [key]: value });
}
registerHelper('theme-setting-injector', function(arr, hash) { registerHelper('theme-setting-injector', function(arr, hash) {
inject(hash.context, hash.key, hash.value); inject(hash.context, hash.key, hash.value);
}); });
Handlebars.registerHelper('theme-setting-injector', function(hash) {
inject(this, hash.hash.key, hash.hash.value);
});

View File

@ -60,16 +60,16 @@ PLUGIN_API_JS
doc.css('script[type="text/x-handlebars"]').each do |node| doc.css('script[type="text/x-handlebars"]').each do |node|
name = node["name"] || node["data-template-name"] || "broken" name = node["name"] || node["data-template-name"] || "broken"
is_raw = name =~ /\.raw$/
setting_helpers = '' setting_helpers = ''
theme.cached_settings.each do |k, v| theme.cached_settings.each do |k, v|
val = v.is_a?(String) ? "\"#{v.gsub('"', "\\u0022")}\"" : v val = v.is_a?(String) ? "\"#{v.gsub('"', "\\u0022")}\"" : v
setting_helpers += "{{theme-setting-injector context=this key=\"#{k}\" value=#{val}}}\n" setting_helpers += "{{theme-setting-injector #{is_raw ? "" : "context=this"} key=\"#{k}\" value=#{val}}}\n"
end end
hbs_template = setting_helpers + node.inner_html hbs_template = setting_helpers + node.inner_html
is_raw = name =~ /\.raw$/
if is_raw if is_raw
template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(node.inner_html)})" template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(hbs_template)})"
node.replace <<COMPILED node.replace <<COMPILED
<script> <script>
(function() { (function() {