import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { catchError , map , timeout } from 'rxjs/operators' ; import { of } from 'rxjs' ; import { EndpointErrorHandler , EndpointErrorHandling , isErrorHandlerFunction , } from '../../../common/endpoints/endpoint.interface' ; import { HsConfig } from '../../../config.service' ; import { HsEventBusService } from '../../core/event-bus.service' ; import { HsLanguageService } from '../../language/language.service' ; import { HsStatusManagerService } from '../../save-map/status-manager.service' ; import { HsToastService } from '../../layout/toast/toast.service' ; import { HsUtilsService } from '../../utils/utils.service' ; @ Injectable ( { providedIn : 'root' , } ) export class HsCompositionsStatusManagerService { constructor ( public HsStatusManagerService , public HsConfig , public HsUtilsService , private $http , public HsEventBusService , public HsToastService , public HsLanguageService ) { } public description param ds param params param bbox loadList ( ds , params , bbox ) { let url = this . HsStatusManagerService . endpointUrl ( ) ; const query = params . query ; const textFilter = query && query . title !== undefined && query . title != '' ? '&q=' + encodeURIComponent ( '*' + query . title + '*' ) : '' ; url += '?request=list&project=' + encodeURIComponent ( this . HsConfig . project_name ) + '&extent=' + bbox . join ( ',' ) + textFilter + '&start=0&limit=1000&sort=' + getStatusSortAttr ( params . sortBy ) ; url = this . HsUtilsService . proxify ( url ) ; if ( ds . listLoading ) { ds . listLoading . unsubscribe ( ) ; delete ds . listLoading ; } const listLoading = this . $http . get ( url ) . pipe ( timeout ( 2000 ) , map ( ( response ) => response ) , catchError ( ( e ) => { if ( isErrorHandlerFunction ( ds . onError ?. compositionLoad ) ) { ( < EndpointErrorHandler > ds . onError ?. compositionLoad ) . handle ( ds , e ) ; return of ( e ) ; } switch ( ds . onError ?. compositionLoad ) { case EndpointErrorHandling . ignore : break ; case EndpointErrorHandling . toast : default : this . HsToastService . createToastPopupMessage ( this . HsLanguageService . getTranslation ( 'COMPOSITIONS.errorWhileRequestingCompositions' ) , ds . title + ': ' + this . HsLanguageService . getTranslationIgnoreNonExisting ( 'ERRORMESSAGES' , e . status ? e . status . toString ( ) : e . message , { url : url } ) , { disableLocalization : true , serviceCalledFrom : 'HsCompositionsStatusManagerService' , } ) ; } return of ( e ) ; } ) ) ; ds . listLoading = listLoading . subscribe ( ( response ) => { if ( response . results === undefined ) { return ; } if ( ds . compositions === undefined ) { ds . compositions = [ ] ; ds . matched = 0 ; } for ( const record of response . results ) { let found = false ; for ( const composition of ds . compositions ) { if ( composition . id == record . id ) { if ( record . edit !== undefined ) { composition . editable = record . edit ; } found = true ; } } if ( ! found ) { record . editable = false ; if ( record . edit !== undefined ) { record . editable = record . edit ; } if ( record . link == undefined ) { record . link = this . HsStatusManagerService . endpointUrl ( ) + '?request=load&id=' + record . id ; } if ( record . thumbnail == undefined ) { record . thumbnail = this . HsStatusManagerService . endpointUrl ( ) + '?request=loadthumb&id=' + record . id ; } const attributes = { record : record , hs_notqueryable : true , highlighted : false , } ; if ( record ) { ds . compositions . push ( record ) ; ds . matched = ds . matched + 1 ; } } } } , ( err ) => { } ) ; } delete ( endpoint , composition ) { let url = this . HsStatusManagerService . endpointUrl ( ) + '?request=delete&id=' + composition . id + '&project=' + encodeURIComponent ( this . HsConfig . project_name ) ; url = this . HsUtilsService . proxify ( url ) ; this . $http . get ( url ) . toPromise ( ) ; this . HsEventBusService . compositionDeletes . next ( composition ) ; } } param sortBy function getStatusSortAttr ( sortBy ) { const sortMap = { bbox : '[{"property":"bbox","direction":"ASC"}]' , title : '[{"property":"title","direction":"ASC"}]' , date : '[{"property":"date","direction":"ASC"}]' , } ; return encodeURIComponent ( sortMap [ sortBy ] ) ; }