Skip to content

Commit

Permalink
feature: add return self wherever sensible to allow call chaining (…
Browse files Browse the repository at this point in the history
…Javascript-style). (#15)

This commit does not change current behavior at all.
`return self` statement was only added to methods not having an explicit `return statement.`
  • Loading branch information
mcondarelli authored Nov 15, 2021
1 parent 9636be5 commit 1ea257d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
10 changes: 8 additions & 2 deletions pyqtlet2/leaflet/layer/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ def _getNewLayerName(self):

def addTo(self, map_):
map_.addLayer(self)
return self

def removeFrom(self, map_):
map_.removeLayer(self)
return self

def bindPopup(self, content, options=None):
js = '{layerName}.bindPopup("{content}"'.format(
Expand All @@ -51,10 +53,12 @@ def bindPopup(self, content, options=None):
js += ', {options}'.format(self._stringifyForJs(options))
js += ')'
self.runJavaScript(js)

return self

def unbindPopup(self):
js = '{layerName}.unbindPopup()'.format(layerName=self._layerName)
self.runJavaScript(js)
return self

def bindTooltip(self, content, options=None):
js = '{layerName}.bindTooltip("{content}"'.format(
Expand All @@ -63,9 +67,11 @@ def bindTooltip(self, content, options=None):
js += ', {options}'.format(self._stringifyForJs(options))
js += ')'
self.runJavaScript(js)

return self

def unbindTooltip(self):
js = '{layerName}.unbindTooltip()'.format(layerName=self._layerName)
self.runJavaScript(js)
return self


6 changes: 5 additions & 1 deletion pyqtlet2/leaflet/layer/marker/marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,23 @@ def setLatLng(self, latLng):
js = '{layerName}.setLatLng({latLng})'.format(
layerName=self._layerName, latLng=latLng)
self.runJavaScript(js)
return self

def setOpacity(self, opacity):
self.opacity = opacity
js = '{layerName}.setOpacity({opacity})'.format(
layerName=self._layerName, opacity=self.opacity)
self.runJavaScript(js)
return self

def setDragging(self, draggable):
self.draggable = draggable
option = 'enable' if self.draggable else 'disable'
js = '{layerName}.dragging.{option}();'.format(layerName=self._layerName, option=option)
self.runJavaScript(js)
return self

def setIcon(self, icon: Icon):
js = '{layerName}.setIcon({markerIcon});'.format(layerName=self._layerName, markerIcon=icon._layerName)
self.runJavaScript(js)
self.runJavaScript(js)
return self
13 changes: 12 additions & 1 deletion pyqtlet2/leaflet/map/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ def setView(self, latLng, zoom=None, options=None):
js += ', {options}'.format(options=options)
js += ');'
self.runJavaScript(js)
return self

def addLayer(self, layer):
self._layers.append(layer)
layer.map = self
js = 'map.addLayer({layerName})'.format(layerName=layer.layerName)
self.runJavaScript(js)
return self

def removeLayer(self, layer):
if layer not in self._layers:
Expand All @@ -118,12 +120,14 @@ def removeLayer(self, layer):
layer.map = None
js = 'map.removeLayer({layerName})'.format(layerName=layer.layerName)
self.runJavaScript(js)
return self

def addControl(self, control):
self._controls.append(control)
control.map = self
js = 'map.addControl({controlName})'.format(controlName=control.controlName)
self.runJavaScript(js)
return self

def removeControl(self, control):
if control not in self._controls:
Expand All @@ -133,6 +137,7 @@ def removeControl(self, control):
control.map = None
js = 'map.removeControl({controlName})'.format(controlName=control.controlName)
self.runJavaScript(js)
return self

def getBounds(self, callback):
return self.getJsResponse('map.getBounds()', callback)
Expand All @@ -155,29 +160,35 @@ def setZoom(self, zoom, options=None):
js += ', {options}'.format(options=options)
js += ');'
self.runJavaScript(js)
return self

def setMaxBounds(self, bounds):
js = 'map.setMaxBounds({bounds})'.format(bounds=bounds)
self.runJavaScript(js)
return self

def fitBounds(self, bounds):
js = 'map.fitBounds({bounds})'.format(bounds=bounds)
self.runJavaScript(js)
return self

def setMaxZoom(self, zoom):
js = 'map.setMaxZoom({zoom})'.format(zoom=zoom)
self.runJavaScript(js)
return self

def setMinZoom(self, zoom):
js = 'map.setMinZoom({zoom})'.format(zoom=zoom)
self.runJavaScript(js)
return self

def panTo(self, latLng, options=None):
js = 'map.panTo({latLng}'.format(latLng=latLng)
if options:
js += ', {options}'.format(options=options)
js += ');'
self.runJavaScript(js)
return self

def flyTo(self, latLng, zoom=None, options=None):
js = 'map.flyTo({latLng}'.format(latLng=latLng)
Expand All @@ -187,4 +198,4 @@ def flyTo(self, latLng, zoom=None, options=None):
js += ', {options}'.format(options=options)
js += ');'
self.runJavaScript(js)

return self

0 comments on commit 1ea257d

Please sign in to comment.