import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { ChangeDetectionStrategy , Component , Input } from '@angular/core' ; import { BehaviorSubject , of , Observable } from 'rxjs' ; import { catchError } from 'rxjs/operators' ; param contentPath @ Component ( { changeDetection : ChangeDetectionStrategy . OnPush , selector : 'shared-text-product' , styleUrls : [ './text-product.component.scss' ] , templateUrl : './text-product.component.html' } ) export class TextProductComponent { _product : any ; content = new BehaviorSubject < any > ( null ) ; readonly content$ = this . content . asObservable ( ) ; @ Input ( ) contentPath = '' ; error : any ; constructor ( public httpClient ) { } returns { of | null } getContent ( ) { if ( ! this . product ) { this . error = null ; this . content . next ( null ) ; return ; } const content = this . product . contents [ this . contentPath ] ; if ( ! content ) { try { if ( this . product . type . toLowerCase ( ) === 'shake-alert' ) { this . content . next ( ' ' ) ; return ; } } catch ( e ) { } this . error = new Error ( 'no content' ) ; this . content . next ( null ) ; return ; } this . error = null ; if ( content . bytes ) { this . content . next ( content . bytes ) ; } else if ( content . url ) { const options = { responseType : 'text' as 'text' } ; this . httpClient . get ( content . url , options ) . pipe ( catchError ( ( err ) : Observable < any > => { this . error = err ; return of ( null ) ; } ) ) . subscribe ( data => { this . content . next ( data ) ; } ) ; } else { this . error = new Error ( 'no content bytes or url' ) ; this . content . next ( null ) ; } } param product @ Input ( ) set product ( product ) { this . _product = product ; this . getContent ( ) ; } returns get product ( ) { return this . _product ; } param text return { string } replaceRelativePaths ( text ) { const product = this . product || { } ; const contents = product . contents || { } ; for ( const path in contents ) { if ( ! path ) { continue ; } text = text . replace ( new RegExp ( '"' + path + '"' , 'g' ) , '"' + contents [ path ] . url + '"' ) ; } return text ; } }