1
- import { Thing } from '@pod-os/core' ;
1
+ import { PodOS , Thing } from '@pod-os/core' ;
2
2
import { Component , Element , Event , h , Prop , State } from '@stencil/core' ;
3
3
import { ResourceAware , ResourceEventEmitter , subscribeResource } from '../events/ResourceAware' ;
4
+ import { PodOsAware , PodOsEventEmitter , subscribePodOs } from '../events/PodOsAware' ;
4
5
5
6
@Component ( {
6
7
tag : 'pos-list' ,
7
8
shadow : false ,
8
9
} )
9
- export class PosList implements ResourceAware {
10
+ export class PosList implements PodOsAware , ResourceAware {
10
11
/**
11
12
* URI of the predicate to follow
12
13
*/
13
14
@Prop ( ) rel : string ;
15
+ /**
16
+ * URI of a class for which instances will be listed
17
+ */
18
+ @Prop ( ) ifTypeof : string ;
14
19
/**
15
20
* Whether listed resources should be fetched before being displayed
16
21
*/
17
22
@Prop ( ) fetch : boolean = false ;
18
23
19
24
@Element ( ) host : HTMLElement ;
20
25
@State ( ) error : string = null ;
26
+ @State ( ) os : PodOS ;
21
27
@State ( ) resource : Thing ;
22
28
@State ( ) items : string [ ] = [ ] ;
23
29
@State ( ) templateString : string ;
24
30
25
31
@Event ( { eventName : 'pod-os:resource' } )
26
32
subscribeResource : ResourceEventEmitter ;
33
+ @Event ( { eventName : 'pod-os:init' } )
34
+ subscribePodOs : PodOsEventEmitter ;
27
35
28
36
componentWillLoad ( ) {
29
- subscribeResource ( this ) ;
37
+ if ( this . rel ) subscribeResource ( this ) ;
38
+ if ( this . ifTypeof ) subscribePodOs ( this ) ;
30
39
const templateElement = this . host . querySelector ( 'template' ) ;
31
40
if ( templateElement == null ) {
32
41
this . error = 'No template element found' ;
@@ -36,10 +45,14 @@ export class PosList implements ResourceAware {
36
45
}
37
46
38
47
receiveResource = ( resource : Thing ) => {
39
- this . items = [ ] ;
40
48
if ( this . rel ) this . items = resource . relations ( this . rel ) . flatMap ( relation => relation . uris ) ;
41
49
} ;
42
50
51
+ receivePodOs = async ( os : PodOS ) => {
52
+ this . os = os ;
53
+ this . items = os . store . findMembers ( this . ifTypeof ) ;
54
+ } ;
55
+
43
56
render ( ) {
44
57
if ( this . error ) return this . error ;
45
58
const elems = this . items . map ( it => (
0 commit comments