-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathLayers.js
197 lines (177 loc) · 6.42 KB
/
Layers.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
BR.Layers = L.Class.extend({
_loadLayers() {
this._customLayers = {};
if (BR.Util.localStorageAvailable()) {
var layers = JSON.parse(localStorage.getItem('map/customLayers'));
if (layers?.type == 'FeatureCollection') {
for (const layerData of layers.features) {
this._addLayer(layerData);
}
}
// convert legacy custom format to GeoJSON Feature
else {
for (a in layers) {
var layerData = {
geometry: null,
properties: {
name: a,
overlay: layers[a].isOverlay,
dataSource: layers[a].dataSource,
query: layers[a].dataSource === 'OverpassAPI' ? layers[a].layer : undefined,
url: layers[a].layer,
},
};
this._addLayer(layerData);
}
}
}
},
_loadTable() {
var layersData = [];
for (layer in this._customLayers) {
var layerProps = this._customLayers[layer].layerData.properties;
if (layerProps.dataSource === 'OverpassAPI') {
layersData.push([layer, layerProps.query, i18next.t('sidebar.layers.table.type_overpass_query')]);
} else {
var isOverlay = layerProps.overlay;
layersData.push([
layer,
layerProps.url,
isOverlay
? i18next.t('sidebar.layers.table.type_overlay')
: i18next.t('sidebar.layers.table.type_layer'),
]);
}
}
if (this._layersTable != null) {
this._layersTable.destroy();
}
this._layersTable = $('#custom_layers_table').DataTable({
data: layersData,
info: false,
searching: false,
bAutoWidth: false,
paging: false,
language: {
emptyTable: i18next.t('sidebar.layers.table.empty'),
},
columns: [
{ title: i18next.t('sidebar.layers.table.name') },
{ title: i18next.t('sidebar.layers.table.URL'), className: 'custom_layers_url' },
{ title: i18next.t('sidebar.layers.table.type') },
],
});
},
init(map, layersControl, baseLayers, overlays) {
this._layersControl = layersControl;
this._map = map;
this._layers = {};
for (var l in overlays) this._layers[l] = [overlays[l], true];
for (var l in baseLayers) this._layers[l] = [baseLayers[l], false];
L.DomUtil.get('custom_layers_add_base').onclick = L.bind(this._addBaseLayer, this);
L.DomUtil.get('custom_layers_add_overlay').onclick = L.bind(this._addOverlay, this);
L.DomUtil.get('custom_layers_add_overpass').onclick = L.bind(this._addOverpassQuery, this);
L.DomUtil.get('custom_layers_remove').onclick = L.bind(this._remove, this);
this._loadLayers();
this._loadTable();
var table = this._layersTable;
$('#custom_layers_table tbody').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});
L.DomUtil.get('custom_layers_button').onclick = function () {
$('#custom_layers').modal();
};
},
_remove(evt) {
var row = this._layersTable.row('.selected').data();
if (row != null) {
var name = row[0];
this._layersControl.removeLayer(this._customLayers[name].layer);
this._map.removeLayer(this._customLayers[name].layer);
delete this._customLayers[name];
this._layersTable.row('.selected').remove().draw(false);
this._sync();
}
},
_addFromInput(layerProps) {
var layer_name = L.DomUtil.get('layer_name').value;
var layer_url = L.DomUtil.get('layer_url').value;
var layerData = {
geometry: null,
properties: {
...layerProps,
name: L.DomUtil.get('layer_name').value,
},
type: 'Feature',
};
if (layer_name.length > 0 && layer_url.length > 0) this._addLayer(layerData);
},
_addBaseLayer(evt) {
var layerProps = {
type: 'tms',
url: L.DomUtil.get('layer_url').value,
};
this._addFromInput(layerProps);
},
_addOverlay(evt) {
var layerProps = {
type: 'tms',
url: L.DomUtil.get('layer_url').value,
overlay: true,
};
this._addFromInput(layerProps);
},
_addOverpassQuery(evt) {
var layerProps = {
overlay: true,
dataSource: 'OverpassAPI',
query: L.DomUtil.get('layer_url').value,
};
this._addFromInput(layerProps);
},
_createTmsProps(props) {
var tmsProps = {
type: 'tms',
...props,
};
return tmsProps;
},
_addLayer(layerData) {
var props = layerData.properties;
var layerName = props.name;
if (layerName in this._layers) return;
if (layerName in this._customLayers) return;
try {
var layer = this._layersControl.layersConfig.createLayer(layerData);
this._customLayers[layerName] = {
layer,
layerData,
};
if (props.overlay) {
this._layersControl.addOverlay(layer, layerName);
} else {
this._layersControl.addBaseLayer(layer, layerName);
}
this._loadTable();
this._sync();
return layer;
} catch (e) {
console.warn('Oops:', e);
return;
}
},
_sync() {
if (BR.Util.localStorageAvailable()) {
var geojson = {
type: 'FeatureCollection',
features: Object.values(this._customLayers).map((layer) => layer.layerData),
};
localStorage.setItem('map/customLayers', JSON.stringify(geojson));
}
},
});