]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/leaflet.share.js
Include current non-OSM.org hostname in share URL
[rails.git] / app / assets / javascripts / leaflet.share.js
index f085bc0e69ab9e0feefdaf0409e9295d6e995bdd..664aebaa5fb19ad47aa08e2bbc7f11f864246faf 100644 (file)
@@ -1,58 +1,85 @@
-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 = this.options.uiPane;
-
-        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');
+
+    $('<section>')
+      .appendTo($ui)
+      .append(
+          $('<a>')
+            .html('&raquo;')
+            .attr('class', 'close-button')
+            .attr('href', '#')
+            .bind('click', toggle))
+      .append(
+      $('<h2>')
+        .text(I18n.t('javascripts.share.title')));
+
+    var $share_link = $('<section></section>')
+      .appendTo($ui);
+
+    var $title = $('<h3></h3>')
+      .text(I18n.t('javascripts.share.link'))
+      .appendTo($share_link);
+
+    var $input = $('<input />')
+      .attr('type', 'text')
+      .appendTo($share_link);
+
+    var $list = $('<ul>')
+      .appendTo($share_link);
+
+    var $short_option = $('<li>')
+      .appendTo($list);
+
+    var $short_url_label = $('<label></label>')
+      .attr('for', 'short_url')
+      .appendTo($short_option);
+
+    var $short_url_input = $('<input />')
+      .attr('id', 'short_url')
+      .attr('type', 'checkbox')
+      .prop('checked', 'checked')
+      .appendTo($short_url_label)
+      .bind('change', function() {
+          options.short = $(this).prop('checked');
+          update();
+      });
+
+    $short_url_label.append(I18n.t('javascripts.share.short_url'));
+
+    map.on('moveend layeradd layerremove', update);
+
+    options.sidebar.addPane($ui);
+
+    function toggle(e) {
+      e.stopPropagation();
+      e.preventDefault();
+      options.sidebar.togglePane($ui);
+      $input.select();
+    }
+
+    function update() {
+      $input.val(
+          options.short ? options.getShortUrl(map) : options.getUrl(map)
+      );
     }
-});
 
-L.control.share = function(options) {
-    return new L.Control.Share(options);
+    return $container[0];
+  };
+
+  return control;
 };