edition-data.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { HttpClient } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { forkJoin, Observable, of } from 'rxjs';
  4. import { catchError, map, mergeMap, publishReplay, refCount, tap } from 'rxjs/operators';
  5. import { AppConfig } from '../app.config';
  6. import { OriginalEncodingNodeType } from '../models/evt-models';
  7. import { parseXml } from '../utils/xml-utils';
  8. @Injectable({
  9. providedIn: 'root',
  10. })
  11. export class EditionDataService {
  12. private editionUrls = AppConfig.evtSettings.files.editionUrls || [];
  13. public parsedEditionSource$: Observable<OriginalEncodingNodeType> = this.loadAndParseEditionData();
  14. constructor(
  15. private http: HttpClient,
  16. ) {
  17. }
  18. private loadAndParseEditionData() {
  19. const editionUrl = this.editionUrls[0];
  20. return this.http.get(editionUrl, { responseType: 'text' }).pipe(
  21. map(source => parseXml(source)),
  22. mergeMap((editionData) => this.loadXIinclude(editionData, editionUrl.substring(0, editionUrl.lastIndexOf('/') + 1))),
  23. publishReplay(1),
  24. refCount(),
  25. catchError(() => this.handleLoadingError()),
  26. );
  27. }
  28. loadXIinclude(doc: HTMLElement, baseUrlPath: string) {
  29. // console.log(document.location.href);
  30. const parametro2 = document.location.href;
  31. const parametro3 = parametro2.split("p=")[1].split("_")[0];
  32. // console.log('parametroSigla : ' + parametro3);
  33. const parametro = '[href="lettere/' + parametro3 + '.xml"]';
  34. //const filesToInclude = Array.from(doc.getElementsByTagName('xi:include'));
  35. const filesToInclude = Array.from(doc.querySelectorAll(parametro));
  36. const xiIncludeLoadsSubs = filesToInclude.map(element =>
  37. this.http.get(baseUrlPath + element.getAttribute('href'), { responseType: 'text' })
  38. .pipe(
  39. tap((fileData) => {
  40. const includedDoc = parseXml(fileData);
  41. const fileXpointer = element.getAttribute('xpointer');
  42. let includedTextElem: Node;
  43. if (fileXpointer) {
  44. includedTextElem = doc.querySelector(`[*|id="${fileXpointer}"]`) || includedDoc.querySelector('text');
  45. } else {
  46. includedTextElem = includedDoc.querySelector('text');
  47. }
  48. // element.parentNode.replaceChild(includedTextElem, element);
  49. element.parentNode.appendChild(includedTextElem);
  50. }),
  51. catchError(_ => {
  52. Array.from(element.getElementsByTagName('xi:fallback')).map((el) => {
  53. const divEl = document.createElement('div');
  54. divEl.classList.add('xiinclude-fallback');
  55. divEl.setAttribute('xml:id', element.getAttribute('xpointer'));
  56. divEl.innerHTML = `<p>${el.innerHTML}</p>`;
  57. return divEl;
  58. }).forEach((el) => element.parentNode.replaceChild(el, element));
  59. return of(doc);
  60. }),
  61. ));
  62. if (xiIncludeLoadsSubs.length > 0) {
  63. return forkJoin(xiIncludeLoadsSubs).pipe(map(() => doc));
  64. }
  65. return of(doc);
  66. }
  67. private handleLoadingError() {
  68. // TODO: TEMP
  69. const errorEl: HTMLElement = document.createElement('div');
  70. if (!this.editionUrls || this.editionUrls.length === 0) {
  71. errorEl.textContent = 'Missing configuration for edition files. Data cannot be loaded.';
  72. } else {
  73. errorEl.textContent = 'There was an error in loading edition files.';
  74. }
  75. return of(errorEl);
  76. }
  77. }