ngx-headway
Version:
This is an Angular library for using the Changelog service Headway, as a simple directive.
92 lines (80 loc) • 2.5 kB
text/typescript
import {
Directive,
ElementRef,
EventEmitter,
Inject,
OnDestroy,
OnInit,
Output,
} from "@angular/core";
import { HEADWAY_ACCOUNT_TOKEN } from "../tokens";
export interface HeadwayWidgetConfig {
headwayAccountId: string;
}
const defaultHeadwayWidgetSelector = "HW_widget_component";
({
selector: "[headwayBadge]",
standalone: true,
})
export class HeadwayBadgeDirective implements OnInit, OnDestroy {
private elementRef: HTMLElement;
private badgePosition: string = "right";
private translations = {};
() widgetReady = new EventEmitter<boolean>();
() showWidget = new EventEmitter<boolean>();
() showDetails = new EventEmitter<boolean>();
() readMore = new EventEmitter<boolean>();
() hideWidget = new EventEmitter<boolean>();
private widget: any;
constructor(
private el: ElementRef<HTMLElement>,
(HEADWAY_ACCOUNT_TOKEN) private headwayAccountId: string
) {
this.elementRef = el.nativeElement;
if (
this.elementRef.classList.length === 0 ||
this.elementRef.classList === undefined ||
this.elementRef.classList.contains(defaultHeadwayWidgetSelector)
) {
this.elementRef.classList.add(defaultHeadwayWidgetSelector);
}
}
initHeadway() {
const hwConfig = {
selector: `.${defaultHeadwayWidgetSelector}`,
account: this.headwayAccountId,
callbacks: {
onWidgetReady: (widget: boolean) => {
this.widgetReady.emit(widget);
},
onShowWidget: () => this.showWidget.emit(),
onShowDetails: (changelog: boolean) => {
this.showDetails.emit(changelog);
},
onReadMore: (changelog: boolean) => this.readMore.emit(changelog),
onHideWidget: () => this.hideWidget.emit(),
},
badgePosition: this.badgePosition,
translations: this.translations,
};
this.widget = (window as any).Headway.getNewWidget();
this.widget.init(hwConfig);
}
ngOnDestroy(): void {
this.widget?.destroy();
}
ngOnInit(): void {
const head = document.getElementsByTagName("head")[0];
if ((window as any).Headway) {
head.removeChild(document.getElementById("HWScript")!);
}
const script = document.createElement("script");
script.type = "text/javascript";
script.id = "HWScript";
script.onload = () => {
this.initHeadway();
};
script.src = "https://cdn.headwayapp.co/widget.js";
head.appendChild(script);
}
}