Add pz field
[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     var map_enabled = false;
12     var marker = L.circleMarker([0,0]);
13     var map = null;
14
15     function enable_map() {
16         $("#reference-map").show('slow');
17
18         if (map_enabled) return;
19
20         map = L.map('reference-map').setView([0, 0], 11);
21         L.tileLayer('https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a8a97f0ae5134403ac38c1a075b03e15', {
22             attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
23         }).addTo(map);
24
25         map_enabled = true;
26     }
27     function disable_map() {
28         $("#reference-map").hide('slow');
29     }
30     
31
32     $("#reference-close").on("click", function(event) {
33         event.preventDefault();
34         $("#reference-box").hide();
35     });
36     
37     $('a.reference').each(function() {
38         $this = $(this);
39         uri = $this.attr('data-uri');
40         if (uri == '') {
41             $this.remove();
42             return;
43         }
44         if (interestingReferences.hasOwnProperty(uri)) {
45             $this.addClass('interesting');
46             ref = interestingReferences[uri];
47
48             $this.attr('href', ref.wikipedia_link);
49             $this.attr('target', '_blank');
50         }
51     });
52
53
54     $('a.reference.interesting').on('click', function(event) {
55         event.preventDefault();
56
57         $("#reference-box").show();
58
59         $this = $(this);
60         uri = $this.attr('data-uri');
61         ref = interestingReferences[uri];
62
63         if (ref.location) {
64             enable_map();
65
66             let newLoc = [
67                 ref.location[0],
68                 ref.location[1] + Math.round(
69                     (map.getCenter().lng - ref.location[1]) / 360
70                 ) * 360
71             ];
72
73             marker.setLatLng(newLoc);
74             marker.bindTooltip(ref.label).openTooltip();
75             map.addLayer(marker);
76
77             map.panTo(newLoc, {
78                 animate: true,
79                 duration: 1,
80             });
81         } else {
82             disable_map();
83             if (map) {
84                 map.removeLayer(marker);
85             }
86         }
87
88         $("#reference-images a").remove();
89         if (ref.images) {
90             $.each(ref.images, function(i, e) {
91                 $i = $("<a target='_blank'><img></a>");
92                 $i.attr('href', e.page);
93                 $('img', $i).attr('src', e.thumburl || e.url);
94                 if (e.thumbresolution) {
95                     $('img', $i).attr('width', e.thumbresolution[0]).attr('height', e.thumbresolution[1]);
96                 }
97
98                 $("#reference-images").append($i);
99             })
100         }
101
102         $("#reference-link").text(ref.label);
103         $("#reference-link").attr('href', ref.wikipedia_link);
104
105         _paq.push(['trackEvent', 'html', 'reference']);
106     });
107 })})(jQuery);