|
| 1 | +import { |
| 2 | + DOCUMENT, |
| 3 | + isPlatformBrowser, |
| 4 | +} from '@angular/common'; |
| 5 | +import { |
| 6 | + DestroyRef, |
| 7 | + Inject, |
| 8 | + inject, |
| 9 | + Injectable, |
| 10 | + PLATFORM_ID, |
| 11 | +} from '@angular/core'; |
| 12 | +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
| 13 | +import { |
| 14 | + ChildActivationEnd, |
| 15 | + Router, |
| 16 | +} from '@angular/router'; |
| 17 | +import { BehaviorSubject } from 'rxjs'; |
| 18 | +import { |
| 19 | + distinctUntilChanged, |
| 20 | + filter, |
| 21 | + map, |
| 22 | +} from 'rxjs/operators'; |
| 23 | + |
| 24 | +import { environment } from '../../environments/environment'; |
| 25 | + |
| 26 | +@Injectable( { providedIn: 'root' } ) |
| 27 | +export class SocialService { |
| 28 | + private destroyRef = inject(DestroyRef); |
| 29 | + |
| 30 | + private showOnCurrentRouteSubject = new BehaviorSubject(false); |
| 31 | + |
| 32 | + /** |
| 33 | + * Show/hide the social buttons according to the activated route |
| 34 | + */ |
| 35 | + showOnCurrentRoute$ = this.showOnCurrentRouteSubject.asObservable(); |
| 36 | + |
| 37 | + private readonly isSocialEnabled: boolean; |
| 38 | + |
| 39 | + constructor( |
| 40 | + @Inject(PLATFORM_ID) protected platformId: any, |
| 41 | + @Inject(DOCUMENT) private _document: Document, |
| 42 | + private router: Router, |
| 43 | + ) { |
| 44 | + this.isSocialEnabled = isPlatformBrowser(this.platformId) && environment.addToAnyPlugin.socialNetworksEnabled; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Traverse tree from bottom to parent in route definition to get whenever feature is enabled via *showSocialButtons* |
| 49 | + */ |
| 50 | + private activatedRouteDataChanges$ = this.router.events.pipe( |
| 51 | + filter(events => events instanceof ChildActivationEnd), |
| 52 | + map((event: ChildActivationEnd) => event.snapshot), |
| 53 | + map(route => { |
| 54 | + while (route.firstChild) { |
| 55 | + route = route.firstChild; |
| 56 | + } |
| 57 | + return route; |
| 58 | + }), |
| 59 | + filter(route => route.outlet === 'primary'), |
| 60 | + map(route => route.data), |
| 61 | + ); |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns whether the social network buttons are enabled |
| 65 | + */ |
| 66 | + get enabled() { |
| 67 | + return this.isSocialEnabled; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the list of available buttons |
| 72 | + */ |
| 73 | + get configuration() { |
| 74 | + return environment.addToAnyPlugin; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Import the AddToAny JavaScript |
| 79 | + */ |
| 80 | + initializeAddToAnyScript(): any { |
| 81 | + // Initializing the addToAny script |
| 82 | + const script = this._document.createElement('script'); |
| 83 | + script.type = 'text/javascript'; |
| 84 | + script.src = environment.addToAnyPlugin.scriptUrl; |
| 85 | + script.async = true; |
| 86 | + |
| 87 | + // Wait for document to finish grow vertically so that script listener handles properly body height changes |
| 88 | + let lastBodyHeight = 0; |
| 89 | + const documentBody = this._document.body; |
| 90 | + |
| 91 | + const bodyHeightInterval = setInterval(() => { |
| 92 | + const currentBodyHeight = documentBody.getBoundingClientRect().height; |
| 93 | + |
| 94 | + if (currentBodyHeight > lastBodyHeight) { |
| 95 | + lastBodyHeight = currentBodyHeight; |
| 96 | + } else { |
| 97 | + this._document.head.appendChild(script); |
| 98 | + clearInterval(bodyHeightInterval); |
| 99 | + } |
| 100 | + }, 200); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Initialize the Social service. This method must be called only inside app component. |
| 105 | + */ |
| 106 | + initialize() { |
| 107 | + if (!this.enabled) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const showSocialButtons = this.activatedRouteDataChanges$.pipe( |
| 112 | + map(data => data?.showSocialButtons === true), |
| 113 | + distinctUntilChanged(), |
| 114 | + takeUntilDestroyed(this.destroyRef), |
| 115 | + ); |
| 116 | + |
| 117 | + showSocialButtons.subscribe(this.showOnCurrentRouteSubject); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments