]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.share.js
Even more map ui js refactor
[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 = L.DomUtil.create('div', 'leaflet-map-ui', map._container);
20
21         L.DomEvent
22             .on(this._uiPane, 'click', L.DomEvent.stopPropagation)
23             .on(this._uiPane, 'click', L.DomEvent.preventDefault)
24             .on(this._uiPane, 'dblclick', L.DomEvent.preventDefault);
25
26         var h2 = L.DomUtil.create('h2', '', this._uiPane);
27         h2.innerHTML = I18n.t('javascripts.share.title');
28
29         this._linkInput = L.DomUtil.create('input', '', this._uiPane);
30
31         L.DomEvent
32             .on(link, 'click', L.DomEvent.stopPropagation)
33             .on(link, 'click', L.DomEvent.preventDefault)
34             .on(link, 'click', this._toggle, this)
35             .on(link, 'dblclick', L.DomEvent.stopPropagation);
36
37         map.on('moveend layeradd layerremove', this._update, this);
38
39         return container;
40     },
41
42     _update: function (e) {
43         var center = map.getCenter().wrap();
44         var layers = getMapLayers();
45         this._linkInput.value = this.options.getUrl(map);
46     },
47
48     _toggle: function() {
49         var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
50
51         if ($(this._uiPane).is(':visible')) {
52             $(this._uiPane).hide();
53             controlContainer.css({paddingRight: '0'});
54         } else {
55             $(this._uiPane).show();
56             controlContainer.css({paddingRight: '200px'});
57         }
58     }
59 });
60
61 L.control.share = function(options) {
62     return new L.Control.Share(options);
63 };