Project

General

Profile

Actions

Bug #12393

open

WLeafletMap JS error when refreshing after panning

Added by Raf Pauwels 3 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
02/08/2024
Due date:
% Done:

0%

Estimated time:

Description

When you pan a WLeafletMap after its coordinates were initially set using panTo() and then rerender the session, a JS error occurs: Wt4_10_3.$(...).wtObj is undefined.

A minimal example is this:

LeafletApplication::LeafletApplication(const Wt::WEnvironment &env)
  : WApplication(env)
{
  auto map = root()->addNew<Wt::WLeafletMap>();
  map->panTo(Wt::WLeafletMap::Coordinate(50.906705, 4.655800));
  auto button = root()->addNew<Wt::WPushButton>("Crash and burn, please");
  button->clicked().connect([this, map] {
    map->panTo(Wt::WLeafletMap::Coordinate(0.0, 0.0));
    redirect(url());
  });
}

The application uses Leaflet 1.9.4 with reload-is-new-session set to false:

<session-management>
  <reload-is-new-session>false</reload-is-new-session>
</session-management>
<properties>
  <property name="leafletJSURL">https://unpkg.com/leaflet@1.9.4/dist/leaflet.js</property>
  <property name="leafletCSSURL">https://unpkg.com/leaflet@1.9.4/dist/leaflet.css</property>
</properties>

The JS error occurs when clicking the button. The error is propagated to the server logs upon clicking again. This can also be triggered by panning and then refreshing manually (after adding an initial size and layer).

Note that when Leaflet option trackResize is true (the default), this also occurs upon resize because the map is then panned automatically. A resize is triggered e.g. when the virtual keyboard opens on Android (even when the map is hidden).

  • Panning causes Leaflet to emit moveend, which causes WLeafletMap::position_ to be changed
  • Upon rerendering, WLeafletMap::defineJavaScript() is re-executed
  • The JS members WLeafletMap (constructor) and wtResize were initially assigned and still exist because we rerender the existing session
  • Member WLeafletMap has the position_ coordinates in its parameters, which now differ from the initial coordinates
  • WWebWidget::setJavaScriptMember() and WWebWidget::addJavaScriptStatement will cause member changes to be put into jsStatements_
  • In WWebWidget::updateDom(), declareJavaScriptMember() is first executed for jsMembers_ that are not in jsStatements_
  • wtResize does not occur in jsStatements_ (because it has not changed), but WLeafletMap does, so assignment for wtResize is executed first

No data to display

Actions

Also available in: Atom PDF