Project

General

Profile

Revision d8e5f1be

IDd8e5f1be0d95922b6aa1a1678d590eeff6e7599c
Parent 0f32b53b
Child 29796987

Added by Andreas Kohlbecker 6 months ago

fix #9175 map detects empty kml layer and removes itself

View differences:

modules/cdm_dataportal/includes/maps.inc
57 57

  
58 58
  $map_settings = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
59 59

  
60
    if($force_map_type === NULL){
61
      $force_map_type = $map_settings['map_type'];
62
    }
60
  if($force_map_type === NULL){
61
    $force_map_type = $map_settings['map_type'];
62
  }
63 63

  
64
    if ($force_map_type == 1) {
65
      _add_jquery_ui();
66
      $map_html = cdm_map_openlayers(
67
        $map_id,
68
        $occurrence_query,
69
        $kml_request_url,
70
        $distribution_query,
71
        $legend_format_query,
72
        $map_settings['caption'],
73
        $event_listeners,
74
        $resizable
75
      );
76
    }
77
     else {
78
      $map_height = round($map_settings['image_map']['width'] / (float)$map_settings['aspect_ratio']);
79
      $map_html = cdm_map_plain_image(
80
        $map_settings['image_map']['width'],
81
        $map_height,
82
        $occurrence_query,
83
        $distribution_query,
84
        $legend_format_query,
85
        $map_settings['caption']
86
      );
87
     }
64
  if ($force_map_type == 1) {
65
    _add_jquery_ui();
66
    $map_html = cdm_map_openlayers(
67
      $map_id,
68
      $occurrence_query,
69
      $kml_request_url,
70
      $distribution_query,
71
      $legend_format_query,
72
      $map_settings['caption'],
73
      $event_listeners,
74
      $resizable
75
    );
76
  }
77
   else {
78
    $map_height = round($map_settings['image_map']['width'] / (float)$map_settings['aspect_ratio']);
79
    $map_html = cdm_map_plain_image(
80
      $map_settings['image_map']['width'],
81
      $map_height,
82
      $occurrence_query,
83
      $distribution_query,
84
      $legend_format_query,
85
      $map_settings['caption']
86
    );
87
   }
88 88
  return markup_to_render_array($map_html);
89 89
}
90 90

  
modules/cdm_dataportal/js/map/openlayers_map.js
37 37
    defaultBaseLayerName: 'open_topomap',
38 38
    maxZoom: 15,
39 39
    minZoom: 0,
40
      // hide the map when the data layer has no features
41
      hideEmptyMap: true,
40 42
    debug: true,
41 43
    /**
42 44
     * allows the map to display parts of the layers which are outside
......
269 271
      }
270 272

  
271 273
        /**
274
         * Removes the map and the parent container from the
275
         * DOM and destroys the OpenLayers map object
276
         */
277
        function removeMap() {
278
            //  if you are using an application which removes a container
279
            //  of the map from the DOM, you need to ensure that you destroy the map before this happens;
280
            map.destroy;
281
            jQuery(map.div).parent().remove();
282
        }
283

  
284
        /**
272 285
         *
273 286
         */
274 287
        this.create = function(){ // public function
......
379 392
                    "featureselected": onKmlFeatureSelect,
380 393
                    "featureunselected": onKmlFeatureUnselect,
381 394
                    'loadend': function(event) {
382
                        applyLayerZoomBounds(event);
383
                        disablePolygonFeatureClick(event);
395
                        if(opts.hideEmptyMap && kmlLayer.features.length == 0){
396
                            log("No feature in KML layer, removing map ...")
397
                            removeMap();
398
                        } else {
399
                            applyLayerZoomBounds(event);
400
                            disablePolygonFeatureClick(event);
401
                        }
384 402
                    }
385 403
                });
386 404
                map.addControl(kmlSelectControl);
src/main/java/eu/etaxonomy/dataportal/elements/OpenLayersMap.java
1
/**
2
* Copyright (C) 2020 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal.elements;
10

  
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.stream.Collectors;
14

  
15
import org.apache.log4j.Logger;
16
import org.openqa.selenium.By;
17
import org.openqa.selenium.NoSuchElementException;
18
import org.openqa.selenium.WebElement;
19

  
20
import eu.etaxonomy.dataportal.pages.PortalPage;
21
import eu.etaxonomy.dataportal.selenium.UrlLoaded;
22

  
23
/**
24
 * @author a.kohlbecker
25
 * @since Jul 31, 2020
26
 */
27
public class OpenLayersMap {
28

  
29
    private WebElement webElement;
30
    private String mapId;
31
    private String mapName;
32

  
33
    private OpenLayersMap(WebElement webElement) {
34
        this.webElement = webElement;
35
        this.mapId = webElement.getAttribute("id");
36
        if (this.mapId != null) {
37
            this.mapName = webElement.getAttribute("id").replace("openlayers-container-", "");
38
        }
39
    }
40

  
41
    public static List<OpenLayersMap> findOpenLayersMaps(PortalPage page) {
42
        try {
43
            page.getWait().until(new UrlLoaded(page.getPageURL().toString()));
44
            List<WebElement> maps = page.getDataPortalContent().getElement()
45
                    .findElements(By.className("openlayers-container"));
46
            return maps.stream().map(m -> new OpenLayersMap(m)).collect(Collectors.toList());
47
        } catch (NoSuchElementException e) {
48
            Logger.getLogger(OpenLayersMap.class).info("No maps found", e);
49
            return new ArrayList<>();
50
        }
51
    }
52

  
53
    /**
54
     * @return the webElement
55
     */
56
    public WebElement getWebElement() {
57
        return webElement;
58
    }
59

  
60
    /**
61
     * @return the mapId
62
     */
63
    public String getMapId() {
64
        return mapId;
65
    }
66

  
67
    /**
68
     * @return the mapName
69
     */
70
    public String getMapName() {
71
        return mapName;
72
    }
73

  
74
}
src/test/java/eu/etaxonomy/dataportal/selenium/tests/reference/MapTest.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal.selenium.tests.reference;
10

  
11
import java.io.UnsupportedEncodingException;
12
import java.net.MalformedURLException;
13
import java.util.List;
14
import java.util.UUID;
15

  
16
import org.junit.Before;
17
import org.junit.Test;
18

  
19
import eu.etaxonomy.dataportal.DataPortalSite;
20
import eu.etaxonomy.dataportal.elements.OpenLayersMap;
21
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
22
import eu.etaxonomy.dataportal.junit.DataPortalContextSuite.DataPortalContexts;
23
import eu.etaxonomy.dataportal.pages.NamePage;
24
import eu.etaxonomy.dataportal.pages.RegistrationPage;
25

  
26
/**
27
 * @author a.kohlbecker
28
 * @since Feb 5, 2019
29
 *
30
 */
31
@DataPortalContexts( { DataPortalSite.reference })
32
public class MapTest extends CdmDataPortalTestBase {
33

  
34

  
35
    private static final String nodosilinea_radiophila_regid = "http://testbank.org/100004";
36

  
37
    private static final String ramsaria_regid = "http://testbank.org/100005";
38

  
39
    private static final UUID nodosilinea_radiophila_name_UUID = UUID.fromString("e97cc25b-ec11-4bb8-88d7-ab40a023f3fb");
40

  
41
    private static final UUID ramsaria_name_UUID = UUID.fromString("3a6d4bf2-5c89-4525-9e87-0bacac96990b");
42

  
43

  
44
    String titleSuffix = " | Integration test reference";
45

  
46

  
47

  
48
    @Before
49
    public void setUp() throws Exception {
50
        driver.get(getContext().getBaseUri().toString());
51
    }
52

  
53
    @Test
54
    public void namePageMapShown() throws MalformedURLException{
55

  
56
        NamePage p = new NamePage(driver, getContext(), nodosilinea_radiophila_name_UUID);
57
        List<OpenLayersMap> openLayersMaps = OpenLayersMap.findOpenLayersMaps(p);
58
        assertFalse(openLayersMaps.isEmpty());
59
        assertEquals(1, openLayersMaps.size());
60
        assertEquals("specimens", openLayersMaps.get(0).getMapName());
61
    }
62

  
63
    @Test
64
    public void registrationPageMapShown() throws MalformedURLException, UnsupportedEncodingException{
65

  
66
        RegistrationPage p = new RegistrationPage(driver, getContext(), nodosilinea_radiophila_regid);
67
        List<OpenLayersMap> openLayersMaps = OpenLayersMap.findOpenLayersMaps(p);
68
        assertFalse(openLayersMaps.isEmpty());
69
        assertEquals(1, openLayersMaps.size());
70
        assertEquals("specimens", openLayersMaps.get(0).getMapName());
71
    }
72

  
73
    @Test
74
    public void namePageMapHidden() throws MalformedURLException{
75

  
76
        NamePage p = new NamePage(driver, getContext(), ramsaria_name_UUID);
77
        List<OpenLayersMap> openLayersMaps = OpenLayersMap.findOpenLayersMaps(p);
78
        assertTrue(openLayersMaps.isEmpty());
79
    }
80

  
81
    @Test
82
    public void registrationPageMapHidden() throws MalformedURLException, UnsupportedEncodingException{
83

  
84
        RegistrationPage p = new RegistrationPage(driver, getContext(), ramsaria_regid);
85
        List<OpenLayersMap> openLayersMaps = OpenLayersMap.findOpenLayersMaps(p);
86
        assertTrue(openLayersMaps.isEmpty());
87
    }
88

  
89
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)