Skip to content

Commit aaa2187

Browse files
committed
Added fill type
1 parent 5fa1e13 commit aaa2187

File tree

7 files changed

+95
-25
lines changed

7 files changed

+95
-25
lines changed

example/index.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
<div style="height: 500px;">
3030
<js-map id="map"
3131
accessToken="pk.eyJ1IjoiZGp0aG9ycGUiLCJhIjoiY2x5ZnJhZjAzMDJsYTJqcjd6eWQ3cjRvcSJ9.LvoT_wihG5VQtv008P-MPw">
32-
<js-mapsource id="area" type="geojson"
33-
data='{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [0,0] } }'></js-mapsource>
34-
<js-maplayer id="points" source="#area" type="circle" paint='{ "circle-color" : "red" , "circle-radius"
35-
: 4, "circle-stroke-width" : 2, "circle-stroke-color" : "red" }'></js-maplayer>
32+
<!-- add a source from the array (must currently be an array of GeoJSON features) -->
33+
<js-mapsource id="area" type="geojson" data="#array"></js-mapsource>
34+
35+
<!-- display the source on the map as polygons -->
36+
<js-maplayer id="points" source="#area" type="fill" paint='{ "fill-opacity": 0.2, "fill-color": "#aa0000" }'></js-maplayer>
3637
</js-map>
3738
</div>
3839
</js-container>

example/load.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { FeatureModel } from '../src/geojson/FeatureModel';
22

33
window.addEventListener('load', () => {
44
const provider = document.querySelector('#provider');
5+
provider.interval = 5;
56
});

src/core/ArrayElement.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { EventType } from './EventType';
1313
*/
1414
export class ArrayElement extends LitElement {
1515
#data = new Array();
16-
#newdata = null;
16+
#newdata = new Array();
1717
#provider = null;
1818

1919
static get localName() {
@@ -54,24 +54,24 @@ export class ArrayElement extends LitElement {
5454

5555
#providerChanged(newVal, oldVal) {
5656
if (oldVal != null && this.#provider && newVal !== oldVal) {
57-
this.#provider.removeEventListener(EventType.OBJECT, this.#providerFetch.bind(this));
57+
this.#provider.removeEventListener(EventType.FETCH, this.#providerFetch.bind(this));
5858
this.#provider.removeEventListener(EventType.OBJECT, this.#providerObject.bind(this));
59-
this.#provider.removeEventListener(EventType.OBJECT, this.#providerDone.bind(this));
59+
this.#provider.removeEventListener(EventType.DONE, this.#providerDone.bind(this));
6060
this.#provider = null;
6161
}
6262
if (newVal != null && newVal !== oldVal) {
6363
this.#provider = document.querySelector(newVal);
6464
if (this.#provider) {
65-
this.#provider.addEventListener(EventType.OBJECT, this.#providerFetch.bind(this));
65+
this.#provider.addEventListener(EventType.FETCH, this.#providerFetch.bind(this));
6666
this.#provider.addEventListener(EventType.OBJECT, this.#providerObject.bind(this));
67-
this.#provider.addEventListener(EventType.OBJECT, this.#providerDone.bind(this));
67+
this.#provider.addEventListener(EventType.DONE, this.#providerDone.bind(this));
6868
} else {
6969
throw new Error(`Provider "${newVal}" not found`);
7070
}
7171
}
7272
}
7373

74-
#providerFetch(event) {
74+
#providerFetch() {
7575
// Reset the data container
7676
this.#newdata = new Array();
7777
}
@@ -81,7 +81,7 @@ export class ArrayElement extends LitElement {
8181
this.#newdata.push(event.detail);
8282
}
8383

84-
#providerDone(event) {
84+
#providerDone() {
8585
let modified = false;
8686
if (this.#newdata.length !== this.#data.length) {
8787
modified = true;

src/core/EventType.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Define event tyoes used by web components
2+
* Define event types used by web components
33
* @readonly
44
* @enum {String}
55
*/

src/core/ProviderElement.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export class ProviderElement extends LitElement {
4848
if (name === 'path') {
4949
this.#pathChanged(newVal, oldVal);
5050
}
51+
if (name === 'interval') {
52+
this.#intervalChanged(newVal, oldVal);
53+
}
5154
}
5255

5356
render() {
@@ -118,8 +121,17 @@ export class ProviderElement extends LitElement {
118121
#pathChanged(newVal, oldVal) {
119122
if (newVal) {
120123
if(newVal !== oldVal) {
121-
console.log(`path changed: ${oldVal} => ${newVal}`);
122-
this.fetch(newVal);
124+
this.fetch();
125+
}
126+
} else {
127+
this.cancel();
128+
}
129+
}
130+
131+
#intervalChanged(newVal, oldVal) {
132+
if (newVal) {
133+
if(newVal !== oldVal) {
134+
this.fetch();
123135
}
124136
} else {
125137
this.cancel();

src/mapbox/MapElement.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Map } from 'mapbox-gl';
33
import styles from './mapbox.css.txt';
44
import { MapSourceElement } from './MapSourceElement';
55
import { MapLayerElement } from './MapLayerElement';
6+
import { EventType } from '../core/EventType';
67

78
/**
89
* @class MapElement
@@ -96,7 +97,16 @@ export class MapElement extends LitElement {
9697
for (const source of sources) {
9798
this.#map.addSource(source.id, {
9899
type: source.type,
99-
data: source.data,
100+
data: source.geojson
101+
});
102+
103+
// Watch source data changes
104+
source.addEventListener(EventType.CHANGE, (evt) => {
105+
// Source data has changed, update the source
106+
const mapSource = this.#map.getSource(evt.detail.id);
107+
if (mapSource && evt.detail.geojson) {
108+
mapSource.setData(evt.detail.geojson);
109+
}
100110
});
101111
}
102112

@@ -105,16 +115,14 @@ export class MapElement extends LitElement {
105115
for (const layer of layers) {
106116
const source = this.querySelector(`${layer.source}`);
107117
if (!source) {
108-
console.error(`Source ${layer.source} not found for layer ${layer.id}`);
109-
} else {
110-
console.log(`Adding layer ${layer.id} with source ${source.data}`);
111-
this.#map.addLayer({
112-
id: layer.id,
113-
source: source.id,
114-
type: layer.type,
115-
paint: layer.paint,
116-
});
118+
throw new Error(`Source ${layer.source} not found for layer ${layer.id}`);
117119
}
120+
this.#map.addLayer({
121+
id: layer.id,
122+
source: source.id,
123+
type: layer.type,
124+
paint: layer.paint || {},
125+
});
118126
}
119127
});
120128
}

src/mapbox/MapSourceElement.js

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { 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
*/
1315
export 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

Comments
 (0)