-L.Control.Share = L.Control.extend({
- options: {
- position: 'topright',
- title: 'Share',
- url: function(map) {
- return '';
- }
- },
-
- onAdd: function (map) {
- var className = 'control-share',
- container = L.DomUtil.create('div', className);
-
- var link = L.DomUtil.create('a', 'control-button', container);
- link.innerHTML = "<span class='icon share'></span>";
- link.href = '#';
- link.title = this.options.title;
-
- this._uiPane = L.DomUtil.create('div', 'leaflet-map-ui', map._container);
-
- L.DomEvent
- .on(this._uiPane, 'click', L.DomEvent.stopPropagation)
- .on(this._uiPane, 'click', L.DomEvent.preventDefault)
- .on(this._uiPane, 'dblclick', L.DomEvent.preventDefault);
-
- var h2 = L.DomUtil.create('h2', '', this._uiPane);
- h2.innerHTML = I18n.t('javascripts.share.title');
-
- this._linkInput = L.DomUtil.create('input', '', this._uiPane);
-
- L.DomEvent
- .on(link, 'click', L.DomEvent.stopPropagation)
- .on(link, 'click', L.DomEvent.preventDefault)
- .on(link, 'click', this._toggle, this)
- .on(link, 'dblclick', L.DomEvent.stopPropagation);
-
- map.on('moveend layeradd layerremove', this._update, this);
-
- return container;
- },
-
- _update: function (e) {
- var center = map.getCenter().wrap();
- var layers = getMapLayers();
- this._linkInput.value = this.options.getUrl(map);
- },
-
- _toggle: function() {
- var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
-
- if ($(this._uiPane).is(':visible')) {
- $(this._uiPane).hide();
- controlContainer.css({paddingRight: '0'});
- } else {
- $(this._uiPane).show();
- controlContainer.css({paddingRight: '200px'});
- }
+L.OSM.share = function (options) {
+ var control = L.control(options);
+
+ control.onAdd = function (map) {
+ var $container = $('<div>')
+ .attr('class', 'control-share');
+
+ $('<a>')
+ .attr('class', 'control-button')
+ .attr('href', '#')
+ .attr('title', 'Share')
+ .html('<span class="icon share"></span>')
+ .on('click', toggle)
+ .appendTo($container);
+
+ var $ui = $('<div>')
+ .attr('class', 'share-ui');
+
+ $('<div>')
+ .attr('class', 'sidebar_heading')
+ .appendTo($ui)
+ .append(
+ $('<a>')
+ .text(I18n.t('javascripts.close'))
+ .attr('class', 'sidebar_close')
+ .attr('href', '#')
+ .bind('click', toggle))
+ .append(
+ $('<h4>')
+ .text(I18n.t('javascripts.share.title')));
+
+ var $linkSection = $('<div>')
+ .attr('class', 'section share-link')
+ .appendTo($ui);
+
+ $('<h4>')
+ .text(I18n.t('javascripts.share.link'))
+ .appendTo($linkSection);
+
+ var $shortLink, $longLink;
+
+ $('<ul>')
+ .appendTo($linkSection)
+ .append($('<li>')
+ .append($longLink = $('<a>')
+ .text(I18n.t('javascripts.share.long_link'))))
+ .append($('<li>')
+ .append($shortLink = $('<a>')
+ .text(I18n.t('javascripts.share.short_link'))));
+
+ map.on('moveend layeradd layerremove', update);
+
+ options.sidebar.addPane($ui);
+
+ function toggle(e) {
+ e.stopPropagation();
+ e.preventDefault();
+ options.sidebar.togglePane($ui);
+ update();
+ }
+
+ function update() {
+ $shortLink.attr('href', map.getShortUrl());
+ $longLink.attr('href', map.getUrl());
+ }
+
+ function select() {
+ $(this).select();
}
-});
-L.control.share = function(options) {
- return new L.Control.Share(options);
+ return $container[0];
+ };
+
+ return control;
};