salix/print/core/component.js

174 lines
4.8 KiB
JavaScript
Raw 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 {
2024-05-21 06:53:12 +00:00
constructor(name, template) {
console.log('cosntructor Component');
console.log('template: ', template);
console.log('name: ', name);
2019-10-29 06:46:44 +00:00
this.name = name;
2024-05-21 06:53:12 +00:00
this._template = template;
2019-10-29 06:46:44 +00:00
}
get path() {
return `./components/${this.name}`;
}
get template() {
2024-05-21 06:53:12 +00:00
console.log('this._template: ', this._template);
if (this._template) return this._template;
2019-10-29 06:46:44 +00:00
const templatePath = `${this.path}/${this.name}.html`;
const fullPath = path.resolve(__dirname, templatePath);
2024-05-21 06:53:12 +00:00
console.log('fullPath: ', fullPath);
if (!fs.existsSync(fullPath)) {
const path = require('path');
const vnPrintPath = path.resolve('print');
return fs.readFileSync(`${vnPrintPath}/core/components/blank-template/blank-template.html`, 'utf8');
}
2019-10-29 06:46:44 +00:00
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() {
2024-05-21 06:53:12 +00:00
let css = [];
const path = require('path');
const vnPrintPath = path.resolve('print');
const styles = require(`${vnPrintPath}/common/css/index.js`);
for (const style of styles)
css.push(fs.readFileSync(style));
2019-10-29 06:46:44 +00:00
2024-05-21 06:53:12 +00:00
const style = `${path.resolve(__dirname, this.path)}/assets/css/style.css`; // regex to match css files
2019-10-29 06:46:44 +00:00
2024-05-21 06:53:12 +00:00
if (fs.existsSync(style)) css.push(fs.readFileSync(style));
return css.join('\n');
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() {
2024-05-21 06:53:12 +00:00
console.log('this.name ', this.name);
console.log('this._template: ', this._template);
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
2024-05-21 06:53:12 +00:00
let component = {};
if (this.template !== undefined) {
component = require(`${this.path}/${this.name}`);
component.template = juice.inlineContent(this.template, this.stylesheet, {
inlinePseudoElements: true
});
}
2019-10-29 06:46:44 +00:00
component.i18n = this.locale;
component.attachments = this.attachments;
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() {
2024-05-21 06:53:12 +00:00
const render = await renderer.renderToString(
2020-05-20 14:23:53 +00:00
this.component()
);
2024-05-21 06:53:12 +00:00
return render;
2019-10-29 06:46:44 +00:00
}
}
module.exports = Component;