@vivocha/public-entities
Advanced tools
Comparing version 5.9.7 to 5.9.8
import { MultiLanguageString } from '../language'; | ||
export declare function getTranslations(data: MultiLanguageString[], requestedLanguage: string, defaultLanguage: string, path?: string): any; | ||
export declare function getStringsObject(data: MultiLanguageString[], requestedLanguage: string, defaultLanguage: string, path?: string): any; |
@@ -28,3 +28,3 @@ "use strict"; | ||
} | ||
function getTranslations(data, requestedLanguage, defaultLanguage, path) { | ||
function getStringsObject(data, requestedLanguage, defaultLanguage, path) { | ||
let tmp = data.reduce((o, i) => { | ||
@@ -42,3 +42,3 @@ let s = getBestMatch(requestedLanguage, defaultLanguage, i.values); | ||
} | ||
exports.getTranslations = getTranslations; | ||
exports.getStringsObject = getStringsObject; | ||
//# sourceMappingURL=language.js.map |
import { WidgetManifest, WidgetSettings, WidgetInstance } from '../widget'; | ||
export interface WidgetInstanceFactory { | ||
new (): WidgetInstanceWrapper; | ||
new (options: WidgetInstanceCreateOptions): WidgetInstanceWrapper; | ||
} | ||
export interface WidgetInstanceCreateOptions { | ||
id: string; | ||
manifest: WidgetManifest; | ||
settings: WidgetSettings; | ||
strings: any; | ||
requestedLanguage: string; | ||
defaultLanguage: string; | ||
assetsBaseUrl: string; | ||
} | ||
export declare abstract class WidgetInstanceWrapper implements WidgetInstance { | ||
@@ -9,10 +18,9 @@ html: string; | ||
selector?: string; | ||
abstract resolveAssetUrl(url: string): string; | ||
constructor(options: WidgetInstanceCreateOptions); | ||
readonly options: WidgetInstanceCreateOptions; | ||
abstract renderSass(scss: string): Promise<string>; | ||
abstract resolveAssetUrl(url: string, base?: string): string; | ||
abstract fetchAsset(url: string): Promise<any>; | ||
create(manifest: WidgetManifest, settings: WidgetSettings, strings: any, requestedLanguage: string, defaultLanguage: string): Promise<WidgetInstance>; | ||
static create(factory: WidgetInstanceFactory, manifest: WidgetManifest, settings: WidgetSettings, strings: any, requestedLanguage: string, defaultLanguage: string): Promise<WidgetInstance>; | ||
create(): Promise<WidgetInstance>; | ||
static create(factory: WidgetInstanceFactory, options: WidgetInstanceCreateOptions): Promise<WidgetInstance>; | ||
} | ||
export declare class DebugWidgetInstance extends WidgetInstanceWrapper { | ||
resolveAssetUrl(url: string): string; | ||
fetchAsset(url: string): Promise<any>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("util"); | ||
const eredita_1 = require("eredita"); | ||
const node_sass_1 = require("node-sass"); | ||
const language_1 = require("./language"); | ||
const script_1 = require("./script"); | ||
const render = util_1.promisify(node_sass_1.render); | ||
const __options = Symbol(); | ||
class WidgetInstanceWrapper { | ||
async create(manifest, settings, strings, requestedLanguage, defaultLanguage) { | ||
constructor(options) { | ||
this[__options] = options; | ||
} | ||
get options() { | ||
return this[__options]; | ||
} | ||
async create() { | ||
let out = { | ||
@@ -15,57 +18,52 @@ html: '', | ||
}; | ||
if (settings.selector) { | ||
out.selector = settings.selector; | ||
if (this.options.settings.selector) { | ||
out.selector = this.options.settings.selector; | ||
} | ||
let assetsById = manifest.assets.reduce((o, i) => { | ||
o[i.id] = i; | ||
return o; | ||
}, {}); | ||
let assetsByPath = manifest.assets.reduce((o, i) => { | ||
o[i.path] = i; | ||
return o; | ||
}, {}); | ||
const rawHtml = await this.fetchAsset(assetsById[manifest.htmlId].path); | ||
const fullStrings = settings.strings ? eredita_1.deepExtend(strings, language_1.getTranslations(settings.strings, requestedLanguage, defaultLanguage)) : strings; | ||
const script = script_1.Script.create(`\`${rawHtml}\``, fullStrings); | ||
out.html = await script(); | ||
const rawScss = await this.fetchAsset(assetsById[manifest.scssId].path); | ||
const rawHtml = await this.fetchAsset(this.options.assetsBaseUrl + this.options.manifest.htmlId); | ||
const fullStrings = this.options.settings.strings ? eredita_1.Eredita.deepExtend(this.options.strings, language_1.getStringsObject(this.options.settings.strings, this.options.requestedLanguage, this.options.defaultLanguage)) : this.options.strings; | ||
out.html = rawHtml.replace(/__([\w\.]+)__/g, (match, p1) => { | ||
if (p1 === 'ENGAGEMENT_ID') { | ||
return this.options.id; | ||
} | ||
else if (p1 === 'CONTEXT') { | ||
// TODO put the context in | ||
return match; | ||
} | ||
else { | ||
let val = eredita_1.Eredita.dot(fullStrings, p1); | ||
if (typeof val === 'string') { | ||
return val; | ||
} | ||
else { | ||
return match; | ||
} | ||
} | ||
}); | ||
const rawScss = await this.fetchAsset(this.options.assetsBaseUrl + this.options.manifest.scssId); | ||
let mainScss = ''; | ||
if (settings.variables) { | ||
for (let k in settings.variables) { | ||
let v = settings.variables[k]; | ||
mainScss += `$${k}: ${typeof v === 'string' ? `'${v}'` : v.toString()};\n`; | ||
if (this.options.manifest.variables && this.options.manifest.variables.length) { | ||
this.options.settings.variables = this.options.settings.variables || {}; | ||
for (let def of this.options.manifest.variables) { | ||
let val = this.options.settings.variables[def.id] || def.defaultValue; | ||
if (typeof val !== 'undefined') { | ||
mainScss += `$${def.id}: ${typeof val === 'string' ? `'${val}'` : val.toString()};\n`; | ||
} | ||
} | ||
} | ||
mainScss += rawScss; | ||
out.css = (await render({ | ||
data: mainScss, | ||
importer: (url, prev, done) => { | ||
console.log('importer', url, prev); | ||
// TODO resolve the url and fetch the asset | ||
done(new Error('not_implemented')); | ||
} | ||
})).css.toString('utf8'); | ||
if (settings.customCss) { | ||
out.css += '\n' + settings.customCss; | ||
out.css = await this.renderSass(mainScss); | ||
if (this.options.settings.customCss) { | ||
out.css += '\n' + this.options.settings.customCss; | ||
} | ||
out.css = out.css.replace(/['"](?:\.\/)?assets\/([^'"]*)/g, (match, p1) => { | ||
return this.resolveAssetUrl(p1); | ||
out.css = out.css.replace(/(url\(['"]?)([^'"\)]*)/g, (match, p1, p2) => { | ||
return p1 + this.resolveAssetUrl(p2); | ||
}); | ||
return out; | ||
} | ||
static create(factory, manifest, settings, strings, requestedLanguage, defaultLanguage) { | ||
const out = new factory(); | ||
return out.create(manifest, settings, strings, requestedLanguage, defaultLanguage); | ||
static create(factory, options) { | ||
const out = new factory(options); | ||
return out.create(); | ||
} | ||
} | ||
exports.WidgetInstanceWrapper = WidgetInstanceWrapper; | ||
class DebugWidgetInstance extends WidgetInstanceWrapper { | ||
resolveAssetUrl(url) { | ||
throw new Error('not_implemented'); | ||
} | ||
fetchAsset(url) { | ||
throw new Error('not_implemented'); | ||
} | ||
} | ||
exports.DebugWidgetInstance = DebugWidgetInstance; | ||
//# sourceMappingURL=widget.js.map |
{ | ||
"name": "@vivocha/public-entities", | ||
"version": "5.9.7", | ||
"version": "5.9.8", | ||
"description": "Vivocha public entities and types", | ||
@@ -49,4 +49,5 @@ "main": "dist/index.js", | ||
"eredita": "^1.1.4", | ||
"node-sass": "^4.5.3" | ||
"node-sass": "^4.5.3", | ||
"request": "^2.83.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30027
23
852
3
+ Addedrequest@^2.83.0