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;
16 function enable_map() {
17 if (!$("#reference-map").length) return;
19 $("#reference-map").show('slow');
21 if (map_enabled) return;
23 map = L.map('reference-map').setView([0, 0], 11);
24 L.tileLayer('https://tile.thunderforest.com/cycle/{z}/{x}/{y}.png?apikey=a8a97f0ae5134403ac38c1a075b03e15', {
25 attribution: 'Maps © <a href="http://www.thunderforest.com">Thunderforest</a>, Data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
27 marker = L.circleMarker([0,0]);
31 function disable_map() {
32 $("#reference-map").hide('slow');
36 $("#reference-close").on("click", function(event) {
37 event.preventDefault();
38 $("#reference-box").hide();
41 $('a.reference').each(function() {
43 uri = $this.attr('data-uri');
48 if (interestingReferences.hasOwnProperty(uri)) {
49 $this.addClass('interesting');
50 ref = interestingReferences[uri];
52 if (ref.wikipedia_link) {
53 $this.attr('href', ref.wikipedia_link);
54 $this.attr('target', '_blank');
56 $this.removeAttr('href')
62 $('a.reference.interesting').on('click', function(event) {
63 event.preventDefault();
65 $("#reference-box").show();
68 uri = $this.attr('data-uri');
69 ref = interestingReferences[uri];
76 ref.location[1] + Math.round(
77 (map.getCenter().lng - ref.location[1]) / 360
81 marker.setLatLng(newLoc);
82 marker.bindTooltip(ref.label).openTooltip();
92 map.removeLayer(marker);
96 $("#reference-images a").remove();
98 $.each(ref.images, function(i, e) {
99 $i = $("<a target='_blank'><img></a>");
100 $i.attr('href', e.page);
101 $('img', $i).attr('src', e.thumburl || e.url);
102 if (e.thumbresolution) {
103 $('img', $i).attr('width', e.thumbresolution[0]).attr('height', e.thumbresolution[1]);
106 $("#reference-images").append($i);
110 $("#reference-link").text(ref.label);
111 if (ref.wikipedia_link) {
112 $("#reference-link").attr('href', ref.wikipedia_link);
114 $('#reference-link').removeAttr('href');
117 $("#reference-print").attr('href', '/mapa/ulotka/' + ref.id);
119 _paq.push(['trackEvent', 'html', 'reference']);
123 function putNoteAt($elem, anchor, side) {
124 $elem.data('anchoredTo', anchor);
125 updateNote($elem, side);
128 function updateNote($elem, side) {
129 anchor = $elem.data('anchoredTo')
131 let anchorRect = anchor.getBoundingClientRect();
133 let x = anchorRect.x + anchorRect.width / 2;
134 let y = anchorRect.y;
135 if ($elem.data('attach-bottom')) {
136 y += anchorRect.height;
138 minx = $("#book-text").position().left;
139 maxx = minx + $("#book-text").width();
147 boxwidth = $elem.width();
149 if (maxx - minx <= boxwidth) {
152 leftoffset = x - margin;
156 leftoffset = $elem.data('default-leftoffset');
157 if (leftoffset === undefined) {
158 leftoffset = $elem.width() / 2;
163 $elem.css({right: ''});
165 // Do we need to move away from the left?
172 // Do we need to move away from the right?
173 if (nx + boxwidth > maxx) {
175 let d = nx + boxwidth - maxx;
187 if (!$elem.data('attach-bottom')) {
188 ny = y - $elem.height() - 10;
195 $('.pointer', $elem).css({
201 function closeNoteBox() {
202 $('#annotation-box').data('anchoredTo', null).fadeOut();
204 $(document).on('click', function(event) {
205 let t = $(event.target);
206 if (t.parents('#annotation-box').length && !t.is('#footnote-link')) {
211 $(window).on('resize', closeNoteBox);
213 function getPositionInBookText($e) {
216 // Ok dla Y, nie ok dla X
218 while ($e.attr('id') != 'book-text') {
219 let p = $e.position();
222 $e = $e.offsetParent();
225 return {"x": x, "y": y}
228 $('#book-text .annotation').on('click', function(event) {
229 if ($(this).parents('#footnotes').length) return;
230 event.preventDefault();
232 href = $(this).attr('href').substr(1);
233 content = $("[name='" + href + "']").next().next().html();
234 if (!content) return;
235 $("#annotation-content").html(content);
236 $("#footnote-link").attr('href', '#' + href)
238 putNoteAt($('#annotation-box'), this);
239 event.stopPropagation();
247 success: function(data) {
249 $.each(zakladki, (i, e) => {
251 // TODO: not just paragraphs.
252 $('[href="#' + e.anchor + '"]').nextAll('.paragraph').first()
258 // TODO: create bookmarks on init
259 // We need to do that from anchors.
261 function zakladkaUpdateFor($item) {
263 let anchor = $item.prevAll('.target').first().attr('name');
265 if (anchor in zakladki) {
266 let $booktag = $item.data('booktag');
269 // TODO: only copy without the dialog.
270 $booktag = $("<div class='zakladka'>");
271 $booktag.append($('.icon', $zakladka).clone());
273 $item.data('booktag', $booktag);
274 $booktag.data('p', $item);
275 $booktag.data('anchor', anchor);
276 $zakladka.after($booktag);
278 zakladkaSetPosition($booktag);
283 if (zakladki[anchor].note) {
284 $z.removeClass('zakladka-exists');
285 $z.addClass('zakladka-note');
287 $z.removeClass('zakladka-note');
288 $z.addClass('zakladka-exists');
291 let $booktag = $item.data('booktag');
293 $item.data('booktag', null);
294 $zakladka.append($("#zakladka-box"));
300 function zakladkaSetPosition($z) {
301 $item = $z.data('p');
302 pos = getPositionInBookText($item);
306 right: ($('#main-text').width() - $('#book-text').width()) / 2,
310 let $zakladka = $('#zakladka');
311 $('#book-text .paragraph').on('mouseover', function() {showMarker($(this));});
312 $('#book-text .verse').on('mouseover', function() {showMarker($(this));});
313 //$.PMarker.showForP(this);
316 function showMarker(p) {
318 // Close the currently tag box when moving to another one.
319 // TBD: Do we want to keep the box open and prevent moving?
320 $("#zakladka-box").hide();
322 let anchor = p.prevAll('.target').first().attr('name');
323 // Don't bother if there's no anchor to use.
326 // Only show tag if there is not already a tag for this p.
327 if (p.data('booktag')) {
330 $zakladka.data('p', p);
331 $zakladka.data('anchor', anchor);
333 // (not needed) zakladkaUpdateClass();
334 // TODO: UPDATE THIS ON OPEN?
335 //let note = anchor in zakladki ? zakladki[anchor].note : '';
336 //$('textarea', $zakladka).val(note);
338 zakladkaSetPosition($zakladka);
343 $(".zakladka-tool_zakladka").on('click', function() {
344 let $z = $("#zakladka-box").data('z');
345 let anchor = $z.data('anchor');
346 let $p = $z.data('p');
350 csrfmiddlewaretoken: csrf,
353 success: function(data) {
354 zakladki[data.anchor] = data;
355 $("#zakladka-box").hide();
357 // Just hide, and create new .zakladka if not already exists?
358 // In general no hiding 'classed' .zakladka.
359 // So the 'cursor' .zakladka doesn't ever need class update.
360 //zakladkaUpdateClass();
361 zakladkaUpdateFor($p);
367 $(".zakladka-tool_notka_text textarea").on('input', function() {
368 // FIXME: no use const $zakladka here, check which .zakladka are we attached to.
369 let $z = $(this).closest('.zakladka');
370 let anchor = $z.data('anchor');
372 $("#notka-saved").hide();
373 //$("#notka-save").show();
375 url: '/zakladki/' + zakladki[anchor].uuid + '/',
377 csrfmiddlewaretoken: csrf,
380 success: function(data) {
381 zakladki[anchor] = data;
382 zakladkaUpdateFor($z.data('p'));
383 $("#notka-save").hide();
384 $("#notka-saved").fadeIn();
389 $(".zakladka-tool_zakladka_delete").on('click', function() {
390 let $z = $(this).closest('.zakladka');
391 let anchor = $z.data('anchor');
393 url: '/zakladki/' + zakladki[anchor].uuid + '/delete/',
395 csrfmiddlewaretoken: csrf,
397 success: function(data) {
398 delete zakladki[anchor];
399 $("#zakladka-box").hide();
400 zakladkaUpdateFor($z.data('p'));
405 $("#main-text").on("click", ".zakladka .icon", function() {
406 let $z = $(this).closest('.zakladka');
407 let $box = $("#zakladka-box");
411 let $p = $z.data('p');
412 let anchor = $z.data('anchor');
413 let note = anchor in zakladki ? zakladki[anchor].note : '';
415 $('.zakladka-tool_zakladka', $box).toggle(!(anchor in zakladki));
416 $('.zakladka-tool_sluchaj', $box).toggle($p.hasClass('syncable')).data('sync', $p.attr('id'));
417 $('textarea', $box).val(note);
427 showForSelection(sel) {
428 // TODO: only consider ranges inside text.?
429 this.selection = sel;
431 // TODO: multiple ranges.
432 let range = sel.getRangeAt(0);
433 let rect = range.getBoundingClientRect();
435 putNoteAt(this.qbox, range)
438 let rect = b.getBoundingClientRect();
440 putNoteAt(this.qbox, b, 'left')
443 this.qbox.data('anchoredTo', null);
447 this.qbox.data('anchoredTo', null);
448 this.qbox.addClass('copied').fadeOut(1500, () => {
449 this.qbox.removeClass('copied');
454 // TODO: only consider ranges inside text.?
455 let range = this.selection.getRangeAt(0);
456 let e = range.startContainer;
457 let anchor = getIdForElem(e);
458 let text = window.location.protocol + '//' +
459 window.location.host +
460 window.location.pathname;
462 navigator.clipboard.writeText(
463 this.selection.toString() +
464 '\n\nCałość czytaj na: ' + text
469 // TODO: only consider ranges inside text.?
470 let range = this.selection.getRangeAt(0);
471 let e = range.startContainer;
472 let anchor = getIdForElem(e);
473 let text = window.location.protocol + '//' +
474 window.location.host +
475 window.location.pathname;
476 if (anchor) text += '#' + anchor;
477 navigator.clipboard.writeText(text);
482 // What aboot non-contiguous selections?
483 let sel = this.selection;
484 let textContent = sel.toString();
485 let anchor = getIdForElem(sel.getRangeAt(0).startContainer);
486 let paths = getSelectionPaths(sel);
490 csrfmiddlewaretoken: csrf,
498 success: function (data) {
499 var win = window.open('/cytaty/' + data.uuid + '/', '_blank');
506 let qbox = new QBox($("#qbox"));
509 function getPathToNode(elem) {
512 while (elem.id != 'book-text') {
513 let p = elem.parentElement;
514 path.unshift([...p.childNodes].indexOf(elem))
519 function getSelectionPaths(selection) {
521 let range1 = selection.getRangeAt(0);
522 let range2 = selection.getRangeAt(selection.rangeCount - 1);
524 getPathToNode(range1.startContainer) + [range1.startOffset],
525 getPathToNode(range2.endContainer) + [range2.endOffset]
531 function getIdForElem(elem) {
534 // check if inside book-text?
537 if ($elem.hasClass('target')) {
538 return $elem.attr('name');
544 // Gdy wychodzimy w górę -- to jest ten moment, w którym znajdujemy element od którego wychodzimy i zliczamy znaki.
558 function getIdForElem(elem) {
560 // check if inside book-text?
563 if ($elem.hasClass('target')) {
564 return $elem.attr('name');
582 function positionToIIDOffset(container, offset) {
583 // Container and offset follow Range rules.
584 // If container is a text node, offset is text offset.
585 // If container is an element node, offset is number of child nodes from container start.
586 // (containers of type Comment, CDATASection - ignore)z
590 function updateQBox() {
591 sel = document.getSelection();
593 if (sel.isCollapsed || sel.rangeCount < 1) {
600 qbox.showForSelection(sel);
603 $(document).on('selectionchange', updateQBox);
605 function updateBoxes() {
606 updateNote(qbox.qbox);
607 updateNote($('#annotation-box'));
610 $(window).on('scroll', updateBoxes);
611 $(window).on('resize', updateBoxes);
614 $(window).on('resize', function() {
615 $('.zakladka').each(function() {
616 zakladkaSetPosition($(this));
620 $('a.anchor').on('click', function(e) {
621 // Workaround for bad TOC markers.
622 if ($(this).closest('#toc').length) return;
623 if ($(this).closest('.toc').length) return;
624 if ($(this).closest('#wltoc').length) return;
627 let sel = window.getSelection();
628 sel.removeAllRanges();
629 let range = document.createRange();
631 let $p = $(this).nextAll('.paragraph').first()
632 range.selectNodeContents($p[0]);
635 qbox.showForSelection(sel);
642 $('.qbox-t-copy').on('click', function(e) {
646 $('.qbox-t-link').on('click', function(e) {
650 $('.qbox-t-quote').on('click', function(e) {
657 $(".paragraph").on('click', function(e) {
658 qbox.showForBlock(this);
663 function scrollToAnchor(anchor) {
665 var anchor_name = anchor.slice(1);
666 var element = $('a[name="' + anchor_name + '"]');
667 if (element.length > 0) {
669 scrollTop: element.offset().top - 55
673 history.pushState({}, '', anchor);
680 scrollToAnchor(window.location.hash)
681 $('#toc, #themes, #book-text, #annotation, .scroll').on('click', 'a', function(event) {
682 if (scrollToAnchor($(this).attr('href'))) {
683 event.preventDefault();