]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.share.js
bea877e6686c0da8be68412fd808cc5c55f1e5e4
[rails.git] / app / assets / javascripts / leaflet.share.js
1 L.OSM.share = function (options) {
2   var control = L.control(options);
3
4   control.onAdd = function (map) {
5     var $container = $('<div>')
6       .attr('class', 'control-share');
7
8     $('<a>')
9       .attr('class', 'control-button')
10       .attr('href', '#')
11       .attr('title', 'Share')
12       .html('<span class="icon share"></span>')
13       .on('click', toggle)
14       .appendTo($container);
15
16     var $ui = $('<div>')
17       .attr('class', 'share-ui');
18
19     $('<div>')
20       .attr('class', 'sidebar_heading')
21       .appendTo($ui)
22       .append(
23         $('<a>')
24           .text(I18n.t('javascripts.close'))
25           .attr('class', 'sidebar_close')
26           .attr('href', '#')
27           .bind('click', toggle))
28       .append(
29         $('<h4>')
30           .text(I18n.t('javascripts.share.title')));
31
32     var $linkSection = $('<div>')
33       .attr('class', 'section share-link')
34       .appendTo($ui);
35
36     $('<h4>')
37       .text(I18n.t('javascripts.share.link'))
38       .appendTo($linkSection);
39
40     var $shortLink, $longLink;
41
42     $('<ul>')
43       .appendTo($linkSection)
44       .append($('<li>')
45         .append($longLink = $('<a>')
46           .text(I18n.t('javascripts.share.long_link'))))
47       .append($('<li>')
48         .append($shortLink = $('<a>')
49           .text(I18n.t('javascripts.share.short_link'))));
50
51     map.on('moveend layeradd layerremove', update);
52
53     options.sidebar.addPane($ui);
54
55     function toggle(e) {
56       e.stopPropagation();
57       e.preventDefault();
58       options.sidebar.togglePane($ui);
59       update();
60     }
61
62     function update() {
63       $shortLink.attr('href', map.getShortUrl());
64       $longLink.attr('href', map.getUrl());
65     }
66
67     function select() {
68       $(this).select();
69     }
70
71     return $container[0];
72   };
73
74   return control;
75 };