2022-03-14 17:35:00 +01:00
|
|
|
import {ActivationStart, NavigationEnd, Router} from '@angular/router';
|
2022-03-17 12:28:25 +01:00
|
|
|
import {Injectable, Type} from '@angular/core';
|
2021-04-16 13:07:43 +02:00
|
|
|
import {Subscription} from 'rxjs';
|
2022-12-09 17:17:06 +01:00
|
|
|
import {LayoutService} from "../dashboard/sharedComponents/sidebar/layout.service";
|
2021-04-12 23:55:09 +02:00
|
|
|
|
|
|
|
@Injectable({
|
2021-04-16 13:07:43 +02:00
|
|
|
providedIn: 'root'
|
2021-04-12 23:55:09 +02:00
|
|
|
})
|
|
|
|
export class SmoothScroll {
|
|
|
|
private interval;
|
|
|
|
private readonly sub;
|
2022-03-14 17:35:00 +01:00
|
|
|
private lastComponent;
|
2022-03-17 12:28:25 +01:00
|
|
|
private currentComponent: string;
|
2022-10-12 11:23:24 +02:00
|
|
|
private extraOffset: number = 0;
|
2021-04-12 23:55:09 +02:00
|
|
|
|
2022-12-09 17:17:06 +01:00
|
|
|
constructor(private router: Router, private layoutService: LayoutService) {
|
2022-03-14 17:35:00 +01:00
|
|
|
if (typeof window !== "undefined") {
|
2021-04-23 18:25:52 +02:00
|
|
|
this.sub = router.events.subscribe(event => {
|
2022-03-14 17:35:00 +01:00
|
|
|
if (event instanceof ActivationStart) {
|
2022-10-12 11:23:24 +02:00
|
|
|
this.extraOffset = event.snapshot.data.extraOffset?event.snapshot.data.extraOffset:0;
|
2022-03-17 12:28:25 +01:00
|
|
|
if(event.snapshot.component instanceof Type) {
|
|
|
|
this.currentComponent = event.snapshot.component.name;
|
|
|
|
}
|
2022-03-14 17:35:00 +01:00
|
|
|
} else if (event instanceof NavigationEnd) {
|
2022-12-09 17:17:06 +01:00
|
|
|
let headerOffset = (this.layoutService.isMobileValue?0:Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--header-height'))) + 35;
|
2022-10-12 11:23:24 +02:00
|
|
|
if(!this.router.getCurrentNavigation().extras?.state?.disableScroll) {
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
const fragment = router.parseUrl(router.url).fragment;
|
|
|
|
if (this.lastComponent !== this.currentComponent) {
|
|
|
|
window.scrollTo({top: 0});
|
|
|
|
}
|
|
|
|
if (fragment) {
|
|
|
|
let i = 0;
|
|
|
|
this.interval = setInterval(() => {
|
|
|
|
i++;
|
|
|
|
const element = document.getElementById(fragment);
|
|
|
|
if (element) {
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
2022-10-24 15:37:05 +02:00
|
|
|
const yOffset = -headerOffset - this.extraOffset;
|
2022-10-12 11:23:24 +02:00
|
|
|
let position = 0;
|
|
|
|
let interval = setInterval(() => {
|
|
|
|
if (position !== element.getBoundingClientRect().top) {
|
|
|
|
position = element.getBoundingClientRect().top;
|
|
|
|
} else {
|
|
|
|
clearInterval(interval);
|
2022-12-09 17:17:06 +01:00
|
|
|
const y = element.getBoundingClientRect().top + window.scrollY + yOffset;
|
2022-10-12 11:23:24 +02:00
|
|
|
window.scrollTo({top: y, behavior: 'smooth'});
|
|
|
|
}
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
if (i > 4 && this.interval) {
|
2021-04-23 18:25:52 +02:00
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
2022-10-12 11:23:24 +02:00
|
|
|
}, 100);
|
|
|
|
} else {
|
|
|
|
setTimeout( () => {
|
|
|
|
window.scrollTo({top: 0, behavior: 'smooth'});
|
|
|
|
}, 0);
|
|
|
|
}
|
2021-04-23 18:25:52 +02:00
|
|
|
}
|
2022-03-14 17:35:00 +01:00
|
|
|
this.lastComponent = this.currentComponent;
|
2021-04-12 23:55:09 +02:00
|
|
|
}
|
2021-04-23 18:25:52 +02:00
|
|
|
});
|
|
|
|
}
|
2021-04-12 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public clearSubscriptions() {
|
2021-04-16 13:07:43 +02:00
|
|
|
if (this.sub && this.sub instanceof Subscription) {
|
2021-04-12 23:55:09 +02:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
2021-04-16 13:07:43 +02:00
|
|
|
if (this.interval) {
|
2021-04-12 23:55:09 +02:00
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|