1
+ import { LiveAnnouncer } from "@angular/cdk/a11y" ;
1
2
import {
2
- AfterContentInit , ChangeDetectionStrategy ,
3
- ChangeDetectorRef , Component , ContentChild ,
4
- ElementRef , Input , OnChanges , OnInit , ViewChild ,
3
+ AfterContentInit , AfterViewInit ,
4
+ ChangeDetectionStrategy , ChangeDetectorRef , Component ,
5
+ ContentChild , ElementRef , Input , OnChanges , OnDestroy , OnInit , ViewChild ,
5
6
} from "@angular/core" ;
6
7
import { FormControl } from "@angular/forms" ;
7
8
import { ActivatedRoute } from "@angular/router" ;
8
9
import { Filter , FilterBuilder , I18nService , autobind } from "@batch-flask/core" ;
9
10
import { KeyCode } from "@batch-flask/core/keys" ;
10
11
import { ListSelection } from "@batch-flask/core/list" ;
11
12
import { SanitizedError } from "@batch-flask/utils" ;
12
- import { Subscription } from "rxjs" ;
13
- import { debounceTime , distinctUntilChanged } from "rxjs/operators" ;
13
+ import { Subject } from "rxjs" ;
14
+ import { debounceTime , distinctUntilChanged , takeUntil } from "rxjs/operators" ;
14
15
import { SplitPaneComponent , SplitPaneConfig } from "../split-pane" ;
15
16
import { BrowseLayoutAdvancedFilterDirective } from "./browse-layout-advanced-filter" ;
16
17
import { BrowseLayoutListDirective } from "./browse-layout-list" ;
17
18
18
- import { LiveAnnouncer } from "@angular/cdk/a11y" ;
19
19
import "./browse-layout.scss" ;
20
20
21
21
export interface BrowseLayoutConfig {
@@ -46,7 +46,7 @@ let idCounter = 0;
46
46
templateUrl : "browse-layout.html" ,
47
47
changeDetection : ChangeDetectionStrategy . OnPush ,
48
48
} )
49
- export class BrowseLayoutComponent implements OnInit , AfterContentInit , OnChanges {
49
+ export class BrowseLayoutComponent implements OnInit , AfterViewInit , AfterContentInit , OnChanges , OnDestroy {
50
50
@Input ( ) public id = `bl-browse-layout-${ idCounter ++ } ` ;
51
51
/**
52
52
* Field for the quicksearch.
@@ -94,20 +94,14 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
94
94
95
95
private _activeItemKey : string = null ;
96
96
private _config : BrowseLayoutConfig = defaultConfig ;
97
- private _selectionChangeSub : Subscription ;
97
+ private _destroy = new Subject ( ) ;
98
98
99
99
constructor (
100
- activeRoute : ActivatedRoute ,
100
+ private activeRoute : ActivatedRoute ,
101
101
private i18n : I18nService ,
102
102
private liveAnouncer : LiveAnnouncer ,
103
103
private changeDetector : ChangeDetectorRef ) {
104
104
105
- activeRoute . queryParams . subscribe ( ( params : any ) => {
106
- if ( params . filter ) {
107
- this . toggleFilter ( true ) ;
108
- }
109
- } ) ;
110
-
111
105
activeRoute . url . subscribe ( ( url ) => {
112
106
const child = activeRoute . snapshot . firstChild ;
113
107
if ( child ) {
@@ -136,6 +130,13 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
136
130
} ) ;
137
131
}
138
132
133
+ public ngAfterViewInit ( ) {
134
+ this . activeRoute . queryParams . pipe ( takeUntil ( this . _destroy ) ) . subscribe ( ( params : any ) => {
135
+ if ( params . filter ) {
136
+ this . toggleFilter ( true ) ;
137
+ }
138
+ } ) ;
139
+ }
139
140
public ngOnChanges ( changes ) {
140
141
if ( changes . config ) {
141
142
this . _updateFilter ( ) ;
@@ -153,16 +154,21 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
153
154
this . deleteSelectionIsEnabled = Boolean ( component . deleteSelection ) ;
154
155
this . refreshEnabled = Boolean ( component . refresh ) ;
155
156
this . changeDetector . markForCheck ( ) ;
156
- this . _selectionChangeSub = this . listDirective . component . selectionChange . subscribe ( ( x ) => {
157
+ this . listDirective . component . selectionChange . pipe ( takeUntil ( this . _destroy ) ) . subscribe ( ( x ) => {
157
158
this . selection = x ;
158
159
this . changeDetector . markForCheck ( ) ;
159
160
} ) ;
161
+
162
+ this . listDirective . component . activeItemChange . pipe ( takeUntil ( this . _destroy ) ) . subscribe ( ( x ) => {
163
+ if ( this . showAdvancedFilter ) {
164
+ this . toggleFilter ( false ) ;
165
+ }
166
+ } ) ;
160
167
}
161
168
162
- public _ngOnDestroy ( ) {
163
- if ( this . _selectionChangeSub ) {
164
- this . _selectionChangeSub . unsubscribe ( ) ;
165
- }
169
+ public ngOnDestroy ( ) {
170
+ this . _destroy . next ( ) ;
171
+ this . _destroy . complete ( ) ;
166
172
}
167
173
168
174
/**
@@ -181,7 +187,7 @@ export class BrowseLayoutComponent implements OnInit, AfterContentInit, OnChange
181
187
182
188
public toggleFilter ( value ?: boolean ) {
183
189
const newValue = ( value === undefined ? ! this . showAdvancedFilter : value ) ;
184
- if ( newValue === this . showAdvancedFilter ) { return ; }
190
+ if ( newValue === this . showAdvancedFilter ) { return ; }
185
191
this . showAdvancedFilter = newValue ;
186
192
187
193
if ( this . listDirective ) {
0 commit comments