1- import { Component , Input } from '@angular/core' ;
1+ import { AfterViewInit , Component , Input , OnDestroy } from '@angular/core' ;
22import { UriService } from '../service/uri.service' ;
3+ import { CaseResourceService } from "../../resources/engine-endpoint/case-resource.service" ;
4+ import { CaseSearchRequestBody } from "../../filter/models/case-search-request-body" ;
5+ import { HttpParams } from "@angular/common/http" ;
6+ import { PaginationParams } from "../../utility/pagination/pagination-params" ;
7+ import { SimpleFilter } from "../../filter/models/simple-filter" ;
8+ import { take } from "rxjs/operators" ;
9+ import { BehaviorSubject , Subscription } from "rxjs" ;
10+ import { ActivatedRoute , Router } from "@angular/router" ;
11+ import {
12+ DynamicNavigationRouteProviderService
13+ } from "../../routing/dynamic-navigation-route-provider/dynamic-navigation-route-provider.service" ;
14+ import { Case } from "../../resources/interface/case" ;
15+ import { I18nFieldValue } from "../../data-fields/i18n-field/models/i18n-field-value" ;
16+ import { TranslateService } from "@ngx-translate/core" ;
317
418@Component ( {
519 selector : 'ncc-breadcrumbs-component' ,
620 template : ''
721} )
8- export abstract class AbstractBreadcrumbsComponent {
22+ export abstract class AbstractBreadcrumbsComponent implements OnDestroy , AfterViewInit {
923
1024 @Input ( ) showHome : boolean = true ;
25+ @Input ( ) showFilter : boolean = true ;
26+ @Input ( ) redirectOnClick : boolean = true ;
1127 @Input ( ) lengthOfPath : number = 30 ;
1228 @Input ( ) partsAfterDots : number = 2 ;
29+ filterName : string ;
30+ breadcrumbsParts : Array < string > ;
1331 private static DOTS : string = '...' ;
1432 private static DELIMETER : string = '/' ;
33+ private static NODE_PATH : string = 'nodePath' ;
34+ private static ITEM_SETTINGS : string = 'item_settings' ;
1535 private _showPaths : boolean = false ;
36+ private nicePath : BehaviorSubject < Array < string > > ;
37+ private redirectUrls : Map < string , Array < string > > ;
38+ private nicePathSubscription : Subscription ;
1639
17- protected constructor ( protected _uriService : UriService ) {
40+ protected constructor ( protected _uriService : UriService ,
41+ protected _caseResourceService : CaseResourceService ,
42+ protected _activatedRoute : ActivatedRoute ,
43+ protected _router : Router ,
44+ protected _dynamicRoutingService : DynamicNavigationRouteProviderService ,
45+ protected _translateService : TranslateService ) {
46+ this . nicePath = new BehaviorSubject < Array < string > > ( undefined ) ;
47+ this . redirectUrls = new Map < string , Array < string > > ( ) ;
48+ this . initNicePath ( ) ;
1849 }
1950
20- public getPath ( ) : Array < string > {
21- const tmp = this . _uriService . splitNodePath ( this . _uriService . activeNode ) ;
22- if ( tmp ?. length > this . partsAfterDots + 1 && this . _uriService . activeNode ?. uriPath . length > this . lengthOfPath && ! this . _showPaths ) {
23- const newPath = [ tmp [ 0 ] , AbstractBreadcrumbsComponent . DOTS ] ;
24- for ( let i = tmp . length - this . partsAfterDots ; i < tmp . length ; i ++ ) {
25- newPath . push ( tmp [ i ] ) ;
51+ ngAfterViewInit ( ) {
52+ this . resolveBreadcrumbs ( ) ;
53+ }
54+
55+ ngOnDestroy ( ) : void {
56+ if ( ! ! this . nicePathSubscription ) {
57+ this . nicePathSubscription . unsubscribe ( ) ;
58+ }
59+ }
60+
61+ public resolveBreadcrumbs ( ) {
62+ const filterId = this . _activatedRoute . snapshot . params . filterCaseId
63+ if ( ! ! filterId ) {
64+ const splitPath = this . _uriService . splitNodePath ( this . _uriService . activeNode ) ;
65+ const fullPath = this . createFullPath ( splitPath ) ;
66+ const fullPathQueries = fullPath . map ( p => '(processIdentifier:preference_item AND dataSet.nodePath.textValue.keyword:\"' + p + '\")' )
67+ fullPathQueries . push ( '(taskMongoIds:\"' + filterId + '\")' )
68+
69+ const searchBody : CaseSearchRequestBody = {
70+ query : fullPathQueries . join ( " OR " )
71+ } ;
72+ let httpParams = new HttpParams ( )
73+ . set ( PaginationParams . PAGE_SIZE , 25 )
74+ . set ( PaginationParams . PAGE_NUMBER , 0 ) ;
75+
76+ this . _caseResourceService . searchCases ( SimpleFilter . fromCaseQuery ( searchBody ) , httpParams ) . pipe ( take ( 1 ) ) . subscribe ( result => {
77+ const cases = result . content ;
78+ const filterCaseIndex = cases . findIndex ( c => c . tasks . some ( t => t . task === filterId ) && ! fullPath . includes ( this . immediateValue ( c , AbstractBreadcrumbsComponent . NODE_PATH ) ) ) ;
79+ if ( filterCaseIndex >= 0 ) {
80+ const filterCase = cases . splice ( cases . findIndex ( c => c . tasks . some ( t => t . task === filterId ) && ! fullPath . includes ( this . immediateValue ( c , AbstractBreadcrumbsComponent . NODE_PATH ) ) ) , 1 ) [ 0 ] ;
81+ this . filterName = this . getTranslation ( this . immediateValue ( filterCase , 'menu_name' ) ) ;
82+ }
83+ cases . sort ( ( a , b ) => fullPath . indexOf ( this . immediateValue ( a , AbstractBreadcrumbsComponent . NODE_PATH ) ) - fullPath . indexOf ( this . immediateValue ( b , AbstractBreadcrumbsComponent . NODE_PATH ) ) ) ;
84+ if ( this . redirectOnClick ) {
85+ cases . forEach ( c => this . redirectUrls . set ( this . immediateValue ( c , AbstractBreadcrumbsComponent . NODE_PATH ) , [ this . _dynamicRoutingService . route , c . tasks . find ( t => t . transition === AbstractBreadcrumbsComponent . ITEM_SETTINGS ) . task ] ) )
86+ }
87+ this . nicePath . next ( [ "" , ...cases . map ( c => this . getTranslation ( this . immediateValue ( c , 'menu_name' ) ) ) ] ) ;
88+ } ) ;
89+ }
90+ }
91+
92+ public initNicePath ( ) {
93+ this . nicePathSubscription = this . nicePath . subscribe ( np => {
94+ if ( ! ! np ) {
95+ const path = np ;
96+ if ( path ?. length > this . partsAfterDots + 1 && this . _uriService . activeNode ?. uriPath . length > this . lengthOfPath && ! this . _showPaths ) {
97+ const newPath = [ path [ 0 ] , AbstractBreadcrumbsComponent . DOTS ] ;
98+ for ( let i = path . length - this . partsAfterDots ; i < path . length ; i ++ ) {
99+ newPath . push ( path [ i ] ) ;
100+ }
101+ this . breadcrumbsParts = newPath ;
102+ return ;
103+ }
104+ this . breadcrumbsParts = path === undefined ? [ ] : path ;
26105 }
27- return newPath ;
106+ } ) ;
107+ }
108+
109+ public redirect ( ) {
110+ if ( ! this . redirectOnClick ) {
111+ return ;
28112 }
29- return tmp === undefined ? [ ] : tmp ;
113+ this . _router . navigate ( this . redirectUrls . get ( this . _uriService . activeNode . uriPath ) ) . then ( r => { } )
30114 }
31115
32116 public reset ( ) : void {
117+ this . filterName = undefined ;
33118 this . _uriService . reset ( ) ;
119+ this . nicePath . next ( [ "" ] )
34120 }
35121
36122 public changePath ( path : string , count : number ) {
37123 if ( path === AbstractBreadcrumbsComponent . DOTS && count === 1 ) {
38124 this . _showPaths = true ;
125+ this . nicePath . next ( this . nicePath . value ) ;
39126 return ;
40127 }
41128 let fullPath : string = '' ;
@@ -48,6 +135,9 @@ export abstract class AbstractBreadcrumbsComponent {
48135 }
49136 this . _uriService . getNodeByPath ( fullPath ) . subscribe ( node => {
50137 this . _uriService . activeNode = node ;
138+ this . filterName = undefined ;
139+ this . nicePath . next ( this . nicePath . value . slice ( 0 , control + 1 ) )
140+ this . redirect ( ) ;
51141 } )
52142 }
53143
@@ -57,4 +147,21 @@ export abstract class AbstractBreadcrumbsComponent {
57147 }
58148 return count ;
59149 }
150+
151+ private createFullPath ( splitPath : Array < string > ) : Array < string > {
152+ let tmp = '' ;
153+ return splitPath . filter ( s => s !== "" ) . map ( ( value ) => {
154+ tmp += AbstractBreadcrumbsComponent . DELIMETER + value ;
155+ return tmp . replace ( "//" , AbstractBreadcrumbsComponent . DELIMETER )
156+ } ) ;
157+ }
158+
159+ private immediateValue ( aCase : Case , fieldId : string ) : any {
160+ return aCase . immediateData . find ( s => s . stringId === fieldId ) ?. value
161+ }
162+
163+ private getTranslation ( value : I18nFieldValue ) : string {
164+ const locale = this . _translateService . currentLang . split ( '-' ) [ 0 ] ;
165+ return locale in value . translations ? value . translations [ locale ] : value . defaultValue ;
166+ }
60167}
0 commit comments