ngx-fastcomments
Version:
This is an Angular library for FastComments, a live embedded commenting library.
138 lines (132 loc) • 5.88 kB
JavaScript
import * as i0 from '@angular/core';
import { Component, ChangeDetectionStrategy, Input, ViewChild, NgModule } from '@angular/core';
export * from 'fastcomments-typescript';
var LoadStatus;
(function (LoadStatus) {
LoadStatus[LoadStatus["Started"] = 0] = "Started";
LoadStatus[LoadStatus["ScriptLoaded"] = 1] = "ScriptLoaded";
LoadStatus[LoadStatus["Done"] = 2] = "Done";
LoadStatus[LoadStatus["Error"] = 3] = "Error";
})(LoadStatus || (LoadStatus = {}));
class FastCommentsComponent {
constructor() {
this.state = {
status: LoadStatus.Started
};
}
ngOnInit() {
if (!this.config) {
throw new Error('Configuration is required!Like: <lib-fastcomments [config]="myConfigObject"></lib-fastcomments>' +
' or <lib-fastcomments [config]="{ tenantId: \'demo\' }"></lib-fastcomments>');
}
if (this.state.status === LoadStatus.ScriptLoaded) {
return this.reset();
}
else {
return this.loadInstance();
}
}
ngOnChanges() {
if (this.state.status === LoadStatus.ScriptLoaded) {
return this.reset();
}
}
async insertScript(src, id, parentElement) {
return new Promise((resolve, reject) => {
const script = window.document.createElement('script');
script.async = true;
script.src = src;
script.id = id;
parentElement.appendChild(script);
script.addEventListener('load', resolve);
script.addEventListener('error', reject);
});
}
async loadInstance() {
return new Promise(async (resolve, reject) => {
switch (this.state.status) {
case LoadStatus.Started:
try {
// @ts-ignore
if (window && !window.FastCommentsUI) {
const src = this.config.region === 'eu' ? 'https://cdn-eu.fastcomments.com/js/embed-v2.min.js' : 'https://cdn.fastcomments.com/js/embed-v2.min.js';
await this.insertScript(src, 'fastcomments-widget-script', window.document.body);
}
this.state.status = LoadStatus.ScriptLoaded;
await this.loadInstance();
resolve();
}
catch (e) {
console.error('FastComments Script Load Failure', e);
this.state.status = LoadStatus.Error;
reject();
}
break;
case LoadStatus.ScriptLoaded:
this.instantiateWidget();
this.state.status = LoadStatus.Done;
resolve();
break;
default:
resolve();
break;
}
});
}
reset() {
if (!this.config) {
return;
}
if (this.lastWidgetInstance) {
this.lastWidgetInstance.update(this.config);
}
else {
this.instantiateWidget();
}
}
instantiateWidget() {
const element = this.fastCommentsElement.nativeElement;
if (element) {
// @ts-ignore
this.lastWidgetInstance = window.FastCommentsUI(element, this.config);
}
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: FastCommentsComponent, selector: "lib-fastcomments", inputs: { config: "config" }, viewQueries: [{ propertyName: "fastCommentsElement", first: true, predicate: ["fastCommentsElement"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0, template: `
<div #fastCommentsElement></div>
`, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsComponent, decorators: [{
type: Component,
args: [{ selector: 'lib-fastcomments', template: `
<div #fastCommentsElement></div>
`, changeDetection: ChangeDetectionStrategy.OnPush }]
}], ctorParameters: () => [], propDecorators: { config: [{
type: Input
}], fastCommentsElement: [{
type: ViewChild,
args: ['fastCommentsElement', {
static: true,
}]
}] } });
class FastCommentsModule {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsModule, declarations: [FastCommentsComponent], exports: [FastCommentsComponent] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FastCommentsModule, decorators: [{
type: NgModule,
args: [{
declarations: [FastCommentsComponent],
imports: [],
exports: [FastCommentsComponent]
}]
}] });
/*
* Public API Surface of ngx-fastcomments
*/
/**
* Generated bundle index. Do not edit.
*/
export { FastCommentsComponent, FastCommentsModule };
//# sourceMappingURL=ngx-fastcomments.mjs.map