1
1
import { LitElement , html , css , nothing } from 'lit' ;
2
+ import { EventType } from '../core/EventType' ;
3
+ import { ArrayElement } from '../core/ArrayElement' ;
2
4
3
5
/**
4
6
* @class MapSourceElement
@@ -11,14 +13,60 @@ import { LitElement, html, css, nothing } from 'lit';
11
13
* </js-map>
12
14
*/
13
15
export class MapSourceElement extends LitElement {
16
+ #data;
17
+
14
18
static get localName ( ) {
15
19
return 'js-mapsource' ;
16
20
}
17
21
18
22
static get properties ( ) {
19
23
return {
20
24
type : { type : String , reflect : true } ,
21
- data : { type : Object , reflect : true } ,
25
+ data : { type : String , reflect : true } ,
22
26
} ;
23
27
}
28
+
29
+ attributeChangedCallback ( name , oldVal , newVal ) {
30
+ super . attributeChangedCallback ( name , oldVal , newVal ) ;
31
+ if ( name === 'data' ) {
32
+ this . #dataChanged( newVal , oldVal ) ;
33
+ }
34
+ }
35
+
36
+ get geojson ( ) {
37
+ if ( this . type !== 'geojson' ) {
38
+ return null ;
39
+ }
40
+ const featurecollection = new Object ( ) ;
41
+ featurecollection . type = 'FeatureCollection' ;
42
+ featurecollection . features = new Array ( ) ;
43
+ if ( this . #data instanceof ArrayElement ) {
44
+ for ( let i = 0 ; i < this . #data. length ; i ++ ) {
45
+ featurecollection . features . push ( this . #data. at ( i ) ) ;
46
+ }
47
+ }
48
+ return featurecollection ;
49
+ }
50
+
51
+ #dataChanged( newVal , oldVal ) {
52
+ if ( oldVal != null && this . #data && newVal !== oldVal ) {
53
+ this . #data. removeEventListener ( EventType . CHANGE , this . #dataFetch. bind ( this ) ) ;
54
+ this . #data = null ;
55
+ }
56
+ if ( newVal != null && newVal !== oldVal ) {
57
+ this . #data = document . querySelector ( newVal ) ;
58
+ if ( this . #data) {
59
+ this . #data. addEventListener ( EventType . CHANGE , this . #dataFetch. bind ( this ) ) ;
60
+ } else {
61
+ throw new Error ( `Data "${ newVal } " not found` ) ;
62
+ }
63
+ }
64
+ }
65
+
66
+ #dataFetch( event ) {
67
+ // Change event
68
+ this . dispatchEvent ( new CustomEvent ( EventType . CHANGE , {
69
+ detail : this
70
+ } ) ) ;
71
+ }
24
72
}
0 commit comments