1 (function($){$(function(){
2 let csrf = $("[name='csrfmiddlewaretoken']").val();
4 var interestingReferences = $("#interesting-references").text();
5 if (interestingReferences) {
6 interestingReferences = $.parseJSON(interestingReferences);
8 if (Object.keys(interestingReferences).length) {
9 $("#settings-references").css('display', 'block');
12 var map_enabled = false;
13 var marker = L.circleMarker([0,0]);
16 function enable_map() {
17 $("#reference-map").show('slow');
19 if (map_enabled) return;
21 map = L.map('reference-map').setView([0, 0], 11);
22 L.tileLayer('https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a8a97f0ae5134403ac38c1a075b03e15', {
23 attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
28 function disable_map() {
29 $("#reference-map").hide('slow');
33 $("#reference-close").on("click", function(event) {
34 event.preventDefault();
35 $("#reference-box").hide();
38 $('a.reference').each(function() {
40 uri = $this.attr('data-uri');
45 if (interestingReferences.hasOwnProperty(uri)) {
46 $this.addClass('interesting');
47 ref = interestingReferences[uri];
49 $this.attr('href', ref.wikipedia_link);
50 $this.attr('target', '_blank');
55 $('a.reference.interesting').on('click', function(event) {
56 event.preventDefault();
58 $("#reference-box").show();
61 uri = $this.attr('data-uri');
62 ref = interestingReferences[uri];
69 ref.location[1] + Math.round(
70 (map.getCenter().lng - ref.location[1]) / 360
74 marker.setLatLng(newLoc);
75 marker.bindTooltip(ref.label).openTooltip();
85 map.removeLayer(marker);
89 $("#reference-images a").remove();
91 $.each(ref.images, function(i, e) {
92 $i = $("<a target='_blank'><img></a>");
93 $i.attr('href', e.page);
94 $('img', $i).attr('src', e.thumburl || e.url);
95 if (e.thumbresolution) {
96 $('img', $i).attr('width', e.thumbresolution[0]).attr('height', e.thumbresolution[1]);
99 $("#reference-images").append($i);
103 $("#reference-link").text(ref.label);
104 $("#reference-link").attr('href', ref.wikipedia_link);
106 _paq.push(['trackEvent', 'html', 'reference']);
110 function putNoteAt($elem, anchor, side) {
111 $elem.data('anchoredTo', anchor);
112 updateNote($elem, side);
115 function updateNote($elem, side) {
116 anchor = $elem.data('anchoredTo')
118 let anchorRect = anchor.getBoundingClientRect();
120 let x = anchorRect.x + anchorRect.width / 2;
121 let y = anchorRect.y;
122 if ($elem.data('attach-bottom')) {
123 y += anchorRect.height;
125 minx = $("#book-text").position().left;
126 maxx = minx + $("#book-text").width();
134 boxwidth = $elem.width();
136 if (maxx - minx <= boxwidth) {
139 leftoffset = x - margin;
143 leftoffset = $elem.data('default-leftoffset');
144 if (leftoffset === undefined) {
145 leftoffset = $elem.width() / 2;
150 $elem.css({right: ''});
152 // Do we need to move away from the left?
159 // Do we need to move away from the right?
160 if (nx + boxwidth > maxx) {
162 let d = nx + boxwidth - maxx;
174 if (!$elem.data('attach-bottom')) {
175 ny = y - $elem.height() - 10;
182 $('.pointer', $elem).css({
188 function closeNoteBox() {
189 $('#annotation-box').data('anchoredTo', null).fadeOut();
191 $(document).on('click', function(event) {
192 let t = $(event.target);
193 if (t.parents('#annotation-box').length && !t.is('#footnote-link')) {
198 $(window).on('resize', closeNoteBox);
200 function getPositionInBookText($e) {
203 // Ok dla Y, nie ok dla X
205 while ($e.attr('id') != 'book-text') {
206 let p = $e.position();
209 $e = $e.offsetParent();
212 return {"x": x, "y": y}
215 $('#book-text .annotation').on('click', function(event) {
216 if ($(this).parents('#footnotes').length) return;
217 event.preventDefault();
219 href = $(this).attr('href').substr(1);
220 content = $("[name='" + href + "']").next().next().html();
221 if (!content) return;
222 $("#annotation-content").html(content);
223 $("#footnote-link").attr('href', '#' + href)
225 putNoteAt($('#annotation-box'), this);
226 event.stopPropagation();
234 success: function(data) {
236 $.each(zakladki, (i, e) => {
238 // TODO: not just paragraphs.
239 $('[href="#' + e.anchor + '"]').nextAll('.paragraph').first()
245 // TODO: create bookmarks on init
246 // We need to do that from anchors.
248 function zakladkaUpdateFor($item) {
250 let anchor = $item.prevAll('.target').first().attr('name');
252 if (anchor in zakladki) {
253 let $booktag = $item.data('booktag');
256 // TODO: only copy without the dialog.
257 $booktag = $("<div class='zakladka'>");
258 $booktag.append($('.icon', $zakladka).clone());
260 $item.data('booktag', $booktag);
261 $booktag.data('p', $item);
262 $booktag.data('anchor', anchor);
263 $zakladka.after($booktag);
265 zakladkaSetPosition($booktag);
270 if (zakladki[anchor].note) {
271 $z.removeClass('zakladka-exists');
272 $z.addClass('zakladka-note');
274 $z.removeClass('zakladka-note');
275 $z.addClass('zakladka-exists');
278 let $booktag = $item.data('booktag');
280 $item.data('booktag', null);
281 $zakladka.append($("#zakladka-box"));
287 function zakladkaSetPosition($z) {
288 $item = $z.data('p');
289 pos = getPositionInBookText($item);
293 right: ($('#main-text').width() - $('#book-text').width()) / 2,
297 let $zakladka = $('#zakladka');
298 $('#book-text .paragraph').on('mouseover', function() {showMarker($(this));});
299 $('#book-text .verse').on('mouseover', function() {showMarker($(this));});
300 //$.PMarker.showForP(this);
303 function showMarker(p) {
305 // Close the currently tag box when moving to another one.
306 // TBD: Do we want to keep the box open and prevent moving?
307 $("#zakladka-box").hide();
309 let anchor = p.prevAll('.target').first().attr('name');
310 // Don't bother if there's no anchor to use.
313 // Only show tag if there is not already a tag for this p.
314 if (p.data('booktag')) {
317 $zakladka.data('p', p);
318 $zakladka.data('anchor', anchor);
320 // (not needed) zakladkaUpdateClass();
321 // TODO: UPDATE THIS ON OPEN?
322 //let note = anchor in zakladki ? zakladki[anchor].note : '';
323 //$('textarea', $zakladka).val(note);
325 zakladkaSetPosition($zakladka);
330 $(".zakladka-tool_zakladka").on('click', function() {
331 let $z = $("#zakladka-box").data('z');
332 let anchor = $z.data('anchor');
333 let $p = $z.data('p');
337 csrfmiddlewaretoken: csrf,
340 success: function(data) {
341 zakladki[data.anchor] = data;
342 $("#zakladka-box").hide();
344 // Just hide, and create new .zakladka if not already exists?
345 // In general no hiding 'classed' .zakladka.
346 // So the 'cursor' .zakladka doesn't ever need class update.
347 //zakladkaUpdateClass();
348 zakladkaUpdateFor($p);
354 $(".zakladka-tool_notka_text textarea").on('input', function() {
355 // FIXME: no use const $zakladka here, check which .zakladka are we attached to.
356 let $z = $(this).closest('.zakladka');
357 let anchor = $z.data('anchor');
359 $("#notka-saved").hide();
360 //$("#notka-save").show();
362 url: '/zakladki/' + zakladki[anchor].uuid + '/',
364 csrfmiddlewaretoken: csrf,
367 success: function(data) {
368 zakladki[anchor] = data;
369 zakladkaUpdateFor($z.data('p'));
370 $("#notka-save").hide();
371 $("#notka-saved").fadeIn();
376 $(".zakladka-tool_zakladka_delete").on('click', function() {
377 let $z = $(this).closest('.zakladka');
378 let anchor = $z.data('anchor');
380 url: '/zakladki/' + zakladki[anchor].uuid + '/delete/',
382 csrfmiddlewaretoken: csrf,
384 success: function(data) {
385 delete zakladki[anchor];
386 $("#zakladka-box").hide();
387 zakladkaUpdateFor($z.data('p'));
392 $("#main-text").on("click", ".zakladka .icon", function() {
393 let $z = $(this).closest('.zakladka');
394 let $box = $("#zakladka-box");
398 let $p = $z.data('p');
399 let anchor = $z.data('anchor');
400 let note = anchor in zakladki ? zakladki[anchor].note : '';
402 $('.zakladka-tool_zakladka', $box).toggle(!(anchor in zakladki));
403 $('.zakladka-tool_sluchaj', $box).toggle($p.hasClass('syncable')).data('sync', $p.attr('id'));
404 $('textarea', $box).val(note);
414 showForSelection(sel) {
415 // TODO: only consider ranges inside text.?
416 this.selection = sel;
418 // TODO: multiple ranges.
419 let range = sel.getRangeAt(0);
420 let rect = range.getBoundingClientRect();
422 putNoteAt(this.qbox, range)
425 let rect = b.getBoundingClientRect();
427 putNoteAt(this.qbox, b, 'left')
430 this.qbox.data('anchoredTo', null);
434 this.qbox.data('anchoredTo', null);
435 this.qbox.addClass('copied').fadeOut(1500, () => {
436 this.qbox.removeClass('copied');
441 // TODO: only consider ranges inside text.?
442 let range = this.selection.getRangeAt(0);
443 let e = range.startContainer;
444 let anchor = getIdForElem(e);
445 let text = window.location.protocol + '//' +
446 window.location.host +
447 window.location.pathname;
449 navigator.clipboard.writeText(
450 this.selection.toString() +
451 '\n\nCałość czytaj na: ' + text
456 // TODO: only consider ranges inside text.?
457 let range = this.selection.getRangeAt(0);
458 let e = range.startContainer;
459 let anchor = getIdForElem(e);
460 let text = window.location.protocol + '//' +
461 window.location.host +
462 window.location.pathname;
463 if (anchor) text += '#' + anchor;
464 navigator.clipboard.writeText(text);
469 // What aboot non-contiguous selections?
470 let sel = this.selection;
471 let textContent = sel.toString();
472 let anchor = getIdForElem(sel.getRangeAt(0).startContainer);
473 let paths = getSelectionPaths(sel);
477 csrfmiddlewaretoken: csrf,
485 success: function (data) {
486 var win = window.open('/cytaty/' + data.uuid + '/', '_blank');
493 let qbox = new QBox($("#qbox"));
496 function getPathToNode(elem) {
499 while (elem.id != 'book-text') {
500 let p = elem.parentElement;
501 path.unshift([...p.childNodes].indexOf(elem))
506 function getSelectionPaths(selection) {
508 let range1 = selection.getRangeAt(0);
509 let range2 = selection.getRangeAt(selection.rangeCount - 1);
511 getPathToNode(range1.startContainer) + [range1.startOffset],
512 getPathToNode(range2.endContainer) + [range2.endOffset]
518 function getIdForElem(elem) {
521 // check if inside book-text?
524 if ($elem.hasClass('target')) {
525 return $elem.attr('name');
531 // Gdy wychodzimy w górę -- to jest ten moment, w którym znajdujemy element od którego wychodzimy i zliczamy znaki.
545 function getIdForElem(elem) {
547 // check if inside book-text?
550 if ($elem.hasClass('target')) {
551 return $elem.attr('name');
569 function positionToIIDOffset(container, offset) {
570 // Container and offset follow Range rules.
571 // If container is a text node, offset is text offset.
572 // If container is an element node, offset is number of child nodes from container start.
573 // (containers of type Comment, CDATASection - ignore)z
577 function updateQBox() {
578 sel = document.getSelection();
580 if (sel.isCollapsed || sel.rangeCount < 1) {
587 qbox.showForSelection(sel);
590 $(document).on('selectionchange', updateQBox);
592 function updateBoxes() {
593 updateNote(qbox.qbox);
594 updateNote($('#annotation-box'));
597 $(window).on('scroll', updateBoxes);
598 $(window).on('resize', updateBoxes);
601 $(window).on('resize', function() {
602 $('.zakladka').each(function() {
603 zakladkaSetPosition($(this));
607 $('a.anchor').on('click', function(e) {
610 let sel = window.getSelection();
611 sel.removeAllRanges();
612 let range = document.createRange();
614 let $p = $(this).nextAll('.paragraph').first()
615 range.selectNode($p[0]);
618 qbox.showForSelection(sel);
625 $('.qbox-t-copy').on('click', function(e) {
629 $('.qbox-t-link').on('click', function(e) {
633 $('.qbox-t-quote').on('click', function(e) {
640 $(".paragraph").on('click', function(e) {
641 qbox.showForBlock(this);
646 function scrollToAnchor(anchor) {
648 var anchor_name = anchor.slice(1);
649 var element = $('a[name="' + anchor_name + '"]');
650 if (element.length > 0) {
652 scrollTop: element.offset().top - 55
656 history.pushState({}, '', anchor);
662 scrollToAnchor(window.location.hash)
663 $('#toc, #themes, #book-text, #annotation').on('click', 'a', function(event) {
664 event.preventDefault();
665 scrollToAnchor($(this).attr('href'));