X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/b94c6ee5854472d0054f42ac5757b84819adcd6c..7bbeecb5dd5c5f20120f9e40e41568a3bff1caa4:/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
index 7e21b21d4..ef6c51c94 100644
--- a/src/wolnelektury/static/js/book_text/references.js
+++ b/src/wolnelektury/static/js/book_text/references.js
@@ -1,25 +1,26 @@
(function($){$(function(){
+ let csrf = $("[name='csrfmiddlewaretoken']").val();
var interestingReferences = $("#interesting-references").text();
if (interestingReferences) {
interestingReferences = $.parseJSON(interestingReferences);
}
- if (interestingReferences) {
- $("#settings-references").show();
+ if (Object.keys(interestingReferences).length) {
+ $("#settings-references").css('display', 'block');
}
-
-
var map_enabled = false;
- var marker = L.marker([0,0]);
+ var marker = L.circleMarker([0,0]);
var map = null;
function enable_map() {
+ $("#reference-map").show('slow');
+
if (map_enabled) return;
map = L.map('reference-map').setView([0, 0], 11);
- L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
- attribution: 'Map data © OpenStreetMap contributors'
+ L.tileLayer('https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a8a97f0ae5134403ac38c1a075b03e15', {
+ attribution: 'Maps © Thunderforest, Data © OpenStreetMap contributors'
}).addTo(map);
map_enabled = true;
@@ -29,13 +30,18 @@
}
- $("#reference-close").on("click", function() {
+ $("#reference-close").on("click", function(event) {
+ event.preventDefault();
$("#reference-box").hide();
});
$('a.reference').each(function() {
$this = $(this);
uri = $this.attr('data-uri');
+ if (uri == '') {
+ $this.remove();
+ return;
+ }
if (interestingReferences.hasOwnProperty(uri)) {
$this.addClass('interesting');
ref = interestingReferences[uri];
@@ -58,11 +64,18 @@
if (ref.location) {
enable_map();
- marker.setLatLng(ref.location);
- //marker.setContent(ref.label);
+ let newLoc = [
+ ref.location[0],
+ ref.location[1] + Math.round(
+ (map.getCenter().lng - ref.location[1]) / 360
+ ) * 360
+ ];
+
+ marker.setLatLng(newLoc);
marker.bindTooltip(ref.label).openTooltip();
map.addLayer(marker);
- map.panTo(ref.location, {
+
+ map.panTo(newLoc, {
animate: true,
duration: 1,
});
@@ -73,17 +86,587 @@
}
}
- $("#reference-images img").remove();
+ $("#reference-images a").remove();
if (ref.images) {
$.each(ref.images, function(i, e) {
$i = $("");
$i.attr('href', e.page);
- $('img', $i).attr('src', e.url);
+ $('img', $i).attr('src', e.thumburl || e.url);
+ if (e.thumbresolution) {
+ $('img', $i).attr('width', e.thumbresolution[0]).attr('height', e.thumbresolution[1]);
+ }
+
$("#reference-images").append($i);
})
}
$("#reference-link").text(ref.label);
$("#reference-link").attr('href', ref.wikipedia_link);
+
+ _paq.push(['trackEvent', 'html', 'reference']);
+ });
+
+
+ function putNoteAt($elem, anchor, side) {
+ $elem.data('anchoredTo', anchor);
+ updateNote($elem, side);
+ }
+
+ function updateNote($elem, side) {
+ anchor = $elem.data('anchoredTo')
+ if (!anchor) return;
+ let anchorRect = anchor.getBoundingClientRect();
+
+ let x = anchorRect.x + anchorRect.width / 2;
+ let y = anchorRect.y;
+ if ($elem.data('attach-bottom')) {
+ y += anchorRect.height;
+ }
+ minx = $("#book-text").position().left;
+ maxx = minx + $("#book-text").width();
+
+ margin = 20;
+ minx += margin;
+ maxx -= margin;
+ maxx += 10000;
+
+ //boxwidth = 470;
+ boxwidth = $elem.width();
+
+ if (maxx - minx <= boxwidth) {
+ nx = margin;
+ right = margin;
+ leftoffset = x - margin;
+ } else {
+ right = '';
+
+ leftoffset = $elem.data('default-leftoffset');
+ if (leftoffset === undefined) {
+ leftoffset = $elem.width() / 2;
+ }
+
+ nx = x - leftoffset;
+
+ $elem.css({right: ''});
+
+ // Do we need to move away from the left?
+ if (nx < minx) {
+ let d = minx - nx;
+ nx += d;
+ leftoffset -= d;
+ }
+
+ // Do we need to move away from the right?
+ if (nx + boxwidth > maxx) {
+ right = '';
+ let d = nx + boxwidth - maxx;
+ nx -= d;
+ leftoffset += d;
+ }
+ }
+ $elem.css({
+ left: nx,
+ right: right
+ });
+ $elem.css({
+ display: "block"
+ });
+ if (!$elem.data('attach-bottom')) {
+ ny = y - $elem.height() - 10;
+ } else {
+ ny = y + 10;
+ }
+ $elem.css({
+ top: ny
+ });
+ $('.pointer', $elem).css({
+ left: leftoffset - 6
+ });
+
+ }
+
+ function closeNoteBox() {
+ $('#annotation-box').data('anchoredTo', null).fadeOut();
+ }
+ $(document).on('click', function(event) {
+ let t = $(event.target);
+ if (t.parents('#annotation-box').length && !t.is('#footnote-link')) {
+ return;
+ }
+ closeNoteBox();
+ });
+ $(window).on('resize', closeNoteBox);
+
+ function getPositionInBookText($e) {
+ let x = 0, y = 0;
+
+ // Ok dla Y, nie ok dla X
+
+ while ($e.attr('id') != 'book-text') {
+ let p = $e.position();
+ x += p.left;
+ y += p.top;
+ $e = $e.offsetParent();
+ break;
+ }
+ return {"x": x, "y": y}
+ }
+
+ $('#book-text .annotation').on('click', function(event) {
+ if ($(this).parents('#footnotes').length) return;
+ event.preventDefault();
+
+ href = $(this).attr('href').substr(1);
+ content = $("[name='" + href + "']").next().next().html();
+ if (!content) return;
+ $("#annotation-content").html(content);
+ $("#footnote-link").attr('href', '#' + href)
+
+ putNoteAt($('#annotation-box'), this);
+ event.stopPropagation();
+ });
+
+
+
+ let zakladki = {};
+ $.get({
+ url: '/zakladki/',
+ success: function(data) {
+ zakladki = data;
+ $.each(zakladki, (i, e) => {
+ zakladkaUpdateFor(
+ // TODO: not just paragraphs.
+ $('[href="#' + e.anchor + '"]').nextAll('.paragraph').first()
+ );
+ });
+ }
+ });
+
+ // TODO: create bookmarks on init
+ // We need to do that from anchors.
+
+ function zakladkaUpdateFor($item) {
+
+ let anchor = $item.prevAll('.target').first().attr('name');
+
+ if (anchor in zakladki) {
+ let $booktag = $item.data('booktag');
+ if (!$booktag) {
+
+ // TODO: only copy without the dialog.
+ $booktag = $("