import {AfterViewChecked, Component, OnInit} from '@angular/core';
import {Title} from '@angular/platform-browser';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'resources',
  templateUrl: 'resources.component.html',
  styleUrls: ['resources.component.css'],
})
export class ResourcesComponent implements OnInit {
  
  constructor(private title: Title,
              private route: ActivatedRoute) {
  }
  
  ngOnInit() {
    this.title.setTitle('OpenAIRE - UsageCounts | Resources');
    this.route.fragment.subscribe(fragment => {
      setTimeout(() => {
        this.goTo(fragment);
      }, 100);
    });
  }
  
  goTo(id: string) {
    const yOffset = -100;
    const element = document.getElementById(id);
    if(element) {
      const y = element.getBoundingClientRect().top + window.pageYOffset + yOffset;
      window.scrollTo({top: y, behavior: 'smooth'});
    }
  }
}
