Skip to content

Commit 649700d

Browse files
kgiacobbimartinRenou
authored andcommitted
Add Attribution control (#398)
* add AttributionControl JS * Add import * Change options JS Side * Add AttributionControl class * Add attributionControl in class Map * fix wrong variable use * Fix position of the AttributionControl
1 parent fe8c6e2 commit 649700d

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

ipyleaflet/leaflet.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,13 @@ class ZoomControl(Control):
788788
zoom_out_title = Unicode('Zoom out').tag(sync=True, o=True)
789789

790790

791+
class AttributionControl(Control):
792+
_view_name = Unicode('LeafletAttributionControlView').tag(sync=True)
793+
_model_name = Unicode('LeafletAttributionControlModel').tag(sync=True)
794+
795+
prefix = Unicode('Leaflet').tag(sync=True, o=True)
796+
797+
791798
class MapStyle(Style, Widget):
792799
""" Map Style Widget """
793800
_model_name = Unicode('LeafletMapStyleModel').tag(sync=True)
@@ -842,7 +849,6 @@ class Map(DOMWidget, InteractMixin):
842849
inertia_deceleration = Int(3000).tag(sync=True, o=True)
843850
inertia_max_speed = Int(1500).tag(sync=True, o=True)
844851
# inertia_threshold = Int(?, o=True).tag(sync=True)
845-
attribution_control = Bool(True).tag(sync=True, o=True)
846852
# fade_animation = Bool(?).tag(sync=True, o=True)
847853
# zoom_animation = Bool(?).tag(sync=True, o=True)
848854
zoom_animation_threshold = Int(4).tag(sync=True, o=True)
@@ -858,6 +864,9 @@ class Map(DOMWidget, InteractMixin):
858864
zoom_control = Bool(True)
859865
zoom_control_instance = ZoomControl()
860866

867+
attribution_control = Bool(True)
868+
attribution_control_instance = AttributionControl(position='bottomright')
869+
861870
@default('dragging_style')
862871
def _default_dragging_style(self):
863872
return {'cursor': 'move'}
@@ -896,6 +905,9 @@ def __init__(self, **kwargs):
896905

897906
if self.zoom_control:
898907
self.add_control(self.zoom_control_instance)
908+
909+
if self.attribution_control:
910+
self.add_control(self.attribution_control_instance)
899911

900912
@observe('zoom_control')
901913
def observe_zoom_control(self, change):
@@ -905,6 +917,14 @@ def observe_zoom_control(self, change):
905917
if self.zoom_control_instance in self.controls:
906918
self.remove_control(self.zoom_control_instance)
907919

920+
@observe('attribution_control')
921+
def observe_attribution_control(self, change):
922+
if change['new']:
923+
self.add_control(self.attribution_control_instance)
924+
else:
925+
if self.attribution_control_instance in self.controls:
926+
self.remove_control(self.attribution_control_instance)
927+
908928
def _fire_children_displayed(self, widget, **kwargs):
909929
for layer in self.layers:
910930
layer._handle_displayed(**kwargs)

js/src/Map.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ var LeafletMapModel = widgets.DOMWidgetModel.extend({
5656
inertia_deceleration : 3000,
5757
inertia_max_speed : 1500,
5858
// inertia_threshold : int(?)
59-
attribution_control : true,
6059
// fade_animation : bool(?),
6160
// zoom_animation : bool(?),
6261
zoom_animation_threshold : 4,
@@ -200,7 +199,9 @@ var LeafletMapView = utils.LeafletDOMWidgetView.extend({
200199
var options = _.extend(
201200
{
202201
crs: L.CRS[this.model.get('crs')],
203-
zoomControl: false
202+
zoomControl: false,
203+
attributionControl : false,
204+
204205
},
205206
this.get_options()
206207
);

js/src/controls/AttributionControl.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var widgets = require('@jupyter-widgets/base');
2+
var _ = require('underscore');
3+
var L = require('../leaflet.js');
4+
var control = require('./Control.js');
5+
var LeafletControlView = control.LeafletControlView;
6+
var LeafletControlModel = control.LeafletControlModel;
7+
8+
var LeafletAttributionControlModel = LeafletControlModel.extend({
9+
defaults: _.extend({}, LeafletControlModel.prototype.defaults, {
10+
_view_name: 'LeafletAttributionControlView',
11+
_model_name: 'LeafletAttributionControlModel',
12+
13+
})
14+
});
15+
16+
var LeafletAttributionControlView = LeafletControlView.extend({
17+
initialize: function (parameters) {
18+
LeafletAttributionControlView.__super__.initialize.apply(this, arguments);
19+
this.map_view = this.options.map_view;
20+
},
21+
22+
create_obj: function () {
23+
this.obj = L.control.attribution(this.get_options());
24+
},
25+
});
26+
27+
module.exports = {
28+
LeafletAttributionControlView : LeafletAttributionControlView,
29+
LeafletAttributionControlModel : LeafletAttributionControlModel,
30+
};

js/src/jupyter-leaflet.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var featuregroup = require('./layers/FeatureGroup.js');
2424
var geojson = require('./layers/GeoJSON.js');
2525

2626
//Controls
27+
var attributioncontrol = require('./controls/AttributionControl.js')
2728
var control = require('./controls/Control.js');
2829
var splitmapcontrol = require('./controls/SplitMapControl.js');
2930
var layerscontrol = require('./controls/LayersControl.js');
@@ -79,6 +80,7 @@ module.exports = {
7980
LeafletLayerGroupView : layergroup.LeafletLayerGroupView,
8081
LeafletFeatureGroupView : featuregroup.LeafletFeatureGroupView,
8182
LeafletGeoJSONView : geojson.LeafletGeoJSONView,
83+
LeafletAttributionControlView : attributioncontrol.LeafletAttributionControlView,
8284
LeafletControlView : control.LeafletControlView,
8385
LeafletLayersControlView : layerscontrol.LeafletLayersControlView,
8486
LeafletMeasureControlView : measurecontrol.LeafletMeasureControlView,
@@ -114,6 +116,7 @@ module.exports = {
114116
LeafletLayerGroupModel : layergroup.LeafletLayerGroupModel,
115117
LeafletFeatureGroupModel : featuregroup.LeafletFeatureGroupModel,
116118
LeafletGeoJSONModel : geojson.LeafletGeoJSONModel,
119+
LeafletAttributionControlModel : attributioncontrol.LeafletAttributionControlModel,
117120
LeafletControlModel : control.LeafletControlModel,
118121
LeafletLayersControlModel : layerscontrol.LeafletLayersControlModel,
119122
LeafletMeasureControlModel : measurecontrol.LeafletMeasureControlModel,

0 commit comments

Comments
 (0)