argos/dmp-frontend/src/app/ui/user-guide/user-guide-content/user-guide-content.componen...

68 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, AfterViewChecked } from '@angular/core';
import { UserGuideService } from '@app/core/services/user-guide/user-guide.service';
import { BaseComponent } from '@common/base/base.component';
import { takeUntil } from 'rxjs/internal/operators/takeUntil';
2020-12-10 14:45:55 +01:00
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
2020-12-10 10:34:01 +01:00
import { LanguageService } from '@app/core/services/language/language.service';
2020-12-11 15:53:22 +01:00
import { SecurityContext } from '@angular/core';
@Component({
2020-12-10 14:45:55 +01:00
selector: 'app-user-guide-content',
templateUrl: './user-guide-content.component.html',
styleUrls: ['./user-guide-content.component.scss']
})
export class UserGuideContentComponent extends BaseComponent implements OnInit, AfterViewChecked {
guideHTML: any;
2020-12-10 14:45:55 +01:00
guideHTMLUrl: SafeResourceUrl;
2020-12-11 15:53:22 +01:00
sanitizedGuideUrl: any;
private scrollEvent: EventListener;
2020-12-10 14:45:55 +01:00
constructor(
private userGuideService: UserGuideService,
private sanitizer: DomSanitizer,
private languageService: LanguageService
2020-12-10 14:45:55 +01:00
) { super(); }
2020-12-10 14:45:55 +01:00
ngOnInit() {
this.scrollEvent = ((ev) => this.scroll(ev));
this.userGuideService.getUserGuide(this.languageService.getCurrentLanguage())
2020-12-10 14:45:55 +01:00
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'text/html' });
this.readBlob(blob);
2020-12-11 15:53:22 +01:00
this.guideHTMLUrl = this.sanitizer.bypassSecurityTrustResourceUrl((window.URL ? URL : webkitURL).createObjectURL(blob));
// this.sanitizedGuideUrl = this.sanitizer.sanitize(SecurityContext.URL, this.guideHTMLUrl);
// console.log(this.guideHTMLUrl);
2020-12-10 14:45:55 +01:00
});
}
2020-12-10 14:45:55 +01:00
ngAfterViewChecked(): void {
this.parse();
2020-12-10 14:45:55 +01:00
}
readBlob(blob: Blob) {
const fr = new FileReader();
fr.onload = ev => {
this.guideHTML = this.sanitizer.bypassSecurityTrustHtml(fr.result as string);
this.parse();
};
fr.readAsText(blob);
}
scroll(ev: Event) {
document.getElementById((ev.srcElement as any).getAttribute('path')).scrollIntoView({ behavior: 'smooth', block: 'start' });
}
private parse() {
const specialElements: HTMLCollection = document.getElementsByClassName('href');
for (let i = 0; i < specialElements.length; i++) {
const element = specialElements.item(i);
element.removeEventListener('click', this.scrollEvent);
element.addEventListener('click', this.scrollEvent);
}
}
}