Settings switch
[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 (interestingReferences) {
8         $("#settings-references").show();
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         if (map_enabled) return;
19
20         map = L.map('reference-map').setView([0, 0], 11);
21         L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png?lang=pl', {
22             attribution: 'Map data &copy; <a href="http://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() {
33         $("#reference-box").hide();
34     });
35     
36     $('a.reference').each(function() {
37         $this = $(this);
38         uri = $this.attr('data-uri');
39         if (interestingReferences.hasOwnProperty(uri)) {
40             $this.addClass('interesting');
41             ref = interestingReferences[uri];
42
43             $this.attr('href', ref.wikipedia_link);
44             $this.attr('target', '_blank');
45         }
46     });
47
48
49     $('a.reference.interesting').on('click', function(event) {
50         event.preventDefault();
51
52         $("#reference-box").show();
53
54         $this = $(this);
55         uri = $this.attr('data-uri');
56         ref = interestingReferences[uri];
57
58         if (ref.location) {
59             enable_map();
60
61             marker.setLatLng(ref.location);
62             //marker.setContent(ref.label);
63             marker.bindTooltip(ref.label).openTooltip();
64             map.addLayer(marker);
65             map.panTo(ref.location, {
66                 animate: true,
67                 duration: 1,
68             });
69         } else {
70             disable_map();
71             if (map) {
72                 map.removeLayer(marker);
73             }
74         }
75
76         $("#reference-images img").remove();
77         if (ref.images) {
78             $.each(ref.images, function(i, e) {
79                 $i = $("<a target='_blank'><img></a>");
80                 $i.attr('href', e.page);
81                 $('img', $i).attr('src', e.url);
82                 $("#reference-images").append($i);
83             })
84         }
85
86         $("#reference-link").text(ref.label);
87         $("#reference-link").attr('href', ref.wikipedia_link);
88     });
89 })})(jQuery);