1
1
import {
2
+ css ,
2
3
customElement ,
3
4
html ,
4
5
LitElement ,
5
6
property ,
7
+ PropertyValues ,
8
+ state ,
6
9
TemplateResult ,
7
10
} from 'lit-element' ;
8
11
import { translate } from 'lit-translate' ;
9
12
13
+ import '@material/mwc-icon' ;
10
14
import '@material/mwc-list' ;
11
15
import '@material/mwc-list/mwc-list-item' ;
12
16
17
+ import { SelectedItemsChangedEvent } from '../oscd-filter-button.js' ;
18
+
19
+ import '../filtered-list.js' ;
20
+ import '../oscd-filter-button.js' ;
21
+
13
22
import {
14
23
CompasSclDataService ,
15
24
SDS_NAMESPACE ,
@@ -21,7 +30,7 @@ export interface SclSelectedDetail {
21
30
}
22
31
export type SclSelectedEvent = CustomEvent < SclSelectedDetail > ;
23
32
export function newSclSelectedEvent ( docId : string ) : SclSelectedEvent {
24
- return new CustomEvent < SclSelectedDetail > ( 'sclSelected ' , {
33
+ return new CustomEvent < SclSelectedDetail > ( 'scl-selected ' , {
25
34
bubbles : true ,
26
35
composed : true ,
27
36
detail : { docId } ,
@@ -30,54 +39,156 @@ export function newSclSelectedEvent(docId: string): SclSelectedEvent {
30
39
31
40
@customElement ( 'compas-scl-list' )
32
41
export class CompasSclList extends LitElement {
33
- @property ( { type : String } )
34
- type = '' ;
35
-
36
42
@property ( )
37
- scls ! : Element [ ] ;
43
+ type ?: string ;
44
+
45
+ @state ( )
46
+ private items ?: Element [ ] ;
47
+
48
+ @state ( )
49
+ private labels : string [ ] = [ ] ;
50
+
51
+ @state ( )
52
+ private selectedLabels : string [ ] = [ ] ;
53
+
54
+ @state ( )
55
+ private get filteredItems ( ) : Element [ ] | undefined {
56
+ // If items are still being retrieved, return undefined.
57
+ if ( ! this . items ) {
58
+ return undefined ;
59
+ }
60
+
61
+ // If all labels are selected, show all items, including the ones not having a Label.
62
+ if ( this . labels . length === this . selectedLabels . length ) {
63
+ return this . items ;
64
+ }
65
+
66
+ return this . items . filter ( item => {
67
+ const labels = Array . from ( item . querySelectorAll ( 'Label' ) ?? [ ] )
68
+ . map ( element => element . textContent )
69
+ . filter ( value => ! ! value ) as string [ ] ;
70
+ return (
71
+ labels . filter ( label => this . selectedLabels . includes ( label ) ) . length > 0
72
+ ) ;
73
+ } ) ;
74
+ }
38
75
39
76
firstUpdated ( ) : void {
40
77
this . fetchData ( ) ;
41
78
}
42
79
80
+ protected updated ( _changedProperties : PropertyValues ) : void {
81
+ super . updated ( _changedProperties ) ;
82
+
83
+ // When the document is updated, we reset the selected IED.
84
+ if ( _changedProperties . has ( 'type' ) ) {
85
+ this . items = undefined ;
86
+ this . labels = [ ] ;
87
+ this . selectedLabels = [ ] ;
88
+ this . fetchData ( ) ;
89
+ }
90
+ }
91
+
43
92
fetchData ( ) : void {
44
- CompasSclDataService ( )
45
- . listScls ( this . type )
46
- . then ( xmlResponse => {
47
- this . scls = Array . from ( xmlResponse . querySelectorAll ( 'Item' ) ?? [ ] ) ;
48
- } ) ;
93
+ if ( this . type ) {
94
+ CompasSclDataService ( )
95
+ . listScls ( this . type )
96
+ . then ( xmlResponse => {
97
+ this . items = Array . from ( xmlResponse . querySelectorAll ( 'Item' ) ?? [ ] ) ;
98
+ this . labels = Array . from (
99
+ new Set (
100
+ Array . from ( xmlResponse . querySelectorAll ( 'Label' ) ?? [ ] )
101
+ . map ( element => element . textContent )
102
+ . filter ( label => ! ! label )
103
+ . sort ( ( label1 , label2 ) => label1 ! . localeCompare ( label2 ! ) )
104
+ )
105
+ ) as string [ ] ;
106
+ this . selectedLabels = this . labels ;
107
+ } ) ;
108
+ }
49
109
}
50
110
51
111
render ( ) : TemplateResult {
52
- if ( ! this . scls ) {
112
+ if ( ! this . items ) {
53
113
return html ` <compas- loading> </ compas- loading> ` ;
54
114
}
55
- if ( this . scls ?. length <= 0 ) {
115
+ if ( this . items ?. length <= 0 ) {
56
116
return html ` <mwc- lis t>
57
117
<mwc- lis t- item> <i> ${ translate ( 'compas.noScls' ) } </ i> </ mwc- lis t- item>
58
118
</ mwc- lis t> ` ;
59
119
}
60
- return html ` < mwc-list >
61
- ${ this . scls . map ( item => {
62
- const id =
63
- item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Id' ) . item ( 0 ) !
64
- . textContent ?? '' ;
65
- let name =
66
- item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Name' ) . item ( 0 ) !
67
- . textContent ?? '' ;
68
- if ( name === '' ) {
69
- name = id ;
70
- }
71
- const version =
72
- item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Version' ) . item ( 0 ) !
73
- . textContent ?? '' ;
74
- return html `< mwc-list-item
75
- tabindex ="0 "
76
- @click =${ ( ) => this . dispatchEvent ( newSclSelectedEvent ( id ) ) }
120
+ const filteredItems = this . filteredItems ;
121
+ return html `
122
+ <div class= "filters" >
123
+ <span> ${ translate ( 'compas.sclFilter' ) } </ span>
124
+ <oscd- filter- butto n
125
+ id= "labelsFilter"
126
+ multi = "true"
127
+ ?dis abled= "${ this . labels . length <= 0 } "
128
+ .header = ${ translate ( 'compas.label.selectLabels' ) }
129
+ @selected-items-changed = "${ ( e : SelectedItemsChangedEvent ) => {
130
+ this . selectedLabels = e . detail . selectedItems ;
131
+ this . requestUpdate ( 'items' ) ;
132
+ this . requestUpdate ( 'filteredItems' ) ;
133
+ this . requestUpdate ( 'selectedLabels' ) ;
134
+ } } "
77
135
>
78
- ${ name } (${ version } )
79
- </ mwc-list-item > ` ;
80
- } ) }
81
- </ mwc-list > ` ;
136
+ <span slot= "icon" >
137
+ <mwc- icon>
138
+ ${ this . labels . length != this . selectedLabels . length
139
+ ? 'label'
140
+ : 'label_off' }
141
+ </ mwc- icon>
142
+ </ span>
143
+ ${ this . labels . map ( label => {
144
+ return html ` <mwc- check- lis t- item
145
+ value= "${ label } "
146
+ ?selected= "${ this . selectedLabels . includes ( label ) } "
147
+ >
148
+ ${ label }
149
+ </ mwc- check- lis t- item> ` ;
150
+ } ) }
151
+ </ oscd- filter- butto n>
152
+ </ div>
153
+ ${ filteredItems && filteredItems . length > 0
154
+ ? html ` <filtered- lis t>
155
+ ${ filteredItems . map ( item => {
156
+ const id =
157
+ item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Id' ) . item ( 0 ) !
158
+ . textContent ?? '' ;
159
+ let name =
160
+ item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Name' ) . item ( 0 ) !
161
+ . textContent ?? '' ;
162
+ if ( name === '' ) {
163
+ name = id ;
164
+ }
165
+ const version =
166
+ item . getElementsByTagNameNS ( SDS_NAMESPACE , 'Version' ) . item ( 0 ) !
167
+ . textContent ?? '' ;
168
+ return html ` <mwc- lis t- item
169
+ tabindex= "0"
170
+ @click = ${ ( ) => this . dispatchEvent ( newSclSelectedEvent ( id ) ) }
171
+ >
172
+ ${ name } (${ version } )
173
+ </ mwc- lis t- item> ` ;
174
+ } ) }
175
+ </ filtered- lis t> `
176
+ : html ` <mwc- lis t>
177
+ <mwc- lis t- item>
178
+ <i> ${ translate ( 'compas.noFilteredScls' ) } </ i>
179
+ </ mwc- lis t- item>
180
+ </ mwc- lis t> ` }
181
+ ` ;
82
182
}
183
+
184
+ static styles = css `
185
+ .filters {
186
+ padding-left : var (--mdc-list-side-padding , 16px );
187
+ display : flex;
188
+ }
189
+
190
+ .filters > span {
191
+ line-height : 48px ;
192
+ }
193
+ ` ;
83
194
}
0 commit comments