Skip to content

Fix issues with overriding maps and loading Leaflet#11105

Merged
andreslucena merged 6 commits intodecidim:developfrom
mainio:fix/map-overriding
Jul 5, 2023
Merged

Fix issues with overriding maps and loading Leaflet#11105
andreslucena merged 6 commits intodecidim:developfrom
mainio:fix/map-overriding

Conversation

@ahukkanen
Copy link
Copy Markdown
Contributor

@ahukkanen ahukkanen commented Jun 29, 2023

🎩 What? Why?

There are several problems when trying to override the maps currently:

  • The core map assets are loaded after the map element has been created but this is problematic because additional map assets can be added during that block as per the map customization documentation.
  • The Leaflet package is loaded several times causing issues with the Here provider with the map customizations. Every time the Leaflet package is loaded, it adds a local version of that library to the file where it is loaded to causing the Here tile layer not to be available in the global L constant.
  • The Here tile layer package is not loaded within the Here provider but within the drag_markers.js map controller which is incorrect as we do not want this file to be included for non-Here providers.

This PR fixes the problems by fixing the map assets and the order of inclusion when the map element is created.

In this PR I am also adding a custom map factory within the decidim-dev gem because this file does not need to be bundled to the production applications, this is why I added it to decidim-dev. This is only used to test that the map factory can be customized and is working correctly. This has to be tested end-to-end why it needs its own asset pack.

This fix should be also backported but it is not straight forward as the map inclusion strategy has changed due to Shakapacker. I can work on the backports once this is merged.

📌 Related Issues

Testing

To see the loading error:

  • Checkout develop and make sure your development app is up to date
  • Within the decidim-dev gem add the following files from this PR:
    • decidim-dev/app/packs/entrypoints/decidim_dev_test_custom_map.js
    • decidim-dev/app/packs/src/decidim/dev/test/custom_map_factory.js
  • Register this new pack within the assets.rb configuration of the decidim-dev gem (as in this PR)
  • Configure your application to use Here maps
    • config.maps = { provider: :here, api_key: "12345" } within the Decidim initializer
  • Add the following snippet (see below) to the index page of proposals and visit that page
  • See errors in the developer console

The snippet to use the custom map within the proposals index page (for instance):

<%= dynamic_map_for type: "custom", markers: [{ title: "Test 1", latitude: 41.385063, longitude: 2.173404 }], popup_template_id: "custom-popup" do %>
  <% append_javascript_pack_tag("decidim_dev_test_custom_map") %>
  <template id="custom-popup">
    <div>
      <h3>${title}</h3>
    </div>
  </template>
<% end %>

After this error is solved as per this PR, reload that page and see the map working correctly without errors. Note that you may have to adjust the content security policy depending on your map configuration.

Also after this PR is applied, you should see a couple of paragraphs of text added to the bottom of the page where the map is added to indicate that the map customization is working correctly. There is a spec added in this PR to test that for both OSM and Here.

The added system spec to the decidim-dev gem is testing this exact case for both OSM and Here.

📷 Screenshots

This is a screenshot of the developer console when trying to load the custom map:
Here tilelayer problems

In text format for easier searching:

jquery.js:3783 Uncaught TypeError: L.TileLayer.HERE is not a constructor
    at push.../decidim-core/app/packs/src/decidim/vendor/leaflet-tilelayer-here.js.L.tileLayer.here (leaflet-tilelayer-here.js:211:1)
    at HTMLDivElement.<anonymous> (here.js:13:1)
    at HTMLDivElement.dispatch (jquery.js:5135:1)
    at elemData.handle (jquery.js:4939:1)
    at Object.trigger (jquery.js:8619:1)
    at HTMLDivElement.<anonymous> (jquery.js:8697:1)
    at Function.each (jquery.js:383:1)
    at jQuery.fn.init.each (jquery.js:205:1)
    at jQuery.fn.init.trigger (jquery.js:8696:1)
    at HTMLDivElement.<anonymous> (map.js:29:1)

@ahukkanen ahukkanen added module: core type: fix PRs that implement a fix for a bug labels Jun 29, 2023
@ahukkanen ahukkanen requested a review from a team June 29, 2023 11:07
Copy link
Copy Markdown
Member

@andreslucena andreslucena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it locally and it works 👌🏽
Outstanding work with the "Testing" explanation, it'd be difficult if I didn't have that.

I have a couple of suggestions and this would be ready to be merged.

Copy link
Copy Markdown
Member

@andreslucena andreslucena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

@andreslucena andreslucena merged commit 253cf01 into decidim:develop Jul 5, 2023
entantoencuanto added a commit that referenced this pull request Jul 7, 2023
* feature/redesign:
  Redesign: replace cells with redesigned versions if present and update references (#11123)
  Remove duplicated constant
  Simplify logic
  Enable specs
  Fix leaflet
  fix failing specs
  Fix issues with overriding maps and loading Leaflet (#11105)
  Update decidim-proposals/app/views/decidim/proposals/proposals/new.html.erb
  Update decidim-comments/lib/decidim/comments/comments_helper.rb
  Update decidim-assemblies/spec/system/filter_assemblies_spec.rb
  Update decidim-assemblies/spec/system/filter_assemblies_spec.rb
  Document how to work locally with Elections/Votings (#10870)
  Fix Admin dashboard disappear if you are in Trustee Zone (#11111)
  Fix Shakapacker upgrade does not work for existing instances (#10814)
  Fix for sending welcome emails for new participants (#10991)
  Fix seeded trustees (#10964)
@ahukkanen ahukkanen deleted the fix/map-overriding branch July 12, 2023 12:47
andreslucena pushed a commit that referenced this pull request Jul 18, 2023
* Fix issues with overriding maps and loading Leaflet

* Remove commented configuration

* Prevent external requests during the custom map spec

* Test that the custom map also shows the marker correctly

* Correct the dummy tiles route for OSM maps

* Apply review recommendations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: core type: fix PRs that implement a fix for a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants