@halloverden/ngx-zendesk-web-widget
Version:
Angular wrapper for the Zendesk Web Widget v1 (classic)
203 lines (194 loc) • 8.47 kB
JavaScript
import * as i0 from '@angular/core';
import { PLATFORM_ID, Injectable, Inject, NgModule } from '@angular/core';
import { firstValueFrom, of, Subject } from 'rxjs';
import { isPlatformBrowser, DOCUMENT } from '@angular/common';
class NgxZendeskWebWidgetConfig {
}
function ngxZendeskWebWidgetFactory(ngxZendeskWebWidgetService) {
return () => {
return ngxZendeskWebWidgetService.init();
};
}
class WindowService {
constructor(platform) {
this.platform = platform;
}
get() {
if (isPlatformBrowser(this.platform)) {
return window;
}
return null;
}
}
WindowService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: WindowService, deps: [{ token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
WindowService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: WindowService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: WindowService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }];
} });
class NgxZendeskWebWidgetService {
constructor(config, windowService, _document) {
this.config = config;
this.windowService = windowService;
this._document = _document;
this._initialized = false;
this._verifyConfig();
if (!this.config.lazyLoadZendeskScripts) {
this.init().then(() => {
// noop
});
}
}
init() {
if (!this.windowService.get()) {
// noop
console.warn('NgxZendeskWebWidgetService.init(): Tried initializing Zendesk Web Widget, but no browser window was found.');
return firstValueFrom(of(false));
}
const w = this.windowService.get();
this._window = w;
let documentDomain = this._document.domain;
const config = this.config;
const injectionElement = this.getInjectionElement();
const iFrameElement = this.buildIframeElement();
const scriptElement = this.buildScriptElement();
w.zEmbed ||
(function (e, t) {
let args = [];
w.zEmbed = function () {
args.push(arguments);
};
w.zE = w.zE || w.zEmbed;
w.zESettings = config.zESettings;
iFrameElement.addEventListener('load', (ev) => {
const iframeElementFromEvent = ev.target;
if (iframeElementFromEvent instanceof HTMLIFrameElement) {
const iframeDocumentInstance = iframeElementFromEvent.contentDocument;
const iframeWindowInstance = iframeElementFromEvent.contentWindow;
let i = setInterval(() => {
if ((iframeWindowInstance === null || iframeWindowInstance === void 0 ? void 0 : iframeWindowInstance.document.body) &&
iframeDocumentInstance) {
clearInterval(i);
iframeDocumentInstance.domain = documentDomain;
iframeDocumentInstance.t = new Date();
iframeDocumentInstance.zendeskHost = config.accountUrl;
iframeDocumentInstance.zEQueue = args;
iframeDocumentInstance.body.appendChild(scriptElement);
}
});
}
});
setTimeout(() => {
injectionElement.appendChild(iFrameElement);
});
})();
return this.finishLoading();
}
getInjectionElement() {
let elements = this._document.getElementsByTagName(this.config.injectionTag);
if (elements.length === 0) {
throw new Error('No element found matching injectionTag from config');
}
return elements[0];
}
finishLoading() {
const o = new Subject();
const timeout = setTimeout(() => {
this._initialized = false;
o.next(false);
o.complete();
}, this.config.timeout || 30000);
this._window.zE(() => {
clearTimeout(timeout);
this._zE = this._window.zE;
this._initialized = true;
this.config.callback(this);
o.next(true);
o.complete();
});
return firstValueFrom(o);
}
buildIframeElement() {
const iframe = this._document.createElement('iframe');
iframe.src = 'javascript:false';
iframe.title = 'Zendesk Web Widget';
iframe.style.cssText = 'display: none';
return iframe;
}
buildScriptElement() {
const script = this._document.createElement('script');
script.type = 'text/javascript';
script.id =
'be-sure-not-to-set-the-id-to-ze-snippet-as-that-changes-the-behavior-of-the-snippet';
script.src = 'https://static.zdassets.com/ekr/snippet.js';
script.async = true;
return script;
}
_verifyConfig() {
if (!this.config.accountUrl) {
throw new Error('NgxZendeskWebWidgetService: NgxZendeskWebWidgetConfig is missing accountUrl.');
}
}
get isInitialized() {
return this._initialized;
}
get zE() {
return this._zE;
}
commandWebWidget(command, options) {
this.zE('webWidget', command, options);
}
onWebWidgetEvent(eventName, handler) {
this.zE('webWidget:on', eventName, handler);
}
}
NgxZendeskWebWidgetService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetService, deps: [{ token: NgxZendeskWebWidgetConfig }, { token: WindowService }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable });
NgxZendeskWebWidgetService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () {
return [{ type: NgxZendeskWebWidgetConfig }, { type: WindowService }, { type: Document, decorators: [{
type: Inject,
args: [DOCUMENT]
}] }];
} });
class NgxZendeskWebWidgetModule {
static forRoot(zendeskConfig) {
return {
ngModule: NgxZendeskWebWidgetModule,
providers: [
{ provide: NgxZendeskWebWidgetConfig, useClass: zendeskConfig },
{
provide: NgxZendeskWebWidgetService,
useClass: NgxZendeskWebWidgetService
},
{ provide: WindowService, useClass: WindowService }
]
};
}
}
NgxZendeskWebWidgetModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgxZendeskWebWidgetModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetModule });
NgxZendeskWebWidgetModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.2.3", ngImport: i0, type: NgxZendeskWebWidgetModule, decorators: [{
type: NgModule,
args: [{}]
}] });
/*
* Public API Surface of ngx-zendesk-web-widget
*/
/**
* Generated bundle index. Do not edit.
*/
export { NgxZendeskWebWidgetConfig, NgxZendeskWebWidgetModule, NgxZendeskWebWidgetService, ngxZendeskWebWidgetFactory };
//# sourceMappingURL=halloverden-ngx-zendesk-web-widget.mjs.map