References
[wolnelektury.git] / src / wolnelektury / static / js / book_text / references.js
diff --git a/src/wolnelektury/static/js/book_text/references.js b/src/wolnelektury/static/js/book_text/references.js
new file mode 100644 (file)
index 0000000..dcbb3c8
--- /dev/null
@@ -0,0 +1,90 @@
+(function($){$(function(){
+
+    var interestingReferences = $("#interesting-references").text();
+    if (interestingReferences) {
+        interestingReferences = $.parseJSON(interestingReferences);
+    }
+    if (interestingReferences) {
+        $("settings-references").show();
+    }
+
+    
+    
+    var map_enabled = false;
+    var marker = L.marker([0,0]);
+    var map = null;
+
+    function enable_map() {
+        if (map_enabled) return;
+
+        map = L.map('reference-map').setView([0, 0], 11);
+        L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png?lang=pl', {
+            attribution: 'Map data &copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
+        }).addTo(map);
+
+        map_enabled = true;
+    }
+    function disable_map() {
+        $("#reference-map").hide('slow');
+    }
+    
+
+    $("#reference-close").on("click", function() {
+        $("#reference-box").hide();
+    });
+    
+    $('a.reference').each(function() {
+        $this = $(this);
+        uri = $this.attr('data-uri');
+        console.log('check ' + uri);
+        if (interestingReferences.hasOwnProperty(uri)) {
+            $this.addClass('interesting');
+            ref = interestingReferences[uri];
+
+            $this.attr('href', ref.wikipedia_link);
+            $this.attr('target', '_blank');
+        }
+    });
+
+
+    $('a.reference.interesting').on('click', function(event) {
+        event.preventDefault();
+
+        $("#reference-box").show();
+
+        $this = $(this);
+        uri = $this.attr('data-uri');
+        ref = interestingReferences[uri];
+
+        if (ref.location) {
+            enable_map();
+
+            marker.setLatLng(ref.location);
+            //marker.setContent(ref.label);
+            marker.bindTooltip(ref.label).openTooltip();
+            map.addLayer(marker);
+            map.panTo(ref.location, {
+                animate: true,
+                duration: 1,
+            });
+        } else {
+            disable_map();
+            if (map) {
+                map.removeLayer(marker);
+            }
+        }
+
+        $("#reference-images img").remove();
+        if (ref.images) {
+            $.each(ref.images, function(i, e) {
+                $i = $("<a target='_blank'><img></a>");
+                $i.attr('href', e.page);
+                $('img', $i).attr('src', e.url);
+                $("#reference-images").append($i);
+            })
+        }
+
+        $("#reference-link").text(ref.label);
+        $("#reference-link").attr('href', ref.wikipedia_link);
+    });
+})})(jQuery);