Skip to content

Commit 8269737

Browse files
committed
feat: allow hide popup if functions return false
1 parent 65cc1a0 commit 8269737

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
* **@dlr-eoc/layer-control:**
1414
- Fix direction of `@cds` icon
1515

16+
### Features
17+
* **@dlr-eoc/services-layers:**
18+
- Allow return `false` for `popup.popupFunction` and `popup.asyncPopup`;
19+
* **@dlr-eoc/map-ol:**
20+
- Do not add a popup if `popup.popupFunction` or `popup.asyncPopup` return `false`;
21+
1622
### Documentation
1723
- Update `TUTORIALS.md`
1824
- Minor changes in some libraries's `README.md`

projects/map-ol/src/lib/map-ol.service.ts

+49-29
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ export class MapOlService {
20062006
}
20072007

20082008

2009-
public addPopup(popupParams: IPopupParams, popupObj?: popup, popupContent?: string | IAnyObject, event?: 'click' | 'move', removePopups?: boolean) {
2009+
public addPopup(popupParams: IPopupParams, popupObj?: popup, popupContent?: string | IAnyObject | false, event?: 'click' | 'move', removePopups?: boolean) {
20102010
const layerpopup: Layer['popup'] = popupParams.layer.get(POPUP_KEY);
20112011
// check if popup is already there and event is move
20122012
const layerID = popupParams.layer.get(ID_KEY);
@@ -2137,13 +2137,16 @@ export class MapOlService {
21372137

21382138
const container = this.createPopupContainer(overlay, popupParams, popupObj, popupContent, event);
21392139
/** edge case when moving and clicking sometimes the browser event is not like the popup event */
2140-
if (overlay.getId() === moveID) {
2141-
overlay.set('addEvent', 'pointermove');
2142-
} else {
2143-
overlay.set('addEvent', browserEvent.type);
2140+
if(container){
2141+
if (overlay.getId() === moveID) {
2142+
overlay.set('addEvent', 'pointermove');
2143+
} else {
2144+
overlay.set('addEvent', browserEvent.type);
2145+
}
2146+
overlay.set(OVERLAY_TYPE_KEY, POPUP_KEY);
2147+
overlay.setElement(container);
21442148
}
2145-
overlay.set(OVERLAY_TYPE_KEY, POPUP_KEY);
2146-
overlay.setElement(container);
2149+
21472150

21482151
let coordinate;
21492152
// When a point is clicked, its coordinate is used for the popup position.
@@ -2154,7 +2157,11 @@ export class MapOlService {
21542157
coordinate = browserEvent.coordinate;
21552158
}
21562159

2157-
overlay.setPosition(coordinate);
2160+
if(container){
2161+
overlay.setPosition(coordinate);
2162+
}else{
2163+
this.map.removeOverlay(overlay);
2164+
}
21582165

21592166
/**
21602167
* edge case prevent add multiple movePopup's
@@ -2166,14 +2173,17 @@ export class MapOlService {
21662173
}
21672174
}
21682175

2169-
private createPopupContainer(overlay: olOverlay, popupParams: IPopupParams, popupObj?: popup, popupContent?: string | IAnyObject, event?: 'click' | 'move') {
2176+
private createPopupContainer(overlay: olOverlay, popupParams: IPopupParams, popupObj?: popup, popupContent?: string | IAnyObject | false, event?: 'click' | 'move') {
21702177
const content = document.createElement('div');
21712178
content.className = 'ol-popup-content';
21722179
let popupHtml = '';
2180+
let hasContent = true;
21732181
if (popupObj?.popupFunction) {
21742182
const content = popupObj.popupFunction(popupParams);
21752183
if (typeof content === 'string') {
21762184
popupHtml = content;
2185+
} else if (content === false) {
2186+
hasContent = false;
21772187
} else {
21782188
popupHtml = this.createPopupHtml(content);
21792189
}
@@ -2183,9 +2193,14 @@ export class MapOlService {
21832193
} else {
21842194
popupHtml = this.createPopupHtml(popupContent);
21852195
}
2196+
} else if(popupContent === false){
2197+
hasContent = false;
21862198
} else if (Object.keys(popupParams.properties).length) {
21872199
popupHtml = this.createPopupHtml(popupParams.properties);
2200+
}else{
2201+
hasContent = false;
21882202
}
2203+
21892204
content.innerHTML = popupHtml;
21902205
if (popupObj?.dynamicPopup) {
21912206
// To prevent memory leak:
@@ -2197,27 +2212,32 @@ export class MapOlService {
21972212
this.createDynamicPopupComponent(id, content, popupParams, popupObj);
21982213
}
21992214

2200-
const container = document.createElement('div');
2201-
container.className = 'ol-popup';
2202-
container.id = overlay.getId().toString();
2203-
container.style.display = 'block';
2204-
2205-
if (!event || event !== 'move') {
2206-
const closer = document.createElement('a');
2207-
closer.className = 'ol-popup-closer';
2208-
container.appendChild(closer);
2209-
2210-
const closeFunction = () => {
2211-
closer.removeEventListener('click', closeFunction, false);
2212-
// removes ol-part of popup
2213-
this.map.removeOverlay(overlay);
2214-
// removes angular-part of popup
2215-
this.destroyDynamicPopupComponent(overlay.getId().toString());
2216-
};
2217-
closer.addEventListener('click', closeFunction, false);
2215+
if (!hasContent) {
2216+
// this sets the Overlay.setElement(undefined);
2217+
return undefined;
2218+
} else {
2219+
const container = document.createElement('div');
2220+
container.className = 'ol-popup';
2221+
container.id = overlay.getId().toString();
2222+
container.style.display = 'block';
2223+
2224+
if (!event || event !== 'move') {
2225+
const closer = document.createElement('a');
2226+
closer.className = 'ol-popup-closer';
2227+
container.appendChild(closer);
2228+
2229+
const closeFunction = () => {
2230+
closer.removeEventListener('click', closeFunction, false);
2231+
// removes ol-part of popup
2232+
this.map.removeOverlay(overlay);
2233+
// removes angular-part of popup
2234+
this.destroyDynamicPopupComponent(overlay.getId().toString());
2235+
};
2236+
closer.addEventListener('click', closeFunction, false);
2237+
}
2238+
container.appendChild(content);
2239+
return container;
22182240
}
2219-
container.appendChild(content);
2220-
return container;
22212241
}
22222242

22232243
/** USED in map-ol.component */

projects/services-layers/src/lib/types/Layers.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ export interface popup {
5151
/**
5252
* function to create the popup content.
5353
* Return an HTML string or an object from which an HTML string is generated.
54+
* If false is returned, only the function is executed, but no popup is added.
5455
*/
55-
popupFunction?: (popupParams: IPopupParams) => string | IAnyObject;
56+
popupFunction?: (popupParams: IPopupParams) => string | IAnyObject | false;
5657
/**
5758
* async function to create the popup content.
5859
* Pass an HTML string, or an object from which an HTML string is generated, to the callback..
60+
* If no callback is used, only the function is executed, but no popup is added.
5961
*/
60-
asyncPopup?: (popupParams: IPopupParams, cb: (content: string | IAnyObject) => void) => void;
62+
asyncPopup?: (popupParams: IPopupParams, cb: (content: string | IAnyObject | false) => void) => void;
6163
/** create popup using angular component */
6264
dynamicPopup?: {
6365
component: Type<any>;

0 commit comments

Comments
 (0)