11import { LitElement , html , css , nothing } from 'lit' ;
2+ import { EventType } from '../core/EventType' ;
3+ import { ArrayElement } from '../core/ArrayElement' ;
24
35/**
46 * @class MapSourceElement
@@ -11,14 +13,60 @@ import { LitElement, html, css, nothing } from 'lit';
1113 * </js-map>
1214 */
1315export class MapSourceElement extends LitElement {
16+ #data;
17+
1418 static get localName ( ) {
1519 return 'js-mapsource' ;
1620 }
1721
1822 static get properties ( ) {
1923 return {
2024 type : { type : String , reflect : true } ,
21- data : { type : Object , reflect : true } ,
25+ data : { type : String , reflect : true } ,
2226 } ;
2327 }
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+ }
2472}
0 commit comments