announcekit-angular
Version:
Use AnnounceKit widgets in your Angular apps
236 lines (230 loc) • 9.64 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Component, ViewChild, Input, Output, NgModule } from '@angular/core';
import * as i1 from '@angular/common';
import { CommonModule } from '@angular/common';
class AnnouncekitComponent {
constructor(ngZone) {
this.ngZone = ngZone;
this.onWidgetOpen = new EventEmitter();
this.onWidgetClose = new EventEmitter();
this.onWidgetUnread = new EventEmitter();
this.onWidgetReady = new EventEmitter();
this.propsValid = true;
this.widgetHandlers = [];
this.widgetHandlers = [];
this.ngZone.runOutsideAngular(() => {
if (!window[`announcekit`]) {
window[`announcekit`] = window[`announcekit`] || {
queue: [],
push(x) {
window[`announcekit`].queue.push(x);
},
on(n, x) {
window[`announcekit`].queue.push([n, x]);
}
};
let scripttag;
scripttag = document.createElement('script');
scripttag.async = true;
scripttag.src = `https://cdn.announcekit.app/widget-v2.js`;
let scr;
scr = document.getElementsByTagName('script')[0];
scr.parentNode.insertBefore(scripttag, scr);
}
});
}
set user(value) {
if (this.isString(value) || this.isString(this._user)) {
this.propsValid = false;
this._user = undefined;
}
else {
if (!value) {
this._user = undefined;
}
else {
this._user = Object.assign({}, value);
}
}
}
set data(value) {
if (this.isString(value) || this.isString(this._data)) {
this.propsValid = false;
this._data = undefined;
}
else {
if (!value) {
this._data = undefined;
}
else {
this._data = Object.assign({}, value);
}
}
}
get user() {
return this._user;
}
get data() {
return this._data;
}
ngOnChanges() {
if (this.propsValid) {
this.loaded();
}
this.propsValid = true;
}
loaded() {
const style = this.widgetStyle;
const styleParams = {
badge: {
style
},
line: {
style
},
float: {
style
}
};
if (this.floatWidget) {
delete styleParams.badge;
delete styleParams.line;
}
const name = Math.random()
.toString(36)
.substring(10);
this.ngZone.runOutsideAngular(() => {
window[`announcekit`].push(Object.assign(Object.assign({ widget: this.widget, name, version: 2, framework: 'angular', framework_version: '4.0.0', embed: !!this.embedWidget, data: this.data, user: this.user, lang: this.lang, labels: this.labels, user_token: this.userToken, selector: this.elementRef.nativeElement, boosters: typeof this.boosters === 'undefined' ? true : this.boosters }, styleParams), { onInit: (initWidget) => {
if (initWidget.conf.name !== name) {
return initWidget.destroy();
}
const ann = window[`announcekit`];
this.widgetInstance = initWidget;
this.barBooster = ann.boosters.bar;
this.modalBooster = ann.boosters.modal;
this.widgetHandlers.forEach((h) => h(initWidget));
this.widgetHandlers = [];
if (this.onWidgetUnread) {
this.onWidgetUnread.emit(this.widgetInstance.state.ui.unreadCount);
}
ann.on('widget-open', ({ widget }) => {
if (widget === initWidget && this.onWidgetOpen) {
this.onWidgetOpen.emit({ widget });
}
});
ann.on('widget-close', ({ widget }) => {
if (widget === initWidget && this.onWidgetClose) {
this.onWidgetClose.emit({ widget });
}
});
ann.on('widget-ready', ({ widget }) => {
if (widget === initWidget && this.onWidgetReady) {
this.onWidgetReady.emit({ widget });
}
});
} }));
});
}
withWidget(fn) {
return new Promise(res => {
if (this.widgetInstance) {
return res(fn(this.widgetInstance));
}
else {
this.widgetHandlers.push((widget) => {
res(fn(widget));
});
}
});
}
unread() {
return this.withWidget((widget) => widget.state.ui.unreadCount);
}
instance() {
return this.withWidget((widget) => widget);
}
open() {
this.withWidget((widget) => widget.open());
}
close() {
this.withWidget((widget) => widget.close());
}
isString(obj) {
return obj !== undefined && obj !== null && obj.constructor === String;
}
ngAfterViewInit() {
if (!(this.user || this.data)) {
this.loaded();
}
}
}
AnnouncekitComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitComponent, deps: [{ token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component });
AnnouncekitComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.2.7", type: AnnouncekitComponent, selector: "announcekit", inputs: { widget: "widget", user: "user", data: "data", lang: "lang", floatWidget: "floatWidget", embedWidget: "embedWidget", boosters: "boosters", widgetStyle: "widgetStyle", userToken: "userToken", labels: "labels" }, outputs: { onWidgetOpen: "onWidgetOpen", onWidgetClose: "onWidgetClose", onWidgetUnread: "onWidgetUnread", onWidgetReady: "onWidgetReady" }, viewQueries: [{ propertyName: "elementRef", first: true, predicate: ["ankRef"], descendants: true, static: true }], usesOnChanges: true, ngImport: i0, template: "<div #ankRef style=\"display: inline\" [ngClass]=\"className\">\r\n <ng-content></ng-content>\r\n</div>", dependencies: [{ kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitComponent, decorators: [{
type: Component,
args: [{ selector: 'announcekit', template: "<div #ankRef style=\"display: inline\" [ngClass]=\"className\">\r\n <ng-content></ng-content>\r\n</div>" }]
}], ctorParameters: function () { return [{ type: i0.NgZone }]; }, propDecorators: { elementRef: [{
type: ViewChild,
args: ['ankRef', { static: true }]
}], widget: [{
type: Input
}], user: [{
type: Input,
args: ['user']
}], data: [{
type: Input,
args: ['data']
}], lang: [{
type: Input
}], floatWidget: [{
type: Input
}], embedWidget: [{
type: Input
}], boosters: [{
type: Input
}], widgetStyle: [{
type: Input
}], userToken: [{
type: Input
}], labels: [{
type: Input
}], onWidgetOpen: [{
type: Output,
args: ['onWidgetOpen']
}], onWidgetClose: [{
type: Output,
args: ['onWidgetClose']
}], onWidgetUnread: [{
type: Output,
args: ['onWidgetUnread']
}], onWidgetReady: [{
type: Output,
args: ['onWidgetReady']
}] } });
class AnnouncekitModule {
}
AnnouncekitModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
AnnouncekitModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitModule, declarations: [AnnouncekitComponent], imports: [CommonModule], exports: [AnnouncekitComponent] });
AnnouncekitModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitModule, imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.7", ngImport: i0, type: AnnouncekitModule, decorators: [{
type: NgModule,
args: [{
declarations: [
AnnouncekitComponent
],
imports: [
CommonModule
],
exports: [
AnnouncekitComponent
]
}]
}] });
/*
* Public API Surface of announcekit-angular
*/
/**
* Generated bundle index. Do not edit.
*/
export { AnnouncekitComponent, AnnouncekitModule };
//# sourceMappingURL=announcekit-angular.mjs.map