1
- import { Component , Input , QueryList , ViewChild , ViewChildren , ChangeDetectionStrategy , OnInit , OnChanges } from '@angular/core' ;
2
- import { PopComponent } from '../../pop/pop.component ' ;
1
+ import { ChangeDetectionStrategy , ChangeDetectorRef , Component , Input , OnChanges , ViewChild } from '@angular/core' ;
2
+ import { Field , List , ListConfig , Selection } from '@ec.components/core ' ;
3
3
import { FormComponent } from '../../form/form.component' ;
4
- import { List , ListConfig } from '@ec.components/core' ;
5
- import { Selection } from '@ec.components/core' ;
6
- import { Field } from '@ec.components/core' ;
4
+ import { PopComponent } from '../../pop/pop.component' ;
7
5
import { ListConfigService } from '../list-config.service' ;
6
+ import { InputComponent } from '../../io/input/input.component' ;
8
7
9
8
/** This component renders, as the name states, the header of a list.*/
10
9
@Component ( {
@@ -18,24 +17,31 @@ export class ListHeaderComponent implements OnChanges {
18
17
/** The selection instance. This is optional. If It is not provided, no checkbox will be visible.*/
19
18
@Input ( ) selection : Selection < any > ;
20
19
/** The pop dropdowns that contain the filtering */
21
- @ViewChildren ( 'filterPop' ) pops : QueryList < PopComponent > ;
20
+ @ViewChild ( 'filterPop' ) filterPop : PopComponent ;
22
21
/** The config for the filter form */
23
22
filterFormConfig : ListConfig < any > ;
24
23
filteredField : any ;
25
24
filterForm : FormComponent < any > ;
25
+ filterInput : InputComponent ;
26
26
27
- constructor ( public listConfig : ListConfigService ) {
27
+ constructor ( public listConfig : ListConfigService , public cdr : ChangeDetectorRef ) {
28
28
}
29
29
30
30
setFilter ( field , value ) {
31
31
this . list . setFilter ( { [ field . property ] : value } ) ;
32
32
}
33
33
34
+ inputReady ( input ) {
35
+ this . filterInput = input ;
36
+ input . focus ( true ) ;
37
+ }
38
+
34
39
initFilterForm ( filterForm ) {
35
40
// is called when form is ready
36
41
this . filterForm = filterForm ;
37
42
if ( this . list . config . defaultFilter ) {
38
43
this . filterField ( this . list . config . defaultFilter ) ;
44
+ }
39
45
}
40
46
41
47
ngOnChanges ( changes ?) {
@@ -45,6 +51,11 @@ export class ListHeaderComponent implements OnChanges {
45
51
if ( ! this . list || ! this . list . config || ! this . list . config . fields ) {
46
52
return ;
47
53
}
54
+ /* this.list.change$.subscribe(() => {
55
+ if (this.filterInput) {
56
+ // this.filterInput.focus(true);
57
+ }
58
+ }); */
48
59
this . filterFormConfig = {
49
60
...this . list . config ,
50
61
fields : this . list . filterableFields ( ) . reduce ( ( fields , field ) => {
@@ -66,34 +77,52 @@ export class ListHeaderComponent implements OnChanges {
66
77
}
67
78
68
79
/** opens the given filter pop and closes all others */
69
- public editFilter ( pop , property ) {
80
+ public filterField ( property ) {
70
81
if ( this . filteredField ) {
71
82
if ( this . filteredField . property === property ) {
72
- pop . hide ( ) ;
83
+ /* this.filterPop.hide(); */
84
+ if ( this . filterInput ) {
85
+ this . filterInput . focus ( true ) ;
86
+ }
73
87
return ;
74
88
}
75
89
this . clearFilter ( ) ;
76
90
}
77
91
// patch current filter value to control
92
+ const control = this . filterForm . group . get ( property ) ;
93
+ if ( ! control ) {
94
+ console . warn ( 'no control found for ' + property + '. Is it filterable?' , this . list . config ) ;
95
+ return ;
96
+ }
78
97
this . filterForm . group . get ( property ) . patchValue ( this . list . getFilterValue ( property ) ) ;
79
98
this . filteredField = this . filterForm . form . getField ( property ) ;
80
- pop . show ( ) ;
99
+ setTimeout ( ( ) => this . filterPop . show ( ) ) ;
100
+ }
101
+
102
+ resetFilter ( ) {
103
+ if ( ! this . filteredField || ! this . list || ! this . list . isFiltered ( this . filteredField . property ) ) {
104
+ return ;
105
+ }
106
+ this . filterForm . group . get ( this . filteredField . property ) . reset ( ) ;
107
+ this . list . clearFilter ( ) ;
81
108
}
82
109
83
110
clearFilter ( ) {
84
111
if ( ! this . filteredField || ! this . list . isFiltered ( this . filteredField . property ) ) {
85
112
delete this . filteredField ;
86
113
return ;
87
114
}
88
- this . filterForm . group . get ( this . filteredField . property ) . reset ( ) ;
89
- this . list . clearFilter ( ) ;
115
+ this . resetFilter ( ) ;
90
116
delete this . filteredField ;
91
117
}
92
118
93
119
toggledFilterPop ( active ) {
94
120
if ( ! active ) {
95
121
this . clearFilter ( ) ;
96
122
}
123
+ if ( this . filterInput ) {
124
+ this . filterInput . focus ( true ) ;
125
+ }
97
126
}
98
127
99
128
/** Returns the fields label */
0 commit comments