Skip to content

feat: add on_click and hover_style to GeomanDrawControl #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions python/ipyleaflet/ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,8 @@ class GeomanDrawControl(DrawControlBase):
_view_name = Unicode("LeafletGeomanDrawControlView").tag(sync=True)
_model_name = Unicode("LeafletGeomanDrawControlModel").tag(sync=True)

_click_callbacks = Instance(CallbackDispatcher, ())

# Current mode & shape
# valid values are: 'draw', 'edit', 'drag', 'remove', 'cut', 'rotate'
# for drawing, the tool can be added after ':' e.g. 'draw:marker'
Expand All @@ -2318,6 +2320,9 @@ class GeomanDrawControl(DrawControlBase):
polygon = Dict({ 'pathOptions': {} }).tag(sync=True)
circlemarker = Dict({ 'pathOptions': {} }).tag(sync=True)

# Hover style (applies for all drawing modes)
hover_style = Dict().tag(sync=True)

# Disabled by default
text = Dict().tag(sync=True)

Expand All @@ -2343,6 +2348,8 @@ def _handle_leaflet_event(self, _, content, buffers):
if not isinstance(geo_json, list):
geo_json = [geo_json]
self._draw_callbacks(self, action=action, geo_json=geo_json)
elif content.get('event', '').startswith('click'):
self._click_callbacks(self, **content)

def on_draw(self, callback, remove=False):
"""Add a draw event listener.
Expand All @@ -2356,6 +2363,19 @@ def on_draw(self, callback, remove=False):
"""
self._draw_callbacks.register_callback(callback, remove=remove)

def on_click(self, callback, remove=False):
"""Add a click event listener.

Parameters
----------
callback : callable
Callback function that will be called on click event.
remove: boolean
Whether to remove this callback or not. Defaults to False.
"""
self._click_callbacks.register_callback(callback, remove=remove)


def clear_text(self):
"""Clear all text."""
self.send({'msg': 'clear_text'})
Expand Down
18 changes: 18 additions & 0 deletions python/jupyter_leaflet/src/controls/GeomanDrawControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ export class LeafletGeomanDrawControlView extends LeafletControlView {
}
},
});

// Click event handler
this.feature_group.on('click', (e) => {
this.send({
event: 'click',
geo_json: this.layer_to_json(e.sourceTarget),
latlng: e.latlng,
});
});
// Hover style
this.feature_group.on('mouseover', (e) => {
const layer = e.sourceTarget;
layer.setStyle(this.model.get('hover_style'));
layer.once('mouseout', () => {
this.feature_group.resetStyle(layer);
});
});

this.data_to_layers();
this.map_view.obj.addLayer(this.feature_group);

Expand Down
Loading