2023-10-27 08:25:01 +02:00
|
|
|
import {Directive, OnDestroy} from "@angular/core";
|
2023-11-02 15:39:14 +01:00
|
|
|
import {BehaviorSubject, Subscriber} from "rxjs";
|
2023-10-27 08:25:01 +02:00
|
|
|
import {EnvProperties} from "../../utils/properties/env-properties";
|
|
|
|
import {properties} from "src/environments/environment";
|
|
|
|
import {PiwikService} from "../../utils/piwik/piwik.service";
|
|
|
|
import {Meta, Title} from "@angular/platform-browser";
|
|
|
|
import {SEOService} from "../SEO/SEO.service";
|
2023-11-02 15:39:14 +01:00
|
|
|
import {ActivatedRoute, Data, NavigationEnd, Params, Router} from "@angular/router";
|
2023-10-27 08:25:01 +02:00
|
|
|
|
|
|
|
@Directive()
|
|
|
|
export abstract class BaseComponent implements OnDestroy {
|
|
|
|
public properties: EnvProperties = properties;
|
2023-11-02 15:39:14 +01:00
|
|
|
/** Router params */
|
|
|
|
protected paramsResolved: boolean = false;
|
|
|
|
protected params: BehaviorSubject<Params>;
|
|
|
|
protected data: BehaviorSubject<Data>;
|
2023-10-27 08:25:01 +02:00
|
|
|
protected subscriptions: any[] = [];
|
|
|
|
/** Metadata */
|
|
|
|
public title: string;
|
|
|
|
public description: string;
|
2023-11-02 15:39:14 +01:00
|
|
|
protected _route: ActivatedRoute;
|
2023-10-27 08:25:01 +02:00
|
|
|
protected _piwikService: PiwikService;
|
|
|
|
protected _meta: Meta;
|
|
|
|
protected seoService: SEOService;
|
|
|
|
protected _title: Title;
|
|
|
|
protected _router: Router;
|
|
|
|
|
|
|
|
protected constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.subscriptions.forEach(subscription => {
|
2023-11-02 15:39:14 +01:00
|
|
|
if (subscription instanceof Subscriber) {
|
2023-10-27 08:25:01 +02:00
|
|
|
subscription.unsubscribe()
|
2023-11-02 15:39:14 +01:00
|
|
|
} else if (subscription instanceof Function) {
|
2023-10-27 08:25:01 +02:00
|
|
|
subscription();
|
2023-11-02 15:39:14 +01:00
|
|
|
} else if (typeof IntersectionObserver !== 'undefined' && subscription instanceof IntersectionObserver) {
|
2023-10-27 08:25:01 +02:00
|
|
|
subscription.disconnect();
|
2023-11-02 15:39:14 +01:00
|
|
|
} else if (typeof ResizeObserver !== 'undefined' && subscription instanceof ResizeObserver) {
|
2023-10-27 08:25:01 +02:00
|
|
|
subscription.disconnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:39:14 +01:00
|
|
|
/**
|
|
|
|
* Initialize router params and data (should be called in the constructor)
|
|
|
|
* */
|
|
|
|
initRouterParams(route: ActivatedRoute = null) {
|
|
|
|
if (route) {
|
|
|
|
this.params = new BehaviorSubject<Params>(null);
|
|
|
|
this.data = new BehaviorSubject<Data>(null);
|
|
|
|
this.subscriptions.push(this._router.events.subscribe(event => {
|
|
|
|
if (event instanceof NavigationEnd) {
|
|
|
|
let r = route;
|
|
|
|
while (r.firstChild) {
|
|
|
|
r = r.firstChild;
|
|
|
|
}
|
|
|
|
this.paramsResolved = true;
|
|
|
|
this.params.next(r.snapshot.params);
|
|
|
|
this.data.next(r.snapshot.data);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 08:25:01 +02:00
|
|
|
public setMetadata() {
|
2023-11-02 15:39:14 +01:00
|
|
|
if (this._title && this.title) {
|
|
|
|
this.title = (this._route?.snapshot.data?.title ? this._route.snapshot.data?.title : '') + this.title;
|
|
|
|
this._title.setTitle(this.title);
|
|
|
|
}
|
|
|
|
if (this._router) {
|
|
|
|
const url = properties.domain + properties.baseLink + this._router.url;
|
|
|
|
if (this.seoService) {
|
|
|
|
this.seoService.createLinkForCanonicalURL(url, false);
|
|
|
|
}
|
|
|
|
if (this._meta) {
|
|
|
|
this._meta.updateTag({content: url}, "property='og:url'");
|
|
|
|
this._meta.updateTag({content: this.description}, "name='description'");
|
|
|
|
this._meta.updateTag({content: this.description}, "property='og:description'");
|
|
|
|
this._meta.updateTag({content: this.title}, "property='og:title'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this._piwikService) {
|
|
|
|
this.subscriptions.push(this._piwikService.trackView(properties, this.title).subscribe());
|
|
|
|
}
|
2023-10-27 08:25:01 +02:00
|
|
|
}
|
|
|
|
}
|