This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/helpers/theme-setting-injector.es6
2018-06-15 17:03:24 +02:00

37 lines
955 B
JavaScript

// A small helper to inject theme settings into
// context objects of handlebars templates used
// in themes
import { registerHelper } from "discourse-common/lib/helpers";
function inject(context, key, value) {
if (typeof value === "string") {
value = value.replace(/\\u0022/g, '"');
}
if (!(context instanceof Ember.Object)) {
injectPlainObject(context, key, value);
return;
}
if (!context.get("themeSettings")) {
context.set("themeSettings", {});
}
context.set(`themeSettings.${key}`, value);
}
function injectPlainObject(context, key, value) {
if (!context.themeSettings) {
_.assign(context, { themeSettings: {} });
}
_.assign(context.themeSettings, { [key]: value });
}
registerHelper("theme-setting-injector", function(arr, hash) {
inject(hash.context, hash.key, hash.value);
});
Handlebars.registerHelper("theme-setting-injector", function(hash) {
inject(this, hash.hash.key, hash.hash.value);
});