@@ -3,7 +3,7 @@ import {Component, Input, OnDestroy, OnInit, TemplateRef} from '@angular/core';
3
3
import { ActivatedRoute , Router } from '@angular/router' ;
4
4
import { ResizeEvent } from 'angular-resizable-element' ;
5
5
import { Observable , of , Subscription } from 'rxjs' ;
6
- import { map } from 'rxjs/operators' ;
6
+ import { map , switchMap , catchError , finalize } from 'rxjs/operators' ;
7
7
import { RoleAccess , View } from '../../../commons/schema' ;
8
8
import { AccessService } from '../../authorization/permission/access.service' ;
9
9
import { ConfigurationService } from '../../configuration/configuration.service' ;
@@ -43,7 +43,6 @@ import {
43
43
SETTINGS_TRANSITION_ID
44
44
} from '../model/navigation-configs' ;
45
45
46
-
47
46
@Component ( {
48
47
selector : 'ncc-abstract-navigation-double-drawer' ,
49
48
template : '' ,
@@ -215,7 +214,7 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
215
214
getItemCaseByPath ( path ?: string ) : Observable < Page < Case > > {
216
215
const searchBody : CaseSearchRequestBody = {
217
216
data : {
218
- [ GroupNavigationConstants . ITEM_FIELD_ID_NODE_PATH ] : path
217
+ [ GroupNavigationConstants . ITEM_FIELD_ID_NODE_PATH ] : path
219
218
} ,
220
219
process : { identifier : "preference_item" }
221
220
} ;
@@ -333,7 +332,7 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
333
332
const path = item . resource . immediateData . find ( f => f . stringId === GroupNavigationConstants . ITEM_FIELD_ID_NODE_PATH ) ?. value
334
333
if ( this . hasItemChildren ( item ) && ! this . leftLoading$ . isActive && ! this . rightLoading$ . isActive ) {
335
334
this . _pathService . activePath = path ;
336
- } else if ( ! path . includes ( this . currentPath ) ) {
335
+ } else if ( ! path . includes ( this . currentPath ) ) {
337
336
this . _pathService . activePath = this . extractParent ( this . currentPath ) ;
338
337
} else {
339
338
this . _pathService . activePath = this . currentPath ;
@@ -355,98 +354,87 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
355
354
return ;
356
355
}
357
356
this . leftLoading$ . on ( ) ;
358
- this . getItemCaseByPath ( this . extractParent ( this . currentPath ) ) . subscribe ( {
359
- next : ( page ) => {
360
- let childCases$ : Observable < Case [ ] > , targetItem : Case , orderedChildCaseIds : string [ ] ;
361
-
362
- if ( page ?. pagination ?. totalElements === 0 ) {
363
- childCases$ = of ( [ ] )
364
- } else {
365
- targetItem = page . content [ 0 ]
366
- orderedChildCaseIds = this . extractChildCaseIds ( targetItem )
367
- childCases$ = this . getItemCasesByIdsInOnePage ( orderedChildCaseIds ) . pipe (
368
- map ( p => p . content ) ,
369
- )
370
- }
371
-
372
- childCases$ . subscribe ( {
373
- next : ( v ) => {
374
- const result = v . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
375
- this . leftItems = result . sort ( ( a , b ) => orderedChildCaseIds . indexOf ( a . resource . stringId ) - orderedChildCaseIds . indexOf ( b . resource . stringId ) ) ;
376
- this . resolveCustomViewsInLeftSide ( )
377
- this . leftLoading$ . off ( ) ;
378
- } ,
379
- error : ( error ) => {
380
- this . _log . error ( error ) ;
381
- this . leftItems = [ ] ;
382
- this . resolveCustomViewsInLeftSide ( )
383
- this . leftLoading$ . off ( ) ;
384
- }
385
- } ) ;
386
- } ,
387
- error : ( error ) => {
357
+
358
+ this . getItemCaseByPath ( this . extractParent ( this . currentPath ) ) . pipe (
359
+ switchMap ( page => this . getChildCasesFromPage ( page ) ) ,
360
+ map ( ( { cases, orderedIds} ) => this . processLeftItems ( cases , orderedIds ) ) ,
361
+ catchError ( ( error ) => {
388
362
this . _log . error ( error ) ;
389
- this . leftItems = [ ] ;
390
- this . resolveCustomViewsInLeftSide ( )
363
+ return of ( [ ] ) ;
364
+ } ) ,
365
+ finalize ( ( ) => {
366
+ this . resolveCustomViewsInLeftSide ( ) ;
391
367
this . leftLoading$ . off ( ) ;
392
- }
368
+ } )
369
+ ) . subscribe ( items => {
370
+ this . leftItems = items ;
393
371
} ) ;
394
372
}
395
373
374
+ private processLeftItems ( cases : Case [ ] , orderedChildCaseIds : string [ ] ) : NavigationItem [ ] {
375
+ const result = cases
376
+ . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) )
377
+ . filter ( i => ! ! i ) ;
378
+ return result . sort ( ( a , b ) =>
379
+ orderedChildCaseIds . indexOf ( a . resource . stringId ) - orderedChildCaseIds . indexOf ( b . resource . stringId )
380
+ ) ;
381
+ }
382
+
396
383
protected loadRightSide ( ) {
397
384
this . rightLoading$ . on ( ) ;
398
385
this . moreItems = [ ] ;
399
- this . getItemCaseByPath ( this . currentPath ) . subscribe ( {
400
- next : ( page ) => {
401
- let childCases$ : Observable < Case [ ] > , targetItem : Case , orderedChildCaseIds : string [ ] ;
402
-
403
- if ( page ?. pagination ?. totalElements === 0 ) {
404
- childCases$ = of ( [ ] )
405
- } else {
406
- targetItem = page . content [ 0 ]
407
- orderedChildCaseIds = this . extractChildCaseIds ( targetItem )
408
- childCases$ = this . getItemCasesByIdsInOnePage ( orderedChildCaseIds ) . pipe (
409
- map ( p => p . content ) ,
410
- )
411
- }
412
-
413
- childCases$ . subscribe ( {
414
- next : ( result ) => {
415
- result = ( result as Case [ ] ) . sort ( ( a , b ) => orderedChildCaseIds . indexOf ( a . stringId ) - orderedChildCaseIds . indexOf ( b . stringId ) ) ;
416
- if ( result . length > RIGHT_SIDE_INIT_PAGE_SIZE ) {
417
- const rawRightItems : Case [ ] = result . splice ( 0 , RIGHT_SIDE_INIT_PAGE_SIZE ) ;
418
- this . rightItems = rawRightItems . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
419
- this . moreItems = result . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
420
- } else {
421
- this . rightItems = result . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
422
- }
423
- this . resolveCustomViewsInRightSide ( )
424
- this . rightLoading$ . off ( ) ;
425
- } ,
426
- error : ( error ) => {
427
- this . _log . error ( error ) ;
428
- this . rightItems = [ ] ;
429
- this . moreItems = [ ] ;
430
- this . resolveCustomViewsInRightSide ( )
431
- this . rightLoading$ . off ( ) ;
432
- }
433
- } ) ;
434
- } ,
435
- error : ( error ) => {
386
+
387
+ this . getItemCaseByPath ( this . currentPath ) . pipe (
388
+ switchMap ( page => this . getChildCasesFromPage ( page ) ) ,
389
+ map ( ( { cases, orderedIds} ) => this . processRightItems ( cases , orderedIds ) ) ,
390
+ catchError ( ( error ) => {
436
391
this . _log . error ( error ) ;
437
- this . rightItems = [ ] ;
438
- this . moreItems = [ ] ;
439
- this . resolveCustomViewsInRightSide ( )
392
+ return of ( { right : [ ] , more : [ ] } ) ;
393
+ } ) ,
394
+ finalize ( ( ) => {
395
+ this . resolveCustomViewsInRightSide ( ) ;
440
396
this . rightLoading$ . off ( ) ;
441
- }
397
+ } )
398
+ ) . subscribe ( ( { right, more} ) => {
399
+ this . rightItems = right ;
400
+ this . moreItems = more ;
442
401
} ) ;
443
402
}
444
403
404
+ private processRightItems ( cases : Case [ ] , orderedChildCaseIds : string [ ] ) : {
405
+ right : NavigationItem [ ] ;
406
+ more : NavigationItem [ ]
407
+ } {
408
+ const result = cases
409
+ . sort ( ( a , b ) => orderedChildCaseIds . indexOf ( a . stringId ) - orderedChildCaseIds . indexOf ( b . stringId ) ) ;
410
+ let right : NavigationItem [ ] , more : NavigationItem [ ] ;
411
+ if ( result . length > RIGHT_SIDE_INIT_PAGE_SIZE ) {
412
+ const rawRightItems : Case [ ] = result . splice ( 0 , RIGHT_SIDE_INIT_PAGE_SIZE ) ;
413
+ right = rawRightItems . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
414
+ more = result . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
415
+ } else {
416
+ right = result . map ( folder => this . resolveItemCaseToNavigationItem ( folder ) ) . filter ( i => ! ! i ) ;
417
+ more = [ ] ;
418
+ }
419
+ return { right, more} ;
420
+ }
421
+
422
+ private getChildCasesFromPage ( page : Page < Case > ) : Observable < { cases : Case [ ] ; orderedIds : string [ ] } > {
423
+ if ( ! page ?. pagination ?. totalElements ) {
424
+ return of ( { cases : [ ] , orderedIds : [ ] } ) ;
425
+ }
426
+ const targetItem = page . content [ 0 ] ;
427
+ const orderedChildCaseIds = this . extractChildCaseIds ( targetItem ) ?? [ ] ;
428
+ return this . getItemCasesByIdsInOnePage ( orderedChildCaseIds ) . pipe (
429
+ map ( p => ( { cases : p . content , orderedIds : orderedChildCaseIds } ) )
430
+ ) ;
431
+ }
432
+
445
433
protected extractChildCaseIds ( item : Case ) : string [ ] {
446
434
return item . immediateData . find ( f => f . stringId === GroupNavigationConstants . ITEM_FIELD_ID_CHILD_ITEM_IDS ) ?. value
447
435
}
448
436
449
- protected getItemCasesByIdsInOnePage ( caseIds : string [ ] ) : Observable < Page < Case > > {
437
+ protected getItemCasesByIdsInOnePage ( caseIds : string [ ] ) : Observable < Page < Case > > {
450
438
return this . getItemCasesByIds ( caseIds , 0 , caseIds . length )
451
439
}
452
440
@@ -620,5 +608,4 @@ export abstract class AbstractNavigationDoubleDrawerComponent implements OnInit,
620
608
}
621
609
return path . substring ( 0 , path . lastIndexOf ( '/' ) ) ;
622
610
}
623
-
624
611
}
0 commit comments