1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 | 1×
1×
1×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
3×
3×
3×
3×
3×
4×
2×
2×
2×
2×
1×
1×
2×
1×
1×
2×
2×
2×
2×
4×
4×
4×
4×
4×
4×
6×
4×
3×
3×
1×
1×
1×
1×
1×
1×
1×
1×
4×
4×
1×
4×
1×
4×
4×
4×
1×
4×
4×
4×
4×
4×
4×
| import $ from 'jquery';
import FilterForm from './filterui';
import L from 'leaflet';
/** AJAX map widget.
* @constructor
* @param {string} name - widget name used as container ID and
endpoint parameter
* @param {string} formID - optional filter ID
* @param {QuerybuilderAPI} api - the endpoint
* @param {string} params - manually defined string that can be passed
to the endpoint
*/
class Map {
constructor(name, formID, api, params) {
this.arrayMarkers = [];
this.map = null;
this.layerData = null;
this.formID = formID ? '#' + formID : null;
this.mapWidgetId = name;
this.api = api;
this.params = params;
new FilterForm(this.formID);
this._setUpdateMapEventHandling('#' + this.mapWidgetId);
this.init();
}
init() {
this.triggerMap = triggerMapFactory(this.mapWidgetId);
this.map_class = new MapLinker(this.formID,
this.api,
this.params, this.triggerMap,
this.mapWidgetId);
this.layerData = new L.LayerGroup();
this.arrayMarkers = [];
this.map = getLeafletMap(this.mapWidgetId, this.layerData);
this.map_class.retrieveData({containerID: this.mapWidgetId},
this.params, this.triggerMap);
}
addMarker (obj) {
var marker = L.marker({"lat": obj.latitude, "lng": obj.longitude});
var popupText = obj.description;
marker.bindPopup(popupText);
marker.addTo(this.layerData);
this.arrayMarkers.push(marker);
}
_setUpdateMapEventHandling(widgetId) {
$(widgetId).on('update:Map', this._updateMap.bind(this));
}
_updateMap(event, filterData) {
this._removeAllLayers();
this._addMarkers(filterData);
this._zoomLeafletViewport();
}
_zoomLeafletViewport() {
if (this.arrayMarkers.length > 0) {
var group = L.featureGroup(this.arrayMarkers);
this.map.fitBounds(group.getBounds());
}
}
_addMarkers(filterData) {
if (filterData) {
filterData.data.forEach(this._addSingleMarker.bind(this));
}
}
_addSingleMarker(marker) {
this.addMarker(marker);
}
_removeAllLayers() {
this.arrayMarkers.forEach(this._removeLayer.bind(this));
this.arrayMarkers = [];
}
_removeLayer(layer) {
this.map.removeLayer(layer);
this.layerData.removeLayer(layer);
}
}
class MapLinker {
constructor(formID, api, params, triggerMap, mapWidgetId) {
this.mapWidgetId = mapWidgetId;
this.api = api;
this._getData(formID);
this.triggerMap = triggerMap;
this.params = params;
}
retrieveData(queryConfig, params, callback) {
return this.api.retrieveData(this.mapWidgetId,
queryConfig, params, callback);
}
_getData(formID) {
if (formID) {
this.form = $(formID).data('FilterForm');
this.form.onSubmit(this._getFilteredData.bind(this));
} else {
this._getDefaultData();
}
}
_getDefaultData() {
var queryConfig = {containerID: this.mapWidgetId};
this.retrieveData(queryConfig, this.params, this.triggerMap);
}
_getFilteredData(event) {
event.preventDefault();
event.stopPropagation();
var queryConfig = this.form.serialize();
this.retrieveData(queryConfig, this.params, this.triggerMap);
}
}
function triggerMapFactory(containerID) {
var container = containerID ? containerID : 'filter';
return function (filteredData) {
$('#' + container).trigger("update:Map", [filteredData]);
};
}
function getLeafletConfig(osm, layerData) {
return {
center: [0.01, 51.405],
zoom: 13,
fullscreenControl: true,
fullscreenOptions: {
position: 'topleft'
},
layers: [osm, layerData]
};
}
function getOsmTileLayer() {
var osmURL = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
var osmAttrib = '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
return new L.TileLayer(osmURL, {attribution: osmAttrib});
}
function getLeafletMap(widgetId, layerData) {
var osm = getOsmTileLayer();
var map = L.map(widgetId, getLeafletConfig(osm, layerData));
var baseLayers = {"OpenStreetMap": osm};
var overlays = {"Data Layer": layerData};
L.control.layers(baseLayers, overlays).addTo(map);
return map;
}
export default Map;
|