ngx-stimulsoft
Version:
Angular reporter for stimulsoftJS
403 lines (392 loc) • 18.3 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Inject, Injectable, makeEnvironmentProviders, NgModule, input, output, inject, signal, computed, effect, Component } from '@angular/core';
import { SourceType, Source, NgxSourceService } from 'ngx-source';
import { DOCUMENT } from '@angular/common';
const config = new InjectionToken('config');
const NEW_CONFIG = new InjectionToken('NEW_CONFIG');
const INITIAL_CONFIG = new InjectionToken('INITIAL_CONFIG');
const initialStimulsoftConfig = {
baseUrl: '',
designerJsUrl: '/content/js/stimulsoft.designer.js',
reportsJsUrl: '/content/js/stimulsoft.reports.js',
viewerJsUrl: '/content/js/stimulsoft.viewer.js',
};
var StimulsoftSourceName;
(function (StimulsoftSourceName) {
StimulsoftSourceName["DESIGNER_SCRIPT"] = "DESIGNER_SCRIPT";
StimulsoftSourceName["REPORTER_SCRIPT"] = "REPORTER_SCRIPT";
StimulsoftSourceName["VIEWER_SCRIPT"] = "VIEWER_SCRIPT";
StimulsoftSourceName["DESIGNER_STYLE"] = "DESIGNER_STYLE";
StimulsoftSourceName["VIEWER_STYLE"] = "VIEWER_STYLE";
})(StimulsoftSourceName || (StimulsoftSourceName = {}));
class StimulsoftService {
stimulsoftConfig;
designerCssUrl;
viewerCssUrl;
designerJsUrl;
reportsJsUrl;
viewerJsUrl;
options;
fonts;
baseUrl;
localizationPath;
constructor(stimulsoftConfig) {
this.stimulsoftConfig = stimulsoftConfig;
const { designerCssUrl, viewerCssUrl, designerJsUrl, reportsJsUrl, viewerJsUrl, options, fonts, baseUrl, localizationPath } = stimulsoftConfig;
this.designerCssUrl = designerCssUrl;
this.viewerCssUrl = viewerCssUrl;
this.designerJsUrl = designerJsUrl || initialStimulsoftConfig.designerJsUrl;
this.reportsJsUrl = reportsJsUrl || initialStimulsoftConfig.reportsJsUrl;
this.viewerJsUrl = viewerJsUrl || initialStimulsoftConfig.viewerJsUrl;
this.localizationPath = localizationPath;
this.options = options;
this.fonts = fonts;
this.baseUrl = baseUrl;
}
get stimulsoftSourceStore() {
const { STYLE, SCRIPT } = SourceType;
const sources = [
[StimulsoftSourceName.DESIGNER_SCRIPT, this.designerJsUrl, SCRIPT],
[StimulsoftSourceName.REPORTER_SCRIPT, this.reportsJsUrl, SCRIPT],
[StimulsoftSourceName.VIEWER_SCRIPT, this.viewerJsUrl, SCRIPT],
];
if (this.designerCssUrl) {
sources.push([StimulsoftSourceName.DESIGNER_STYLE, this.designerCssUrl, STYLE]);
}
if (this.viewerCssUrl) {
sources.push([StimulsoftSourceName.VIEWER_STYLE, this.viewerCssUrl, STYLE]);
}
return sources
.map(([name, url, type]) => (Array.isArray(url) ? url : [url]).map(u => new Source(name, u, type)))
.reduce((resources, item) => [...resources, ...item], []);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftService, deps: [{ token: config }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftService });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: undefined, decorators: [{
type: Inject,
args: [config]
}] }] });
function configFactory(initConfig, configValue) {
return {
...initConfig,
...(typeof configValue === 'function' ? configValue() : (configValue ?? {})),
};
}
function provideStimulsoft(configValue) {
return makeEnvironmentProviders([
NgxSourceService,
{ provide: NEW_CONFIG, useValue: configValue },
{ provide: INITIAL_CONFIG, useValue: initialStimulsoftConfig },
{
provide: config,
useFactory: configFactory,
deps: [INITIAL_CONFIG, NEW_CONFIG],
},
StimulsoftService,
]);
}
class NgxStimulsoftModule {
static forRoot(configValue) {
return {
ngModule: NgxStimulsoftModule,
providers: [provideStimulsoft(configValue)],
};
}
static forChild() {
return {
ngModule: NgxStimulsoftModule,
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxStimulsoftModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: NgxStimulsoftModule });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxStimulsoftModule });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxStimulsoftModule, decorators: [{
type: NgModule,
args: [{
declarations: [],
exports: [],
providers: [],
}]
}] });
let lastId = 0;
const UniqueComponentId = (prefix = 'stimulsoft_id_') => `${prefix}${++lastId}`;
class StimulsoftDesignerComponent {
id = input(UniqueComponentId());
licenseKey = input();
licenseFile = input();
local = input('en');
onLoadResourceFailed = output();
loading = output();
sourceService = inject(NgxSourceService);
stimulsoftService = inject(StimulsoftService);
isLoadedSource = signal(false);
failResourcesDownload = signal(false);
resourcesReady = computed(() => this.isLoadedSource() && !this.failResourcesDownload());
constructor() {
this.sourceService.addSources(...this.stimulsoftService.stimulsoftSourceStore);
this.loadResources().then();
effect(() => {
if (this.resourcesReady()) {
// eslint-disable-next-line
const dependencies = [this.id(), this.licenseKey(), this.licenseFile(), this.local()];
this.initializeDesigner(this.id());
}
});
}
async loadResources() {
this.loading.emit(true);
const sources = [StimulsoftSourceName.REPORTER_SCRIPT, StimulsoftSourceName.VIEWER_SCRIPT, StimulsoftSourceName.DESIGNER_SCRIPT];
if (this.stimulsoftService.viewerCssUrl) {
sources.push(StimulsoftSourceName.VIEWER_STYLE);
}
if (this.stimulsoftService.designerCssUrl) {
sources.push(StimulsoftSourceName.DESIGNER_STYLE);
}
try {
await this.sourceService.loadBySourceNames(...sources);
this.isLoadedSource.set(true);
}
catch (error) {
this.failResourcesDownload.set(true);
this.onLoadResourceFailed.emit({ source: 'stimulsoft', error });
}
finally {
this.loading.emit(false);
}
}
initializeDesigner(id) {
const container = document.getElementById(id);
if (!container) {
console.warn(`Container with id "${id}" not found.`);
return;
}
this.applyLicense();
const options = new Stimulsoft.Designer.StiDesignerOptions();
const designer = new Stimulsoft.Designer.StiDesigner(options, 'StiDesigner', false);
designer.report = new Stimulsoft.Report.StiReport();
this.applyLocalization();
designer.renderHtml(id);
}
applyLocalization() {
if (!this.stimulsoftService.localizationPath) {
return;
}
const stiLocalization = Stimulsoft.Base.Localization.StiLocalization;
const local = this.local();
if (local === 'en') {
stiLocalization.cultureName = 'English';
return;
}
const path = `${this.stimulsoftService.localizationPath.replace(/\/$/g, '')}/${local}.xml`;
stiLocalization.cultureName = stiLocalization.loadLocalizationFile(path);
}
applyLicense() {
const key = this.licenseKey() ?? this.stimulsoftService.stimulsoftConfig.licenseKey;
const file = this.licenseFile() ?? this.stimulsoftService.stimulsoftConfig.licenseFile;
if (key) {
Stimulsoft.Base.StiLicense.Key = key;
}
else if (file) {
Stimulsoft.Base.StiLicense.loadFromFile(file);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftDesignerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "18.2.13", type: StimulsoftDesignerComponent, isStandalone: true, selector: "ngx-stimulsoft-designer", inputs: { id: { classPropertyName: "id", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, licenseKey: { classPropertyName: "licenseKey", publicName: "licenseKey", isSignal: true, isRequired: false, transformFunction: null }, licenseFile: { classPropertyName: "licenseFile", publicName: "licenseFile", isSignal: true, isRequired: false, transformFunction: null }, local: { classPropertyName: "local", publicName: "local", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onLoadResourceFailed: "onLoadResourceFailed", loading: "loading" }, ngImport: i0, template: ` <div dir="ltr" [id]="id()"></div>`, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftDesignerComponent, decorators: [{
type: Component,
args: [{
selector: 'ngx-stimulsoft-designer',
template: ` <div dir="ltr" [id]="id()"></div>`,
standalone: true,
}]
}], ctorParameters: () => [] });
class StimulsoftViewerComponent {
stimulsoftService = inject(StimulsoftService);
sourceService = inject(NgxSourceService);
document = inject(DOCUMENT);
id = input(UniqueComponentId());
baseUrl = input(this.stimulsoftService.baseUrl);
fileName = input.required();
fonts = input(this.stimulsoftService.fonts);
dataSet = input.required();
options = input(this.stimulsoftService.options);
licenseKey = input();
licenseFile = input();
local = input('en');
dir = input('ltr');
ready = output();
loading = output();
onLoadResourceFailed = output();
viewerOptions;
viewer;
report;
isLoadedSource = signal(false);
failResourcesDownload = signal(false);
canInitialize = computed(() => this.isLoadedSource() && !this.failResourcesDownload() && !!this.fileName() && !!this.dataSet());
constructor() {
this.sourceService.addSources(...this.stimulsoftService.stimulsoftSourceStore);
this.loadResources().then();
effect(() => {
if (this.canInitialize()) {
// eslint-disable-next-line
const dependencies = [
this.options(),
this.fonts(),
this.dataSet(),
this.fileName(),
this.id(),
this.licenseKey(),
this.licenseFile(),
this.local(),
this.dir(),
];
this.initializeViewer();
}
});
}
async loadResources() {
this.loading.emit(true);
const sources = [StimulsoftSourceName.REPORTER_SCRIPT, StimulsoftSourceName.VIEWER_SCRIPT];
if (this.stimulsoftService.viewerCssUrl) {
sources.push(StimulsoftSourceName.VIEWER_STYLE);
}
try {
await this.sourceService.loadBySourceNames(...sources);
this.isLoadedSource.set(true);
}
catch (error) {
this.failResourcesDownload.set(true);
this.onLoadResourceFailed.emit({ source: 'stimulsoft', error });
}
finally {
this.loading.emit(false);
}
}
initializeViewer() {
if (typeof Stimulsoft === 'undefined') {
this.onLoadResourceFailed.emit({ error: 'Stimulsoft undefined', source: 'global' });
return;
}
this.ready.emit(false);
this.viewerOptions = new Stimulsoft.Viewer.StiViewerOptions();
this.viewerOptions.appearance.rightToLeft = this.dir() === 'rtl';
this.viewer = new Stimulsoft.Viewer.StiViewer(this.viewerOptions, 'StiViewer', false);
this.report = new Stimulsoft.Report.StiReport();
this.applyOptions();
this.setupReport();
}
applyOptions() {
const options = this.options();
this.deepMerge(this.stimulsoftService.options, options);
this.deepMerge(options, this.viewerOptions);
}
deepMerge(source, target) {
if (!source || !target) {
return;
}
for (const [key, value] of Object.entries(source)) {
if (typeof value === 'object' && value !== null) {
this.deepMerge(value, (target[key] ??= {}));
}
else {
target[key] = value;
}
}
}
setupReport() {
if (!this.viewerOptions || !this.viewer || !this.report) {
return;
}
this.applyFonts();
this.loadReportFile();
this.registerDataSet();
this.applyLicense();
this.setLocale();
const targetElement = this.document.getElementById(this.id());
if (!targetElement) {
console.warn(`Element with id "${this.id()}" not found.`);
return;
}
this.ready.emit(true);
this.report.renderAsync(() => {
this.viewer.report = this.report;
this.viewer.renderHtml(this.id());
});
}
applyFonts() {
const fonts = this.fonts();
if (!fonts) {
return;
}
for (const [name, url] of Object.entries(fonts)) {
try {
Stimulsoft.Base.StiFontCollection.addFontFile(url, name);
}
catch {
console.error(`Can't load font: ${url}`);
}
}
}
registerDataSet() {
const json = this.dataSet();
if (!json) {
return;
}
const dataSet = new Stimulsoft.System.Data.DataSet('DataSet');
dataSet.readJson(JSON.stringify(json));
this.report.dictionary.databases.clear();
this.report.regData('DataSet', 'DataSet', dataSet);
}
applyLicense() {
const key = this.licenseKey() ?? this.stimulsoftService.stimulsoftConfig.licenseKey;
const file = this.licenseFile() ?? this.stimulsoftService.stimulsoftConfig.licenseFile;
if (key) {
Stimulsoft.Base.StiLicense.Key = key;
}
else if (file) {
Stimulsoft.Base.StiLicense.loadFromFile(file);
}
}
loadReportFile() {
const name = this.fileName();
if (!name) {
return;
}
const base = this.baseUrl() ?? '';
this.report.loadFile(base + name);
}
setLocale() {
if (!this.stimulsoftService.localizationPath) {
return;
}
const local = this.local();
const stiLocalization = Stimulsoft.Base.Localization.StiLocalization;
if (local === 'en') {
stiLocalization.cultureName = 'English';
}
else {
const path = [this.stimulsoftService.localizationPath.replace(/\/$/g, ''), `${local}.xml`].join('/');
stiLocalization.cultureName = stiLocalization.loadLocalizationFile(path);
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftViewerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "18.2.13", type: StimulsoftViewerComponent, isStandalone: true, selector: "ngx-stimulsoft-viewer", inputs: { id: { classPropertyName: "id", publicName: "id", isSignal: true, isRequired: false, transformFunction: null }, baseUrl: { classPropertyName: "baseUrl", publicName: "baseUrl", isSignal: true, isRequired: false, transformFunction: null }, fileName: { classPropertyName: "fileName", publicName: "fileName", isSignal: true, isRequired: true, transformFunction: null }, fonts: { classPropertyName: "fonts", publicName: "fonts", isSignal: true, isRequired: false, transformFunction: null }, dataSet: { classPropertyName: "dataSet", publicName: "dataSet", isSignal: true, isRequired: true, transformFunction: null }, options: { classPropertyName: "options", publicName: "options", isSignal: true, isRequired: false, transformFunction: null }, licenseKey: { classPropertyName: "licenseKey", publicName: "licenseKey", isSignal: true, isRequired: false, transformFunction: null }, licenseFile: { classPropertyName: "licenseFile", publicName: "licenseFile", isSignal: true, isRequired: false, transformFunction: null }, local: { classPropertyName: "local", publicName: "local", isSignal: true, isRequired: false, transformFunction: null }, dir: { classPropertyName: "dir", publicName: "dir", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { ready: "ready", loading: "loading", onLoadResourceFailed: "onLoadResourceFailed" }, ngImport: i0, template: ` <div dir="ltr" [id]="id()"></div>`, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: StimulsoftViewerComponent, decorators: [{
type: Component,
args: [{
selector: 'ngx-stimulsoft-viewer',
template: ` <div dir="ltr" [id]="id()"></div>`,
standalone: true,
}]
}], ctorParameters: () => [] });
/**
* Generated bundle index. Do not edit.
*/
export { INITIAL_CONFIG, NEW_CONFIG, NgxStimulsoftModule, StimulsoftDesignerComponent, StimulsoftService, StimulsoftSourceName, StimulsoftViewerComponent, config, initialStimulsoftConfig, provideStimulsoft };
//# sourceMappingURL=ngx-stimulsoft.mjs.map