Skip to content

Commit 7f8e18b

Browse files
committed
fix: return promise
1 parent 601280c commit 7f8e18b

File tree

4 files changed

+69
-49
lines changed

4 files changed

+69
-49
lines changed

docs/configure.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ Behaviors control the look & feel of the map. Changing them will always regenera
158158
|Method|Parameters|Description|
159159
|---|---|---|
160160
|animate|`coordinates`:array, `duration`:number, `properties`:object,`animation`:string|Perform animation on the specified coordinates and for the duration in milliseconds. properties |
161-
|map||Return the last `map object`|
161+
|map|[returns `MapObject`]|Return the last `map`|
162162
|notify|`message`:string, `duration`:number| Show notify `message` at bottom of map. Duration defaults too 2000ms|
163163
|showPopup|`coordinates`:array,`message`:string|Show a popup at the Coordinates with message
164-
|setFeatures|`data`:object/string, `layer`:string,`clear`:boolean|Add one ore more GEOJson features to the given layer, when clear is true all features of layer will be deleted before adding.|
165-
|getFeatures|`layer`:string|Get all features returning a `GEOJson` for the given layer|
164+
|setFeatures|`data`:object/string, `layer`:string,`clear`:boolean, `merge`:boolean [returns `promise`]|Add one ore more GEOJson features to the given layer, when clear is true all features of layer will be deleted before adding. When `merge` is true merge will done base on feature type and location. |
165+
|getFeatures|`layer`:string [returns `promise`]|Get all features returning a `GEOJson` for the given layer|
166166
|clearFeatures|`layer`:string|Clear all features for the given layer|
167-
|setConfig|`json`:object,`filter`:string/array |Set configuration of the plugin by json, filter by properties. When filter is empty array[] all items will be update, when null only layers is updateor the specified in filter|
168-
|getConfig|`filter`:string/array |Get configuration of the plugin by returning `json`, filter by properties. When filter is empty array[] all items will be returned, when null only layers is update, or the specified in filter|
169-
|getZoom||Get current zoom value|
167+
|setConfig|`json`:object,`filter`:string/array |Set configuration of the plugin by json, filter by properties. When filter is empty array[] all items will be update, when null only layers is update or the specified in filter|
168+
|getConfig|`filter`:string/array [returns `object`]|Get configuration of the plugin by returning `json`, filter by properties. When filter is empty array[] all items will be returned, when null only layers is update, or the specified in filter|
169+
|getZoom|[returns `float`]|Get current zoom value|

src/GEOComp.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ var GEOComp = (function () {
255255
eventDefinitions.forEach((k) => { if (k.value == n || k.value == name) { eventName = k.value } })
256256
//Double switch will allow fine grained event catching
257257
switch (name) { //Catch first on name
258+
case 'destroy':
259+
props.events.onChange({})
260+
props.event.onChange({})
261+
props.feature.onChange({})
262+
_events = {}
263+
break;
258264
case 'map:create':
259265
return //Internal event only, user should use map:init
260266
case 'click:feature':
@@ -485,15 +491,20 @@ GEOComp = withMethodExposing(GEOComp, [
485491
type: "string",
486492
},
487493
{
488-
name: "clear (optional)",
494+
name: "clear",
495+
type: "boolean",
496+
}
497+
,
498+
{
499+
name: "merge",
489500
type: "boolean",
490501
}
491502
]
492503
},
493504
execute: async (comp: any, params: any) => {
494505
var map = comp.exposingValues.events['map:create']
495-
if (map) return setFeatures(map, params[0], params[1], params[2] == true)
496-
return false
506+
if (map) return setFeatures(map, params[0], params[1], params[2] == true, params[3] == true)
507+
return Promise.reject(new Error("Map not defined"))
497508
}
498509
},
499510
{
@@ -510,7 +521,7 @@ GEOComp = withMethodExposing(GEOComp, [
510521
execute: async (comp: any, params: any) => {
511522
var map = comp.exposingValues.events['map:create']
512523
if (map) return getFeatures(map, params[0])
513-
return false
524+
return Promise.reject(new Error("Map not defined"))
514525
}
515526
},
516527
{

src/vendors/Geo.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ function Geo(props) {
199199
geoRef.innerHTML = "<div id='GEO_" + geoId + "' " + (featureEnabled('largeButtons') ? "class='ol-large'" : "") +
200200
" style='height:100%;width:100%'></div>"
201201

202+
//Check if we have an old geo map if so clean out
203+
if (map) {
204+
fireEvent("destroy", map)
205+
map.getLayers().clear();
206+
}
202207
//The real map object
203208
var olMap = new Map({
204209
controls: [],

src/vendors/helpers/Layers.js

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -289,54 +289,58 @@ export function findLayer(map, name) {
289289
* @param {boolean} clear Whether to clear existing features before adding new ones
290290
* @returns {boolean} True if the layer was found and updated, false otherwise
291291
*/
292-
export function setFeatures(map, data, name, clear) {
293-
const layer = findLayer(map, name);
294-
if (layer) {
295-
const source = layer.getSource()
296-
const reader = (source.getFormat ? source.getFormat() : null) || new GeoJSON({
297-
dataProjection: source.get('projection') || 'EPSG:4326', // Assuming the GeoJSON is in WGS 84,
298-
featureProjection: map.getView().getProjection() || 'EPSG:3857' // Assuming the map projection
299-
})
292+
export function setFeatures(map, data, name, clear, merge) {
293+
return new Promise((reject, resolve) => {
294+
const layer = findLayer(map, name);
295+
if (layer) {
296+
const source = layer.getSource()
297+
const reader = (source.getFormat ? source.getFormat() : null) || new GeoJSON({
298+
dataProjection: source.get('projection') || 'EPSG:4326', // Assuming the GeoJSON is in WGS 84,
299+
featureProjection: map.getView().getProjection() || 'EPSG:3857' // Assuming the map projection
300+
})
300301

301-
//Check if there is a undo stack connected to this source, if so clear and disable
302-
var undos = []
303-
//Disable all undo stacks
304-
map.getControls().forEach((c) => {
305-
if (c instanceof UndoRedo && c.getActive()) {
306-
undos.push(c)
307-
c.setActive(false)
302+
//Check if there is a undo stack connected to this source, if so clear and disable
303+
var undos = []
304+
//Disable all undo stacks
305+
map.getControls().forEach((c) => {
306+
if (c instanceof UndoRedo && c.getActive()) {
307+
undos.push(c)
308+
c.setActive(false)
309+
}
310+
})
311+
312+
//Check if we should clear te source
313+
if (clear) {
314+
source.clear()
315+
undos.forEach((c) => { c.clear() })
308316
}
309-
})
310317

311-
//Check if we should clear te source
312-
if (clear) {
313-
source.clear()
314-
undos.forEach((c) => { c.clear() })
315-
}
316-
if (reader && data) {
317-
//Now add the features based on types
318-
if (Array.isArray(data)) {
319-
data.forEach((rec) => {
318+
var features
319+
if (reader && data) {
320+
//Now add the features based on types
321+
if (Array.isArray(data)) {
322+
data.forEach((rec) => {
323+
if (source.setFeatures) {
324+
source.setFeatures(reader.readFeatures(rec))
325+
} else {
326+
source.addFeatures(reader.readFeatures(rec))
327+
}
328+
})
329+
} else {
320330
if (source.setFeatures) {
321-
source.setFeatures(reader.readFeatures(rec))
331+
source.setFeatures(reader.readFeatures(data))
322332
} else {
323-
source.addFeatures(reader.readFeatures(rec))
333+
source.addFeatures(reader.readFeatures(data))
324334
}
325-
})
326-
} else {
327-
if (source.setFeatures) {
328-
source.setFeatures(reader.readFeatures(data))
329-
} else {
330-
source.addFeatures(reader.readFeatures(data))
331335
}
332336
}
337+
//Enable the undo stack
338+
undos.forEach((c) => { c.setActive(true) })
339+
//Enable the connected undo check
340+
resolve(features)
333341
}
334-
//Enable the undo stack
335-
undos.forEach((c) => { c.setActive(true) })
336-
//Enable the connected undo check
337-
return true//Exit, work is done
338-
}
339-
return false
342+
reject(new Error('Layer (' + name + ') not found'))
343+
})
340344
}
341345

342346

0 commit comments

Comments
 (0)