Skip to content

Commit 50f4021

Browse files
author
Stefan Kuethe
committed
Refactor adding and removing controls
1 parent 1cb79be commit 50f4021

File tree

3 files changed

+87
-72
lines changed

3 files changed

+87
-72
lines changed

notebooks/layers/Untitled.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{
5353
"data": {
5454
"application/vnd.jupyter.widget-view+json": {
55-
"model_id": "7769c318a0154c1b9303dd2994ce4035",
55+
"model_id": "46b832d13c90456994750e52e852d7ce",
5656
"version_major": 2,
5757
"version_minor": 1
5858
},
@@ -71,7 +71,7 @@
7171
},
7272
{
7373
"cell_type": "code",
74-
"execution_count": 8,
74+
"execution_count": 7,
7575
"id": "005ffbf7",
7676
"metadata": {},
7777
"outputs": [],
@@ -104,7 +104,7 @@
104104
},
105105
{
106106
"cell_type": "code",
107-
"execution_count": 6,
107+
"execution_count": 8,
108108
"id": "1aa4155d",
109109
"metadata": {},
110110
"outputs": [],
@@ -115,7 +115,7 @@
115115
},
116116
{
117117
"cell_type": "code",
118-
"execution_count": 8,
118+
"execution_count": 9,
119119
"id": "2e7cee1d",
120120
"metadata": {},
121121
"outputs": [],

src/openlayers/js/openlayers.anywidget.js

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/ipywidget-ts/map.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export default class MapWidget {
131131
}
132132

133133
// TODO: Obsolete
134+
/*
134135
transformCenter(viewOptions: ViewOptions): ViewOptions {
135136
const center = viewOptions.center;
136137
if (center && viewOptions.projection !== "EPSG:4326") {
@@ -141,28 +142,27 @@ export default class MapWidget {
141142
}
142143
return viewOptions;
143144
}
144-
145+
*/
145146

146147
getMap(): Map {
147148
return this._map;
148149
}
149150

151+
getMetadata(): Metadata {
152+
return this._metadata;
153+
}
154+
150155
// TODO: Obsolete
151156
getLayerOld(layerId: string): Layer {
152157
return this._layerStore[layerId];
153158
}
154159

155160
// TODO: Obsolete
161+
/*
156162
getControlOld(controlId: string): Control {
157163
return this._controlStore[controlId];
158164
}
159-
160-
getControl(controId: string): Control | undefined {
161-
for (let control of this._map.getControls().getArray()) {
162-
if (control.get("id") === controId)
163-
return control;
164-
}
165-
}
165+
*/
166166

167167
setViewFromSource(layerId: string): void {
168168
const layer = this.getLayer(layerId); // this._layerStore[layerId];
@@ -175,6 +175,7 @@ export default class MapWidget {
175175

176176
}
177177

178+
/*
178179
addGeoJSONToSource(layer: Layer, geoJSONObject: any): void {
179180
if (geoJSONObject === undefined)
180181
return;
@@ -183,6 +184,7 @@ export default class MapWidget {
183184
source.addFeatures(new GeoJSON().readFeatures(geoJSONObject));
184185
console.log("geojsonObject added to VectorSource", geoJSONObject);
185186
}
187+
*/
186188

187189
/*
188190
addLayerOld(layerJSONDef: JSONDef): void {
@@ -198,6 +200,7 @@ export default class MapWidget {
198200
}
199201
*/
200202

203+
// --- Layers
201204
getLayer(layerId: string): Layer | undefined {
202205
for (let layer of this._map.getLayers().getArray()) {
203206
if (layer.get("id") === layerId)
@@ -210,8 +213,7 @@ export default class MapWidget {
210213
this._map.addLayer(layer);
211214
this._metadata.layers.push({
212215
id: layer.get("id"),
213-
type: layerDef[TYPE_IDENTIFIER],
214-
extent: layer.getExtent()
216+
type: layerDef[TYPE_IDENTIFIER]
215217
});
216218
console.log("layer", layer.get("id"), "added", this._metadata);
217219
}
@@ -222,42 +224,56 @@ export default class MapWidget {
222224
return;
223225

224226
this._map.removeLayer(layer);
225-
// delete this._layerStore[layerId];
226227
this._metadata.layers = this._metadata.layers.filter(item => item["id"] != layerId);
227228
console.log("layer", layerId, "removed", this._metadata);
228229
}
229230

230-
addControl(controlJSONDef: JSONDef): void {
231-
const control = jsonConverter.parse(controlJSONDef);
232-
control.set("id", controlJSONDef.id);
233-
// ...
231+
// --- Controls
232+
getControl(controlId: string): Control | undefined {
233+
for (let control of this._map.getControls().getArray()) {
234+
if (control.get("id") === controlId)
235+
return control;
236+
}
237+
}
238+
239+
addControl(controlDef: JSONDef): void {
240+
const control = jsonConverter.parse(controlDef);
241+
control.set("id", controlDef.id);
234242
this._map.addControl(control);
235-
this._controlStore[controlJSONDef.id] = control;
236-
console.log("controlStore", this._controlStore);
243+
this._metadata.controls.push({
244+
id: control.get("id"),
245+
type: controlDef[TYPE_IDENTIFIER],
246+
});
247+
console.log("control", control.get("id"), "added", this._metadata);
237248
}
238249

239250
removeControl(controlId: string): void {
240251
const control = this.getControl(controlId);
241-
if (control === undefined) return;
242-
243-
this._map.removeControl(control);
244-
delete this._controlStore[controlId];
245-
console.log("control", controlId, "removed", this._controlStore);
252+
if (control) {
253+
this._map.removeControl(control);
254+
this._metadata.controls = this._metadata.controls.filter(item => item["id"] != controlId);
255+
console.log("control", controlId, "removed", this._metadata);
256+
}
246257
}
247258

259+
// --- Misc
248260
setLayerStyle(layerId: string, style: any): void {
249261
const layer = this.getLayer(layerId) as VectorLayer | WebGLVectorLayer;
250-
layer.setStyle(style)
262+
if (layer) {
263+
console.log("set layer style", layerId, style);
264+
layer.setStyle(style)
265+
}
251266
}
252267

253268
applyCallToLayer(layerId: string, call: OLAnyWidgetCall): void {
254-
console.log("layer call", "layerId", layerId);
269+
console.log("run layer method", layerId);
255270
const layer = this.getLayer(layerId);
256271

257272
// @ts-expect-error
258273
layer[call.method](...call.args)
259274
}
260275

276+
// TODO: Remove
261277
testJSONDef(jsonDef: JSONDef): any {
262278
return jsonConverter.parse(jsonDef);
263279
}
@@ -266,7 +282,7 @@ export default class MapWidget {
266282
debugData(data: any): void {
267283
}
268284

269-
// ...
285+
// TODO: Test only at the moment
270286
addOverlay(position: Coordinate | undefined): void {
271287
const el = document.createElement("div");
272288
el.style.cssText = "";
@@ -277,7 +293,6 @@ export default class MapWidget {
277293

278294
// ...
279295
addTooltip(prop: string | null): void {
280-
// addTooltipTo(this._map, prop);
281296
addTooltip2(this._map, prop);
282297
}
283298
}

0 commit comments

Comments
 (0)