]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.share.js
Refactor out 'globals' from more functions
[rails.git] / app / assets / javascripts / leaflet.share.js
1 L.Control.Share = L.Control.extend({
2     options: {
3         position: 'topright',
4         title: 'Share',
5         url: function(map) {
6             return '';
7         }
8     },
9
10     onAdd: function (map) {
11         var className = 'control-share',
12             container = L.DomUtil.create('div', className);
13
14         var link = L.DomUtil.create('a', 'control-button', container);
15         link.innerHTML = "<span class='icon share'></span>";
16         link.href = '#';
17         link.title = this.options.title;
18
19         this._uiPane = this.options.uiPane;
20
21         this._map = map;
22
23         var h2 = L.DomUtil.create('h2', '', this._uiPane);
24         h2.innerHTML = I18n.t('javascripts.share.title');
25
26         this._linkInput = L.DomUtil.create('input', '', this._uiPane);
27
28         L.DomEvent
29             .on(link, 'click', L.DomEvent.stopPropagation)
30             .on(link, 'click', L.DomEvent.preventDefault)
31             .on(link, 'click', this._toggle, this)
32             .on(link, 'dblclick', L.DomEvent.stopPropagation);
33
34         map.on('moveend layeradd layerremove', this._update, this);
35
36         return container;
37     },
38
39     _update: function (e) {
40         var center = this._map.getCenter().wrap();
41         var layers = getMapLayers(this._map);
42         this._linkInput.value = this.options.getUrl(this._map);
43     },
44
45     _toggle: function() {
46         var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
47
48         if ($(this._uiPane).is(':visible')) {
49             $(this._uiPane).hide();
50             controlContainer.css({paddingRight: '0'});
51         } else {
52             $(this._uiPane).show();
53             controlContainer.css({paddingRight: '200px'});
54         }
55     }
56 });
57
58 L.control.share = function(options) {
59     return new L.Control.Share(options);
60 };