2020-12-31 15:34:36 +01:00
|
|
|
import { Component, OnInit, AfterViewChecked, ViewEncapsulation, ViewChild, AfterContentInit, AfterViewInit } from '@angular/core';
|
2020-02-12 17:24:42 +01:00
|
|
|
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-14 18:28:13 +01:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { MatomoService } from '@app/core/services/matomo/matomo-service';
|
2020-12-31 15:34:36 +01:00
|
|
|
import { ElementRef } from '@angular/core';
|
|
|
|
import { interval, Subject } from 'rxjs';
|
|
|
|
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
|
2020-02-12 17:24:42 +01:00
|
|
|
|
|
|
|
@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']
|
2020-02-12 17:24:42 +01:00
|
|
|
})
|
2020-12-31 15:34:36 +01:00
|
|
|
export class UserGuideContentComponent extends BaseComponent implements OnInit {
|
|
|
|
readonly useInnerHTML: boolean = false; //GK: Change for TESTING PURPOSES ONLY
|
2020-02-12 17:24:42 +01:00
|
|
|
|
|
|
|
guideHTML: any;
|
2020-12-10 14:45:55 +01:00
|
|
|
guideHTMLUrl: SafeResourceUrl;
|
2020-12-11 15:53:22 +01:00
|
|
|
sanitizedGuideUrl: any;
|
2020-02-13 17:18:54 +01:00
|
|
|
private scrollEvent: EventListener;
|
2020-12-31 15:34:36 +01:00
|
|
|
private tocScrollEvent: EventListener;
|
|
|
|
private _transformed: Subject<boolean> = new Subject();
|
|
|
|
private _parsed: Subject<boolean> = new Subject();
|
|
|
|
|
|
|
|
@ViewChild('guide', {static: false}) guide: ElementRef;
|
2020-02-12 17:24:42 +01:00
|
|
|
|
2020-12-10 14:45:55 +01:00
|
|
|
constructor(
|
|
|
|
private userGuideService: UserGuideService,
|
2020-12-10 14:49:34 +01:00
|
|
|
private sanitizer: DomSanitizer,
|
2020-12-14 18:28:13 +01:00
|
|
|
private languageService: LanguageService,
|
|
|
|
private httpClient: HttpClient,
|
2020-12-31 15:34:36 +01:00
|
|
|
private matomoService: MatomoService,
|
|
|
|
private configurationService: ConfigurationService
|
2020-12-10 14:45:55 +01:00
|
|
|
) { super(); }
|
2020-02-12 17:24:42 +01:00
|
|
|
|
2020-12-10 14:45:55 +01:00
|
|
|
ngOnInit() {
|
2020-12-14 18:28:13 +01:00
|
|
|
this.matomoService.trackPageView('User Guide Content');
|
2020-12-10 14:45:55 +01:00
|
|
|
this.scrollEvent = ((ev) => this.scroll(ev));
|
2020-12-31 15:34:36 +01:00
|
|
|
this.tocScrollEvent = ((ev) => {
|
|
|
|
this.activeToc(ev);
|
|
|
|
this.scroll(ev);
|
|
|
|
});
|
2020-12-10 14:49:34 +01:00
|
|
|
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' });
|
2020-12-31 15:34:36 +01:00
|
|
|
if (this.useInnerHTML) {
|
|
|
|
this.readBlob(blob);
|
|
|
|
} else {
|
|
|
|
this.guideHTMLUrl = this.sanitizer.bypassSecurityTrustResourceUrl((window.URL ? URL : webkitURL).createObjectURL(blob));
|
|
|
|
//GK: In case the app is in localhost (dev/debug build) apply the following transformation
|
|
|
|
//in order to show the user guide's images
|
|
|
|
if (this.guideHTMLUrl && this.configurationService.app.includes('localhost')) {
|
|
|
|
interval(1000).pipe(takeUntil(this._transformed)).subscribe(() => this.transform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.guideHTML = this.sanitizer.sanitize(SecurityContext.HTML, blob);
|
2020-12-11 15:53:22 +01:00
|
|
|
// this.sanitizedGuideUrl = this.sanitizer.sanitize(SecurityContext.URL, this.guideHTMLUrl);
|
|
|
|
// console.log(this.guideHTMLUrl);
|
2020-12-10 14:45:55 +01:00
|
|
|
});
|
|
|
|
}
|
2020-02-12 17:24:42 +01:00
|
|
|
|
2020-12-10 14:45:55 +01:00
|
|
|
readBlob(blob: Blob) {
|
|
|
|
const fr = new FileReader();
|
|
|
|
fr.onload = ev => {
|
2020-12-31 15:34:36 +01:00
|
|
|
const HTMLstring = fr.result as string;
|
|
|
|
this.guideHTML = this.sanitizer.bypassSecurityTrustHtml(HTMLstring);
|
|
|
|
this.addScripts(HTMLstring);
|
|
|
|
if (this.guideHTML) {
|
|
|
|
interval(1000).pipe(takeUntil(this._transformed)).subscribe(() => this.transform());
|
|
|
|
interval(1000).pipe(takeUntil(this._parsed)).subscribe(() => this.parse());
|
|
|
|
}
|
2020-12-10 14:45:55 +01:00
|
|
|
};
|
|
|
|
fr.readAsText(blob);
|
|
|
|
}
|
|
|
|
|
2020-12-31 15:34:36 +01:00
|
|
|
activeToc(ev: Event) {
|
|
|
|
const current = document.getElementsByClassName('active');
|
|
|
|
if (current.length > 0) {
|
|
|
|
current[0].classList.remove('active');
|
|
|
|
}
|
|
|
|
(ev.currentTarget as Element).classList.add('active');
|
|
|
|
}
|
|
|
|
|
2020-12-10 14:45:55 +01:00
|
|
|
scroll(ev: Event) {
|
2020-12-31 15:34:36 +01:00
|
|
|
document.getElementById((ev.currentTarget as Element).getAttribute('path')).scrollIntoView({ behavior: 'smooth', block: 'start' });
|
2020-12-10 14:45:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private parse() {
|
2020-12-31 15:34:36 +01:00
|
|
|
const specialElements: HTMLCollection = document.getElementsByTagName('a');
|
2020-12-10 14:45:55 +01:00
|
|
|
for (let i = 0; i < specialElements.length; i++) {
|
2020-12-31 15:34:36 +01:00
|
|
|
const element = specialElements.item(i) as HTMLAnchorElement;
|
|
|
|
if(element.href.includes('#')) {
|
|
|
|
this.hrefToPath(element);
|
|
|
|
element.removeEventListener('click', this.scrollEvent);
|
|
|
|
element.addEventListener('click', this.scrollEvent);
|
|
|
|
}
|
|
|
|
if (!this._parsed.isStopped) {
|
|
|
|
this._parsed.next(true);
|
|
|
|
this._parsed.complete();
|
|
|
|
}
|
2020-12-10 14:45:55 +01:00
|
|
|
}
|
2020-12-31 15:34:36 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private addScripts(HTMLString: string) {
|
|
|
|
const fragment = document.createRange().createContextualFragment(HTMLString);
|
|
|
|
this.guide.nativeElement.appendChild(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
transform() {
|
|
|
|
if (this.useInnerHTML) {
|
|
|
|
const tocElements = document.getElementsByClassName('nav-link');
|
|
|
|
for (let i = 0; i < tocElements.length; i++) {
|
|
|
|
const href = (tocElements[i] as HTMLAnchorElement).href;
|
|
|
|
if (href.includes('#')) {
|
|
|
|
this.hrefToPath(tocElements[i] as HTMLAnchorElement);
|
|
|
|
tocElements[i].addEventListener('click', this.tocScrollEvent);
|
|
|
|
tocElements[i].classList.add('tocElement');
|
|
|
|
if (!this._transformed.isStopped) {
|
|
|
|
this._transformed.next(true);
|
|
|
|
this._transformed.complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const userguide = document.getElementById('userguide') as HTMLIFrameElement;
|
|
|
|
const images = userguide.contentWindow.document.getElementsByTagName('img');
|
|
|
|
for (let i = 0; i < images.length; i++) {
|
|
|
|
const image = images[i];
|
|
|
|
image.src = `${this.configurationService.app}${image.src}`;
|
|
|
|
if (!this._transformed.isStopped) {
|
|
|
|
this._transformed.next(true);
|
|
|
|
this._transformed.complete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private hrefToPath(element: HTMLAnchorElement) {
|
|
|
|
const href = element.href;
|
|
|
|
const hashtagIndex = href.lastIndexOf('#');
|
|
|
|
element.removeAttribute('href');
|
|
|
|
element.setAttribute('path', href.slice(hashtagIndex + 1));
|
2020-12-10 14:45:55 +01:00
|
|
|
}
|
2020-02-12 17:24:42 +01:00
|
|
|
|
|
|
|
}
|