salix/print/core/component.js

144 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2019-10-29 06:46:44 +00:00
const Vue = require('vue');
const VueI18n = require('vue-i18n');
const renderer = require('vue-server-renderer').createRenderer();
Vue.use(VueI18n);
const fs = require('fs');
const yaml = require('js-yaml');
const juice = require('juice');
const path = require('path');
const config = require('./config');
class Component {
constructor(name) {
this.name = name;
}
get path() {
return `./components/${this.name}`;
}
get template() {
const templatePath = `${this.path}/${this.name}.html`;
const fullPath = path.resolve(__dirname, templatePath);
return fs.readFileSync(fullPath, 'utf8');
}
get locale() {
2019-10-31 11:43:04 +00:00
if (!this._locale)
this._locale = this.getLocales();
2019-10-31 11:43:04 +00:00
return this._locale;
}
getLocales() {
const mergedLocales = {messages: {}};
2019-10-29 06:46:44 +00:00
const localePath = path.resolve(__dirname, `${this.path}/locale`);
if (!fs.existsSync(localePath))
return mergedLocales;
2019-10-29 06:46:44 +00:00
const localeDir = fs.readdirSync(localePath);
for (const locale of localeDir) {
2019-10-29 06:46:44 +00:00
const fullPath = path.join(localePath, '/', locale);
const yamlLocale = fs.readFileSync(fullPath, 'utf8');
const jsonLocale = yaml.safeLoad(yamlLocale);
const localeName = locale.replace('.yml', '');
mergedLocales.messages[localeName] = jsonLocale;
}
2019-10-29 06:46:44 +00:00
return mergedLocales;
}
async getUserLocale() {
2022-09-19 10:59:32 +00:00
let lang = this.args.lang;
// Fetches user locale from mixing method getLocale()
if (this.args.recipientId) {
const component = await this.component();
2022-09-19 10:59:32 +00:00
lang = await component.getLocale(this.args.recipientId);
}
const messages = this.locale.messages;
2022-09-19 10:59:32 +00:00
const userTranslations = messages[lang];
if (!userTranslations) {
2022-09-19 10:59:32 +00:00
const fallbackLang = config.i18n.fallbackLocale;
2022-09-19 10:59:32 +00:00
return messages[fallbackLang];
}
return userTranslations;
2019-10-29 06:46:44 +00:00
}
get stylesheet() {
let mergedStyles = '';
const stylePath = path.resolve(__dirname, `${this.path}/assets/css`);
if (!fs.existsSync(stylePath))
return mergedStyles;
2019-10-31 11:43:04 +00:00
return require(`${stylePath}/import`);
2019-10-29 06:46:44 +00:00
}
get attachments() {
const attachmentsPath = `${this.path}/attachments.json`;
const fullPath = path.resolve(__dirname, attachmentsPath);
if (!fs.existsSync(fullPath))
return [];
return require(fullPath);
}
build() {
2019-10-31 11:43:04 +00:00
const fullPath = path.resolve(__dirname, this.path);
if (!fs.existsSync(fullPath))
throw new Error(`Template "${this.name}" not found`);
2019-10-29 06:46:44 +00:00
2019-10-31 11:43:04 +00:00
const component = require(`${this.path}/${this.name}`);
2019-10-29 06:46:44 +00:00
component.i18n = this.locale;
component.attachments = this.attachments;
component.template = juice.inlineContent(this.template, this.stylesheet, {
inlinePseudoElements: true
});
2021-03-01 10:25:37 +00:00
const tplPath = this.path;
if (!component.computed) component.computed = {};
component.computed.path = function() {
return tplPath;
};
2019-10-29 06:46:44 +00:00
return component;
}
2020-05-20 14:23:53 +00:00
component() {
if (this._component)
return this._component;
2019-10-29 06:46:44 +00:00
const component = this.build();
const i18n = new VueI18n(config.i18n);
2021-03-01 10:25:37 +00:00
const props = {...this.args};
2020-05-20 14:23:53 +00:00
this._component = new Vue({
2019-10-29 06:46:44 +00:00
i18n: i18n,
render: h => h(component, {
2020-09-28 11:54:02 +00:00
props: props
2019-10-29 06:46:44 +00:00
})
});
2020-05-20 14:23:53 +00:00
return this._component;
}
/**
* @return {Promise} Rendered component
*/
async render() {
return renderer.renderToString(
this.component()
);
2019-10-29 06:46:44 +00:00
}
}
module.exports = Component;