Fix displaying map.
[wolnelektury.git] / src / wolnelektury / static / js / book_text / references.js
1 (function($){$(function(){
2
3     var interestingReferences = $("#interesting-references").text();
4     if (interestingReferences) {
5         interestingReferences = $.parseJSON(interestingReferences);
6     }
7     if (Object.keys(interestingReferences).length) {
8         $("#settings-references").css('display', 'block');
9     }
10
11     
12     
13     var map_enabled = false;
14     var marker = L.marker([0,0]);
15     var map = null;
16
17     function enable_map() {
18         $("#reference-map").show('slow');
19
20         if (map_enabled) return;
21
22         map = L.map('reference-map').setView([0, 0], 11);
23         L.tileLayer('https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a8a97f0ae5134403ac38c1a075b03e15', {
24             attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
25         }).addTo(map);
26
27         map_enabled = true;
28     }
29     function disable_map() {
30         $("#reference-map").hide('slow');
31     }
32     
33
34     $("#reference-close").on("click", function(event) {
35         event.preventDefault();
36         $("#reference-box").hide();
37     });
38     
39     $('a.reference').each(function() {
40         $this = $(this);
41         uri = $this.attr('data-uri');
42         if (interestingReferences.hasOwnProperty(uri)) {
43             $this.addClass('interesting');
44             ref = interestingReferences[uri];
45
46             $this.attr('href', ref.wikipedia_link);
47             $this.attr('target', '_blank');
48         }
49     });
50
51
52     $('a.reference.interesting').on('click', function(event) {
53         event.preventDefault();
54
55         $("#reference-box").show();
56
57         $this = $(this);
58         uri = $this.attr('data-uri');
59         ref = interestingReferences[uri];
60
61         if (ref.location) {
62             enable_map();
63
64             marker.setLatLng(ref.location);
65             //marker.setContent(ref.label);
66             marker.bindTooltip(ref.label).openTooltip();
67             map.addLayer(marker);
68             map.panTo(ref.location, {
69                 animate: true,
70                 duration: 1,
71             });
72         } else {
73             disable_map();
74             if (map) {
75                 map.removeLayer(marker);
76             }
77         }
78
79         $("#reference-images a").remove();
80         if (ref.images) {
81             $.each(ref.images, function(i, e) {
82                 $i = $("<a target='_blank'><img></a>");
83                 $i.attr('href', e.page);
84                 $('img', $i).attr('src', e.url);
85                 $("#reference-images").append($i);
86             })
87         }
88
89         $("#reference-link").text(ref.label);
90         $("#reference-link").attr('href', ref.wikipedia_link);
91     });
92 })})(jQuery);